blob: 2eba4a950bdf2907225607bfeff3b72a6773dc97 [file] [log] [blame]
Steven Moreland65b3fd92017-12-06 14:18:35 -08001// 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
15package android
16
17import (
18 "path/filepath"
19 "reflect"
Anton Hanssonfec6c232020-04-09 14:18:21 +010020 "regexp"
Steven Moreland65b3fd92017-12-06 14:18:35 -080021 "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 Duffin730f2a52019-06-27 14:08:51 +010035// - 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 Moreland65b3fd92017-12-06 14:18:35 -080038// - - 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 Duffin730f2a52019-06-27 14:08:51 +010043// - it has none of the "Without" properties matched (same rules as above)
Steven Moreland65b3fd92017-12-06 14:18:35 -080044
Artur Satayevb39ea9b2020-04-09 16:06:36 +010045func RegisterNeverallowMutator(ctx RegisterMutatorsContext) {
Steven Moreland65b3fd92017-12-06 14:18:35 -080046 ctx.BottomUp("neverallow", neverallowMutator).Parallel()
47}
48
Paul Duffin730f2a52019-06-27 14:08:51 +010049var neverallows = []Rule{}
Steven Moreland65b3fd92017-12-06 14:18:35 -080050
Paul Duffin730f2a52019-06-27 14:08:51 +010051func init() {
Paul Duffinc8111702019-07-22 12:13:55 +010052 AddNeverAllowRules(createIncludeDirsRules()...)
Paul Duffin730f2a52019-06-27 14:08:51 +010053 AddNeverAllowRules(createTrebleRules()...)
54 AddNeverAllowRules(createLibcoreRules()...)
55 AddNeverAllowRules(createMediaRules()...)
56 AddNeverAllowRules(createJavaDeviceForHostRules()...)
Colin Cross01fd7cc2020-02-19 16:54:04 -080057 AddNeverAllowRules(createCcSdkVariantRules()...)
David Srbecky98c71222020-05-20 22:20:28 +010058 AddNeverAllowRules(createUncompressDexRules()...)
Neil Fullerdf5f3562018-10-21 17:19:10 +010059}
Steven Moreland65b3fd92017-12-06 14:18:35 -080060
Paul Duffin730f2a52019-06-27 14:08:51 +010061// Add a NeverAllow rule to the set of rules to apply.
62func AddNeverAllowRules(rules ...Rule) {
63 neverallows = append(neverallows, rules...)
64}
65
Paul Duffinc8111702019-07-22 12:13:55 +010066func createIncludeDirsRules() []Rule {
67 // The list of paths that cannot be referenced using include_dirs
68 paths := []string{
69 "art",
Orion Hodson6341f012019-11-06 13:39:46 +000070 "art/libnativebridge",
71 "art/libnativeloader",
Paul Duffinc8111702019-07-22 12:13:55 +010072 "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 Duffinc8111702019-07-22 12:13:55 +010083 }
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 Duffin730f2a52019-06-27 14:08:51 +0100102func createTrebleRules() []Rule {
103 return []Rule{
104 NeverAllow().
105 In("vendor", "device").
106 With("vndk.enabled", "true").
107 Without("vendor", "true").
Justin Yun98df0d12020-02-28 15:07:59 +0900108 Without("product_specific", "true").
Paul Duffin730f2a52019-06-27 14:08:51 +0100109 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 Moreland65b3fd92017-12-06 14:18:35 -0800115
Neil Fullerdf5f3562018-10-21 17:19:10 +0100116 // TODO(b/67974785): always enforce the manifest
Paul Duffin730f2a52019-06-27 14:08:51 +0100117 NeverAllow().
Steven Moreland51ce4f62020-02-10 17:21:32 -0800118 Without("name", "libhidlbase-combined-impl").
119 Without("name", "libhidlbase").
120 Without("name", "libhidlbase_pgo").
Paul Duffin730f2a52019-06-27 14:08:51 +0100121 With("product_variables.enforce_vintf_manifest.cflags", "*").
122 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100123
124 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffin730f2a52019-06-27 14:08:51 +0100125 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 Fullerdf5f3562018-10-21 17:19:10 +0100129
130 // Example:
Paul Duffin730f2a52019-06-27 14:08:51 +0100131 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100132 }
133}
134
Paul Duffin730f2a52019-06-27 14:08:51 +0100135func createLibcoreRules() []Rule {
Neil Fullerdf5f3562018-10-21 17:19:10 +0100136 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 Duffincd9b6972020-05-12 15:27:56 +0100145 "prebuilts",
Neil Fullerdf5f3562018-10-21 17:19:10 +0100146 }
147
Paul Duffina3d09862019-06-11 13:40:47 +0100148 // 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 Duffin730f2a52019-06-27 14:08:51 +0100150 rules := []Rule{
151 NeverAllow().
152 NotIn(coreLibraryProjects...).
Anton Hanssonfec6c232020-04-09 14:18:21 +0100153 With("sdk_version", "none").
154 WithoutMatcher("name", Regexp("^android_.*stubs_current$")),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100155 }
156
Neil Fullerdf5f3562018-10-21 17:19:10 +0100157 return rules
Steven Moreland65b3fd92017-12-06 14:18:35 -0800158}
159
Paul Duffin730f2a52019-06-27 14:08:51 +0100160func 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 Kang50a299f2019-02-04 09:00:51 -0800165 }
166}
167
Paul Duffin730f2a52019-06-27 14:08:51 +0100168func createJavaDeviceForHostRules() []Rule {
Colin Cross95f7b342020-06-11 11:32:11 -0700169 javaDeviceForHostProjectsAllowedList := []string{
Colin Crossb5191a52019-04-11 14:07:38 -0700170 "external/guava",
Colin Crossfd4f7432019-03-05 15:06:16 -0800171 "external/robolectric-shadows",
172 "framework/layoutlib",
173 }
174
Paul Duffin730f2a52019-06-27 14:08:51 +0100175 return []Rule{
176 NeverAllow().
Colin Cross95f7b342020-06-11 11:32:11 -0700177 NotIn(javaDeviceForHostProjectsAllowedList...).
Paul Duffin730f2a52019-06-27 14:08:51 +0100178 ModuleType("java_device_for_host", "java_host_for_device").
Colin Cross95f7b342020-06-11 11:32:11 -0700179 Because("java_device_for_host can only be used in allowed projects"),
Colin Crossfd4f7432019-03-05 15:06:16 -0800180 }
181}
182
Colin Cross01fd7cc2020-02-19 16:54:04 -0800183func createCcSdkVariantRules() []Rule {
Colin Cross95f7b342020-06-11 11:32:11 -0700184 sdkVersionOnlyAllowedList := []string{
Colin Cross01fd7cc2020-02-19 16:54:04 -0800185 // 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 Cross95f7b342020-06-11 11:32:11 -0700192 platformVariantPropertiesAllowedList := []string{
Colin Cross01fd7cc2020-02-19 16:54:04 -0800193 // 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 Cross95f7b342020-06-11 11:32:11 -0700204 NotIn(sdkVersionOnlyAllowedList...).
Colin Cross01fd7cc2020-02-19 16:54:04 -0800205 WithMatcher("sdk_variant_only", isSetMatcherInstance).
Colin Cross95f7b342020-06-11 11:32:11 -0700206 Because("sdk_variant_only can only be used in allowed projects"),
Colin Cross01fd7cc2020-02-19 16:54:04 -0800207 NeverAllow().
Colin Cross95f7b342020-06-11 11:32:11 -0700208 NotIn(platformVariantPropertiesAllowedList...).
Colin Cross01fd7cc2020-02-19 16:54:04 -0800209 WithMatcher("platform.shared_libs", isSetMatcherInstance).
Colin Cross95f7b342020-06-11 11:32:11 -0700210 Because("platform variant properties can only be used in allowed projects"),
Colin Cross01fd7cc2020-02-19 16:54:04 -0800211 }
212}
213
David Srbecky98c71222020-05-20 22:20:28 +0100214func 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 Moreland65b3fd92017-12-06 14:18:35 -0800223func 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 Duffinf1c9bbe2019-07-26 10:48:06 +0100232 osClass := ctx.Module().Target().Os.Class
233
Paul Duffin115445b2019-08-07 15:31:07 +0100234 for _, r := range neverallowRules(ctx.Config()) {
Paul Duffin730f2a52019-06-27 14:08:51 +0100235 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800236 if !n.appliesToPath(dir) {
237 continue
238 }
239
Colin Crossfd4f7432019-03-05 15:06:16 -0800240 if !n.appliesToModuleType(ctx.ModuleType()) {
241 continue
242 }
243
Steven Moreland65b3fd92017-12-06 14:18:35 -0800244 if !n.appliesToProperties(properties) {
245 continue
246 }
247
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100248 if !n.appliesToOsClass(osClass) {
249 continue
250 }
251
Paul Duffin35781882019-07-25 15:41:09 +0100252 if !n.appliesToDirectDeps(ctx) {
253 continue
254 }
255
Andrei Onea115e7e72020-06-05 21:14:03 +0100256 if !n.appliesToBootclasspathJar(ctx) {
257 continue
258 }
259
Steven Moreland65b3fd92017-12-06 14:18:35 -0800260 ctx.ModuleErrorf("violates " + n.String())
261 }
262}
263
Paul Duffin73bf0542019-07-12 14:12:49 +0100264type ValueMatcher interface {
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100265 Test(string) bool
Paul Duffin73bf0542019-07-12 14:12:49 +0100266 String() string
267}
268
269type equalMatcher struct {
270 expected string
271}
272
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100273func (m *equalMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100274 return m.expected == value
275}
276
277func (m *equalMatcher) String() string {
278 return "=" + m.expected
279}
280
281type anyMatcher struct {
282}
283
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100284func (m *anyMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100285 return true
286}
287
288func (m *anyMatcher) String() string {
289 return "=*"
290}
291
292var anyMatcherInstance = &anyMatcher{}
293
Paul Duffinc8111702019-07-22 12:13:55 +0100294type startsWithMatcher struct {
295 prefix string
296}
297
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100298func (m *startsWithMatcher) Test(value string) bool {
Paul Duffinc8111702019-07-22 12:13:55 +0100299 return strings.HasPrefix(value, m.prefix)
300}
301
302func (m *startsWithMatcher) String() string {
303 return ".starts-with(" + m.prefix + ")"
304}
305
Anton Hanssonfec6c232020-04-09 14:18:21 +0100306type regexMatcher struct {
307 re *regexp.Regexp
308}
309
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100310func (m *regexMatcher) Test(value string) bool {
Anton Hanssonfec6c232020-04-09 14:18:21 +0100311 return m.re.MatchString(value)
312}
313
314func (m *regexMatcher) String() string {
315 return ".regexp(" + m.re.String() + ")"
316}
317
Andrei Onea115e7e72020-06-05 21:14:03 +0100318type notInListMatcher struct {
319 allowed []string
320}
321
322func (m *notInListMatcher) Test(value string) bool {
323 return !InList(value, m.allowed)
324}
325
326func (m *notInListMatcher) String() string {
327 return ".not-in-list(" + strings.Join(m.allowed, ",") + ")"
328}
329
Colin Cross01fd7cc2020-02-19 16:54:04 -0800330type isSetMatcher struct{}
331
332func (m *isSetMatcher) Test(value string) bool {
333 return value != ""
334}
335
336func (m *isSetMatcher) String() string {
337 return ".is-set"
338}
339
340var isSetMatcherInstance = &isSetMatcher{}
341
Steven Moreland65b3fd92017-12-06 14:18:35 -0800342type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100343 fields []string // e.x.: Vndk.Enabled
344 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800345}
346
Paul Duffin730f2a52019-06-27 14:08:51 +0100347// A NeverAllow rule.
348type Rule interface {
349 In(path ...string) Rule
350
351 NotIn(path ...string) Rule
352
Paul Duffin35781882019-07-25 15:41:09 +0100353 InDirectDeps(deps ...string) Rule
354
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100355 WithOsClass(osClasses ...OsClass) Rule
356
Paul Duffin730f2a52019-06-27 14:08:51 +0100357 ModuleType(types ...string) Rule
358
359 NotModuleType(types ...string) Rule
360
Andrei Onea115e7e72020-06-05 21:14:03 +0100361 BootclasspathJar() Rule
362
Paul Duffin730f2a52019-06-27 14:08:51 +0100363 With(properties, value string) Rule
364
Paul Duffinc8111702019-07-22 12:13:55 +0100365 WithMatcher(properties string, matcher ValueMatcher) Rule
366
Paul Duffin730f2a52019-06-27 14:08:51 +0100367 Without(properties, value string) Rule
368
Paul Duffinc8111702019-07-22 12:13:55 +0100369 WithoutMatcher(properties string, matcher ValueMatcher) Rule
370
Paul Duffin730f2a52019-06-27 14:08:51 +0100371 Because(reason string) Rule
372}
373
Steven Moreland65b3fd92017-12-06 14:18:35 -0800374type rule struct {
375 // User string for why this is a thing.
376 reason string
377
378 paths []string
379 unlessPaths []string
380
Paul Duffin35781882019-07-25 15:41:09 +0100381 directDeps map[string]bool
382
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100383 osClasses []OsClass
384
Colin Crossfd4f7432019-03-05 15:06:16 -0800385 moduleTypes []string
386 unlessModuleTypes []string
387
Steven Moreland65b3fd92017-12-06 14:18:35 -0800388 props []ruleProperty
389 unlessProps []ruleProperty
Andrei Onea115e7e72020-06-05 21:14:03 +0100390
391 onlyBootclasspathJar bool
Steven Moreland65b3fd92017-12-06 14:18:35 -0800392}
393
Paul Duffin730f2a52019-06-27 14:08:51 +0100394// Create a new NeverAllow rule.
395func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100396 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800397}
Colin Crossfd4f7432019-03-05 15:06:16 -0800398
Paul Duffin730f2a52019-06-27 14:08:51 +0100399func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800400 r.paths = append(r.paths, cleanPaths(path)...)
401 return r
402}
Colin Crossfd4f7432019-03-05 15:06:16 -0800403
Paul Duffin730f2a52019-06-27 14:08:51 +0100404func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800405 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
406 return r
407}
Colin Crossfd4f7432019-03-05 15:06:16 -0800408
Paul Duffin35781882019-07-25 15:41:09 +0100409func (r *rule) InDirectDeps(deps ...string) Rule {
410 for _, d := range deps {
411 r.directDeps[d] = true
412 }
413 return r
414}
415
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100416func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
417 r.osClasses = append(r.osClasses, osClasses...)
418 return r
419}
420
Paul Duffin730f2a52019-06-27 14:08:51 +0100421func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800422 r.moduleTypes = append(r.moduleTypes, types...)
423 return r
424}
425
Paul Duffin730f2a52019-06-27 14:08:51 +0100426func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800427 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
428 return r
429}
430
Paul Duffin730f2a52019-06-27 14:08:51 +0100431func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100432 return r.WithMatcher(properties, selectMatcher(value))
433}
434
435func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800436 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100437 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100438 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800439 })
440 return r
441}
Colin Crossfd4f7432019-03-05 15:06:16 -0800442
Paul Duffin730f2a52019-06-27 14:08:51 +0100443func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100444 return r.WithoutMatcher(properties, selectMatcher(value))
445}
446
447func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800448 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100449 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100450 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800451 })
452 return r
453}
Colin Crossfd4f7432019-03-05 15:06:16 -0800454
Paul Duffin73bf0542019-07-12 14:12:49 +0100455func selectMatcher(expected string) ValueMatcher {
456 if expected == "*" {
457 return anyMatcherInstance
458 }
459 return &equalMatcher{expected: expected}
460}
461
Paul Duffin730f2a52019-06-27 14:08:51 +0100462func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800463 r.reason = reason
464 return r
465}
466
Andrei Onea115e7e72020-06-05 21:14:03 +0100467func (r *rule) BootclasspathJar() Rule {
468 r.onlyBootclasspathJar = true
469 return r
470}
471
Steven Moreland65b3fd92017-12-06 14:18:35 -0800472func (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 Crossfd4f7432019-03-05 15:06:16 -0800480 for _, v := range r.moduleTypes {
481 s += " type:" + v
482 }
483 for _, v := range r.unlessModuleTypes {
484 s += " -type:" + v
485 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800486 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100487 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800488 }
489 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100490 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800491 }
Paul Duffin35781882019-07-25 15:41:09 +0100492 for k := range r.directDeps {
493 s += " deps:" + k
494 }
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100495 for _, v := range r.osClasses {
496 s += " os:" + v.String()
497 }
Andrei Onea115e7e72020-06-05 21:14:03 +0100498 if r.onlyBootclasspathJar {
499 s += " inBcp"
500 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800501 if len(r.reason) != 0 {
502 s += " which is restricted because " + r.reason
503 }
504 return s
505}
506
507func (r *rule) appliesToPath(dir string) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800508 includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
509 excludePath := HasAnyPrefix(dir, r.unlessPaths)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800510 return includePath && !excludePath
511}
512
Paul Duffin35781882019-07-25 15:41:09 +0100513func (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 Onea115e7e72020-06-05 21:14:03 +0100529func (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 Duffinf1c9bbe2019-07-26 10:48:06 +0100537func (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 Crossfd4f7432019-03-05 15:06:16 -0800551func (r *rule) appliesToModuleType(moduleType string) bool {
552 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
553}
554
Steven Moreland65b3fd92017-12-06 14:18:35 -0800555func (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 Duffinc8111702019-07-22 12:13:55 +0100561func StartsWith(prefix string) ValueMatcher {
562 return &startsWithMatcher{prefix}
563}
564
Anton Hanssonfec6c232020-04-09 14:18:21 +0100565func Regexp(re string) ValueMatcher {
566 r, err := regexp.Compile(re)
567 if err != nil {
568 panic(err)
569 }
570 return &regexMatcher{r}
571}
572
Andrei Onea115e7e72020-06-05 21:14:03 +0100573func NotInList(allowed []string) ValueMatcher {
574 return &notInListMatcher{allowed}
575}
576
Steven Moreland65b3fd92017-12-06 14:18:35 -0800577// assorted utils
578
579func 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
587func 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 Moreland65b3fd92017-12-06 14:18:35 -0800595func 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
604func 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
613func 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 Duffin73bf0542019-07-12 14:12:49 +0100626 check := func(value string) bool {
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100627 return prop.matcher.Test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800628 }
629
630 if matchValue(propertiesValue, check) {
631 return true
632 }
633 }
634 return false
635}
636
637func 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 Duffin115445b2019-08-07 15:31:07 +0100671
672var neverallowRulesKey = NewOnceKey("neverallowRules")
673
674func 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 Satayevb39ea9b2020-04-09 16:06:36 +0100684func SetTestNeverallowRules(config Config, testRules []Rule) {
Paul Duffin115445b2019-08-07 15:31:07 +0100685 config.Once(neverallowRulesKey, func() interface{} { return testRules })
686}