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