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