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