Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "path/filepath" |
| 19 | "reflect" |
Anton Hansson | fec6c23 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 20 | "regexp" |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 21 | "strconv" |
| 22 | "strings" |
| 23 | |
| 24 | "github.com/google/blueprint/proptools" |
| 25 | ) |
| 26 | |
| 27 | // "neverallow" rules for the build system. |
| 28 | // |
| 29 | // This allows things which aren't related to the build system and are enforced |
| 30 | // for sanity, in progress code refactors, or policy to be expressed in a |
| 31 | // straightforward away disjoint from implementations and tests which should |
| 32 | // work regardless of these restrictions. |
| 33 | // |
| 34 | // A module is disallowed if all of the following are true: |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 35 | // - it is in one of the "In" paths |
| 36 | // - it is not in one of the "NotIn" paths |
| 37 | // - it has all "With" properties matched |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 38 | // - - values are matched in their entirety |
| 39 | // - - nil is interpreted as an empty string |
| 40 | // - - nested properties are separated with a '.' |
| 41 | // - - if the property is a list, any of the values in the list being matches |
| 42 | // counts as a match |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 43 | // - it has none of the "Without" properties matched (same rules as above) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 44 | |
Artur Satayev | b39ea9b | 2020-04-09 16:06:36 +0100 | [diff] [blame^] | 45 | func RegisterNeverallowMutator(ctx RegisterMutatorsContext) { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 46 | ctx.BottomUp("neverallow", neverallowMutator).Parallel() |
| 47 | } |
| 48 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 49 | var neverallows = []Rule{} |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 50 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 51 | func init() { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 52 | AddNeverAllowRules(createIncludeDirsRules()...) |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 53 | AddNeverAllowRules(createTrebleRules()...) |
| 54 | AddNeverAllowRules(createLibcoreRules()...) |
| 55 | AddNeverAllowRules(createMediaRules()...) |
| 56 | AddNeverAllowRules(createJavaDeviceForHostRules()...) |
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 | 98df0d1 | 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...). |
Anton Hansson | fec6c23 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 150 | With("sdk_version", "none"). |
| 151 | WithoutMatcher("name", Regexp("^android_.*stubs_current$")), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 154 | return rules |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 157 | func createMediaRules() []Rule { |
| 158 | return []Rule{ |
| 159 | NeverAllow(). |
| 160 | With("libs", "updatable-media"). |
| 161 | Because("updatable-media includes private APIs. Use updatable_media_stubs instead."), |
Dongwon Kang | 50a299f | 2019-02-04 09:00:51 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 165 | func createJavaDeviceForHostRules() []Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 166 | javaDeviceForHostProjectsWhitelist := []string{ |
Colin Cross | b5191a5 | 2019-04-11 14:07:38 -0700 | [diff] [blame] | 167 | "external/guava", |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 168 | "external/robolectric-shadows", |
| 169 | "framework/layoutlib", |
| 170 | } |
| 171 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 172 | return []Rule{ |
| 173 | NeverAllow(). |
| 174 | NotIn(javaDeviceForHostProjectsWhitelist...). |
| 175 | ModuleType("java_device_for_host", "java_host_for_device"). |
| 176 | Because("java_device_for_host can only be used in whitelisted projects"), |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 180 | func neverallowMutator(ctx BottomUpMutatorContext) { |
| 181 | m, ok := ctx.Module().(Module) |
| 182 | if !ok { |
| 183 | return |
| 184 | } |
| 185 | |
| 186 | dir := ctx.ModuleDir() + "/" |
| 187 | properties := m.GetProperties() |
| 188 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 189 | osClass := ctx.Module().Target().Os.Class |
| 190 | |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 191 | for _, r := range neverallowRules(ctx.Config()) { |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 192 | n := r.(*rule) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 193 | if !n.appliesToPath(dir) { |
| 194 | continue |
| 195 | } |
| 196 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 197 | if !n.appliesToModuleType(ctx.ModuleType()) { |
| 198 | continue |
| 199 | } |
| 200 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 201 | if !n.appliesToProperties(properties) { |
| 202 | continue |
| 203 | } |
| 204 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 205 | if !n.appliesToOsClass(osClass) { |
| 206 | continue |
| 207 | } |
| 208 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 209 | if !n.appliesToDirectDeps(ctx) { |
| 210 | continue |
| 211 | } |
| 212 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 213 | ctx.ModuleErrorf("violates " + n.String()) |
| 214 | } |
| 215 | } |
| 216 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 217 | type ValueMatcher interface { |
Artur Satayev | b39ea9b | 2020-04-09 16:06:36 +0100 | [diff] [blame^] | 218 | Test(string) bool |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 219 | String() string |
| 220 | } |
| 221 | |
| 222 | type equalMatcher struct { |
| 223 | expected string |
| 224 | } |
| 225 | |
Artur Satayev | b39ea9b | 2020-04-09 16:06:36 +0100 | [diff] [blame^] | 226 | func (m *equalMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 227 | return m.expected == value |
| 228 | } |
| 229 | |
| 230 | func (m *equalMatcher) String() string { |
| 231 | return "=" + m.expected |
| 232 | } |
| 233 | |
| 234 | type anyMatcher struct { |
| 235 | } |
| 236 | |
Artur Satayev | b39ea9b | 2020-04-09 16:06:36 +0100 | [diff] [blame^] | 237 | func (m *anyMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 238 | return true |
| 239 | } |
| 240 | |
| 241 | func (m *anyMatcher) String() string { |
| 242 | return "=*" |
| 243 | } |
| 244 | |
| 245 | var anyMatcherInstance = &anyMatcher{} |
| 246 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 247 | type startsWithMatcher struct { |
| 248 | prefix string |
| 249 | } |
| 250 | |
Artur Satayev | b39ea9b | 2020-04-09 16:06:36 +0100 | [diff] [blame^] | 251 | func (m *startsWithMatcher) Test(value string) bool { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 252 | return strings.HasPrefix(value, m.prefix) |
| 253 | } |
| 254 | |
| 255 | func (m *startsWithMatcher) String() string { |
| 256 | return ".starts-with(" + m.prefix + ")" |
| 257 | } |
| 258 | |
Anton Hansson | fec6c23 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 259 | type regexMatcher struct { |
| 260 | re *regexp.Regexp |
| 261 | } |
| 262 | |
Artur Satayev | b39ea9b | 2020-04-09 16:06:36 +0100 | [diff] [blame^] | 263 | func (m *regexMatcher) Test(value string) bool { |
Anton Hansson | fec6c23 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 264 | return m.re.MatchString(value) |
| 265 | } |
| 266 | |
| 267 | func (m *regexMatcher) String() string { |
| 268 | return ".regexp(" + m.re.String() + ")" |
| 269 | } |
| 270 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 271 | type ruleProperty struct { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 272 | fields []string // e.x.: Vndk.Enabled |
| 273 | matcher ValueMatcher |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 276 | // A NeverAllow rule. |
| 277 | type Rule interface { |
| 278 | In(path ...string) Rule |
| 279 | |
| 280 | NotIn(path ...string) Rule |
| 281 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 282 | InDirectDeps(deps ...string) Rule |
| 283 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 284 | WithOsClass(osClasses ...OsClass) Rule |
| 285 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 286 | ModuleType(types ...string) Rule |
| 287 | |
| 288 | NotModuleType(types ...string) Rule |
| 289 | |
| 290 | With(properties, value string) Rule |
| 291 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 292 | WithMatcher(properties string, matcher ValueMatcher) Rule |
| 293 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 294 | Without(properties, value string) Rule |
| 295 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 296 | WithoutMatcher(properties string, matcher ValueMatcher) Rule |
| 297 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 298 | Because(reason string) Rule |
| 299 | } |
| 300 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 301 | type rule struct { |
| 302 | // User string for why this is a thing. |
| 303 | reason string |
| 304 | |
| 305 | paths []string |
| 306 | unlessPaths []string |
| 307 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 308 | directDeps map[string]bool |
| 309 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 310 | osClasses []OsClass |
| 311 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 312 | moduleTypes []string |
| 313 | unlessModuleTypes []string |
| 314 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 315 | props []ruleProperty |
| 316 | unlessProps []ruleProperty |
| 317 | } |
| 318 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 319 | // Create a new NeverAllow rule. |
| 320 | func NeverAllow() Rule { |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 321 | return &rule{directDeps: make(map[string]bool)} |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 322 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 323 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 324 | func (r *rule) In(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 325 | r.paths = append(r.paths, cleanPaths(path)...) |
| 326 | return r |
| 327 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 328 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 329 | func (r *rule) NotIn(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 330 | r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...) |
| 331 | return r |
| 332 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 333 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 334 | func (r *rule) InDirectDeps(deps ...string) Rule { |
| 335 | for _, d := range deps { |
| 336 | r.directDeps[d] = true |
| 337 | } |
| 338 | return r |
| 339 | } |
| 340 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 341 | func (r *rule) WithOsClass(osClasses ...OsClass) Rule { |
| 342 | r.osClasses = append(r.osClasses, osClasses...) |
| 343 | return r |
| 344 | } |
| 345 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 346 | func (r *rule) ModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 347 | r.moduleTypes = append(r.moduleTypes, types...) |
| 348 | return r |
| 349 | } |
| 350 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 351 | func (r *rule) NotModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 352 | r.unlessModuleTypes = append(r.unlessModuleTypes, types...) |
| 353 | return r |
| 354 | } |
| 355 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 356 | func (r *rule) With(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 357 | return r.WithMatcher(properties, selectMatcher(value)) |
| 358 | } |
| 359 | |
| 360 | func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 361 | r.props = append(r.props, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 362 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 363 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 364 | }) |
| 365 | return r |
| 366 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 367 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 368 | func (r *rule) Without(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 369 | return r.WithoutMatcher(properties, selectMatcher(value)) |
| 370 | } |
| 371 | |
| 372 | func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 373 | r.unlessProps = append(r.unlessProps, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 374 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 375 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 376 | }) |
| 377 | return r |
| 378 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 379 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 380 | func selectMatcher(expected string) ValueMatcher { |
| 381 | if expected == "*" { |
| 382 | return anyMatcherInstance |
| 383 | } |
| 384 | return &equalMatcher{expected: expected} |
| 385 | } |
| 386 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 387 | func (r *rule) Because(reason string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 388 | r.reason = reason |
| 389 | return r |
| 390 | } |
| 391 | |
| 392 | func (r *rule) String() string { |
| 393 | s := "neverallow" |
| 394 | for _, v := range r.paths { |
| 395 | s += " dir:" + v + "*" |
| 396 | } |
| 397 | for _, v := range r.unlessPaths { |
| 398 | s += " -dir:" + v + "*" |
| 399 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 400 | for _, v := range r.moduleTypes { |
| 401 | s += " type:" + v |
| 402 | } |
| 403 | for _, v := range r.unlessModuleTypes { |
| 404 | s += " -type:" + v |
| 405 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 406 | for _, v := range r.props { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 407 | s += " " + strings.Join(v.fields, ".") + v.matcher.String() |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 408 | } |
| 409 | for _, v := range r.unlessProps { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 410 | s += " -" + strings.Join(v.fields, ".") + v.matcher.String() |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 411 | } |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 412 | for k := range r.directDeps { |
| 413 | s += " deps:" + k |
| 414 | } |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 415 | for _, v := range r.osClasses { |
| 416 | s += " os:" + v.String() |
| 417 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 418 | if len(r.reason) != 0 { |
| 419 | s += " which is restricted because " + r.reason |
| 420 | } |
| 421 | return s |
| 422 | } |
| 423 | |
| 424 | func (r *rule) appliesToPath(dir string) bool { |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 425 | includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths) |
| 426 | excludePath := HasAnyPrefix(dir, r.unlessPaths) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 427 | return includePath && !excludePath |
| 428 | } |
| 429 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 430 | func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool { |
| 431 | if len(r.directDeps) == 0 { |
| 432 | return true |
| 433 | } |
| 434 | |
| 435 | matches := false |
| 436 | ctx.VisitDirectDeps(func(m Module) { |
| 437 | if !matches { |
| 438 | name := ctx.OtherModuleName(m) |
| 439 | matches = r.directDeps[name] |
| 440 | } |
| 441 | }) |
| 442 | |
| 443 | return matches |
| 444 | } |
| 445 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 446 | func (r *rule) appliesToOsClass(osClass OsClass) bool { |
| 447 | if len(r.osClasses) == 0 { |
| 448 | return true |
| 449 | } |
| 450 | |
| 451 | for _, c := range r.osClasses { |
| 452 | if c == osClass { |
| 453 | return true |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | return false |
| 458 | } |
| 459 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 460 | func (r *rule) appliesToModuleType(moduleType string) bool { |
| 461 | return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) |
| 462 | } |
| 463 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 464 | func (r *rule) appliesToProperties(properties []interface{}) bool { |
| 465 | includeProps := hasAllProperties(properties, r.props) |
| 466 | excludeProps := hasAnyProperty(properties, r.unlessProps) |
| 467 | return includeProps && !excludeProps |
| 468 | } |
| 469 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 470 | func StartsWith(prefix string) ValueMatcher { |
| 471 | return &startsWithMatcher{prefix} |
| 472 | } |
| 473 | |
Anton Hansson | fec6c23 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 474 | func Regexp(re string) ValueMatcher { |
| 475 | r, err := regexp.Compile(re) |
| 476 | if err != nil { |
| 477 | panic(err) |
| 478 | } |
| 479 | return ®exMatcher{r} |
| 480 | } |
| 481 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 482 | // assorted utils |
| 483 | |
| 484 | func cleanPaths(paths []string) []string { |
| 485 | res := make([]string, len(paths)) |
| 486 | for i, v := range paths { |
| 487 | res[i] = filepath.Clean(v) + "/" |
| 488 | } |
| 489 | return res |
| 490 | } |
| 491 | |
| 492 | func fieldNamesForProperties(propertyNames string) []string { |
| 493 | names := strings.Split(propertyNames, ".") |
| 494 | for i, v := range names { |
| 495 | names[i] = proptools.FieldNameForProperty(v) |
| 496 | } |
| 497 | return names |
| 498 | } |
| 499 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 500 | func hasAnyProperty(properties []interface{}, props []ruleProperty) bool { |
| 501 | for _, v := range props { |
| 502 | if hasProperty(properties, v) { |
| 503 | return true |
| 504 | } |
| 505 | } |
| 506 | return false |
| 507 | } |
| 508 | |
| 509 | func hasAllProperties(properties []interface{}, props []ruleProperty) bool { |
| 510 | for _, v := range props { |
| 511 | if !hasProperty(properties, v) { |
| 512 | return false |
| 513 | } |
| 514 | } |
| 515 | return true |
| 516 | } |
| 517 | |
| 518 | func hasProperty(properties []interface{}, prop ruleProperty) bool { |
| 519 | for _, propertyStruct := range properties { |
| 520 | propertiesValue := reflect.ValueOf(propertyStruct).Elem() |
| 521 | for _, v := range prop.fields { |
| 522 | if !propertiesValue.IsValid() { |
| 523 | break |
| 524 | } |
| 525 | propertiesValue = propertiesValue.FieldByName(v) |
| 526 | } |
| 527 | if !propertiesValue.IsValid() { |
| 528 | continue |
| 529 | } |
| 530 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 531 | check := func(value string) bool { |
Artur Satayev | b39ea9b | 2020-04-09 16:06:36 +0100 | [diff] [blame^] | 532 | return prop.matcher.Test(value) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | if matchValue(propertiesValue, check) { |
| 536 | return true |
| 537 | } |
| 538 | } |
| 539 | return false |
| 540 | } |
| 541 | |
| 542 | func matchValue(value reflect.Value, check func(string) bool) bool { |
| 543 | if !value.IsValid() { |
| 544 | return false |
| 545 | } |
| 546 | |
| 547 | if value.Kind() == reflect.Ptr { |
| 548 | if value.IsNil() { |
| 549 | return check("") |
| 550 | } |
| 551 | value = value.Elem() |
| 552 | } |
| 553 | |
| 554 | switch value.Kind() { |
| 555 | case reflect.String: |
| 556 | return check(value.String()) |
| 557 | case reflect.Bool: |
| 558 | return check(strconv.FormatBool(value.Bool())) |
| 559 | case reflect.Int: |
| 560 | return check(strconv.FormatInt(value.Int(), 10)) |
| 561 | case reflect.Slice: |
| 562 | slice, ok := value.Interface().([]string) |
| 563 | if !ok { |
| 564 | panic("Can only handle slice of string") |
| 565 | } |
| 566 | for _, v := range slice { |
| 567 | if check(v) { |
| 568 | return true |
| 569 | } |
| 570 | } |
| 571 | return false |
| 572 | } |
| 573 | |
| 574 | panic("Can't handle type: " + value.Kind().String()) |
| 575 | } |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 576 | |
| 577 | var neverallowRulesKey = NewOnceKey("neverallowRules") |
| 578 | |
| 579 | func neverallowRules(config Config) []Rule { |
| 580 | return config.Once(neverallowRulesKey, func() interface{} { |
| 581 | // No test rules were set by setTestNeverallowRules, use the global rules |
| 582 | return neverallows |
| 583 | }).([]Rule) |
| 584 | } |
| 585 | |
| 586 | // Overrides the default neverallow rules for the supplied config. |
| 587 | // |
| 588 | // For testing only. |
Artur Satayev | b39ea9b | 2020-04-09 16:06:36 +0100 | [diff] [blame^] | 589 | func SetTestNeverallowRules(config Config, testRules []Rule) { |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 590 | config.Once(neverallowRulesKey, func() interface{} { return testRules }) |
| 591 | } |