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