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