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