Debian Codegraph @

config/security/whitelist.go

raw
1 // Copyright 2021 The Hugo Authors. 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 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package security
15
16 import (
17 "encoding/json"
18 "fmt"
19 "regexp"
20 "strings"
21
22 "github.com/gohugoio/hugo/hugofs/hglob"
23 )
24
25 const acceptNoneKeyword = "none"
26
27 // Whitelist holds a whitelist.
28 //
29 // Patterns are regular expressions. A pattern prefixed with "! "
30 // (see hglob.NegationPrefix) is a deny rule: a name that matches any
31 // deny rule is rejected even if it matches an allow rule.
32 // A whitelist made up exclusively of deny rules implicitly allows
33 // names that do not match any of them.
34 type Whitelist struct {
35 acceptNone bool
36 allow []*regexp.Regexp
37 deny []*regexp.Regexp
38
39 // Store this for debugging/error reporting.
40 patternsStrings []string
41 }
42
43 // MarshalJSON is for internal use only.
44 func (w Whitelist) MarshalJSON() ([]byte, error) {
45 if w.acceptNone {
46 return json.Marshal(acceptNoneKeyword)
47 }
48
49 return json.Marshal(w.patternsStrings)
50 }
51
52 // NewWhitelist creates a new Whitelist from zero or more patterns.
53 // An empty patterns list or a pattern with the value 'none' will create
54 // a whitelist that will Accept none. Patterns prefixed with "! " act as
55 // deny rules; see Whitelist.
56 func NewWhitelist(patterns ...string) (Whitelist, error) {
57 if len(patterns) == 0 {
58 return Whitelist{acceptNone: true}, nil
59 }
60
61 var (
62 acceptSome bool
63 patternsStrings []string
64 )
65
66 for _, p := range patterns {
67 if p == acceptNoneKeyword {
68 acceptSome = false
69 break
70 }
71
72 if ps := strings.TrimSpace(p); ps != "" {
73 acceptSome = true
74 patternsStrings = append(patternsStrings, ps)
75 }
76 }
77
78 if !acceptSome {
79 return Whitelist{acceptNone: true}, nil
80 }
81
82 var allow, deny []*regexp.Regexp
83 for _, p := range patternsStrings {
84 raw := p
85 negate := strings.HasPrefix(p, hglob.NegationPrefix)
86 if negate {
87 raw = p[len(hglob.NegationPrefix):]
88 }
89 re, err := regexp.Compile(raw)
90 if err != nil {
91 return Whitelist{}, fmt.Errorf("failed to compile whitelist pattern %q: %w", p, err)
92 }
93 if negate {
94 deny = append(deny, re)
95 } else {
96 allow = append(allow, re)
97 }
98 }
99
100 return Whitelist{allow: allow, deny: deny, patternsStrings: patternsStrings}, nil
101 }
102
103 // MustNewWhitelist creates a new Whitelist from zero or more patterns and panics on error.
104 func MustNewWhitelist(patterns ...string) Whitelist {
105 w, err := NewWhitelist(patterns...)
106 if err != nil {
107 panic(err)
108 }
109 return w
110 }
111
112 // Accept reports whether name is whitelisted.
113 func (w Whitelist) Accept(name string) bool {
114 if w.acceptNone {
115 return false
116 }
117
118 for _, p := range w.deny {
119 if p.MatchString(name) {
120 return false
121 }
122 }
123
124 if len(w.allow) == 0 {
125 // A whitelist with only deny rules implicitly allows everything
126 // that is not denied. An empty (zero-value) whitelist rejects.
127 return len(w.deny) > 0
128 }
129
130 for _, p := range w.allow {
131 if p.MatchString(name) {
132 return true
133 }
134 }
135 return false
136 }
137
138 func (w Whitelist) String() string {
139 return fmt.Sprint(w.patternsStrings)
140 }