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