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