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