blob: cf09792e057c8c541dff1210b9a246be9be97176 [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 Hansson45376402020-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 Satayevc5570ac2020-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 Crossc511bc52020-04-07 16:50:32 +000057 AddNeverAllowRules(createCcSdkVariantRules()...)
Neil Fullerdf5f3562018-10-21 17:19:10 +010058}
Steven Moreland65b3fd92017-12-06 14:18:35 -080059
Paul Duffin730f2a52019-06-27 14:08:51 +010060// Add a NeverAllow rule to the set of rules to apply.
61func AddNeverAllowRules(rules ...Rule) {
62 neverallows = append(neverallows, rules...)
63}
64
Paul Duffinc8111702019-07-22 12:13:55 +010065func createIncludeDirsRules() []Rule {
66 // The list of paths that cannot be referenced using include_dirs
67 paths := []string{
68 "art",
Orion Hodson6341f012019-11-06 13:39:46 +000069 "art/libnativebridge",
70 "art/libnativeloader",
Paul Duffinc8111702019-07-22 12:13:55 +010071 "libcore",
72 "libnativehelper",
73 "external/apache-harmony",
74 "external/apache-xml",
75 "external/boringssl",
76 "external/bouncycastle",
77 "external/conscrypt",
78 "external/icu",
79 "external/okhttp",
80 "external/vixl",
81 "external/wycheproof",
Paul Duffinc8111702019-07-22 12:13:55 +010082 }
83
84 // Create a composite matcher that will match if the value starts with any of the restricted
85 // paths. A / is appended to the prefix to ensure that restricting path X does not affect paths
86 // XY.
87 rules := make([]Rule, 0, len(paths))
88 for _, path := range paths {
89 rule :=
90 NeverAllow().
91 WithMatcher("include_dirs", StartsWith(path+"/")).
92 Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" +
93 " to use alternate mechanisms and so can no longer be used.")
94
95 rules = append(rules, rule)
96 }
97
98 return rules
99}
100
Paul Duffin730f2a52019-06-27 14:08:51 +0100101func createTrebleRules() []Rule {
102 return []Rule{
103 NeverAllow().
104 In("vendor", "device").
105 With("vndk.enabled", "true").
106 Without("vendor", "true").
Justin Yun0ecf0b22020-02-28 15:07:59 +0900107 Without("product_specific", "true").
Paul Duffin730f2a52019-06-27 14:08:51 +0100108 Because("the VNDK can never contain a library that is device dependent."),
109 NeverAllow().
110 With("vndk.enabled", "true").
111 Without("vendor", "true").
112 Without("owner", "").
113 Because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -0800114
Neil Fullerdf5f3562018-10-21 17:19:10 +0100115 // TODO(b/67974785): always enforce the manifest
Paul Duffin730f2a52019-06-27 14:08:51 +0100116 NeverAllow().
Steven Moreland51ce4f62020-02-10 17:21:32 -0800117 Without("name", "libhidlbase-combined-impl").
118 Without("name", "libhidlbase").
119 Without("name", "libhidlbase_pgo").
Paul Duffin730f2a52019-06-27 14:08:51 +0100120 With("product_variables.enforce_vintf_manifest.cflags", "*").
121 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100122
123 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffin730f2a52019-06-27 14:08:51 +0100124 NeverAllow().
125 Without("name", "libc_bionic_ndk").
126 With("product_variables.treble_linker_namespaces.cflags", "*").
127 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100128
129 // Example:
Paul Duffin730f2a52019-06-27 14:08:51 +0100130 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100131 }
132}
133
Paul Duffin730f2a52019-06-27 14:08:51 +0100134func createLibcoreRules() []Rule {
Neil Fullerdf5f3562018-10-21 17:19:10 +0100135 var coreLibraryProjects = []string{
136 "libcore",
137 "external/apache-harmony",
138 "external/apache-xml",
139 "external/bouncycastle",
140 "external/conscrypt",
141 "external/icu",
142 "external/okhttp",
143 "external/wycheproof",
144 }
145
Paul Duffina3d09862019-06-11 13:40:47 +0100146 // Core library constraints. The sdk_version: "none" can only be used in core library projects.
147 // Access to core library targets is restricted using visibility rules.
Paul Duffin730f2a52019-06-27 14:08:51 +0100148 rules := []Rule{
149 NeverAllow().
150 NotIn(coreLibraryProjects...).
Anton Hansson45376402020-04-09 14:18:21 +0100151 With("sdk_version", "none").
152 WithoutMatcher("name", Regexp("^android_.*stubs_current$")),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100153 }
154
Neil Fullerdf5f3562018-10-21 17:19:10 +0100155 return rules
Steven Moreland65b3fd92017-12-06 14:18:35 -0800156}
157
Paul Duffin730f2a52019-06-27 14:08:51 +0100158func createMediaRules() []Rule {
159 return []Rule{
160 NeverAllow().
161 With("libs", "updatable-media").
162 Because("updatable-media includes private APIs. Use updatable_media_stubs instead."),
Dongwon Kang50a299f2019-02-04 09:00:51 -0800163 }
164}
165
Paul Duffin730f2a52019-06-27 14:08:51 +0100166func createJavaDeviceForHostRules() []Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800167 javaDeviceForHostProjectsWhitelist := []string{
Colin Crossb5191a52019-04-11 14:07:38 -0700168 "external/guava",
Colin Crossfd4f7432019-03-05 15:06:16 -0800169 "external/robolectric-shadows",
170 "framework/layoutlib",
171 }
172
Paul Duffin730f2a52019-06-27 14:08:51 +0100173 return []Rule{
174 NeverAllow().
175 NotIn(javaDeviceForHostProjectsWhitelist...).
176 ModuleType("java_device_for_host", "java_host_for_device").
177 Because("java_device_for_host can only be used in whitelisted projects"),
Colin Crossfd4f7432019-03-05 15:06:16 -0800178 }
179}
180
Colin Crossc511bc52020-04-07 16:50:32 +0000181func createCcSdkVariantRules() []Rule {
182 sdkVersionOnlyWhitelist := []string{
183 // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk.
184 // This sometimes works because the APEX modules that contain derive_sdk and
185 // derive_sdk_prefer32 suppress the platform installation rules, but fails when
186 // the APEX modules contain the SDK variant and the platform variant still exists.
187 "frameworks/base/apex/sdkextensions/derive_sdk",
188 }
189
190 platformVariantPropertiesWhitelist := []string{
191 // android_native_app_glue and libRSSupport use native_window.h but target old
192 // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist,
193 // so they can't add libnativewindow to shared_libs to get the header directory
194 // for the platform variant. Allow them to use the platform variant
195 // property to set shared_libs.
196 "prebuilts/ndk",
197 "frameworks/rs",
198 }
199
200 return []Rule{
201 NeverAllow().
202 NotIn(sdkVersionOnlyWhitelist...).
203 WithMatcher("sdk_variant_only", isSetMatcherInstance).
204 Because("sdk_variant_only can only be used in whitelisted projects"),
205 NeverAllow().
206 NotIn(platformVariantPropertiesWhitelist...).
207 WithMatcher("platform.shared_libs", isSetMatcherInstance).
208 Because("platform variant properties can only be used in whitelisted projects"),
209 }
210}
211
Steven Moreland65b3fd92017-12-06 14:18:35 -0800212func neverallowMutator(ctx BottomUpMutatorContext) {
213 m, ok := ctx.Module().(Module)
214 if !ok {
215 return
216 }
217
218 dir := ctx.ModuleDir() + "/"
219 properties := m.GetProperties()
220
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100221 osClass := ctx.Module().Target().Os.Class
222
Paul Duffin115445b2019-08-07 15:31:07 +0100223 for _, r := range neverallowRules(ctx.Config()) {
Paul Duffin730f2a52019-06-27 14:08:51 +0100224 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800225 if !n.appliesToPath(dir) {
226 continue
227 }
228
Colin Crossfd4f7432019-03-05 15:06:16 -0800229 if !n.appliesToModuleType(ctx.ModuleType()) {
230 continue
231 }
232
Steven Moreland65b3fd92017-12-06 14:18:35 -0800233 if !n.appliesToProperties(properties) {
234 continue
235 }
236
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100237 if !n.appliesToOsClass(osClass) {
238 continue
239 }
240
Paul Duffin35781882019-07-25 15:41:09 +0100241 if !n.appliesToDirectDeps(ctx) {
242 continue
243 }
244
Steven Moreland65b3fd92017-12-06 14:18:35 -0800245 ctx.ModuleErrorf("violates " + n.String())
246 }
247}
248
Paul Duffin73bf0542019-07-12 14:12:49 +0100249type ValueMatcher interface {
Artur Satayevc5570ac2020-04-09 16:06:36 +0100250 Test(string) bool
Paul Duffin73bf0542019-07-12 14:12:49 +0100251 String() string
252}
253
254type equalMatcher struct {
255 expected string
256}
257
Artur Satayevc5570ac2020-04-09 16:06:36 +0100258func (m *equalMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100259 return m.expected == value
260}
261
262func (m *equalMatcher) String() string {
263 return "=" + m.expected
264}
265
266type anyMatcher struct {
267}
268
Artur Satayevc5570ac2020-04-09 16:06:36 +0100269func (m *anyMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100270 return true
271}
272
273func (m *anyMatcher) String() string {
274 return "=*"
275}
276
277var anyMatcherInstance = &anyMatcher{}
278
Paul Duffinc8111702019-07-22 12:13:55 +0100279type startsWithMatcher struct {
280 prefix string
281}
282
Artur Satayevc5570ac2020-04-09 16:06:36 +0100283func (m *startsWithMatcher) Test(value string) bool {
Paul Duffinc8111702019-07-22 12:13:55 +0100284 return strings.HasPrefix(value, m.prefix)
285}
286
287func (m *startsWithMatcher) String() string {
288 return ".starts-with(" + m.prefix + ")"
289}
290
Anton Hansson45376402020-04-09 14:18:21 +0100291type regexMatcher struct {
292 re *regexp.Regexp
293}
294
Artur Satayevc5570ac2020-04-09 16:06:36 +0100295func (m *regexMatcher) Test(value string) bool {
Anton Hansson45376402020-04-09 14:18:21 +0100296 return m.re.MatchString(value)
297}
298
299func (m *regexMatcher) String() string {
300 return ".regexp(" + m.re.String() + ")"
301}
302
Colin Crossc511bc52020-04-07 16:50:32 +0000303type isSetMatcher struct{}
304
Artur Satayevc5570ac2020-04-09 16:06:36 +0100305func (m *isSetMatcher) Test(value string) bool {
Colin Crossc511bc52020-04-07 16:50:32 +0000306 return value != ""
307}
308
309func (m *isSetMatcher) String() string {
310 return ".is-set"
311}
312
313var isSetMatcherInstance = &isSetMatcher{}
314
Steven Moreland65b3fd92017-12-06 14:18:35 -0800315type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100316 fields []string // e.x.: Vndk.Enabled
317 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800318}
319
Paul Duffin730f2a52019-06-27 14:08:51 +0100320// A NeverAllow rule.
321type Rule interface {
322 In(path ...string) Rule
323
324 NotIn(path ...string) Rule
325
Paul Duffin35781882019-07-25 15:41:09 +0100326 InDirectDeps(deps ...string) Rule
327
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100328 WithOsClass(osClasses ...OsClass) Rule
329
Paul Duffin730f2a52019-06-27 14:08:51 +0100330 ModuleType(types ...string) Rule
331
332 NotModuleType(types ...string) Rule
333
334 With(properties, value string) Rule
335
Paul Duffinc8111702019-07-22 12:13:55 +0100336 WithMatcher(properties string, matcher ValueMatcher) Rule
337
Paul Duffin730f2a52019-06-27 14:08:51 +0100338 Without(properties, value string) Rule
339
Paul Duffinc8111702019-07-22 12:13:55 +0100340 WithoutMatcher(properties string, matcher ValueMatcher) Rule
341
Paul Duffin730f2a52019-06-27 14:08:51 +0100342 Because(reason string) Rule
343}
344
Steven Moreland65b3fd92017-12-06 14:18:35 -0800345type rule struct {
346 // User string for why this is a thing.
347 reason string
348
349 paths []string
350 unlessPaths []string
351
Paul Duffin35781882019-07-25 15:41:09 +0100352 directDeps map[string]bool
353
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100354 osClasses []OsClass
355
Colin Crossfd4f7432019-03-05 15:06:16 -0800356 moduleTypes []string
357 unlessModuleTypes []string
358
Steven Moreland65b3fd92017-12-06 14:18:35 -0800359 props []ruleProperty
360 unlessProps []ruleProperty
361}
362
Paul Duffin730f2a52019-06-27 14:08:51 +0100363// Create a new NeverAllow rule.
364func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100365 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800366}
Colin Crossfd4f7432019-03-05 15:06:16 -0800367
Paul Duffin730f2a52019-06-27 14:08:51 +0100368func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800369 r.paths = append(r.paths, cleanPaths(path)...)
370 return r
371}
Colin Crossfd4f7432019-03-05 15:06:16 -0800372
Paul Duffin730f2a52019-06-27 14:08:51 +0100373func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800374 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
375 return r
376}
Colin Crossfd4f7432019-03-05 15:06:16 -0800377
Paul Duffin35781882019-07-25 15:41:09 +0100378func (r *rule) InDirectDeps(deps ...string) Rule {
379 for _, d := range deps {
380 r.directDeps[d] = true
381 }
382 return r
383}
384
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100385func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
386 r.osClasses = append(r.osClasses, osClasses...)
387 return r
388}
389
Paul Duffin730f2a52019-06-27 14:08:51 +0100390func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800391 r.moduleTypes = append(r.moduleTypes, types...)
392 return r
393}
394
Paul Duffin730f2a52019-06-27 14:08:51 +0100395func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800396 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
397 return r
398}
399
Paul Duffin730f2a52019-06-27 14:08:51 +0100400func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100401 return r.WithMatcher(properties, selectMatcher(value))
402}
403
404func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800405 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100406 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100407 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800408 })
409 return r
410}
Colin Crossfd4f7432019-03-05 15:06:16 -0800411
Paul Duffin730f2a52019-06-27 14:08:51 +0100412func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100413 return r.WithoutMatcher(properties, selectMatcher(value))
414}
415
416func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800417 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100418 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100419 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800420 })
421 return r
422}
Colin Crossfd4f7432019-03-05 15:06:16 -0800423
Paul Duffin73bf0542019-07-12 14:12:49 +0100424func selectMatcher(expected string) ValueMatcher {
425 if expected == "*" {
426 return anyMatcherInstance
427 }
428 return &equalMatcher{expected: expected}
429}
430
Paul Duffin730f2a52019-06-27 14:08:51 +0100431func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800432 r.reason = reason
433 return r
434}
435
436func (r *rule) String() string {
437 s := "neverallow"
438 for _, v := range r.paths {
439 s += " dir:" + v + "*"
440 }
441 for _, v := range r.unlessPaths {
442 s += " -dir:" + v + "*"
443 }
Colin Crossfd4f7432019-03-05 15:06:16 -0800444 for _, v := range r.moduleTypes {
445 s += " type:" + v
446 }
447 for _, v := range r.unlessModuleTypes {
448 s += " -type:" + v
449 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800450 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100451 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800452 }
453 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100454 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800455 }
Paul Duffin35781882019-07-25 15:41:09 +0100456 for k := range r.directDeps {
457 s += " deps:" + k
458 }
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100459 for _, v := range r.osClasses {
460 s += " os:" + v.String()
461 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800462 if len(r.reason) != 0 {
463 s += " which is restricted because " + r.reason
464 }
465 return s
466}
467
468func (r *rule) appliesToPath(dir string) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800469 includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
470 excludePath := HasAnyPrefix(dir, r.unlessPaths)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800471 return includePath && !excludePath
472}
473
Paul Duffin35781882019-07-25 15:41:09 +0100474func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
475 if len(r.directDeps) == 0 {
476 return true
477 }
478
479 matches := false
480 ctx.VisitDirectDeps(func(m Module) {
481 if !matches {
482 name := ctx.OtherModuleName(m)
483 matches = r.directDeps[name]
484 }
485 })
486
487 return matches
488}
489
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100490func (r *rule) appliesToOsClass(osClass OsClass) bool {
491 if len(r.osClasses) == 0 {
492 return true
493 }
494
495 for _, c := range r.osClasses {
496 if c == osClass {
497 return true
498 }
499 }
500
501 return false
502}
503
Colin Crossfd4f7432019-03-05 15:06:16 -0800504func (r *rule) appliesToModuleType(moduleType string) bool {
505 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
506}
507
Steven Moreland65b3fd92017-12-06 14:18:35 -0800508func (r *rule) appliesToProperties(properties []interface{}) bool {
509 includeProps := hasAllProperties(properties, r.props)
510 excludeProps := hasAnyProperty(properties, r.unlessProps)
511 return includeProps && !excludeProps
512}
513
Paul Duffinc8111702019-07-22 12:13:55 +0100514func StartsWith(prefix string) ValueMatcher {
515 return &startsWithMatcher{prefix}
516}
517
Anton Hansson45376402020-04-09 14:18:21 +0100518func Regexp(re string) ValueMatcher {
519 r, err := regexp.Compile(re)
520 if err != nil {
521 panic(err)
522 }
523 return &regexMatcher{r}
524}
525
Steven Moreland65b3fd92017-12-06 14:18:35 -0800526// assorted utils
527
528func cleanPaths(paths []string) []string {
529 res := make([]string, len(paths))
530 for i, v := range paths {
531 res[i] = filepath.Clean(v) + "/"
532 }
533 return res
534}
535
536func fieldNamesForProperties(propertyNames string) []string {
537 names := strings.Split(propertyNames, ".")
538 for i, v := range names {
539 names[i] = proptools.FieldNameForProperty(v)
540 }
541 return names
542}
543
Steven Moreland65b3fd92017-12-06 14:18:35 -0800544func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
545 for _, v := range props {
546 if hasProperty(properties, v) {
547 return true
548 }
549 }
550 return false
551}
552
553func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
554 for _, v := range props {
555 if !hasProperty(properties, v) {
556 return false
557 }
558 }
559 return true
560}
561
562func hasProperty(properties []interface{}, prop ruleProperty) bool {
563 for _, propertyStruct := range properties {
564 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
565 for _, v := range prop.fields {
566 if !propertiesValue.IsValid() {
567 break
568 }
569 propertiesValue = propertiesValue.FieldByName(v)
570 }
571 if !propertiesValue.IsValid() {
572 continue
573 }
574
Paul Duffin73bf0542019-07-12 14:12:49 +0100575 check := func(value string) bool {
Artur Satayevc5570ac2020-04-09 16:06:36 +0100576 return prop.matcher.Test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800577 }
578
579 if matchValue(propertiesValue, check) {
580 return true
581 }
582 }
583 return false
584}
585
586func matchValue(value reflect.Value, check func(string) bool) bool {
587 if !value.IsValid() {
588 return false
589 }
590
591 if value.Kind() == reflect.Ptr {
592 if value.IsNil() {
593 return check("")
594 }
595 value = value.Elem()
596 }
597
598 switch value.Kind() {
599 case reflect.String:
600 return check(value.String())
601 case reflect.Bool:
602 return check(strconv.FormatBool(value.Bool()))
603 case reflect.Int:
604 return check(strconv.FormatInt(value.Int(), 10))
605 case reflect.Slice:
606 slice, ok := value.Interface().([]string)
607 if !ok {
608 panic("Can only handle slice of string")
609 }
610 for _, v := range slice {
611 if check(v) {
612 return true
613 }
614 }
615 return false
616 }
617
618 panic("Can't handle type: " + value.Kind().String())
619}
Paul Duffin115445b2019-08-07 15:31:07 +0100620
621var neverallowRulesKey = NewOnceKey("neverallowRules")
622
623func neverallowRules(config Config) []Rule {
624 return config.Once(neverallowRulesKey, func() interface{} {
625 // No test rules were set by setTestNeverallowRules, use the global rules
626 return neverallows
627 }).([]Rule)
628}
629
630// Overrides the default neverallow rules for the supplied config.
631//
632// For testing only.
Artur Satayevc5570ac2020-04-09 16:06:36 +0100633func SetTestNeverallowRules(config Config, testRules []Rule) {
Paul Duffin115445b2019-08-07 15:31:07 +0100634 config.Once(neverallowRulesKey, func() interface{} { return testRules })
635}