Commit e213a0c0 authored by Mike Samuel's avatar Mike Samuel

exp/template/html: recognize whitespace at start of URLs.

HTML5 uses "Valid URL potentially surrounded by spaces" for
attrs: http://www.w3.org/TR/html5/index.html#attributes-1

    <a href=" {{.}}">

should be escaped to filter out "javascript:..." as data.

R=nigeltao
CC=golang-dev
https://golang.org/cl/5027045
parent 605e57d8
...@@ -120,6 +120,11 @@ func TestEscape(t *testing.T) { ...@@ -120,6 +120,11 @@ func TestEscape(t *testing.T) {
`<a href='{{"javascript:alert(%22pwned%22)"}}'>`, `<a href='{{"javascript:alert(%22pwned%22)"}}'>`,
`<a href='#ZgotmplZ'>`, `<a href='#ZgotmplZ'>`,
}, },
{
"dangerousURLStart2",
`<a href=' {{"javascript:alert(%22pwned%22)"}}'>`,
`<a href=' #ZgotmplZ'>`,
},
{ {
"nonHierURL", "nonHierURL",
`<a href={{"mailto:Muhammed \"The Greatest\" Ali <m.ali@example.com>"}}>`, `<a href={{"mailto:Muhammed \"The Greatest\" Ali <m.ali@example.com>"}}>`,
......
...@@ -169,7 +169,9 @@ func tAttr(c context, s []byte) (context, []byte) { ...@@ -169,7 +169,9 @@ func tAttr(c context, s []byte) (context, []byte) {
func tURL(c context, s []byte) (context, []byte) { func tURL(c context, s []byte) (context, []byte) {
if bytes.IndexAny(s, "#?") >= 0 { if bytes.IndexAny(s, "#?") >= 0 {
c.urlPart = urlPartQueryOrFrag c.urlPart = urlPartQueryOrFrag
} else if len(s) != 0 && c.urlPart == urlPartNone { } else if len(s) != eatWhiteSpace(s, 0) && c.urlPart == urlPartNone {
// HTML5 uses "Valid URL potentially surrounded by spaces" for
// attrs: http://www.w3.org/TR/html5/index.html#attributes-1
c.urlPart = urlPartPreQuery c.urlPart = urlPartPreQuery
} }
return c, nil return c, nil
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment