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()...) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 60 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 61 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 62 | // Add a NeverAllow rule to the set of rules to apply. |
| 63 | func AddNeverAllowRules(rules ...Rule) { |
| 64 | neverallows = append(neverallows, rules...) |
| 65 | } |
| 66 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 67 | func createIncludeDirsRules() []Rule { |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 68 | notInIncludeDir := []string{ |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 69 | "art", |
Orion Hodson | 6341f01 | 2019-11-06 13:39:46 +0000 | [diff] [blame] | 70 | "art/libnativebridge", |
| 71 | "art/libnativeloader", |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 72 | "libcore", |
| 73 | "libnativehelper", |
| 74 | "external/apache-harmony", |
| 75 | "external/apache-xml", |
| 76 | "external/boringssl", |
| 77 | "external/bouncycastle", |
| 78 | "external/conscrypt", |
| 79 | "external/icu", |
| 80 | "external/okhttp", |
| 81 | "external/vixl", |
| 82 | "external/wycheproof", |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 83 | } |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 84 | noUseIncludeDir := []string{ |
Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 85 | "frameworks/av/apex", |
| 86 | "frameworks/av/tools", |
| 87 | "frameworks/native/cmds", |
| 88 | "system/apex", |
| 89 | "system/bpf", |
| 90 | "system/gatekeeper", |
| 91 | "system/hwservicemanager", |
| 92 | "system/libbase", |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 93 | "system/libfmq", |
Steven Moreland | f36a3ac | 2021-04-27 18:03:14 +0000 | [diff] [blame] | 94 | "system/libvintf", |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 95 | } |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 96 | |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 97 | rules := make([]Rule, 0, len(notInIncludeDir)+len(noUseIncludeDir)) |
| 98 | |
| 99 | for _, path := range notInIncludeDir { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 100 | rule := |
| 101 | NeverAllow(). |
| 102 | WithMatcher("include_dirs", StartsWith(path+"/")). |
| 103 | Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" + |
| 104 | " to use alternate mechanisms and so can no longer be used.") |
| 105 | |
| 106 | rules = append(rules, rule) |
| 107 | } |
| 108 | |
Steven Moreland | 8fc8dbf | 2021-04-27 02:31:07 +0000 | [diff] [blame] | 109 | for _, path := range noUseIncludeDir { |
| 110 | rule := NeverAllow().In(path+"/").WithMatcher("include_dirs", isSetMatcherInstance). |
| 111 | Because("include_dirs is deprecated, all usages of them in '" + path + "' have been migrated" + |
| 112 | " to use alternate mechanisms and so can no longer be used.") |
| 113 | rules = append(rules, rule) |
| 114 | } |
| 115 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 116 | return rules |
| 117 | } |
| 118 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 119 | func createTrebleRules() []Rule { |
| 120 | return []Rule{ |
| 121 | NeverAllow(). |
| 122 | In("vendor", "device"). |
| 123 | With("vndk.enabled", "true"). |
| 124 | Without("vendor", "true"). |
Justin Yun | 0ecf0b2 | 2020-02-28 15:07:59 +0900 | [diff] [blame] | 125 | Without("product_specific", "true"). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 126 | Because("the VNDK can never contain a library that is device dependent."), |
| 127 | NeverAllow(). |
| 128 | With("vndk.enabled", "true"). |
| 129 | Without("vendor", "true"). |
| 130 | Without("owner", ""). |
| 131 | Because("a VNDK module can never have an owner."), |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 132 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 133 | // TODO(b/67974785): always enforce the manifest |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 134 | NeverAllow(). |
Steven Moreland | 51ce4f6 | 2020-02-10 17:21:32 -0800 | [diff] [blame] | 135 | Without("name", "libhidlbase-combined-impl"). |
| 136 | Without("name", "libhidlbase"). |
| 137 | Without("name", "libhidlbase_pgo"). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 138 | With("product_variables.enforce_vintf_manifest.cflags", "*"). |
| 139 | Because("manifest enforcement should be independent of ."), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 140 | |
| 141 | // TODO(b/67975799): vendor code should always use /vendor/bin/sh |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 142 | NeverAllow(). |
| 143 | Without("name", "libc_bionic_ndk"). |
| 144 | With("product_variables.treble_linker_namespaces.cflags", "*"). |
| 145 | Because("nothing should care if linker namespaces are enabled or not"), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 146 | |
| 147 | // Example: |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 148 | // *NeverAllow().with("Srcs", "main.cpp")) |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 152 | func createJavaDeviceForHostRules() []Rule { |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 153 | javaDeviceForHostProjectsAllowedList := []string{ |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 154 | "development/build", |
Colin Cross | b5191a5 | 2019-04-11 14:07:38 -0700 | [diff] [blame] | 155 | "external/guava", |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 156 | "external/robolectric-shadows", |
Jerome Gaillard | 655ee02 | 2021-09-23 11:38:08 +0000 | [diff] [blame] | 157 | "frameworks/layoutlib", |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 160 | return []Rule{ |
| 161 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 162 | NotIn(javaDeviceForHostProjectsAllowedList...). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 163 | ModuleType("java_device_for_host", "java_host_for_device"). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 164 | Because("java_device_for_host can only be used in allowed projects"), |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 168 | func createCcSdkVariantRules() []Rule { |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 169 | sdkVersionOnlyAllowedList := []string{ |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 170 | // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk. |
| 171 | // This sometimes works because the APEX modules that contain derive_sdk and |
| 172 | // derive_sdk_prefer32 suppress the platform installation rules, but fails when |
| 173 | // the APEX modules contain the SDK variant and the platform variant still exists. |
Anton Hansson | 4b8e64b | 2020-05-27 18:25:23 +0100 | [diff] [blame] | 174 | "packages/modules/SdkExtensions/derive_sdk", |
Dan Albert | e2054a9 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 175 | // These are for apps and shouldn't be used by non-SDK variant modules. |
| 176 | "prebuilts/ndk", |
| 177 | "tools/test/graphicsbenchmark/apps/sample_app", |
| 178 | "tools/test/graphicsbenchmark/functional_tests/java", |
Dan Albert | 5557605 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 179 | "vendor/xts/gts-tests/hostsidetests/gamedevicecert/apps/javatests", |
Chang Li | 66d3cb7 | 2021-06-18 14:04:50 +0000 | [diff] [blame] | 180 | "external/libtextclassifier/native", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 183 | platformVariantPropertiesAllowedList := []string{ |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 184 | // android_native_app_glue and libRSSupport use native_window.h but target old |
| 185 | // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist, |
| 186 | // so they can't add libnativewindow to shared_libs to get the header directory |
| 187 | // for the platform variant. Allow them to use the platform variant |
| 188 | // property to set shared_libs. |
| 189 | "prebuilts/ndk", |
| 190 | "frameworks/rs", |
| 191 | } |
| 192 | |
| 193 | return []Rule{ |
| 194 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 195 | NotIn(sdkVersionOnlyAllowedList...). |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 196 | WithMatcher("sdk_variant_only", isSetMatcherInstance). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 197 | Because("sdk_variant_only can only be used in allowed projects"), |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 198 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 199 | NotIn(platformVariantPropertiesAllowedList...). |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 200 | WithMatcher("platform.shared_libs", isSetMatcherInstance). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 201 | Because("platform variant properties can only be used in allowed projects"), |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 205 | func createUncompressDexRules() []Rule { |
| 206 | return []Rule{ |
| 207 | NeverAllow(). |
| 208 | NotIn("art"). |
| 209 | WithMatcher("uncompress_dex", isSetMatcherInstance). |
| 210 | Because("uncompress_dex is only allowed for certain jars for test in art."), |
| 211 | } |
| 212 | } |
| 213 | |
Yifan Hong | 696ed4d | 2020-07-27 12:59:58 -0700 | [diff] [blame] | 214 | func createMakefileGoalRules() []Rule { |
| 215 | return []Rule{ |
| 216 | NeverAllow(). |
| 217 | ModuleType("makefile_goal"). |
Yuntao Xu | feb0756 | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 218 | // TODO(b/33691272): remove this after migrating seapp to Soong |
| 219 | Without("product_out_path", "obj/ETC/plat_seapp_contexts_intermediates/plat_seapp_contexts"). |
| 220 | Without("product_out_path", "obj/ETC/plat_seapp_neverallows_intermediates/plat_seapp_neverallows"). |
Yifan Hong | 696ed4d | 2020-07-27 12:59:58 -0700 | [diff] [blame] | 221 | WithoutMatcher("product_out_path", Regexp("^boot[0-9a-zA-Z.-]*[.]img$")). |
Yuntao Xu | feb0756 | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 222 | Because("Only boot images and seapp contexts may be imported as a makefile goal."), |
Yifan Hong | 696ed4d | 2020-07-27 12:59:58 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Inseob Kim | 800d114 | 2021-06-14 12:03:51 +0900 | [diff] [blame] | 226 | func createInitFirstStageRules() []Rule { |
| 227 | return []Rule{ |
| 228 | NeverAllow(). |
| 229 | Without("name", "init_first_stage"). |
| 230 | With("install_in_root", "true"). |
| 231 | Because("install_in_root is only for init_first_stage."), |
| 232 | } |
| 233 | } |
| 234 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 235 | func neverallowMutator(ctx BottomUpMutatorContext) { |
| 236 | m, ok := ctx.Module().(Module) |
| 237 | if !ok { |
| 238 | return |
| 239 | } |
| 240 | |
| 241 | dir := ctx.ModuleDir() + "/" |
| 242 | properties := m.GetProperties() |
| 243 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 244 | osClass := ctx.Module().Target().Os.Class |
| 245 | |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 246 | for _, r := range neverallowRules(ctx.Config()) { |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 247 | n := r.(*rule) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 248 | if !n.appliesToPath(dir) { |
| 249 | continue |
| 250 | } |
| 251 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 252 | if !n.appliesToModuleType(ctx.ModuleType()) { |
| 253 | continue |
| 254 | } |
| 255 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 256 | if !n.appliesToProperties(ctx, properties) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 257 | continue |
| 258 | } |
| 259 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 260 | if !n.appliesToOsClass(osClass) { |
| 261 | continue |
| 262 | } |
| 263 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 264 | if !n.appliesToDirectDeps(ctx) { |
| 265 | continue |
| 266 | } |
| 267 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 268 | if !n.appliesToBootclasspathJar(ctx) { |
| 269 | continue |
| 270 | } |
| 271 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 272 | ctx.ModuleErrorf("violates " + n.String()) |
| 273 | } |
| 274 | } |
| 275 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 276 | type ValueMatcherContext interface { |
| 277 | Config() Config |
| 278 | } |
| 279 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 280 | type ValueMatcher interface { |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 281 | Test(ValueMatcherContext, string) bool |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 282 | String() string |
| 283 | } |
| 284 | |
| 285 | type equalMatcher struct { |
| 286 | expected string |
| 287 | } |
| 288 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 289 | func (m *equalMatcher) Test(ctx ValueMatcherContext, value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 290 | return m.expected == value |
| 291 | } |
| 292 | |
| 293 | func (m *equalMatcher) String() string { |
| 294 | return "=" + m.expected |
| 295 | } |
| 296 | |
| 297 | type anyMatcher struct { |
| 298 | } |
| 299 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 300 | func (m *anyMatcher) Test(ctx ValueMatcherContext, value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 301 | return true |
| 302 | } |
| 303 | |
| 304 | func (m *anyMatcher) String() string { |
| 305 | return "=*" |
| 306 | } |
| 307 | |
| 308 | var anyMatcherInstance = &anyMatcher{} |
| 309 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 310 | type startsWithMatcher struct { |
| 311 | prefix string |
| 312 | } |
| 313 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 314 | func (m *startsWithMatcher) Test(ctx ValueMatcherContext, value string) bool { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 315 | return strings.HasPrefix(value, m.prefix) |
| 316 | } |
| 317 | |
| 318 | func (m *startsWithMatcher) String() string { |
| 319 | return ".starts-with(" + m.prefix + ")" |
| 320 | } |
| 321 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 322 | type regexMatcher struct { |
| 323 | re *regexp.Regexp |
| 324 | } |
| 325 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 326 | func (m *regexMatcher) Test(ctx ValueMatcherContext, value string) bool { |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 327 | return m.re.MatchString(value) |
| 328 | } |
| 329 | |
| 330 | func (m *regexMatcher) String() string { |
| 331 | return ".regexp(" + m.re.String() + ")" |
| 332 | } |
| 333 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 334 | type notInListMatcher struct { |
| 335 | allowed []string |
| 336 | } |
| 337 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 338 | func (m *notInListMatcher) Test(ctx ValueMatcherContext, value string) bool { |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 339 | return !InList(value, m.allowed) |
| 340 | } |
| 341 | |
| 342 | func (m *notInListMatcher) String() string { |
| 343 | return ".not-in-list(" + strings.Join(m.allowed, ",") + ")" |
| 344 | } |
| 345 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 346 | type isSetMatcher struct{} |
| 347 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 348 | func (m *isSetMatcher) Test(ctx ValueMatcherContext, value string) bool { |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 349 | return value != "" |
| 350 | } |
| 351 | |
| 352 | func (m *isSetMatcher) String() string { |
| 353 | return ".is-set" |
| 354 | } |
| 355 | |
| 356 | var isSetMatcherInstance = &isSetMatcher{} |
| 357 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 358 | type sdkVersionMatcher struct { |
| 359 | condition func(ctx ValueMatcherContext, spec SdkSpec) bool |
| 360 | description string |
| 361 | } |
| 362 | |
| 363 | func (m *sdkVersionMatcher) Test(ctx ValueMatcherContext, value string) bool { |
| 364 | return m.condition(ctx, SdkSpecFromWithConfig(ctx.Config(), value)) |
| 365 | } |
| 366 | |
| 367 | func (m *sdkVersionMatcher) String() string { |
| 368 | return ".sdk-version(" + m.description + ")" |
| 369 | } |
| 370 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 371 | type ruleProperty struct { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 372 | fields []string // e.x.: Vndk.Enabled |
| 373 | matcher ValueMatcher |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame^] | 376 | func (r *ruleProperty) String() string { |
| 377 | return fmt.Sprintf("%q matches: %s", strings.Join(r.fields, "."), r.matcher) |
| 378 | } |
| 379 | |
| 380 | type ruleProperties []ruleProperty |
| 381 | |
| 382 | func (r ruleProperties) String() string { |
| 383 | var s []string |
| 384 | for _, r := range r { |
| 385 | s = append(s, r.String()) |
| 386 | } |
| 387 | return strings.Join(s, " ") |
| 388 | } |
| 389 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 390 | // A NeverAllow rule. |
| 391 | type Rule interface { |
| 392 | In(path ...string) Rule |
| 393 | |
| 394 | NotIn(path ...string) Rule |
| 395 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 396 | InDirectDeps(deps ...string) Rule |
| 397 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 398 | WithOsClass(osClasses ...OsClass) Rule |
| 399 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 400 | ModuleType(types ...string) Rule |
| 401 | |
| 402 | NotModuleType(types ...string) Rule |
| 403 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 404 | BootclasspathJar() Rule |
| 405 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 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 | |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame^] | 521 | // BootclasspathJar whether this rule only applies to Jars in the Bootclasspath |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 522 | func (r *rule) BootclasspathJar() Rule { |
| 523 | r.onlyBootclasspathJar = true |
| 524 | return r |
| 525 | } |
| 526 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 527 | func (r *rule) String() string { |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame^] | 528 | s := []string{"neverallow requirements. Not allowed:"} |
| 529 | if len(r.paths) > 0 { |
| 530 | s = append(s, fmt.Sprintf("in dirs: %q", r.paths)) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 531 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame^] | 532 | if len(r.moduleTypes) > 0 { |
| 533 | s = append(s, fmt.Sprintf("module types: %q", r.moduleTypes)) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 534 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame^] | 535 | if len(r.props) > 0 { |
| 536 | s = append(s, fmt.Sprintf("properties matching: %s", r.props)) |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 537 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame^] | 538 | if len(r.directDeps) > 0 { |
| 539 | s = append(s, fmt.Sprintf("dep(s): %q", SortedStringKeys(r.directDeps))) |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 540 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame^] | 541 | if len(r.osClasses) > 0 { |
| 542 | s = append(s, fmt.Sprintf("os class(es): %q", r.osClasses)) |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 543 | } |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 544 | if r.onlyBootclasspathJar { |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame^] | 545 | s = append(s, "in bootclasspath jar") |
| 546 | } |
| 547 | if len(r.unlessPaths) > 0 { |
| 548 | s = append(s, fmt.Sprintf("EXCEPT in dirs: %q", r.unlessPaths)) |
| 549 | } |
| 550 | if len(r.unlessModuleTypes) > 0 { |
| 551 | s = append(s, fmt.Sprintf("EXCEPT module types: %q", r.unlessModuleTypes)) |
| 552 | } |
| 553 | if len(r.unlessProps) > 0 { |
| 554 | s = append(s, fmt.Sprintf("EXCEPT properties matching: %q", r.unlessProps)) |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 555 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 556 | if len(r.reason) != 0 { |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame^] | 557 | s = append(s, " which is restricted because "+r.reason) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 558 | } |
Liz Kammer | a3d7915 | 2021-10-28 18:14:04 -0400 | [diff] [blame^] | 559 | if len(s) == 1 { |
| 560 | s[0] = "neverallow requirements (empty)" |
| 561 | } |
| 562 | return strings.Join(s, "\n\t") |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | func (r *rule) appliesToPath(dir string) bool { |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 566 | includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths) |
| 567 | excludePath := HasAnyPrefix(dir, r.unlessPaths) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 568 | return includePath && !excludePath |
| 569 | } |
| 570 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 571 | func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool { |
| 572 | if len(r.directDeps) == 0 { |
| 573 | return true |
| 574 | } |
| 575 | |
| 576 | matches := false |
| 577 | ctx.VisitDirectDeps(func(m Module) { |
| 578 | if !matches { |
| 579 | name := ctx.OtherModuleName(m) |
| 580 | matches = r.directDeps[name] |
| 581 | } |
| 582 | }) |
| 583 | |
| 584 | return matches |
| 585 | } |
| 586 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 587 | func (r *rule) appliesToBootclasspathJar(ctx BottomUpMutatorContext) bool { |
| 588 | if !r.onlyBootclasspathJar { |
| 589 | return true |
| 590 | } |
| 591 | |
| 592 | return InList(ctx.ModuleName(), ctx.Config().BootJars()) |
| 593 | } |
| 594 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 595 | func (r *rule) appliesToOsClass(osClass OsClass) bool { |
| 596 | if len(r.osClasses) == 0 { |
| 597 | return true |
| 598 | } |
| 599 | |
| 600 | for _, c := range r.osClasses { |
| 601 | if c == osClass { |
| 602 | return true |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | return false |
| 607 | } |
| 608 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 609 | func (r *rule) appliesToModuleType(moduleType string) bool { |
| 610 | return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) |
| 611 | } |
| 612 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 613 | func (r *rule) appliesToProperties(ctx ValueMatcherContext, |
| 614 | properties []interface{}) bool { |
| 615 | includeProps := hasAllProperties(ctx, properties, r.props) |
| 616 | excludeProps := hasAnyProperty(ctx, properties, r.unlessProps) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 617 | return includeProps && !excludeProps |
| 618 | } |
| 619 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 620 | func StartsWith(prefix string) ValueMatcher { |
| 621 | return &startsWithMatcher{prefix} |
| 622 | } |
| 623 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 624 | func Regexp(re string) ValueMatcher { |
| 625 | r, err := regexp.Compile(re) |
| 626 | if err != nil { |
| 627 | panic(err) |
| 628 | } |
| 629 | return ®exMatcher{r} |
| 630 | } |
| 631 | |
Andrei Onea | 115e7e7 | 2020-06-05 21:14:03 +0100 | [diff] [blame] | 632 | func NotInList(allowed []string) ValueMatcher { |
| 633 | return ¬InListMatcher{allowed} |
| 634 | } |
| 635 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 636 | func LessThanSdkVersion(sdk string) ValueMatcher { |
| 637 | return &sdkVersionMatcher{ |
| 638 | condition: func(ctx ValueMatcherContext, spec SdkSpec) bool { |
| 639 | return spec.ApiLevel.LessThan( |
| 640 | SdkSpecFromWithConfig(ctx.Config(), sdk).ApiLevel) |
| 641 | }, |
| 642 | description: "lessThan=" + sdk, |
| 643 | } |
| 644 | } |
| 645 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 646 | // assorted utils |
| 647 | |
| 648 | func cleanPaths(paths []string) []string { |
| 649 | res := make([]string, len(paths)) |
| 650 | for i, v := range paths { |
| 651 | res[i] = filepath.Clean(v) + "/" |
| 652 | } |
| 653 | return res |
| 654 | } |
| 655 | |
| 656 | func fieldNamesForProperties(propertyNames string) []string { |
| 657 | names := strings.Split(propertyNames, ".") |
| 658 | for i, v := range names { |
| 659 | names[i] = proptools.FieldNameForProperty(v) |
| 660 | } |
| 661 | return names |
| 662 | } |
| 663 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 664 | func hasAnyProperty(ctx ValueMatcherContext, properties []interface{}, |
| 665 | props []ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 666 | for _, v := range props { |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 667 | if hasProperty(ctx, properties, v) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 668 | return true |
| 669 | } |
| 670 | } |
| 671 | return false |
| 672 | } |
| 673 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 674 | func hasAllProperties(ctx ValueMatcherContext, properties []interface{}, |
| 675 | props []ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 676 | for _, v := range props { |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 677 | if !hasProperty(ctx, properties, v) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 678 | return false |
| 679 | } |
| 680 | } |
| 681 | return true |
| 682 | } |
| 683 | |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 684 | func hasProperty(ctx ValueMatcherContext, properties []interface{}, |
| 685 | prop ruleProperty) bool { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 686 | for _, propertyStruct := range properties { |
| 687 | propertiesValue := reflect.ValueOf(propertyStruct).Elem() |
| 688 | for _, v := range prop.fields { |
| 689 | if !propertiesValue.IsValid() { |
| 690 | break |
| 691 | } |
| 692 | propertiesValue = propertiesValue.FieldByName(v) |
| 693 | } |
| 694 | if !propertiesValue.IsValid() { |
| 695 | continue |
| 696 | } |
| 697 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 698 | check := func(value string) bool { |
Remi NGUYEN VAN | 1fdd6ca | 2021-12-02 19:39:35 +0900 | [diff] [blame] | 699 | return prop.matcher.Test(ctx, value) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | if matchValue(propertiesValue, check) { |
| 703 | return true |
| 704 | } |
| 705 | } |
| 706 | return false |
| 707 | } |
| 708 | |
| 709 | func matchValue(value reflect.Value, check func(string) bool) bool { |
| 710 | if !value.IsValid() { |
| 711 | return false |
| 712 | } |
| 713 | |
| 714 | if value.Kind() == reflect.Ptr { |
| 715 | if value.IsNil() { |
| 716 | return check("") |
| 717 | } |
| 718 | value = value.Elem() |
| 719 | } |
| 720 | |
| 721 | switch value.Kind() { |
| 722 | case reflect.String: |
| 723 | return check(value.String()) |
| 724 | case reflect.Bool: |
| 725 | return check(strconv.FormatBool(value.Bool())) |
| 726 | case reflect.Int: |
| 727 | return check(strconv.FormatInt(value.Int(), 10)) |
| 728 | case reflect.Slice: |
| 729 | slice, ok := value.Interface().([]string) |
| 730 | if !ok { |
| 731 | panic("Can only handle slice of string") |
| 732 | } |
| 733 | for _, v := range slice { |
| 734 | if check(v) { |
| 735 | return true |
| 736 | } |
| 737 | } |
| 738 | return false |
| 739 | } |
| 740 | |
| 741 | panic("Can't handle type: " + value.Kind().String()) |
| 742 | } |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 743 | |
| 744 | var neverallowRulesKey = NewOnceKey("neverallowRules") |
| 745 | |
| 746 | func neverallowRules(config Config) []Rule { |
| 747 | return config.Once(neverallowRulesKey, func() interface{} { |
| 748 | // No test rules were set by setTestNeverallowRules, use the global rules |
| 749 | return neverallows |
| 750 | }).([]Rule) |
| 751 | } |
| 752 | |
| 753 | // Overrides the default neverallow rules for the supplied config. |
| 754 | // |
| 755 | // For testing only. |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 756 | func setTestNeverallowRules(config Config, testRules []Rule) { |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 757 | config.Once(neverallowRulesKey, func() interface{} { return testRules }) |
| 758 | } |
Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 759 | |
| 760 | // Prepares for a test by setting neverallow rules and enabling the mutator. |
| 761 | // |
| 762 | // If the supplied rules are nil then the default rules are used. |
| 763 | func PrepareForTestWithNeverallowRules(testRules []Rule) FixturePreparer { |
| 764 | return GroupFixturePreparers( |
| 765 | FixtureModifyConfig(func(config Config) { |
| 766 | if testRules != nil { |
| 767 | setTestNeverallowRules(config, testRules) |
| 768 | } |
| 769 | }), |
| 770 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 771 | ctx.PostDepsMutators(registerNeverallowMutator) |
| 772 | }), |
| 773 | ) |
| 774 | } |