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