File tree Expand file tree Collapse file tree 3 files changed +25
-0
lines changed
test/query-tests/Security/CWE-079 Expand file tree Collapse file tree 3 files changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -127,4 +127,20 @@ module SharedXss {
127
127
)
128
128
}
129
129
}
130
+
131
+ /**
132
+ * A `Template` from `html/template` will HTML-escape data automatically
133
+ * and therefore acts as a sanitizer for XSS vulnerabilities.
134
+ */
135
+ class HtmlTemplateSanitizer extends Sanitizer , DataFlow:: Node {
136
+ HtmlTemplateSanitizer ( ) {
137
+ exists ( Method m , DataFlow:: CallNode call | m = call .getCall ( ) .getTarget ( ) |
138
+ m .hasQualifiedName ( "html/template" , "Template" , "ExecuteTemplate" ) and
139
+ call .getArgument ( 2 ) = this
140
+ or
141
+ m .hasQualifiedName ( "html/template" , "Template" , "Execute" ) and
142
+ call .getArgument ( 1 ) = this
143
+ )
144
+ }
145
+ }
130
146
}
Original file line number Diff line number Diff line change @@ -3,16 +3,22 @@ package main
3
3
import (
4
4
"fmt"
5
5
"html"
6
+ "html/template"
6
7
"net/http"
7
8
)
8
9
9
10
func serve1 () {
11
+ var template template.Template
12
+
10
13
http .HandleFunc ("/user" , func (w http.ResponseWriter , r * http.Request ) {
11
14
r .ParseForm ()
12
15
username := r .Form .Get ("username" )
13
16
if ! isValidUsername (username ) {
14
17
// GOOD: a request parameter is escaped before being put into the response
15
18
fmt .Fprintf (w , "%q is an unknown user" , html .EscapeString (username ))
19
+ // GOOD: using html/template escapes values for us
20
+ template .Execute (w , username )
21
+ template .ExecuteTemplate (w , "test" , username )
16
22
} else {
17
23
// TODO: do something exciting
18
24
}
Original file line number Diff line number Diff line change @@ -2,15 +2,18 @@ package main
2
2
3
3
import (
4
4
"html"
5
+ "html/template"
5
6
"io"
6
7
"io/ioutil"
7
8
"net/http"
8
9
)
9
10
10
11
func ListFiles1 (w http.ResponseWriter , r * http.Request ) {
12
+ var template template.Template
11
13
files , _ := ioutil .ReadDir ("." )
12
14
13
15
for _ , file := range files {
14
16
io .WriteString (w , html .EscapeString (file .Name ())+ "\n " )
17
+ template .Execute (w , file .Name ())
15
18
}
16
19
}
You can’t perform that action at this time.
0 commit comments