internal/codescan/attrs.go
169 lines · 4629 bytes
14 symbols in this file
1package codescan
2
3import "strings"
4
5// unquotedAttrRule flags a template expression inside a tag unless it sees
6// the expression within ="…" or ='…'. It matches in generic mode, where a
7// quoted string ends at the next quote, so href="{{ f("a") }}" is reported,
8// and so is an action that only adds an attribute name
9// ({{if .X}} selected{{end}}). unquotedLines reads the template as HTML to
10// tell those apart from the real thing.
11const unquotedAttrRule = "html-templates.security.unquoted-attribute-var"
12
13// rawText are elements whose content is not markup.
14var rawText = map[string]bool{"script": true, "style": true, "textarea": true, "title": true}
15
16// Tag states.
17const (
18 inText = iota
19 beforeAttr
20 attrName
21 afterName
22 beforeValue
23 unquotedValue
24 quotedValue
25)
26
27// unquotedLines returns the lines of an HTML template where an output
28// expression is an unquoted attribute value or stands in for attributes.
29// ok is false when the file does not read as HTML to the end, and then
30// nothing is known.
31func unquotedLines(src []byte) (lines map[int]bool, ok bool) {
32 s := string(src)
33 lines = map[int]bool{}
34 line := 1
35 state := inText
36 var quote byte
37 var tag string
38 advance := func(to int, from int) int {
39 line += strings.Count(s[from:to], "\n")
40 return to
41 }
42 for i := 0; i < len(s); {
43 if end, output, found := expression(s, i); found {
44 if end < 0 {
45 return nil, false
46 }
47 if output && state != inText && state != quotedValue {
48 lines[line] = true
49 if state == beforeValue {
50 state = unquotedValue
51 } else if state != unquotedValue {
52 state = attrName
53 }
54 }
55 i = advance(end, i)
56 continue
57 }
58 c, prev := s[i], state
59 switch state {
60 case inText:
61 switch {
62 case strings.HasPrefix(s[i:], "<!--"):
63 end := strings.Index(s[i+4:], "-->")
64 if end < 0 {
65 return nil, false
66 }
67 i = advance(i+4+end+3, i)
68 continue
69 case c == '<' && i+1 < len(s) && isLetter(s[i+1]):
70 j := i + 1
71 for j < len(s) && (isLetter(s[j]) || s[j] >= '0' && s[j] <= '9' || s[j] == '-' || s[j] == ':') {
72 j++
73 }
74 tag = strings.ToLower(s[i+1 : j])
75 state = beforeAttr
76 i = j
77 continue
78 }
79 case quotedValue:
80 if c == quote {
81 state = beforeAttr
82 }
83 case beforeValue:
84 switch {
85 case c == '"' || c == '\'':
86 quote, state = c, quotedValue
87 case c == '>':
88 state = inText
89 case !isSpace(c):
90 state = unquotedValue
91 }
92 case unquotedValue:
93 if isSpace(c) {
94 state = beforeAttr
95 } else if c == '>' {
96 state = inText
97 }
98 default: // beforeAttr, attrName, afterName
99 switch {
100 case c == '=':
101 state = beforeValue
102 case c == '>':
103 state = inText
104 case isSpace(c):
105 if state == attrName {
106 state = afterName
107 }
108 case c != '/':
109 state = attrName
110 }
111 }
112 i = advance(i+1, i)
113 if prev != inText && state == inText && rawText[tag] && s[i-2] != '/' {
114 end := strings.Index(strings.ToLower(s[i:]), "</"+tag)
115 if end < 0 {
116 return nil, false
117 }
118 i = advance(i+end, i)
119 }
120 }
121 return lines, state == inText
122}
123
124// expression reports whether a template expression opens at i: {{ }},
125// {{{ }}}, {% %} or {# #}. end is just past its close, or -1 when it is
126// not closed. output is whether it writes a value, rather than being a
127// control action or a comment.
128func expression(s string, i int) (end int, output, found bool) {
129 open, close := "", ""
130 switch {
131 case strings.HasPrefix(s[i:], "{{{"):
132 open, close = "{{{", "}}}"
133 case strings.HasPrefix(s[i:], "{{"):
134 open, close = "{{", "}}"
135 case strings.HasPrefix(s[i:], "{%"):
136 open, close = "{%", "%}"
137 case strings.HasPrefix(s[i:], "{#"):
138 open, close = "{#", "#}"
139 default:
140 return 0, false, false
141 }
142 n := strings.Index(s[i+len(open):], close)
143 if n < 0 {
144 return -1, false, true
145 }
146 if open[1] != '{' {
147 return i + len(open) + n + len(close), false, true
148 }
149 body := strings.TrimLeft(s[i+len(open):i+len(open)+n], "-~ \t\r\n")
150 return i + len(open) + n + len(close), !control(body), true
151}
152
153// control reports whether the body of a {{ }} is a Go template action or a
154// Handlebars block, comment or else, which write nothing themselves.
155func control(body string) bool {
156 if body == "" || strings.ContainsRune("#/^!", rune(body[0])) {
157 return true
158 }
159 word, _, _ := strings.Cut(strings.TrimSpace(body), " ")
160 switch word {
161 case "if", "else", "end", "range", "with", "block", "define", "break", "continue":
162 return true
163 }
164 return false
165}
166
167func isLetter(c byte) bool { return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' }
168
169func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' }