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