blob: 547230c08662bd0af43e9897548236a3a9f03167 [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"
20 "strconv"
21 "strings"
22
23 "github.com/google/blueprint/proptools"
24)
25
26// "neverallow" rules for the build system.
27//
28// This allows things which aren't related to the build system and are enforced
29// for sanity, in progress code refactors, or policy to be expressed in a
30// straightforward away disjoint from implementations and tests which should
31// work regardless of these restrictions.
32//
33// A module is disallowed if all of the following are true:
Paul Duffin730f2a52019-06-27 14:08:51 +010034// - it is in one of the "In" paths
35// - it is not in one of the "NotIn" paths
36// - it has all "With" properties matched
Steven Moreland65b3fd92017-12-06 14:18:35 -080037// - - values are matched in their entirety
38// - - nil is interpreted as an empty string
39// - - nested properties are separated with a '.'
40// - - if the property is a list, any of the values in the list being matches
41// counts as a match
Paul Duffin730f2a52019-06-27 14:08:51 +010042// - it has none of the "Without" properties matched (same rules as above)
Steven Moreland65b3fd92017-12-06 14:18:35 -080043
44func registerNeverallowMutator(ctx RegisterMutatorsContext) {
45 ctx.BottomUp("neverallow", neverallowMutator).Parallel()
46}
47
Paul Duffin730f2a52019-06-27 14:08:51 +010048var neverallows = []Rule{}
Steven Moreland65b3fd92017-12-06 14:18:35 -080049
Paul Duffin730f2a52019-06-27 14:08:51 +010050func init() {
Paul Duffinc8111702019-07-22 12:13:55 +010051 AddNeverAllowRules(createIncludeDirsRules()...)
Paul Duffin730f2a52019-06-27 14:08:51 +010052 AddNeverAllowRules(createTrebleRules()...)
53 AddNeverAllowRules(createLibcoreRules()...)
54 AddNeverAllowRules(createMediaRules()...)
55 AddNeverAllowRules(createJavaDeviceForHostRules()...)
Colin Crossc511bc52020-04-07 16:50:32 +000056 AddNeverAllowRules(createCcSdkVariantRules()...)
Neil Fullerdf5f3562018-10-21 17:19:10 +010057}
Steven Moreland65b3fd92017-12-06 14:18:35 -080058
Paul Duffin730f2a52019-06-27 14:08:51 +010059// Add a NeverAllow rule to the set of rules to apply.
60func AddNeverAllowRules(rules ...Rule) {
61 neverallows = append(neverallows, rules...)
62}
63
Paul Duffinc8111702019-07-22 12:13:55 +010064func createIncludeDirsRules() []Rule {
65 // The list of paths that cannot be referenced using include_dirs
66 paths := []string{
67 "art",
Orion Hodson6341f012019-11-06 13:39:46 +000068 "art/libnativebridge",
69 "art/libnativeloader",
Paul Duffinc8111702019-07-22 12:13:55 +010070 "libcore",
71 "libnativehelper",
72 "external/apache-harmony",
73 "external/apache-xml",
74 "external/boringssl",
75 "external/bouncycastle",
76 "external/conscrypt",
77 "external/icu",
78 "external/okhttp",
79 "external/vixl",
80 "external/wycheproof",
Paul Duffinc8111702019-07-22 12:13:55 +010081 }
82
83 // Create a composite matcher that will match if the value starts with any of the restricted
84 // paths. A / is appended to the prefix to ensure that restricting path X does not affect paths
85 // XY.
86 rules := make([]Rule, 0, len(paths))
87 for _, path := range paths {
88 rule :=
89 NeverAllow().
90 WithMatcher("include_dirs", StartsWith(path+"/")).
91 Because("include_dirs is deprecated, all usages of '" + path + "' have been migrated" +
92 " to use alternate mechanisms and so can no longer be used.")
93
94 rules = append(rules, rule)
95 }
96
97 return rules
98}
99
Paul Duffin730f2a52019-06-27 14:08:51 +0100100func createTrebleRules() []Rule {
101 return []Rule{
102 NeverAllow().
103 In("vendor", "device").
104 With("vndk.enabled", "true").
105 Without("vendor", "true").
Justin Yun0ecf0b22020-02-28 15:07:59 +0900106 Without("product_specific", "true").
Paul Duffin730f2a52019-06-27 14:08:51 +0100107 Because("the VNDK can never contain a library that is device dependent."),
108 NeverAllow().
109 With("vndk.enabled", "true").
110 Without("vendor", "true").
111 Without("owner", "").
112 Because("a VNDK module can never have an owner."),
Steven Moreland65b3fd92017-12-06 14:18:35 -0800113
Neil Fullerdf5f3562018-10-21 17:19:10 +0100114 // TODO(b/67974785): always enforce the manifest
Paul Duffin730f2a52019-06-27 14:08:51 +0100115 NeverAllow().
Steven Moreland51ce4f62020-02-10 17:21:32 -0800116 Without("name", "libhidlbase-combined-impl").
117 Without("name", "libhidlbase").
118 Without("name", "libhidlbase_pgo").
Paul Duffin730f2a52019-06-27 14:08:51 +0100119 With("product_variables.enforce_vintf_manifest.cflags", "*").
120 Because("manifest enforcement should be independent of ."),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100121
122 // TODO(b/67975799): vendor code should always use /vendor/bin/sh
Paul Duffin730f2a52019-06-27 14:08:51 +0100123 NeverAllow().
124 Without("name", "libc_bionic_ndk").
125 With("product_variables.treble_linker_namespaces.cflags", "*").
126 Because("nothing should care if linker namespaces are enabled or not"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100127
128 // Example:
Paul Duffin730f2a52019-06-27 14:08:51 +0100129 // *NeverAllow().with("Srcs", "main.cpp"))
Neil Fullerdf5f3562018-10-21 17:19:10 +0100130 }
131}
132
Paul Duffin730f2a52019-06-27 14:08:51 +0100133func createLibcoreRules() []Rule {
Neil Fullerdf5f3562018-10-21 17:19:10 +0100134 var coreLibraryProjects = []string{
135 "libcore",
136 "external/apache-harmony",
137 "external/apache-xml",
138 "external/bouncycastle",
139 "external/conscrypt",
140 "external/icu",
141 "external/okhttp",
142 "external/wycheproof",
143 }
144
Paul Duffina3d09862019-06-11 13:40:47 +0100145 // Core library constraints. The sdk_version: "none" can only be used in core library projects.
146 // Access to core library targets is restricted using visibility rules.
Paul Duffin730f2a52019-06-27 14:08:51 +0100147 rules := []Rule{
148 NeverAllow().
149 NotIn(coreLibraryProjects...).
150 With("sdk_version", "none"),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100151 }
152
Neil Fullerdf5f3562018-10-21 17:19:10 +0100153 return rules
Steven Moreland65b3fd92017-12-06 14:18:35 -0800154}
155
Paul Duffin730f2a52019-06-27 14:08:51 +0100156func createMediaRules() []Rule {
157 return []Rule{
158 NeverAllow().
159 With("libs", "updatable-media").
160 Because("updatable-media includes private APIs. Use updatable_media_stubs instead."),
Dongwon Kang50a299f2019-02-04 09:00:51 -0800161 }
162}
163
Paul Duffin730f2a52019-06-27 14:08:51 +0100164func createJavaDeviceForHostRules() []Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800165 javaDeviceForHostProjectsWhitelist := []string{
Colin Crossb5191a52019-04-11 14:07:38 -0700166 "external/guava",
Colin Crossfd4f7432019-03-05 15:06:16 -0800167 "external/robolectric-shadows",
168 "framework/layoutlib",
169 }
170
Paul Duffin730f2a52019-06-27 14:08:51 +0100171 return []Rule{
172 NeverAllow().
173 NotIn(javaDeviceForHostProjectsWhitelist...).
174 ModuleType("java_device_for_host", "java_host_for_device").
175 Because("java_device_for_host can only be used in whitelisted projects"),
Colin Crossfd4f7432019-03-05 15:06:16 -0800176 }
177}
178
Colin Crossc511bc52020-04-07 16:50:32 +0000179func createCcSdkVariantRules() []Rule {
180 sdkVersionOnlyWhitelist := []string{
181 // derive_sdk_prefer32 has stem: "derive_sdk" which conflicts with the derive_sdk.
182 // This sometimes works because the APEX modules that contain derive_sdk and
183 // derive_sdk_prefer32 suppress the platform installation rules, but fails when
184 // the APEX modules contain the SDK variant and the platform variant still exists.
185 "frameworks/base/apex/sdkextensions/derive_sdk",
186 }
187
188 platformVariantPropertiesWhitelist := []string{
189 // android_native_app_glue and libRSSupport use native_window.h but target old
190 // sdk versions (minimum and 9 respectively) where libnativewindow didn't exist,
191 // so they can't add libnativewindow to shared_libs to get the header directory
192 // for the platform variant. Allow them to use the platform variant
193 // property to set shared_libs.
194 "prebuilts/ndk",
195 "frameworks/rs",
196 }
197
198 return []Rule{
199 NeverAllow().
200 NotIn(sdkVersionOnlyWhitelist...).
201 WithMatcher("sdk_variant_only", isSetMatcherInstance).
202 Because("sdk_variant_only can only be used in whitelisted projects"),
203 NeverAllow().
204 NotIn(platformVariantPropertiesWhitelist...).
205 WithMatcher("platform.shared_libs", isSetMatcherInstance).
206 Because("platform variant properties can only be used in whitelisted projects"),
207 }
208}
209
Steven Moreland65b3fd92017-12-06 14:18:35 -0800210func neverallowMutator(ctx BottomUpMutatorContext) {
211 m, ok := ctx.Module().(Module)
212 if !ok {
213 return
214 }
215
216 dir := ctx.ModuleDir() + "/"
217 properties := m.GetProperties()
218
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100219 osClass := ctx.Module().Target().Os.Class
220
Paul Duffin115445b2019-08-07 15:31:07 +0100221 for _, r := range neverallowRules(ctx.Config()) {
Paul Duffin730f2a52019-06-27 14:08:51 +0100222 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800223 if !n.appliesToPath(dir) {
224 continue
225 }
226
Colin Crossfd4f7432019-03-05 15:06:16 -0800227 if !n.appliesToModuleType(ctx.ModuleType()) {
228 continue
229 }
230
Steven Moreland65b3fd92017-12-06 14:18:35 -0800231 if !n.appliesToProperties(properties) {
232 continue
233 }
234
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100235 if !n.appliesToOsClass(osClass) {
236 continue
237 }
238
Paul Duffin35781882019-07-25 15:41:09 +0100239 if !n.appliesToDirectDeps(ctx) {
240 continue
241 }
242
Steven Moreland65b3fd92017-12-06 14:18:35 -0800243 ctx.ModuleErrorf("violates " + n.String())
244 }
245}
246
Paul Duffin73bf0542019-07-12 14:12:49 +0100247type ValueMatcher interface {
248 test(string) bool
249 String() string
250}
251
252type equalMatcher struct {
253 expected string
254}
255
256func (m *equalMatcher) test(value string) bool {
257 return m.expected == value
258}
259
260func (m *equalMatcher) String() string {
261 return "=" + m.expected
262}
263
264type anyMatcher struct {
265}
266
267func (m *anyMatcher) test(value string) bool {
268 return true
269}
270
271func (m *anyMatcher) String() string {
272 return "=*"
273}
274
275var anyMatcherInstance = &anyMatcher{}
276
Paul Duffinc8111702019-07-22 12:13:55 +0100277type startsWithMatcher struct {
278 prefix string
279}
280
281func (m *startsWithMatcher) test(value string) bool {
282 return strings.HasPrefix(value, m.prefix)
283}
284
285func (m *startsWithMatcher) String() string {
286 return ".starts-with(" + m.prefix + ")"
287}
288
Colin Crossc511bc52020-04-07 16:50:32 +0000289type isSetMatcher struct{}
290
291func (m *isSetMatcher) test(value string) bool {
292 return value != ""
293}
294
295func (m *isSetMatcher) String() string {
296 return ".is-set"
297}
298
299var isSetMatcherInstance = &isSetMatcher{}
300
Steven Moreland65b3fd92017-12-06 14:18:35 -0800301type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100302 fields []string // e.x.: Vndk.Enabled
303 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800304}
305
Paul Duffin730f2a52019-06-27 14:08:51 +0100306// A NeverAllow rule.
307type Rule interface {
308 In(path ...string) Rule
309
310 NotIn(path ...string) Rule
311
Paul Duffin35781882019-07-25 15:41:09 +0100312 InDirectDeps(deps ...string) Rule
313
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100314 WithOsClass(osClasses ...OsClass) Rule
315
Paul Duffin730f2a52019-06-27 14:08:51 +0100316 ModuleType(types ...string) Rule
317
318 NotModuleType(types ...string) Rule
319
320 With(properties, value string) Rule
321
Paul Duffinc8111702019-07-22 12:13:55 +0100322 WithMatcher(properties string, matcher ValueMatcher) Rule
323
Paul Duffin730f2a52019-06-27 14:08:51 +0100324 Without(properties, value string) Rule
325
Paul Duffinc8111702019-07-22 12:13:55 +0100326 WithoutMatcher(properties string, matcher ValueMatcher) Rule
327
Paul Duffin730f2a52019-06-27 14:08:51 +0100328 Because(reason string) Rule
329}
330
Steven Moreland65b3fd92017-12-06 14:18:35 -0800331type rule struct {
332 // User string for why this is a thing.
333 reason string
334
335 paths []string
336 unlessPaths []string
337
Paul Duffin35781882019-07-25 15:41:09 +0100338 directDeps map[string]bool
339
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100340 osClasses []OsClass
341
Colin Crossfd4f7432019-03-05 15:06:16 -0800342 moduleTypes []string
343 unlessModuleTypes []string
344
Steven Moreland65b3fd92017-12-06 14:18:35 -0800345 props []ruleProperty
346 unlessProps []ruleProperty
347}
348
Paul Duffin730f2a52019-06-27 14:08:51 +0100349// Create a new NeverAllow rule.
350func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100351 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800352}
Colin Crossfd4f7432019-03-05 15:06:16 -0800353
Paul Duffin730f2a52019-06-27 14:08:51 +0100354func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800355 r.paths = append(r.paths, cleanPaths(path)...)
356 return r
357}
Colin Crossfd4f7432019-03-05 15:06:16 -0800358
Paul Duffin730f2a52019-06-27 14:08:51 +0100359func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800360 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
361 return r
362}
Colin Crossfd4f7432019-03-05 15:06:16 -0800363
Paul Duffin35781882019-07-25 15:41:09 +0100364func (r *rule) InDirectDeps(deps ...string) Rule {
365 for _, d := range deps {
366 r.directDeps[d] = true
367 }
368 return r
369}
370
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100371func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
372 r.osClasses = append(r.osClasses, osClasses...)
373 return r
374}
375
Paul Duffin730f2a52019-06-27 14:08:51 +0100376func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800377 r.moduleTypes = append(r.moduleTypes, types...)
378 return r
379}
380
Paul Duffin730f2a52019-06-27 14:08:51 +0100381func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800382 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
383 return r
384}
385
Paul Duffin730f2a52019-06-27 14:08:51 +0100386func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100387 return r.WithMatcher(properties, selectMatcher(value))
388}
389
390func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800391 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100392 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100393 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800394 })
395 return r
396}
Colin Crossfd4f7432019-03-05 15:06:16 -0800397
Paul Duffin730f2a52019-06-27 14:08:51 +0100398func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100399 return r.WithoutMatcher(properties, selectMatcher(value))
400}
401
402func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800403 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100404 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100405 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800406 })
407 return r
408}
Colin Crossfd4f7432019-03-05 15:06:16 -0800409
Paul Duffin73bf0542019-07-12 14:12:49 +0100410func selectMatcher(expected string) ValueMatcher {
411 if expected == "*" {
412 return anyMatcherInstance
413 }
414 return &equalMatcher{expected: expected}
415}
416
Paul Duffin730f2a52019-06-27 14:08:51 +0100417func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800418 r.reason = reason
419 return r
420}
421
422func (r *rule) String() string {
423 s := "neverallow"
424 for _, v := range r.paths {
425 s += " dir:" + v + "*"
426 }
427 for _, v := range r.unlessPaths {
428 s += " -dir:" + v + "*"
429 }
Colin Crossfd4f7432019-03-05 15:06:16 -0800430 for _, v := range r.moduleTypes {
431 s += " type:" + v
432 }
433 for _, v := range r.unlessModuleTypes {
434 s += " -type:" + v
435 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800436 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100437 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800438 }
439 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100440 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800441 }
Paul Duffin35781882019-07-25 15:41:09 +0100442 for k := range r.directDeps {
443 s += " deps:" + k
444 }
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100445 for _, v := range r.osClasses {
446 s += " os:" + v.String()
447 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800448 if len(r.reason) != 0 {
449 s += " which is restricted because " + r.reason
450 }
451 return s
452}
453
454func (r *rule) appliesToPath(dir string) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800455 includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
456 excludePath := HasAnyPrefix(dir, r.unlessPaths)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800457 return includePath && !excludePath
458}
459
Paul Duffin35781882019-07-25 15:41:09 +0100460func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
461 if len(r.directDeps) == 0 {
462 return true
463 }
464
465 matches := false
466 ctx.VisitDirectDeps(func(m Module) {
467 if !matches {
468 name := ctx.OtherModuleName(m)
469 matches = r.directDeps[name]
470 }
471 })
472
473 return matches
474}
475
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100476func (r *rule) appliesToOsClass(osClass OsClass) bool {
477 if len(r.osClasses) == 0 {
478 return true
479 }
480
481 for _, c := range r.osClasses {
482 if c == osClass {
483 return true
484 }
485 }
486
487 return false
488}
489
Colin Crossfd4f7432019-03-05 15:06:16 -0800490func (r *rule) appliesToModuleType(moduleType string) bool {
491 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
492}
493
Steven Moreland65b3fd92017-12-06 14:18:35 -0800494func (r *rule) appliesToProperties(properties []interface{}) bool {
495 includeProps := hasAllProperties(properties, r.props)
496 excludeProps := hasAnyProperty(properties, r.unlessProps)
497 return includeProps && !excludeProps
498}
499
Paul Duffinc8111702019-07-22 12:13:55 +0100500func StartsWith(prefix string) ValueMatcher {
501 return &startsWithMatcher{prefix}
502}
503
Steven Moreland65b3fd92017-12-06 14:18:35 -0800504// assorted utils
505
506func cleanPaths(paths []string) []string {
507 res := make([]string, len(paths))
508 for i, v := range paths {
509 res[i] = filepath.Clean(v) + "/"
510 }
511 return res
512}
513
514func fieldNamesForProperties(propertyNames string) []string {
515 names := strings.Split(propertyNames, ".")
516 for i, v := range names {
517 names[i] = proptools.FieldNameForProperty(v)
518 }
519 return names
520}
521
Steven Moreland65b3fd92017-12-06 14:18:35 -0800522func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
523 for _, v := range props {
524 if hasProperty(properties, v) {
525 return true
526 }
527 }
528 return false
529}
530
531func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
532 for _, v := range props {
533 if !hasProperty(properties, v) {
534 return false
535 }
536 }
537 return true
538}
539
540func hasProperty(properties []interface{}, prop ruleProperty) bool {
541 for _, propertyStruct := range properties {
542 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
543 for _, v := range prop.fields {
544 if !propertiesValue.IsValid() {
545 break
546 }
547 propertiesValue = propertiesValue.FieldByName(v)
548 }
549 if !propertiesValue.IsValid() {
550 continue
551 }
552
Paul Duffin73bf0542019-07-12 14:12:49 +0100553 check := func(value string) bool {
554 return prop.matcher.test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800555 }
556
557 if matchValue(propertiesValue, check) {
558 return true
559 }
560 }
561 return false
562}
563
564func matchValue(value reflect.Value, check func(string) bool) bool {
565 if !value.IsValid() {
566 return false
567 }
568
569 if value.Kind() == reflect.Ptr {
570 if value.IsNil() {
571 return check("")
572 }
573 value = value.Elem()
574 }
575
576 switch value.Kind() {
577 case reflect.String:
578 return check(value.String())
579 case reflect.Bool:
580 return check(strconv.FormatBool(value.Bool()))
581 case reflect.Int:
582 return check(strconv.FormatInt(value.Int(), 10))
583 case reflect.Slice:
584 slice, ok := value.Interface().([]string)
585 if !ok {
586 panic("Can only handle slice of string")
587 }
588 for _, v := range slice {
589 if check(v) {
590 return true
591 }
592 }
593 return false
594 }
595
596 panic("Can't handle type: " + value.Kind().String())
597}
Paul Duffin115445b2019-08-07 15:31:07 +0100598
599var neverallowRulesKey = NewOnceKey("neverallowRules")
600
601func neverallowRules(config Config) []Rule {
602 return config.Once(neverallowRulesKey, func() interface{} {
603 // No test rules were set by setTestNeverallowRules, use the global rules
604 return neverallows
605 }).([]Rule)
606}
607
608// Overrides the default neverallow rules for the supplied config.
609//
610// For testing only.
611func setTestNeverallowRules(config Config, testRules []Rule) {
612 config.Once(neverallowRulesKey, func() interface{} { return testRules })
613}