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