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