blob: 4d3a16fb0d5d036fe32c8365dd8c67a0ca9d53af [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()...)
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 Yun98df0d12020-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...).
Anton Hanssonfec6c232020-04-09 14:18:21 +0100150 With("sdk_version", "none").
151 WithoutMatcher("name", Regexp("^android_.*stubs_current$")),
Neil Fullerdf5f3562018-10-21 17:19:10 +0100152 }
153
Neil Fullerdf5f3562018-10-21 17:19:10 +0100154 return rules
Steven Moreland65b3fd92017-12-06 14:18:35 -0800155}
156
Paul Duffin730f2a52019-06-27 14:08:51 +0100157func createMediaRules() []Rule {
158 return []Rule{
159 NeverAllow().
160 With("libs", "updatable-media").
161 Because("updatable-media includes private APIs. Use updatable_media_stubs instead."),
Dongwon Kang50a299f2019-02-04 09:00:51 -0800162 }
163}
164
Paul Duffin730f2a52019-06-27 14:08:51 +0100165func createJavaDeviceForHostRules() []Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800166 javaDeviceForHostProjectsWhitelist := []string{
Colin Crossb5191a52019-04-11 14:07:38 -0700167 "external/guava",
Colin Crossfd4f7432019-03-05 15:06:16 -0800168 "external/robolectric-shadows",
169 "framework/layoutlib",
170 }
171
Paul Duffin730f2a52019-06-27 14:08:51 +0100172 return []Rule{
173 NeverAllow().
174 NotIn(javaDeviceForHostProjectsWhitelist...).
175 ModuleType("java_device_for_host", "java_host_for_device").
176 Because("java_device_for_host can only be used in whitelisted projects"),
Colin Crossfd4f7432019-03-05 15:06:16 -0800177 }
178}
179
Steven Moreland65b3fd92017-12-06 14:18:35 -0800180func neverallowMutator(ctx BottomUpMutatorContext) {
181 m, ok := ctx.Module().(Module)
182 if !ok {
183 return
184 }
185
186 dir := ctx.ModuleDir() + "/"
187 properties := m.GetProperties()
188
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100189 osClass := ctx.Module().Target().Os.Class
190
Paul Duffin115445b2019-08-07 15:31:07 +0100191 for _, r := range neverallowRules(ctx.Config()) {
Paul Duffin730f2a52019-06-27 14:08:51 +0100192 n := r.(*rule)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800193 if !n.appliesToPath(dir) {
194 continue
195 }
196
Colin Crossfd4f7432019-03-05 15:06:16 -0800197 if !n.appliesToModuleType(ctx.ModuleType()) {
198 continue
199 }
200
Steven Moreland65b3fd92017-12-06 14:18:35 -0800201 if !n.appliesToProperties(properties) {
202 continue
203 }
204
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100205 if !n.appliesToOsClass(osClass) {
206 continue
207 }
208
Paul Duffin35781882019-07-25 15:41:09 +0100209 if !n.appliesToDirectDeps(ctx) {
210 continue
211 }
212
Steven Moreland65b3fd92017-12-06 14:18:35 -0800213 ctx.ModuleErrorf("violates " + n.String())
214 }
215}
216
Paul Duffin73bf0542019-07-12 14:12:49 +0100217type ValueMatcher interface {
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100218 Test(string) bool
Paul Duffin73bf0542019-07-12 14:12:49 +0100219 String() string
220}
221
222type equalMatcher struct {
223 expected string
224}
225
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100226func (m *equalMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100227 return m.expected == value
228}
229
230func (m *equalMatcher) String() string {
231 return "=" + m.expected
232}
233
234type anyMatcher struct {
235}
236
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100237func (m *anyMatcher) Test(value string) bool {
Paul Duffin73bf0542019-07-12 14:12:49 +0100238 return true
239}
240
241func (m *anyMatcher) String() string {
242 return "=*"
243}
244
245var anyMatcherInstance = &anyMatcher{}
246
Paul Duffinc8111702019-07-22 12:13:55 +0100247type startsWithMatcher struct {
248 prefix string
249}
250
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100251func (m *startsWithMatcher) Test(value string) bool {
Paul Duffinc8111702019-07-22 12:13:55 +0100252 return strings.HasPrefix(value, m.prefix)
253}
254
255func (m *startsWithMatcher) String() string {
256 return ".starts-with(" + m.prefix + ")"
257}
258
Anton Hanssonfec6c232020-04-09 14:18:21 +0100259type regexMatcher struct {
260 re *regexp.Regexp
261}
262
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100263func (m *regexMatcher) Test(value string) bool {
Anton Hanssonfec6c232020-04-09 14:18:21 +0100264 return m.re.MatchString(value)
265}
266
267func (m *regexMatcher) String() string {
268 return ".regexp(" + m.re.String() + ")"
269}
270
Steven Moreland65b3fd92017-12-06 14:18:35 -0800271type ruleProperty struct {
Paul Duffin73bf0542019-07-12 14:12:49 +0100272 fields []string // e.x.: Vndk.Enabled
273 matcher ValueMatcher
Steven Moreland65b3fd92017-12-06 14:18:35 -0800274}
275
Paul Duffin730f2a52019-06-27 14:08:51 +0100276// A NeverAllow rule.
277type Rule interface {
278 In(path ...string) Rule
279
280 NotIn(path ...string) Rule
281
Paul Duffin35781882019-07-25 15:41:09 +0100282 InDirectDeps(deps ...string) Rule
283
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100284 WithOsClass(osClasses ...OsClass) Rule
285
Paul Duffin730f2a52019-06-27 14:08:51 +0100286 ModuleType(types ...string) Rule
287
288 NotModuleType(types ...string) Rule
289
290 With(properties, value string) Rule
291
Paul Duffinc8111702019-07-22 12:13:55 +0100292 WithMatcher(properties string, matcher ValueMatcher) Rule
293
Paul Duffin730f2a52019-06-27 14:08:51 +0100294 Without(properties, value string) Rule
295
Paul Duffinc8111702019-07-22 12:13:55 +0100296 WithoutMatcher(properties string, matcher ValueMatcher) Rule
297
Paul Duffin730f2a52019-06-27 14:08:51 +0100298 Because(reason string) Rule
299}
300
Steven Moreland65b3fd92017-12-06 14:18:35 -0800301type rule struct {
302 // User string for why this is a thing.
303 reason string
304
305 paths []string
306 unlessPaths []string
307
Paul Duffin35781882019-07-25 15:41:09 +0100308 directDeps map[string]bool
309
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100310 osClasses []OsClass
311
Colin Crossfd4f7432019-03-05 15:06:16 -0800312 moduleTypes []string
313 unlessModuleTypes []string
314
Steven Moreland65b3fd92017-12-06 14:18:35 -0800315 props []ruleProperty
316 unlessProps []ruleProperty
317}
318
Paul Duffin730f2a52019-06-27 14:08:51 +0100319// Create a new NeverAllow rule.
320func NeverAllow() Rule {
Paul Duffin35781882019-07-25 15:41:09 +0100321 return &rule{directDeps: make(map[string]bool)}
Steven Moreland65b3fd92017-12-06 14:18:35 -0800322}
Colin Crossfd4f7432019-03-05 15:06:16 -0800323
Paul Duffin730f2a52019-06-27 14:08:51 +0100324func (r *rule) In(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800325 r.paths = append(r.paths, cleanPaths(path)...)
326 return r
327}
Colin Crossfd4f7432019-03-05 15:06:16 -0800328
Paul Duffin730f2a52019-06-27 14:08:51 +0100329func (r *rule) NotIn(path ...string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800330 r.unlessPaths = append(r.unlessPaths, cleanPaths(path)...)
331 return r
332}
Colin Crossfd4f7432019-03-05 15:06:16 -0800333
Paul Duffin35781882019-07-25 15:41:09 +0100334func (r *rule) InDirectDeps(deps ...string) Rule {
335 for _, d := range deps {
336 r.directDeps[d] = true
337 }
338 return r
339}
340
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100341func (r *rule) WithOsClass(osClasses ...OsClass) Rule {
342 r.osClasses = append(r.osClasses, osClasses...)
343 return r
344}
345
Paul Duffin730f2a52019-06-27 14:08:51 +0100346func (r *rule) ModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800347 r.moduleTypes = append(r.moduleTypes, types...)
348 return r
349}
350
Paul Duffin730f2a52019-06-27 14:08:51 +0100351func (r *rule) NotModuleType(types ...string) Rule {
Colin Crossfd4f7432019-03-05 15:06:16 -0800352 r.unlessModuleTypes = append(r.unlessModuleTypes, types...)
353 return r
354}
355
Paul Duffin730f2a52019-06-27 14:08:51 +0100356func (r *rule) With(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100357 return r.WithMatcher(properties, selectMatcher(value))
358}
359
360func (r *rule) WithMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800361 r.props = append(r.props, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100362 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100363 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800364 })
365 return r
366}
Colin Crossfd4f7432019-03-05 15:06:16 -0800367
Paul Duffin730f2a52019-06-27 14:08:51 +0100368func (r *rule) Without(properties, value string) Rule {
Paul Duffinc8111702019-07-22 12:13:55 +0100369 return r.WithoutMatcher(properties, selectMatcher(value))
370}
371
372func (r *rule) WithoutMatcher(properties string, matcher ValueMatcher) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800373 r.unlessProps = append(r.unlessProps, ruleProperty{
Paul Duffin73bf0542019-07-12 14:12:49 +0100374 fields: fieldNamesForProperties(properties),
Paul Duffinc8111702019-07-22 12:13:55 +0100375 matcher: matcher,
Steven Moreland65b3fd92017-12-06 14:18:35 -0800376 })
377 return r
378}
Colin Crossfd4f7432019-03-05 15:06:16 -0800379
Paul Duffin73bf0542019-07-12 14:12:49 +0100380func selectMatcher(expected string) ValueMatcher {
381 if expected == "*" {
382 return anyMatcherInstance
383 }
384 return &equalMatcher{expected: expected}
385}
386
Paul Duffin730f2a52019-06-27 14:08:51 +0100387func (r *rule) Because(reason string) Rule {
Steven Moreland65b3fd92017-12-06 14:18:35 -0800388 r.reason = reason
389 return r
390}
391
392func (r *rule) String() string {
393 s := "neverallow"
394 for _, v := range r.paths {
395 s += " dir:" + v + "*"
396 }
397 for _, v := range r.unlessPaths {
398 s += " -dir:" + v + "*"
399 }
Colin Crossfd4f7432019-03-05 15:06:16 -0800400 for _, v := range r.moduleTypes {
401 s += " type:" + v
402 }
403 for _, v := range r.unlessModuleTypes {
404 s += " -type:" + v
405 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800406 for _, v := range r.props {
Paul Duffin73bf0542019-07-12 14:12:49 +0100407 s += " " + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800408 }
409 for _, v := range r.unlessProps {
Paul Duffin73bf0542019-07-12 14:12:49 +0100410 s += " -" + strings.Join(v.fields, ".") + v.matcher.String()
Steven Moreland65b3fd92017-12-06 14:18:35 -0800411 }
Paul Duffin35781882019-07-25 15:41:09 +0100412 for k := range r.directDeps {
413 s += " deps:" + k
414 }
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100415 for _, v := range r.osClasses {
416 s += " os:" + v.String()
417 }
Steven Moreland65b3fd92017-12-06 14:18:35 -0800418 if len(r.reason) != 0 {
419 s += " which is restricted because " + r.reason
420 }
421 return s
422}
423
424func (r *rule) appliesToPath(dir string) bool {
Jaewoong Jung3aff5782020-02-11 07:54:35 -0800425 includePath := len(r.paths) == 0 || HasAnyPrefix(dir, r.paths)
426 excludePath := HasAnyPrefix(dir, r.unlessPaths)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800427 return includePath && !excludePath
428}
429
Paul Duffin35781882019-07-25 15:41:09 +0100430func (r *rule) appliesToDirectDeps(ctx BottomUpMutatorContext) bool {
431 if len(r.directDeps) == 0 {
432 return true
433 }
434
435 matches := false
436 ctx.VisitDirectDeps(func(m Module) {
437 if !matches {
438 name := ctx.OtherModuleName(m)
439 matches = r.directDeps[name]
440 }
441 })
442
443 return matches
444}
445
Paul Duffinf1c9bbe2019-07-26 10:48:06 +0100446func (r *rule) appliesToOsClass(osClass OsClass) bool {
447 if len(r.osClasses) == 0 {
448 return true
449 }
450
451 for _, c := range r.osClasses {
452 if c == osClass {
453 return true
454 }
455 }
456
457 return false
458}
459
Colin Crossfd4f7432019-03-05 15:06:16 -0800460func (r *rule) appliesToModuleType(moduleType string) bool {
461 return (len(r.moduleTypes) == 0 || InList(moduleType, r.moduleTypes)) && !InList(moduleType, r.unlessModuleTypes)
462}
463
Steven Moreland65b3fd92017-12-06 14:18:35 -0800464func (r *rule) appliesToProperties(properties []interface{}) bool {
465 includeProps := hasAllProperties(properties, r.props)
466 excludeProps := hasAnyProperty(properties, r.unlessProps)
467 return includeProps && !excludeProps
468}
469
Paul Duffinc8111702019-07-22 12:13:55 +0100470func StartsWith(prefix string) ValueMatcher {
471 return &startsWithMatcher{prefix}
472}
473
Anton Hanssonfec6c232020-04-09 14:18:21 +0100474func Regexp(re string) ValueMatcher {
475 r, err := regexp.Compile(re)
476 if err != nil {
477 panic(err)
478 }
479 return &regexMatcher{r}
480}
481
Steven Moreland65b3fd92017-12-06 14:18:35 -0800482// assorted utils
483
484func cleanPaths(paths []string) []string {
485 res := make([]string, len(paths))
486 for i, v := range paths {
487 res[i] = filepath.Clean(v) + "/"
488 }
489 return res
490}
491
492func fieldNamesForProperties(propertyNames string) []string {
493 names := strings.Split(propertyNames, ".")
494 for i, v := range names {
495 names[i] = proptools.FieldNameForProperty(v)
496 }
497 return names
498}
499
Steven Moreland65b3fd92017-12-06 14:18:35 -0800500func hasAnyProperty(properties []interface{}, props []ruleProperty) bool {
501 for _, v := range props {
502 if hasProperty(properties, v) {
503 return true
504 }
505 }
506 return false
507}
508
509func hasAllProperties(properties []interface{}, props []ruleProperty) bool {
510 for _, v := range props {
511 if !hasProperty(properties, v) {
512 return false
513 }
514 }
515 return true
516}
517
518func hasProperty(properties []interface{}, prop ruleProperty) bool {
519 for _, propertyStruct := range properties {
520 propertiesValue := reflect.ValueOf(propertyStruct).Elem()
521 for _, v := range prop.fields {
522 if !propertiesValue.IsValid() {
523 break
524 }
525 propertiesValue = propertiesValue.FieldByName(v)
526 }
527 if !propertiesValue.IsValid() {
528 continue
529 }
530
Paul Duffin73bf0542019-07-12 14:12:49 +0100531 check := func(value string) bool {
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100532 return prop.matcher.Test(value)
Steven Moreland65b3fd92017-12-06 14:18:35 -0800533 }
534
535 if matchValue(propertiesValue, check) {
536 return true
537 }
538 }
539 return false
540}
541
542func matchValue(value reflect.Value, check func(string) bool) bool {
543 if !value.IsValid() {
544 return false
545 }
546
547 if value.Kind() == reflect.Ptr {
548 if value.IsNil() {
549 return check("")
550 }
551 value = value.Elem()
552 }
553
554 switch value.Kind() {
555 case reflect.String:
556 return check(value.String())
557 case reflect.Bool:
558 return check(strconv.FormatBool(value.Bool()))
559 case reflect.Int:
560 return check(strconv.FormatInt(value.Int(), 10))
561 case reflect.Slice:
562 slice, ok := value.Interface().([]string)
563 if !ok {
564 panic("Can only handle slice of string")
565 }
566 for _, v := range slice {
567 if check(v) {
568 return true
569 }
570 }
571 return false
572 }
573
574 panic("Can't handle type: " + value.Kind().String())
575}
Paul Duffin115445b2019-08-07 15:31:07 +0100576
577var neverallowRulesKey = NewOnceKey("neverallowRules")
578
579func neverallowRules(config Config) []Rule {
580 return config.Once(neverallowRulesKey, func() interface{} {
581 // No test rules were set by setTestNeverallowRules, use the global rules
582 return neverallows
583 }).([]Rule)
584}
585
586// Overrides the default neverallow rules for the supplied config.
587//
588// For testing only.
Artur Satayevb39ea9b2020-04-09 16:06:36 +0100589func SetTestNeverallowRules(config Config, testRules []Rule) {
Paul Duffin115445b2019-08-07 15:31:07 +0100590 config.Once(neverallowRulesKey, func() interface{} { return testRules })
591}