| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. | 
 | 2 | // | 
 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 4 | // you may not use this file except in compliance with the License. | 
 | 5 | // You may obtain a copy of the License at | 
 | 6 | // | 
 | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
 | 8 | // | 
 | 9 | // Unless required by applicable law or agreed to in writing, software | 
 | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
 | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 12 | // See the License for the specific language governing permissions and | 
 | 13 | // limitations under the License. | 
 | 14 |  | 
 | 15 | package android | 
 | 16 |  | 
 | 17 | import ( | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 18 | 	"fmt" | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 19 | 	"path/filepath" | 
 | 20 | 	"reflect" | 
| Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 21 | 	"regexp" | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 22 | 	"strconv" | 
 | 23 | 	"strings" | 
 | 24 |  | 
 | 25 | 	"github.com/google/blueprint/proptools" | 
 | 26 | ) | 
 | 27 |  | 
 | 28 | // "neverallow" rules for the build system. | 
 | 29 | // | 
 | 30 | // This allows things which aren't related to the build system and are enforced | 
| Joe Onorato | b4638c1 | 2021-10-27 15:47:06 -0700 | [diff] [blame] | 31 | // against assumptions, in progress code refactors, or policy to be expressed in a | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 32 | // straightforward away disjoint from implementations and tests which should | 
 | 33 | // work regardless of these restrictions. | 
 | 34 | // | 
 | 35 | // A module is disallowed if all of the following are true: | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 36 | // - it is in one of the "In" paths | 
 | 37 | // - it is not in one of the "NotIn" paths | 
 | 38 | // - it has all "With" properties matched | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 39 | // - - values are matched in their entirety | 
 | 40 | // - - nil is interpreted as an empty string | 
 | 41 | // - - nested properties are separated with a '.' | 
 | 42 | // - - if the property is a list, any of the values in the list being matches | 
 | 43 | //     counts as a match | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 44 | // - it has none of the "Without" properties matched (same rules as above) | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 45 |  | 
| Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 46 | func registerNeverallowMutator(ctx RegisterMutatorsContext) { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 47 | 	ctx.BottomUp("neverallow", neverallowMutator).Parallel() | 
 | 48 | } | 
 | 49 |  | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 50 | var neverallows = []Rule{} | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 51 |  | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 52 | func init() { | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 53 | 	AddNeverAllowRules(createIncludeDirsRules()...) | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 54 | 	AddNeverAllowRules(createTrebleRules()...) | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 55 | 	AddNeverAllowRules(createJavaDeviceForHostRules()...) | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 56 | 	AddNeverAllowRules(createCcSdkVariantRules()...) | 
| David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 57 | 	AddNeverAllowRules(createUncompressDexRules()...) | 
| Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 58 | 	AddNeverAllowRules(createInitFirstStageRules()...) | 
| Jiyong Park | 3c306f3 | 2022-04-05 15:29:53 +0900 | [diff] [blame] | 59 | 	AddNeverAllowRules(createProhibitFrameworkAccessRules()...) | 
| Jingwen Chen | 1735b2e | 2022-10-10 14:30:03 +0000 | [diff] [blame] | 60 | 	AddNeverAllowRules(createBp2BuildRule()) | 
| Alan Stokes | 73feba3 | 2022-11-14 12:21:24 +0000 | [diff] [blame] | 61 | 	AddNeverAllowRules(createCcStubsRule()) | 
| Jihoon Kang | 381c2fa | 2023-06-01 22:17:32 +0000 | [diff] [blame] | 62 | 	AddNeverAllowRules(createJavaExcludeStaticLibsRule()) | 
| Mark White | a15790a | 2023-08-22 21:28:11 +0000 | [diff] [blame] | 63 | 	AddNeverAllowRules(createProhibitHeaderOnlyRule()) | 
| Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 64 | } | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 65 |  | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 66 | // Add a NeverAllow rule to the set of rules to apply. | 
 | 67 | func AddNeverAllowRules(rules ...Rule) { | 
 | 68 | 	neverallows = append(neverallows, rules...) | 
 | 69 | } | 
 | 70 |  | 
| Jingwen Chen | 1735b2e | 2022-10-10 14:30:03 +0000 | [diff] [blame] | 71 | func createBp2BuildRule() Rule { | 
 | 72 | 	return NeverAllow(). | 
 | 73 | 		With("bazel_module.bp2build_available", "true"). | 
| Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 74 | 		NotIn("soong_tests"). // only used in tests | 
| Jingwen Chen | 1735b2e | 2022-10-10 14:30:03 +0000 | [diff] [blame] | 75 | 		Because("setting bp2build_available in Android.bp is not " + | 
 | 76 | 			"supported for custom conversion, use allowlists.go instead.") | 
| Jingwen Chen | a4b7eed | 2022-10-07 09:54:16 +0000 | [diff] [blame] | 77 | } | 
 | 78 |  | 
| Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 79 | var ( | 
 | 80 | 	neverallowNotInIncludeDir = []string{ | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 81 | 		"art", | 
| Orion Hodson | 6341f01 | 2019-11-06 13:39:46 +0000 | [diff] [blame] | 82 | 		"art/libnativebridge", | 
 | 83 | 		"art/libnativeloader", | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 84 | 		"libcore", | 
 | 85 | 		"libnativehelper", | 
 | 86 | 		"external/apache-harmony", | 
 | 87 | 		"external/apache-xml", | 
 | 88 | 		"external/boringssl", | 
 | 89 | 		"external/bouncycastle", | 
 | 90 | 		"external/conscrypt", | 
 | 91 | 		"external/icu", | 
 | 92 | 		"external/okhttp", | 
 | 93 | 		"external/vixl", | 
 | 94 | 		"external/wycheproof", | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 95 | 	} | 
| Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 96 | 	neverallowNoUseIncludeDir = []string{ | 
| Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 97 | 		"frameworks/av/apex", | 
 | 98 | 		"frameworks/av/tools", | 
 | 99 | 		"frameworks/native/cmds", | 
 | 100 | 		"system/apex", | 
 | 101 | 		"system/bpf", | 
 | 102 | 		"system/gatekeeper", | 
 | 103 | 		"system/hwservicemanager", | 
 | 104 | 		"system/libbase", | 
| Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 105 | 		"system/libfmq", | 
| Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 106 | 		"system/libvintf", | 
| Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 107 | 	} | 
| Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 108 | ) | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 109 |  | 
| Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 110 | func createIncludeDirsRules() []Rule { | 
 | 111 | 	rules := make([]Rule, 0, len(neverallowNotInIncludeDir)+len(neverallowNoUseIncludeDir)) | 
| Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 112 |  | 
| Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 113 | 	for _, path := range neverallowNotInIncludeDir { | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 114 | 		rule := | 
 | 115 | 			NeverAllow(). | 
 | 116 | 				WithMatcher("include_dirs", StartsWith(path+"/")). | 
 | 117 | 				Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" + | 
 | 118 | 					" to use alternate mechanisms and so can no longer be used.") | 
 | 119 |  | 
 | 120 | 		rules = append(rules, rule) | 
 | 121 | 	} | 
 | 122 |  | 
| Sam Delmerico | 46d08b4 | 2022-11-15 15:51:04 -0500 | [diff] [blame] | 123 | 	for _, path := range neverallowNoUseIncludeDir { | 
| Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 124 | 		rule := NeverAllow().In(path+"/").WithMatcher("include_dirs", isSetMatcherInstance). | 
 | 125 | 			Because("include_dirs is deprecated, all usages of them in '" + path + "' have been migrated" + | 
 | 126 | 				" to use alternate mechanisms and so can no longer be used.") | 
 | 127 | 		rules = append(rules, rule) | 
 | 128 | 	} | 
 | 129 |  | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 130 | 	return rules | 
 | 131 | } | 
 | 132 |  | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 133 | func createTrebleRules() []Rule { | 
 | 134 | 	return []Rule{ | 
 | 135 | 		NeverAllow(). | 
 | 136 | 			In("vendor", "device"). | 
 | 137 | 			With("vndk.enabled", "true"). | 
 | 138 | 			Without("vendor", "true"). | 
| Justin Yun | 0ecf0b2 | 2020-02-28 15:07:59 +0900 | [diff] [blame] | 139 | 			Without("product_specific", "true"). | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 140 | 			Because("the VNDK can never contain a library that is device dependent."), | 
 | 141 | 		NeverAllow(). | 
 | 142 | 			With("vndk.enabled", "true"). | 
 | 143 | 			Without("vendor", "true"). | 
 | 144 | 			Without("owner", ""). | 
 | 145 | 			Because("a VNDK module can never have an owner."), | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 146 |  | 
| Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 147 | 		// TODO(b/67974785): always enforce the manifest | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 148 | 		NeverAllow(). | 
| Steven Moreland | 51ce4f6 | 2020-02-10 17:21:32 -0800 | [diff] [blame] | 149 | 			Without("name", "libhidlbase-combined-impl"). | 
 | 150 | 			Without("name", "libhidlbase"). | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 151 | 			With("product_variables.enforce_vintf_manifest.cflags", "*"). | 
 | 152 | 			Because("manifest enforcement should be independent of ."), | 
| Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 153 |  | 
 | 154 | 		// TODO(b/67975799): vendor code should always use /vendor/bin/sh | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 155 | 		NeverAllow(). | 
 | 156 | 			Without("name", "libc_bionic_ndk"). | 
 | 157 | 			With("product_variables.treble_linker_namespaces.cflags", "*"). | 
 | 158 | 			Because("nothing should care if linker namespaces are enabled or not"), | 
| Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 159 |  | 
 | 160 | 		// Example: | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 161 | 		// *NeverAllow().with("Srcs", "main.cpp")) | 
| Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 162 | 	} | 
 | 163 | } | 
 | 164 |  | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 165 | func createJavaDeviceForHostRules() []Rule { | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 166 | 	javaDeviceForHostProjectsAllowedList := []string{ | 
| Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 167 | 		"development/build", | 
| Colin Cross | b5191a5 | 2019-04-11 14:07:38 -0700 | [diff] [blame] | 168 | 		"external/guava", | 
| Steve Elliott | 8053f82 | 2022-10-18 17:09:28 -0400 | [diff] [blame] | 169 | 		"external/kotlinx.coroutines", | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 170 | 		"external/robolectric-shadows", | 
| Rex Hoffman | 54641d2 | 2022-08-25 17:29:50 +0000 | [diff] [blame] | 171 | 		"external/robolectric", | 
| Jerome Gaillard | 655ee02 | 2021-09-23 11:38:08 +0000 | [diff] [blame] | 172 | 		"frameworks/layoutlib", | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 173 | 	} | 
 | 174 |  | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 175 | 	return []Rule{ | 
 | 176 | 		NeverAllow(). | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 177 | 			NotIn(javaDeviceForHostProjectsAllowedList...). | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 178 | 			ModuleType("java_device_for_host", "java_host_for_device"). | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 179 | 			Because("java_device_for_host can only be used in allowed projects"), | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 180 | 	} | 
 | 181 | } | 
 | 182 |  | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 183 | func createCcSdkVariantRules() []Rule { | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 184 | 	sdkVersionOnlyAllowedList := []string{ | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 185 | 		// derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk. | 
 | 186 | 		// This sometimes works because the APEX modules that contain derive_sdk and | 
 | 187 | 		// derive_sdk_prefer32 suppress the platform installation rules, but fails when | 
 | 188 | 		// the APEX modules contain the SDK variant and the platform variant still exists. | 
| Anton Hansson | 4b8e64b | 2020-05-27 18:25:23 +0100 | [diff] [blame] | 189 | 		"packages/modules/SdkExtensions/derive_sdk", | 
| Dan Albert | e2054a9 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 190 | 		// These are for apps and shouldn't be used by non-SDK variant modules. | 
 | 191 | 		"prebuilts/ndk", | 
 | 192 | 		"tools/test/graphicsbenchmark/apps/sample_app", | 
 | 193 | 		"tools/test/graphicsbenchmark/functional_tests/java", | 
| Dan Albert | 5557605 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 194 | 		"vendor/xts/gts-tests/hostsidetests/gamedevicecert/apps/javatests", | 
| Chang Li | 66d3cb7 | 2021-06-18 14:04:50 +0000 | [diff] [blame] | 195 | 		"external/libtextclassifier/native", | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 196 | 	} | 
 | 197 |  | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 198 | 	platformVariantPropertiesAllowedList := []string{ | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 199 | 		// android_native_app_glue and libRSSupport use native_window.h but target old | 
 | 200 | 		// sdk versions (minimum and 9 respectively) where libnativewindow didn't exist, | 
 | 201 | 		// so they can't add libnativewindow to shared_libs to get the header directory | 
 | 202 | 		// for the platform variant.  Allow them to use the platform variant | 
 | 203 | 		// property to set shared_libs. | 
 | 204 | 		"prebuilts/ndk", | 
 | 205 | 		"frameworks/rs", | 
 | 206 | 	} | 
 | 207 |  | 
 | 208 | 	return []Rule{ | 
 | 209 | 		NeverAllow(). | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 210 | 			NotIn(sdkVersionOnlyAllowedList...). | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 211 | 			WithMatcher("sdk_variant_only", isSetMatcherInstance). | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 212 | 			Because("sdk_variant_only can only be used in allowed projects"), | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 213 | 		NeverAllow(). | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 214 | 			NotIn(platformVariantPropertiesAllowedList...). | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 215 | 			WithMatcher("platform.shared_libs", isSetMatcherInstance). | 
| Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 216 | 			Because("platform variant properties can only be used in allowed projects"), | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 217 | 	} | 
 | 218 | } | 
 | 219 |  | 
| Alan Stokes | 73feba3 | 2022-11-14 12:21:24 +0000 | [diff] [blame] | 220 | func createCcStubsRule() Rule { | 
 | 221 | 	ccStubsImplementationInstallableProjectsAllowedList := []string{ | 
 | 222 | 		"packages/modules/Virtualization/vm_payload", | 
 | 223 | 	} | 
 | 224 |  | 
 | 225 | 	return NeverAllow(). | 
 | 226 | 		NotIn(ccStubsImplementationInstallableProjectsAllowedList...). | 
 | 227 | 		WithMatcher("stubs.implementation_installable", isSetMatcherInstance). | 
 | 228 | 		Because("implementation_installable can only be used in allowed projects.") | 
 | 229 | } | 
 | 230 |  | 
| David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 231 | func createUncompressDexRules() []Rule { | 
 | 232 | 	return []Rule{ | 
 | 233 | 		NeverAllow(). | 
 | 234 | 			NotIn("art"). | 
 | 235 | 			WithMatcher("uncompress_dex", isSetMatcherInstance). | 
 | 236 | 			Because("uncompress_dex is only allowed for certain jars for test in art."), | 
 | 237 | 	} | 
 | 238 | } | 
 | 239 |  | 
| Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 240 | func createInitFirstStageRules() []Rule { | 
 | 241 | 	return []Rule{ | 
 | 242 | 		NeverAllow(). | 
| Nikita Ioffe | 11a9c2c | 2023-06-21 16:51:09 +0100 | [diff] [blame] | 243 | 			Without("name", "init_first_stage_defaults"). | 
| Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 244 | 			Without("name", "init_first_stage"). | 
| Nikita Ioffe | 11a9c2c | 2023-06-21 16:51:09 +0100 | [diff] [blame] | 245 | 			Without("name", "init_first_stage.microdroid"). | 
| Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 246 | 			With("install_in_root", "true"). | 
 | 247 | 			Because("install_in_root is only for init_first_stage."), | 
 | 248 | 	} | 
 | 249 | } | 
 | 250 |  | 
| Jiyong Park | 3c306f3 | 2022-04-05 15:29:53 +0900 | [diff] [blame] | 251 | func createProhibitFrameworkAccessRules() []Rule { | 
 | 252 | 	return []Rule{ | 
 | 253 | 		NeverAllow(). | 
 | 254 | 			With("libs", "framework"). | 
 | 255 | 			WithoutMatcher("sdk_version", Regexp("(core_.*|^$)")). | 
 | 256 | 			Because("framework can't be used when building against SDK"), | 
 | 257 | 	} | 
 | 258 | } | 
 | 259 |  | 
| Jihoon Kang | 381c2fa | 2023-06-01 22:17:32 +0000 | [diff] [blame] | 260 | func createJavaExcludeStaticLibsRule() Rule { | 
 | 261 | 	return NeverAllow(). | 
| Jihoon Kang | 3d4d88d | 2023-06-14 23:14:42 +0000 | [diff] [blame] | 262 | 		NotIn("build/soong", "libcore", "frameworks/base/api"). | 
| Jihoon Kang | 381c2fa | 2023-06-01 22:17:32 +0000 | [diff] [blame] | 263 | 		ModuleType("java_library"). | 
 | 264 | 		WithMatcher("exclude_static_libs", isSetMatcherInstance). | 
| Jihoon Kang | 3d4d88d | 2023-06-14 23:14:42 +0000 | [diff] [blame] | 265 | 		Because("exclude_static_libs property is only allowed for java modules defined in build/soong, libcore, and frameworks/base/api") | 
| Jihoon Kang | 381c2fa | 2023-06-01 22:17:32 +0000 | [diff] [blame] | 266 | } | 
 | 267 |  | 
| Mark White | a15790a | 2023-08-22 21:28:11 +0000 | [diff] [blame] | 268 | func createProhibitHeaderOnlyRule() Rule { | 
 | 269 | 	return NeverAllow(). | 
 | 270 | 		Without("name", "framework-minus-apex-headers"). | 
 | 271 | 		With("headers_only", "true"). | 
 | 272 | 		Because("headers_only can only be used for generating framework-minus-apex headers for non-updatable modules") | 
 | 273 | } | 
 | 274 |  | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 275 | func neverallowMutator(ctx BottomUpMutatorContext) { | 
 | 276 | 	m, ok := ctx.Module().(Module) | 
 | 277 | 	if !ok { | 
 | 278 | 		return | 
 | 279 | 	} | 
 | 280 |  | 
 | 281 | 	dir := ctx.ModuleDir() + "/" | 
 | 282 | 	properties := m.GetProperties() | 
 | 283 |  | 
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 284 | 	osClass := ctx.Module().Target().Os.Class | 
 | 285 |  | 
| Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 286 | 	for _, r := range neverallowRules(ctx.Config()) { | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 287 | 		n := r.(*rule) | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 288 | 		if !n.appliesToPath(dir) { | 
 | 289 | 			continue | 
 | 290 | 		} | 
 | 291 |  | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 292 | 		if !n.appliesToModuleType(ctx.ModuleType()) { | 
 | 293 | 			continue | 
 | 294 | 		} | 
 | 295 |  | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 296 | 		if !n.appliesToProperties(properties) { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 297 | 			continue | 
 | 298 | 		} | 
 | 299 |  | 
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 300 | 		if !n.appliesToOsClass(osClass) { | 
 | 301 | 			continue | 
 | 302 | 		} | 
 | 303 |  | 
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 304 | 		if !n.appliesToDirectDeps(ctx) { | 
 | 305 | 			continue | 
 | 306 | 		} | 
 | 307 |  | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 308 | 		ctx.ModuleErrorf("violates " + n.String()) | 
 | 309 | 	} | 
 | 310 | } | 
 | 311 |  | 
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 312 | type ValueMatcher interface { | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 313 | 	Test(string) bool | 
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 314 | 	String() string | 
 | 315 | } | 
 | 316 |  | 
 | 317 | type equalMatcher struct { | 
 | 318 | 	expected string | 
 | 319 | } | 
 | 320 |  | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 321 | func (m *equalMatcher) Test(value string) bool { | 
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 322 | 	return m.expected == value | 
 | 323 | } | 
 | 324 |  | 
 | 325 | func (m *equalMatcher) String() string { | 
 | 326 | 	return "=" + m.expected | 
 | 327 | } | 
 | 328 |  | 
 | 329 | type anyMatcher struct { | 
 | 330 | } | 
 | 331 |  | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 332 | func (m *anyMatcher) Test(value string) bool { | 
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 333 | 	return true | 
 | 334 | } | 
 | 335 |  | 
 | 336 | func (m *anyMatcher) String() string { | 
 | 337 | 	return "=*" | 
 | 338 | } | 
 | 339 |  | 
 | 340 | var anyMatcherInstance = &anyMatcher{} | 
 | 341 |  | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 342 | type startsWithMatcher struct { | 
 | 343 | 	prefix string | 
 | 344 | } | 
 | 345 |  | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 346 | func (m *startsWithMatcher) Test(value string) bool { | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 347 | 	return strings.HasPrefix(value, m.prefix) | 
 | 348 | } | 
 | 349 |  | 
 | 350 | func (m *startsWithMatcher) String() string { | 
 | 351 | 	return ".starts-with(" + m.prefix + ")" | 
 | 352 | } | 
 | 353 |  | 
| Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 354 | type regexMatcher struct { | 
 | 355 | 	re *regexp.Regexp | 
 | 356 | } | 
 | 357 |  | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 358 | func (m *regexMatcher) Test(value string) bool { | 
| Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 359 | 	return m.re.MatchString(value) | 
 | 360 | } | 
 | 361 |  | 
 | 362 | func (m *regexMatcher) String() string { | 
 | 363 | 	return ".regexp(" + m.re.String() + ")" | 
 | 364 | } | 
 | 365 |  | 
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 366 | type notInListMatcher struct { | 
 | 367 | 	allowed []string | 
 | 368 | } | 
 | 369 |  | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 370 | func (m *notInListMatcher) Test(value string) bool { | 
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 371 | 	return !InList(value, m.allowed) | 
 | 372 | } | 
 | 373 |  | 
 | 374 | func (m *notInListMatcher) String() string { | 
 | 375 | 	return ".not-in-list(" + strings.Join(m.allowed, ",") + ")" | 
 | 376 | } | 
 | 377 |  | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 378 | type isSetMatcher struct{} | 
 | 379 |  | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 380 | func (m *isSetMatcher) Test(value string) bool { | 
| Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 381 | 	return value != "" | 
 | 382 | } | 
 | 383 |  | 
 | 384 | func (m *isSetMatcher) String() string { | 
 | 385 | 	return ".is-set" | 
 | 386 | } | 
 | 387 |  | 
 | 388 | var isSetMatcherInstance = &isSetMatcher{} | 
 | 389 |  | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 390 | type ruleProperty struct { | 
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 391 | 	fields  []string // e.x.: Vndk.Enabled | 
 | 392 | 	matcher ValueMatcher | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 393 | } | 
 | 394 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 395 | func (r *ruleProperty) String() string { | 
 | 396 | 	return fmt.Sprintf("%q matches: %s", strings.Join(r.fields, "."), r.matcher) | 
 | 397 | } | 
 | 398 |  | 
 | 399 | type ruleProperties []ruleProperty | 
 | 400 |  | 
 | 401 | func (r ruleProperties) String() string { | 
 | 402 | 	var s []string | 
 | 403 | 	for _, r := range r { | 
 | 404 | 		s = append(s, r.String()) | 
 | 405 | 	} | 
 | 406 | 	return strings.Join(s, " ") | 
 | 407 | } | 
 | 408 |  | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 409 | // A NeverAllow rule. | 
 | 410 | type Rule interface { | 
 | 411 | 	In(path ...string) Rule | 
 | 412 |  | 
 | 413 | 	NotIn(path ...string) Rule | 
 | 414 |  | 
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 415 | 	InDirectDeps(deps ...string) Rule | 
 | 416 |  | 
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 417 | 	WithOsClass(osClasses ...OsClass) Rule | 
 | 418 |  | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 419 | 	ModuleType(types ...string) Rule | 
 | 420 |  | 
 | 421 | 	NotModuleType(types ...string) Rule | 
 | 422 |  | 
 | 423 | 	With(properties, value string) Rule | 
 | 424 |  | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 425 | 	WithMatcher(properties string, matcher ValueMatcher) Rule | 
 | 426 |  | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 427 | 	Without(properties, value string) Rule | 
 | 428 |  | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 429 | 	WithoutMatcher(properties string, matcher ValueMatcher) Rule | 
 | 430 |  | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 431 | 	Because(reason string) Rule | 
 | 432 | } | 
 | 433 |  | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 434 | type rule struct { | 
 | 435 | 	// User string for why this is a thing. | 
 | 436 | 	reason string | 
 | 437 |  | 
 | 438 | 	paths       []string | 
 | 439 | 	unlessPaths []string | 
 | 440 |  | 
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 441 | 	directDeps map[string]bool | 
 | 442 |  | 
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 443 | 	osClasses []OsClass | 
 | 444 |  | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 445 | 	moduleTypes       []string | 
 | 446 | 	unlessModuleTypes []string | 
 | 447 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 448 | 	props       ruleProperties | 
 | 449 | 	unlessProps ruleProperties | 
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 450 |  | 
 | 451 | 	onlyBootclasspathJar bool | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 452 | } | 
 | 453 |  | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 454 | // Create a new NeverAllow rule. | 
 | 455 | func NeverAllow() Rule { | 
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 456 | 	return &rule{directDeps: make(map[string]bool)} | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 457 | } | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 458 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 459 | // In adds path(s) where this rule applies. | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 460 | func (r *rule) In(path ...string) Rule { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 461 | 	r.paths = append(r.paths, cleanPaths(path)...) | 
 | 462 | 	return r | 
 | 463 | } | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 464 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 465 | // NotIn adds path(s) to that this rule does not apply to. | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 466 | func (r *rule) NotIn(path ...string) Rule { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 467 | 	r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...) | 
 | 468 | 	return r | 
 | 469 | } | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 470 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 471 | // InDirectDeps adds dep(s) that are not allowed with this rule. | 
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 472 | func (r *rule) InDirectDeps(deps ...string) Rule { | 
 | 473 | 	for _, d := range deps { | 
 | 474 | 		r.directDeps[d] = true | 
 | 475 | 	} | 
 | 476 | 	return r | 
 | 477 | } | 
 | 478 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 479 | // WithOsClass adds osClass(es) that this rule applies to. | 
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 480 | func (r *rule) WithOsClass(osClasses ...OsClass) Rule { | 
 | 481 | 	r.osClasses = append(r.osClasses, osClasses...) | 
 | 482 | 	return r | 
 | 483 | } | 
 | 484 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 485 | // ModuleType adds type(s) that this rule applies to. | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 486 | func (r *rule) ModuleType(types ...string) Rule { | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 487 | 	r.moduleTypes = append(r.moduleTypes, types...) | 
 | 488 | 	return r | 
 | 489 | } | 
 | 490 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 491 | // NotModuleType adds type(s) that this rule does not apply to.. | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 492 | func (r *rule) NotModuleType(types ...string) Rule { | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 493 | 	r.unlessModuleTypes = append(r.unlessModuleTypes, types...) | 
 | 494 | 	return r | 
 | 495 | } | 
 | 496 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 497 | // With specifies property/value combinations that are restricted for this rule. | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 498 | func (r *rule) With(properties, value string) Rule { | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 499 | 	return r.WithMatcher(properties, selectMatcher(value)) | 
 | 500 | } | 
 | 501 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 502 | // WithMatcher specifies property/matcher combinations that are restricted for this rule. | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 503 | func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 504 | 	r.props = append(r.props, ruleProperty{ | 
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 505 | 		fields:  fieldNamesForProperties(properties), | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 506 | 		matcher: matcher, | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 507 | 	}) | 
 | 508 | 	return r | 
 | 509 | } | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 510 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 511 | // Without specifies property/value combinations that this rule does not apply to. | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 512 | func (r *rule) Without(properties, value string) Rule { | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 513 | 	return r.WithoutMatcher(properties, selectMatcher(value)) | 
 | 514 | } | 
 | 515 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 516 | // Without specifies property/matcher combinations that this rule does not apply to. | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 517 | func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 518 | 	r.unlessProps = append(r.unlessProps, ruleProperty{ | 
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 519 | 		fields:  fieldNamesForProperties(properties), | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 520 | 		matcher: matcher, | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 521 | 	}) | 
 | 522 | 	return r | 
 | 523 | } | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 524 |  | 
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 525 | func selectMatcher(expected string) ValueMatcher { | 
 | 526 | 	if expected == "*" { | 
 | 527 | 		return anyMatcherInstance | 
 | 528 | 	} | 
 | 529 | 	return &equalMatcher{expected: expected} | 
 | 530 | } | 
 | 531 |  | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 532 | // Because specifies a reason for this rule. | 
| Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 533 | func (r *rule) Because(reason string) Rule { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 534 | 	r.reason = reason | 
 | 535 | 	return r | 
 | 536 | } | 
 | 537 |  | 
 | 538 | func (r *rule) String() string { | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 539 | 	s := []string{"neverallow requirements. Not allowed:"} | 
 | 540 | 	if len(r.paths) > 0 { | 
 | 541 | 		s = append(s, fmt.Sprintf("in dirs: %q", r.paths)) | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 542 | 	} | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 543 | 	if len(r.moduleTypes) > 0 { | 
 | 544 | 		s = append(s, fmt.Sprintf("module types: %q", r.moduleTypes)) | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 545 | 	} | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 546 | 	if len(r.props) > 0 { | 
 | 547 | 		s = append(s, fmt.Sprintf("properties matching: %s", r.props)) | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 548 | 	} | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 549 | 	if len(r.directDeps) > 0 { | 
| Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 550 | 		s = append(s, fmt.Sprintf("dep(s): %q", SortedKeys(r.directDeps))) | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 551 | 	} | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 552 | 	if len(r.osClasses) > 0 { | 
 | 553 | 		s = append(s, fmt.Sprintf("os class(es): %q", r.osClasses)) | 
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 554 | 	} | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 555 | 	if len(r.unlessPaths) > 0 { | 
 | 556 | 		s = append(s, fmt.Sprintf("EXCEPT in dirs: %q", r.unlessPaths)) | 
 | 557 | 	} | 
 | 558 | 	if len(r.unlessModuleTypes) > 0 { | 
 | 559 | 		s = append(s, fmt.Sprintf("EXCEPT module types: %q", r.unlessModuleTypes)) | 
 | 560 | 	} | 
 | 561 | 	if len(r.unlessProps) > 0 { | 
 | 562 | 		s = append(s, fmt.Sprintf("EXCEPT properties matching: %q", r.unlessProps)) | 
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 563 | 	} | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 564 | 	if len(r.reason) != 0 { | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 565 | 		s = append(s, " which is restricted because "+r.reason) | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 566 | 	} | 
| Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame] | 567 | 	if len(s) == 1 { | 
 | 568 | 		s[0] = "neverallow requirements (empty)" | 
 | 569 | 	} | 
 | 570 | 	return strings.Join(s, "\n\t") | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 571 | } | 
 | 572 |  | 
 | 573 | func (r *rule) appliesToPath(dir string) bool { | 
| Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 574 | 	includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths) | 
 | 575 | 	excludePath := HasAnyPrefix(dir, r.unlessPaths) | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 576 | 	return includePath && !excludePath | 
 | 577 | } | 
 | 578 |  | 
| Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 579 | func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool { | 
 | 580 | 	if len(r.directDeps) == 0 { | 
 | 581 | 		return true | 
 | 582 | 	} | 
 | 583 |  | 
 | 584 | 	matches := false | 
 | 585 | 	ctx.VisitDirectDeps(func(m Module) { | 
 | 586 | 		if !matches { | 
 | 587 | 			name := ctx.OtherModuleName(m) | 
 | 588 | 			matches = r.directDeps[name] | 
 | 589 | 		} | 
 | 590 | 	}) | 
 | 591 |  | 
 | 592 | 	return matches | 
 | 593 | } | 
 | 594 |  | 
| Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 595 | func (r *rule) appliesToOsClass(osClass OsClass) bool { | 
 | 596 | 	if len(r.osClasses) == 0 { | 
 | 597 | 		return true | 
 | 598 | 	} | 
 | 599 |  | 
 | 600 | 	for _, c := range r.osClasses { | 
 | 601 | 		if c == osClass { | 
 | 602 | 			return true | 
 | 603 | 		} | 
 | 604 | 	} | 
 | 605 |  | 
 | 606 | 	return false | 
 | 607 | } | 
 | 608 |  | 
| Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 609 | func (r *rule) appliesToModuleType(moduleType string) bool { | 
 | 610 | 	return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) | 
 | 611 | } | 
 | 612 |  | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 613 | func (r *rule) appliesToProperties(properties []interface{}) bool { | 
 | 614 | 	includeProps := hasAllProperties(properties, r.props) | 
 | 615 | 	excludeProps := hasAnyProperty(properties, r.unlessProps) | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 616 | 	return includeProps && !excludeProps | 
 | 617 | } | 
 | 618 |  | 
| Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 619 | func StartsWith(prefix string) ValueMatcher { | 
 | 620 | 	return &startsWithMatcher{prefix} | 
 | 621 | } | 
 | 622 |  | 
| Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 623 | func Regexp(re string) ValueMatcher { | 
 | 624 | 	r, err := regexp.Compile(re) | 
 | 625 | 	if err != nil { | 
 | 626 | 		panic(err) | 
 | 627 | 	} | 
 | 628 | 	return ®exMatcher{r} | 
 | 629 | } | 
 | 630 |  | 
| Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 631 | func NotInList(allowed []string) ValueMatcher { | 
 | 632 | 	return ¬InListMatcher{allowed} | 
 | 633 | } | 
 | 634 |  | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 635 | // assorted utils | 
 | 636 |  | 
 | 637 | func cleanPaths(paths []string) []string { | 
 | 638 | 	res := make([]string, len(paths)) | 
 | 639 | 	for i, v := range paths { | 
 | 640 | 		res[i] = filepath.Clean(v) + "/" | 
 | 641 | 	} | 
 | 642 | 	return res | 
 | 643 | } | 
 | 644 |  | 
 | 645 | func fieldNamesForProperties(propertyNames string) []string { | 
 | 646 | 	names := strings.Split(propertyNames, ".") | 
 | 647 | 	for i, v := range names { | 
 | 648 | 		names[i] = proptools.FieldNameForProperty(v) | 
 | 649 | 	} | 
 | 650 | 	return names | 
 | 651 | } | 
 | 652 |  | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 653 | func hasAnyProperty(properties []interface{}, props []ruleProperty) bool { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 654 | 	for _, v := range props { | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 655 | 		if hasProperty(properties, v) { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 656 | 			return true | 
 | 657 | 		} | 
 | 658 | 	} | 
 | 659 | 	return false | 
 | 660 | } | 
 | 661 |  | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 662 | func hasAllProperties(properties []interface{}, props []ruleProperty) bool { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 663 | 	for _, v := range props { | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 664 | 		if !hasProperty(properties, v) { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 665 | 			return false | 
 | 666 | 		} | 
 | 667 | 	} | 
 | 668 | 	return true | 
 | 669 | } | 
 | 670 |  | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 671 | func hasProperty(properties []interface{}, prop ruleProperty) bool { | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 672 | 	for _, propertyStruct := range properties { | 
 | 673 | 		propertiesValue := reflect.ValueOf(propertyStruct).Elem() | 
 | 674 | 		for _, v := range prop.fields { | 
 | 675 | 			if !propertiesValue.IsValid() { | 
 | 676 | 				break | 
 | 677 | 			} | 
 | 678 | 			propertiesValue = propertiesValue.FieldByName(v) | 
 | 679 | 		} | 
 | 680 | 		if !propertiesValue.IsValid() { | 
 | 681 | 			continue | 
 | 682 | 		} | 
 | 683 |  | 
| Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 684 | 		check := func(value string) bool { | 
| Anton Hansson | e1b1836 | 2021-12-23 15:05:38 +0000 | [diff] [blame] | 685 | 			return prop.matcher.Test(value) | 
| Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 686 | 		} | 
 | 687 |  | 
 | 688 | 		if matchValue(propertiesValue, check) { | 
 | 689 | 			return true | 
 | 690 | 		} | 
 | 691 | 	} | 
 | 692 | 	return false | 
 | 693 | } | 
 | 694 |  | 
 | 695 | func matchValue(value reflect.Value, check func(string) bool) bool { | 
 | 696 | 	if !value.IsValid() { | 
 | 697 | 		return false | 
 | 698 | 	} | 
 | 699 |  | 
 | 700 | 	if value.Kind() == reflect.Ptr { | 
 | 701 | 		if value.IsNil() { | 
 | 702 | 			return check("") | 
 | 703 | 		} | 
 | 704 | 		value = value.Elem() | 
 | 705 | 	} | 
 | 706 |  | 
 | 707 | 	switch value.Kind() { | 
 | 708 | 	case reflect.String: | 
 | 709 | 		return check(value.String()) | 
 | 710 | 	case reflect.Bool: | 
 | 711 | 		return check(strconv.FormatBool(value.Bool())) | 
 | 712 | 	case reflect.Int: | 
 | 713 | 		return check(strconv.FormatInt(value.Int(), 10)) | 
 | 714 | 	case reflect.Slice: | 
 | 715 | 		slice, ok := value.Interface().([]string) | 
 | 716 | 		if !ok { | 
 | 717 | 			panic("Can only handle slice of string") | 
 | 718 | 		} | 
 | 719 | 		for _, v := range slice { | 
 | 720 | 			if check(v) { | 
 | 721 | 				return true | 
 | 722 | 			} | 
 | 723 | 		} | 
 | 724 | 		return false | 
 | 725 | 	} | 
 | 726 |  | 
 | 727 | 	panic("Can't handle type: " + value.Kind().String()) | 
 | 728 | } | 
| Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 729 |  | 
 | 730 | var neverallowRulesKey = NewOnceKey("neverallowRules") | 
 | 731 |  | 
 | 732 | func neverallowRules(config Config) []Rule { | 
 | 733 | 	return config.Once(neverallowRulesKey, func() interface{} { | 
 | 734 | 		// No test rules were set by setTestNeverallowRules, use the global rules | 
 | 735 | 		return neverallows | 
 | 736 | 	}).([]Rule) | 
 | 737 | } | 
 | 738 |  | 
 | 739 | // Overrides the default neverallow rules for the supplied config. | 
 | 740 | // | 
 | 741 | // For testing only. | 
| Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 742 | func setTestNeverallowRules(config Config, testRules []Rule) { | 
| Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 743 | 	config.Once(neverallowRulesKey, func() interface{} { return testRules }) | 
 | 744 | } | 
| Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 745 |  | 
 | 746 | // Prepares for a test by setting neverallow rules and enabling the mutator. | 
 | 747 | // | 
 | 748 | // If the supplied rules are nil then the default rules are used. | 
 | 749 | func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer { | 
 | 750 | 	return GroupFixturePreparers( | 
 | 751 | 		FixtureModifyConfig(func(config Config) { | 
 | 752 | 			if testRules != nil { | 
 | 753 | 				setTestNeverallowRules(config, testRules) | 
 | 754 | 			} | 
 | 755 | 		}), | 
 | 756 | 		FixtureRegisterWithContext(func(ctx RegistrationContext) { | 
 | 757 | 			ctx.PostDepsMutators(registerNeverallowMutator) | 
 | 758 | 		}), | 
 | 759 | 	) | 
 | 760 | } |