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