-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.go
309 lines (245 loc) · 6.63 KB
/
html.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package revisor
import (
"bytes"
"errors"
"fmt"
"io"
"strings"
"golang.org/x/net/html"
)
// HTMLPolicy is used to declare supported elements, and what attributes they
// can have.
type HTMLPolicy struct {
ref string
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
// Uses will base the policy on another policy.
Uses string `json:"uses,omitempty"`
// Extends will add the declared elements to another policy.
Extends string `json:"extends,omitempty"`
Elements map[string]HTMLElement `json:"elements"`
}
// HTMLElement describes the constraints for a HTML element.
type HTMLElement struct {
Attributes ConstraintMap `json:"attributes,omitempty"`
}
var (
nl byte = '\n'
nlSlice = []byte{nl}
)
// Check that the given value follows the constraints of the policy.
func (hp *HTMLPolicy) Check(v string) error {
z := html.NewTokenizer(strings.NewReader(v))
var (
line = 1
char int
tagStack []string
)
var err error
for {
tagStack, err = hp.handleToken(z, tagStack)
if err != nil {
break
}
nls := bytes.Count(z.Raw(), nlSlice)
if nls > 0 {
line += nls
char = len(z.Raw()) - bytes.LastIndexByte(z.Raw(), nl)
} else {
char += len(z.Raw())
}
}
if err != nil && !errors.Is(err, io.EOF) {
return fmt.Errorf("invalid html after line %d char %d: %w", line, char, err)
}
if len(tagStack) > 0 {
return fmt.Errorf("unclosed tag <%s>", tagStack[0])
}
return nil
}
func (hp *HTMLPolicy) handleToken(z *html.Tokenizer, tagStack []string) ([]string, error) {
tt := z.Next()
switch tt {
case html.CommentToken:
case html.DoctypeToken:
case html.ErrorToken:
if errors.Is(z.Err(), io.EOF) {
return tagStack, z.Err() //nolint:wrapcheck
}
return nil, fmt.Errorf("parsing error: %w", z.Err())
case html.StartTagToken, html.SelfClosingTagToken:
n, hasAttr := z.TagName()
name := string(n)
spec, ok := hp.Elements[name]
if !ok {
return nil, fmt.Errorf("unsupported tag <%s>", name)
}
attrs := make(map[string]bool)
for hasAttr {
k, v, more := z.TagAttr()
attrName := string(k)
if spec.Attributes.Constraints == nil {
return nil, fmt.Errorf("no attributes allowed for <%s>",
name,
)
}
constraint, ok := spec.Attributes.Constraints[attrName]
if !ok {
return nil, fmt.Errorf("unsupported <%s> attribute %q",
name, attrName,
)
}
// TODO: Handle deprecation of HTML attribute values.
_, err := constraint.Validate(string(v), true, nil)
if err != nil {
return nil, fmt.Errorf(
"<%s> attribute %q: %w",
name, attrName, err,
)
}
attrs[attrName] = true
hasAttr = more
}
for _, attrName := range spec.Attributes.Keys {
ok := attrs[attrName]
if !ok && !spec.Attributes.Constraints[attrName].Optional {
return nil, fmt.Errorf(
"missing required <%s> attribute %q",
name, attrName)
}
}
if tt != html.SelfClosingTagToken {
tagStack = append(tagStack, name)
}
case html.EndTagToken:
endIndex := len(tagStack) - 1
n, _ := z.TagName()
name := string(n)
if endIndex < 0 || name != tagStack[endIndex] {
return nil, fmt.Errorf("unexpected end tag </%s>", name)
}
tagStack = tagStack[0:endIndex]
case html.TextToken:
data := z.Raw()
for i := 0; i < len(data); i++ {
if data[i] != '&' {
continue
}
l, err := ValidateEntity(data[i:])
if err != nil {
return nil, fmt.Errorf("invalid html entity: %w", err)
}
i += l
}
}
return tagStack, nil
}
// HTMLPolicySet is a set of declared HTML policies.
type HTMLPolicySet struct {
namedPolicies map[string]*HTMLPolicy
extensions []HTMLPolicy
}
func NewHTMLPolicySet() *HTMLPolicySet {
return &HTMLPolicySet{
namedPolicies: make(map[string]*HTMLPolicy),
}
}
// Add policies to the set.
func (s *HTMLPolicySet) Add(source string, policies ...HTMLPolicy) error {
for i := range policies {
policy := policies[i]
casedElems := make(map[string]HTMLElement)
policy.ref = policy.Name
if policy.ref == "" {
policy.ref = fmt.Sprintf("%s policy %d", source, i+1)
}
for k, e := range policy.Elements {
k := strings.ToLower(k)
casedElems[k] = e
}
policy.Elements = casedElems
if policy.Uses != "" && policy.Name == "" {
return fmt.Errorf(
"a html policy must have a name to be able to use another policy")
}
if policy.Extends != "" {
s.extensions = append(s.extensions, policy)
}
if policy.Name != "" {
_, exists := s.namedPolicies[policy.Name]
if exists {
return fmt.Errorf(
"html policy %q redeclared", policy.Name)
}
s.namedPolicies[policy.Name] = &policy
}
}
return nil
}
// Resolve all extensions and usages and return the finished policies.
func (s *HTMLPolicySet) Resolve() (map[string]*HTMLPolicy, error) {
for _, policy := range s.extensions {
extending, ok := s.namedPolicies[policy.Extends]
if !ok {
return nil, fmt.Errorf("the html policy %q cannot be extended, because it doesn't exist", policy.Extends)
}
if extending.Extends != "" {
return nil, fmt.Errorf(
"only one level of 'extends' is allowed, %q attempted to extend %q, which extends %q",
policy.ref, policy.Extends, extending.Extends,
)
}
err := extendHTMLPolicy(extending, policy)
if err != nil {
return nil, err
}
}
for _, p := range s.namedPolicies {
if p.Uses == "" {
continue
}
source, ok := s.namedPolicies[p.Uses]
if !ok {
return nil, fmt.Errorf(
"the policy %q could not use %q: it doesn't exist",
p.Name, p.Uses,
)
}
if source.Uses != "" {
return nil, fmt.Errorf(
"only one level of 'uses' references is allowed, %q attempted to use %q, which uses %q",
p.Name, p.Uses, source.Uses,
)
}
err := extendHTMLPolicy(p, *source)
if err != nil {
return nil, fmt.Errorf(
"the policy %q could not use %q: %w",
p.Name, p.Uses, err,
)
}
}
return s.namedPolicies, nil
}
func extendHTMLPolicy(extending *HTMLPolicy, policy HTMLPolicy) error {
for eName, eDef := range policy.Elements {
eCurrent := extending.Elements[eName]
if eCurrent.Attributes.Constraints == nil &&
eDef.Attributes.Constraints != nil {
eCurrent.Attributes.Constraints = make(map[string]StringConstraint)
}
for _, attrName := range eDef.Attributes.Keys {
_, aExists := eCurrent.Attributes.Constraints[attrName]
if aExists {
return fmt.Errorf(
"attribute %q of <%s> in the policy %q was redeclared",
attrName, eName, policy.Extends,
)
}
eCurrent.Attributes.Constraints[attrName] = eDef.Attributes.Constraints[attrName]
eCurrent.Attributes.Keys = append(eCurrent.Attributes.Keys, attrName)
}
extending.Elements[eName] = eCurrent
}
return nil
}