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