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