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