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()...) |
David Srbecky | e033cba | 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 | 0ecf0b2 | 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 | e5c3b85 | 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 | |
Roland Levillain | aca9449 | 2020-02-13 15:22:05 +0000 | [diff] [blame^] | 148 | // Additional whitelisted path only used for ART testing, which needs access to core library |
| 149 | // targets. This does not affect the contents of a device image (system, vendor, etc.). |
| 150 | var artTests = []string{ |
| 151 | "art/test", |
| 152 | } |
| 153 | |
| 154 | // Core library constraints. The sdk_version: "none" can only be used in core library projects and ART tests. |
Paul Duffin | a3d0986 | 2019-06-11 13:40:47 +0100 | [diff] [blame] | 155 | // Access to core library targets is restricted using visibility rules. |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 156 | rules := []Rule{ |
| 157 | NeverAllow(). |
| 158 | NotIn(coreLibraryProjects...). |
Roland Levillain | aca9449 | 2020-02-13 15:22:05 +0000 | [diff] [blame^] | 159 | NotIn(artTests...). |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 160 | With("sdk_version", "none"). |
| 161 | WithoutMatcher("name", Regexp("^android_.*stubs_current$")), |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Neil Fuller | df5f356 | 2018-10-21 17:19:10 +0100 | [diff] [blame] | 164 | return rules |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 167 | func createMediaRules() []Rule { |
| 168 | return []Rule{ |
| 169 | NeverAllow(). |
| 170 | With("libs", "updatable-media"). |
| 171 | Because("updatable-media includes private APIs. Use updatable_media_stubs instead."), |
Dongwon Kang | 50a299f | 2019-02-04 09:00:51 -0800 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 175 | func createJavaDeviceForHostRules() []Rule { |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 176 | javaDeviceForHostProjectsAllowedList := []string{ |
Colin Cross | b5191a5 | 2019-04-11 14:07:38 -0700 | [diff] [blame] | 177 | "external/guava", |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 178 | "external/robolectric-shadows", |
| 179 | "framework/layoutlib", |
| 180 | } |
| 181 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 182 | return []Rule{ |
| 183 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 184 | NotIn(javaDeviceForHostProjectsAllowedList...). |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 185 | ModuleType("java_device_for_host", "java_host_for_device"). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 186 | Because("java_device_for_host can only be used in allowed projects"), |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 190 | func createCcSdkVariantRules() []Rule { |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 191 | sdkVersionOnlyAllowedList := []string{ |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 192 | // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk. |
| 193 | // This sometimes works because the APEX modules that contain derive_sdk and |
| 194 | // derive_sdk_prefer32 suppress the platform installation rules, but fails when |
| 195 | // the APEX modules contain the SDK variant and the platform variant still exists. |
| 196 | "frameworks/base/apex/sdkextensions/derive_sdk", |
Dan Albert | e2054a9 | 2020-04-20 14:46:47 -0700 | [diff] [blame] | 197 | // These are for apps and shouldn't be used by non-SDK variant modules. |
| 198 | "prebuilts/ndk", |
| 199 | "tools/test/graphicsbenchmark/apps/sample_app", |
| 200 | "tools/test/graphicsbenchmark/functional_tests/java", |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 203 | platformVariantPropertiesAllowedList := []string{ |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 204 | // android_native_app_glue and libRSSupport use native_window.h but target old |
| 205 | // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist, |
| 206 | // so they can't add libnativewindow to shared_libs to get the header directory |
| 207 | // for the platform variant. Allow them to use the platform variant |
| 208 | // property to set shared_libs. |
| 209 | "prebuilts/ndk", |
| 210 | "frameworks/rs", |
| 211 | } |
| 212 | |
| 213 | return []Rule{ |
| 214 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 215 | NotIn(sdkVersionOnlyAllowedList...). |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 216 | WithMatcher("sdk_variant_only", isSetMatcherInstance). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 217 | Because("sdk_variant_only can only be used in allowed projects"), |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 218 | NeverAllow(). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 219 | NotIn(platformVariantPropertiesAllowedList...). |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 220 | WithMatcher("platform.shared_libs", isSetMatcherInstance). |
Colin Cross | 440e0d0 | 2020-06-11 11:32:11 -0700 | [diff] [blame] | 221 | Because("platform variant properties can only be used in allowed projects"), |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
David Srbecky | e033cba | 2020-05-20 22:20:28 +0100 | [diff] [blame] | 225 | func createUncompressDexRules() []Rule { |
| 226 | return []Rule{ |
| 227 | NeverAllow(). |
| 228 | NotIn("art"). |
| 229 | WithMatcher("uncompress_dex", isSetMatcherInstance). |
| 230 | Because("uncompress_dex is only allowed for certain jars for test in art."), |
| 231 | } |
| 232 | } |
| 233 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 234 | func neverallowMutator(ctx BottomUpMutatorContext) { |
| 235 | m, ok := ctx.Module().(Module) |
| 236 | if !ok { |
| 237 | return |
| 238 | } |
| 239 | |
| 240 | dir := ctx.ModuleDir() + "/" |
| 241 | properties := m.GetProperties() |
| 242 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 243 | osClass := ctx.Module().Target().Os.Class |
| 244 | |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 245 | for _, r := range neverallowRules(ctx.Config()) { |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 246 | n := r.(*rule) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 247 | if !n.appliesToPath(dir) { |
| 248 | continue |
| 249 | } |
| 250 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 251 | if !n.appliesToModuleType(ctx.ModuleType()) { |
| 252 | continue |
| 253 | } |
| 254 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 255 | if !n.appliesToProperties(properties) { |
| 256 | continue |
| 257 | } |
| 258 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 259 | if !n.appliesToOsClass(osClass) { |
| 260 | continue |
| 261 | } |
| 262 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 263 | if !n.appliesToDirectDeps(ctx) { |
| 264 | continue |
| 265 | } |
| 266 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 267 | ctx.ModuleErrorf("violates " + n.String()) |
| 268 | } |
| 269 | } |
| 270 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 271 | type ValueMatcher interface { |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 272 | Test(string) bool |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 273 | String() string |
| 274 | } |
| 275 | |
| 276 | type equalMatcher struct { |
| 277 | expected string |
| 278 | } |
| 279 | |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 280 | func (m *equalMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 281 | return m.expected == value |
| 282 | } |
| 283 | |
| 284 | func (m *equalMatcher) String() string { |
| 285 | return "=" + m.expected |
| 286 | } |
| 287 | |
| 288 | type anyMatcher struct { |
| 289 | } |
| 290 | |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 291 | func (m *anyMatcher) Test(value string) bool { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 292 | return true |
| 293 | } |
| 294 | |
| 295 | func (m *anyMatcher) String() string { |
| 296 | return "=*" |
| 297 | } |
| 298 | |
| 299 | var anyMatcherInstance = &anyMatcher{} |
| 300 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 301 | type startsWithMatcher struct { |
| 302 | prefix string |
| 303 | } |
| 304 | |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 305 | func (m *startsWithMatcher) Test(value string) bool { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 306 | return strings.HasPrefix(value, m.prefix) |
| 307 | } |
| 308 | |
| 309 | func (m *startsWithMatcher) String() string { |
| 310 | return ".starts-with(" + m.prefix + ")" |
| 311 | } |
| 312 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 313 | type regexMatcher struct { |
| 314 | re *regexp.Regexp |
| 315 | } |
| 316 | |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 317 | func (m *regexMatcher) Test(value string) bool { |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 318 | return m.re.MatchString(value) |
| 319 | } |
| 320 | |
| 321 | func (m *regexMatcher) String() string { |
| 322 | return ".regexp(" + m.re.String() + ")" |
| 323 | } |
| 324 | |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 325 | type isSetMatcher struct{} |
| 326 | |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 327 | func (m *isSetMatcher) Test(value string) bool { |
Colin Cross | c511bc5 | 2020-04-07 16:50:32 +0000 | [diff] [blame] | 328 | return value != "" |
| 329 | } |
| 330 | |
| 331 | func (m *isSetMatcher) String() string { |
| 332 | return ".is-set" |
| 333 | } |
| 334 | |
| 335 | var isSetMatcherInstance = &isSetMatcher{} |
| 336 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 337 | type ruleProperty struct { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 338 | fields []string // e.x.: Vndk.Enabled |
| 339 | matcher ValueMatcher |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 342 | // A NeverAllow rule. |
| 343 | type Rule interface { |
| 344 | In(path ...string) Rule |
| 345 | |
| 346 | NotIn(path ...string) Rule |
| 347 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 348 | InDirectDeps(deps ...string) Rule |
| 349 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 350 | WithOsClass(osClasses ...OsClass) Rule |
| 351 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 352 | ModuleType(types ...string) Rule |
| 353 | |
| 354 | NotModuleType(types ...string) Rule |
| 355 | |
| 356 | With(properties, value string) Rule |
| 357 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 358 | WithMatcher(properties string, matcher ValueMatcher) Rule |
| 359 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 360 | Without(properties, value string) Rule |
| 361 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 362 | WithoutMatcher(properties string, matcher ValueMatcher) Rule |
| 363 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 364 | Because(reason string) Rule |
| 365 | } |
| 366 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 367 | type rule struct { |
| 368 | // User string for why this is a thing. |
| 369 | reason string |
| 370 | |
| 371 | paths []string |
| 372 | unlessPaths []string |
| 373 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 374 | directDeps map[string]bool |
| 375 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 376 | osClasses []OsClass |
| 377 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 378 | moduleTypes []string |
| 379 | unlessModuleTypes []string |
| 380 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 381 | props []ruleProperty |
| 382 | unlessProps []ruleProperty |
| 383 | } |
| 384 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 385 | // Create a new NeverAllow rule. |
| 386 | func NeverAllow() Rule { |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 387 | return &rule{directDeps: make(map[string]bool)} |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 388 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 389 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 390 | func (r *rule) In(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 391 | r.paths = append(r.paths, cleanPaths(path)...) |
| 392 | return r |
| 393 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 394 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 395 | func (r *rule) NotIn(path ...string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 396 | r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...) |
| 397 | return r |
| 398 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 399 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 400 | func (r *rule) InDirectDeps(deps ...string) Rule { |
| 401 | for _, d := range deps { |
| 402 | r.directDeps[d] = true |
| 403 | } |
| 404 | return r |
| 405 | } |
| 406 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 407 | func (r *rule) WithOsClass(osClasses ...OsClass) Rule { |
| 408 | r.osClasses = append(r.osClasses, osClasses...) |
| 409 | return r |
| 410 | } |
| 411 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 412 | func (r *rule) ModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 413 | r.moduleTypes = append(r.moduleTypes, types...) |
| 414 | return r |
| 415 | } |
| 416 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 417 | func (r *rule) NotModuleType(types ...string) Rule { |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 418 | r.unlessModuleTypes = append(r.unlessModuleTypes, types...) |
| 419 | return r |
| 420 | } |
| 421 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 422 | func (r *rule) With(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 423 | return r.WithMatcher(properties, selectMatcher(value)) |
| 424 | } |
| 425 | |
| 426 | func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 427 | r.props = append(r.props, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 428 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 429 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 430 | }) |
| 431 | return r |
| 432 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 433 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 434 | func (r *rule) Without(properties, value string) Rule { |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 435 | return r.WithoutMatcher(properties, selectMatcher(value)) |
| 436 | } |
| 437 | |
| 438 | func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 439 | r.unlessProps = append(r.unlessProps, ruleProperty{ |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 440 | fields: fieldNamesForProperties(properties), |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 441 | matcher: matcher, |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 442 | }) |
| 443 | return r |
| 444 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 445 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 446 | func selectMatcher(expected string) ValueMatcher { |
| 447 | if expected == "*" { |
| 448 | return anyMatcherInstance |
| 449 | } |
| 450 | return &equalMatcher{expected: expected} |
| 451 | } |
| 452 | |
Paul Duffin | 730f2a5 | 2019-06-27 14:08:51 +0100 | [diff] [blame] | 453 | func (r *rule) Because(reason string) Rule { |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 454 | r.reason = reason |
| 455 | return r |
| 456 | } |
| 457 | |
| 458 | func (r *rule) String() string { |
| 459 | s := "neverallow" |
| 460 | for _, v := range r.paths { |
| 461 | s += " dir:" + v + "*" |
| 462 | } |
| 463 | for _, v := range r.unlessPaths { |
| 464 | s += " -dir:" + v + "*" |
| 465 | } |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 466 | for _, v := range r.moduleTypes { |
| 467 | s += " type:" + v |
| 468 | } |
| 469 | for _, v := range r.unlessModuleTypes { |
| 470 | s += " -type:" + v |
| 471 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 472 | for _, v := range r.props { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 473 | s += " " + strings.Join(v.fields, ".") + v.matcher.String() |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 474 | } |
| 475 | for _, v := range r.unlessProps { |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 476 | s += " -" + strings.Join(v.fields, ".") + v.matcher.String() |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 477 | } |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 478 | for k := range r.directDeps { |
| 479 | s += " deps:" + k |
| 480 | } |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 481 | for _, v := range r.osClasses { |
| 482 | s += " os:" + v.String() |
| 483 | } |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 484 | if len(r.reason) != 0 { |
| 485 | s += " which is restricted because " + r.reason |
| 486 | } |
| 487 | return s |
| 488 | } |
| 489 | |
| 490 | func (r *rule) appliesToPath(dir string) bool { |
Jaewoong Jung | 3aff578 | 2020-02-11 07:54:35 -0800 | [diff] [blame] | 491 | includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths) |
| 492 | excludePath := HasAnyPrefix(dir, r.unlessPaths) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 493 | return includePath && !excludePath |
| 494 | } |
| 495 | |
Paul Duffin | 3578188 | 2019-07-25 15:41:09 +0100 | [diff] [blame] | 496 | func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool { |
| 497 | if len(r.directDeps) == 0 { |
| 498 | return true |
| 499 | } |
| 500 | |
| 501 | matches := false |
| 502 | ctx.VisitDirectDeps(func(m Module) { |
| 503 | if !matches { |
| 504 | name := ctx.OtherModuleName(m) |
| 505 | matches = r.directDeps[name] |
| 506 | } |
| 507 | }) |
| 508 | |
| 509 | return matches |
| 510 | } |
| 511 | |
Paul Duffin | f1c9bbe | 2019-07-26 10:48:06 +0100 | [diff] [blame] | 512 | func (r *rule) appliesToOsClass(osClass OsClass) bool { |
| 513 | if len(r.osClasses) == 0 { |
| 514 | return true |
| 515 | } |
| 516 | |
| 517 | for _, c := range r.osClasses { |
| 518 | if c == osClass { |
| 519 | return true |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | return false |
| 524 | } |
| 525 | |
Colin Cross | fd4f743 | 2019-03-05 15:06:16 -0800 | [diff] [blame] | 526 | func (r *rule) appliesToModuleType(moduleType string) bool { |
| 527 | return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes) |
| 528 | } |
| 529 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 530 | func (r *rule) appliesToProperties(properties []interface{}) bool { |
| 531 | includeProps := hasAllProperties(properties, r.props) |
| 532 | excludeProps := hasAnyProperty(properties, r.unlessProps) |
| 533 | return includeProps && !excludeProps |
| 534 | } |
| 535 | |
Paul Duffin | c811170 | 2019-07-22 12:13:55 +0100 | [diff] [blame] | 536 | func StartsWith(prefix string) ValueMatcher { |
| 537 | return &startsWithMatcher{prefix} |
| 538 | } |
| 539 | |
Anton Hansson | 4537640 | 2020-04-09 14:18:21 +0100 | [diff] [blame] | 540 | func Regexp(re string) ValueMatcher { |
| 541 | r, err := regexp.Compile(re) |
| 542 | if err != nil { |
| 543 | panic(err) |
| 544 | } |
| 545 | return ®exMatcher{r} |
| 546 | } |
| 547 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 548 | // assorted utils |
| 549 | |
| 550 | func cleanPaths(paths []string) []string { |
| 551 | res := make([]string, len(paths)) |
| 552 | for i, v := range paths { |
| 553 | res[i] = filepath.Clean(v) + "/" |
| 554 | } |
| 555 | return res |
| 556 | } |
| 557 | |
| 558 | func fieldNamesForProperties(propertyNames string) []string { |
| 559 | names := strings.Split(propertyNames, ".") |
| 560 | for i, v := range names { |
| 561 | names[i] = proptools.FieldNameForProperty(v) |
| 562 | } |
| 563 | return names |
| 564 | } |
| 565 | |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 566 | func hasAnyProperty(properties []interface{}, props []ruleProperty) bool { |
| 567 | for _, v := range props { |
| 568 | if hasProperty(properties, v) { |
| 569 | return true |
| 570 | } |
| 571 | } |
| 572 | return false |
| 573 | } |
| 574 | |
| 575 | func hasAllProperties(properties []interface{}, props []ruleProperty) bool { |
| 576 | for _, v := range props { |
| 577 | if !hasProperty(properties, v) { |
| 578 | return false |
| 579 | } |
| 580 | } |
| 581 | return true |
| 582 | } |
| 583 | |
| 584 | func hasProperty(properties []interface{}, prop ruleProperty) bool { |
| 585 | for _, propertyStruct := range properties { |
| 586 | propertiesValue := reflect.ValueOf(propertyStruct).Elem() |
| 587 | for _, v := range prop.fields { |
| 588 | if !propertiesValue.IsValid() { |
| 589 | break |
| 590 | } |
| 591 | propertiesValue = propertiesValue.FieldByName(v) |
| 592 | } |
| 593 | if !propertiesValue.IsValid() { |
| 594 | continue |
| 595 | } |
| 596 | |
Paul Duffin | 73bf054 | 2019-07-12 14:12:49 +0100 | [diff] [blame] | 597 | check := func(value string) bool { |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 598 | return prop.matcher.Test(value) |
Steven Moreland | 65b3fd9 | 2017-12-06 14:18:35 -0800 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | if matchValue(propertiesValue, check) { |
| 602 | return true |
| 603 | } |
| 604 | } |
| 605 | return false |
| 606 | } |
| 607 | |
| 608 | func matchValue(value reflect.Value, check func(string) bool) bool { |
| 609 | if !value.IsValid() { |
| 610 | return false |
| 611 | } |
| 612 | |
| 613 | if value.Kind() == reflect.Ptr { |
| 614 | if value.IsNil() { |
| 615 | return check("") |
| 616 | } |
| 617 | value = value.Elem() |
| 618 | } |
| 619 | |
| 620 | switch value.Kind() { |
| 621 | case reflect.String: |
| 622 | return check(value.String()) |
| 623 | case reflect.Bool: |
| 624 | return check(strconv.FormatBool(value.Bool())) |
| 625 | case reflect.Int: |
| 626 | return check(strconv.FormatInt(value.Int(), 10)) |
| 627 | case reflect.Slice: |
| 628 | slice, ok := value.Interface().([]string) |
| 629 | if !ok { |
| 630 | panic("Can only handle slice of string") |
| 631 | } |
| 632 | for _, v := range slice { |
| 633 | if check(v) { |
| 634 | return true |
| 635 | } |
| 636 | } |
| 637 | return false |
| 638 | } |
| 639 | |
| 640 | panic("Can't handle type: " + value.Kind().String()) |
| 641 | } |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 642 | |
| 643 | var neverallowRulesKey = NewOnceKey("neverallowRules") |
| 644 | |
| 645 | func neverallowRules(config Config) []Rule { |
| 646 | return config.Once(neverallowRulesKey, func() interface{} { |
| 647 | // No test rules were set by setTestNeverallowRules, use the global rules |
| 648 | return neverallows |
| 649 | }).([]Rule) |
| 650 | } |
| 651 | |
| 652 | // Overrides the default neverallow rules for the supplied config. |
| 653 | // |
| 654 | // For testing only. |
Artur Satayev | c5570ac | 2020-04-09 16:06:36 +0100 | [diff] [blame] | 655 | func SetTestNeverallowRules(config Config, testRules []Rule) { |
Paul Duffin | 115445b | 2019-08-07 15:31:07 +0100 | [diff] [blame] | 656 | config.Once(neverallowRulesKey, func() interface{} { return testRules }) |
| 657 | } |