Skip to content

Commit e44d1dc

Browse files
committed
add test for escaping html entities, when using the error page template
1 parent 39f3840 commit e44d1dc

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

apps/internal/local/server_test.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ func TestServer(t *testing.T) {
2121
defer cancel()
2222

2323
tests := []struct {
24-
desc string
25-
reqState string
26-
port int
27-
q url.Values
28-
failPage bool
29-
statusCode int
30-
successPage []byte
31-
errorPage []byte
32-
testTemplate bool
24+
desc string
25+
reqState string
26+
port int
27+
q url.Values
28+
failPage bool
29+
statusCode int
30+
successPage []byte
31+
errorPage []byte
32+
testTemplate bool
33+
testHTMLInjection bool
3334
}{
3435
{
3536
desc: "Error: Query Values has 'error' key",
@@ -111,6 +112,15 @@ func TestServer(t *testing.T) {
111112
statusCode: 200,
112113
testTemplate: true,
113114
},
115+
{
116+
desc: "Error: Query Values missing 'state' key, using default fail error page - XSS test",
117+
reqState: "state",
118+
port: 0,
119+
q: url.Values{"error": []string{"<script>alert('this code snippet was executed')</script>"}, "error_description": []string{"error_description"}},
120+
statusCode: 200,
121+
testTemplate: true,
122+
testHTMLInjection: true,
123+
},
114124
}
115125

116126
for _, test := range tests {
@@ -185,6 +195,13 @@ func TestServer(t *testing.T) {
185195
}
186196

187197
if test.testTemplate {
198+
if test.testHTMLInjection {
199+
if !strings.Contains(string(content), "&lt;script&gt;alert(&#39;this code snippet was executed&#39;)&lt;/script&gt;") {
200+
t.Errorf("TestServer(%s): want escaped html entities", test.desc)
201+
}
202+
continue
203+
}
204+
188205
if len(test.errorPage) > 0 {
189206
errCode := bytes.Contains(test.errorPage, []byte("{{.Code}}"))
190207
errDescription := bytes.Contains(test.errorPage, []byte("{{.Err}}"))

0 commit comments

Comments
 (0)