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