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