Skip to content

Commit

Permalink
Resolves #56 strings.ToLower() results in false match
Browse files Browse the repository at this point in the history
Use of strings.ToLower() within the match for script or style handler for
TextToken results in unsanitized content being printed.
  • Loading branch information
David Kitchen committed Dec 22, 2017
1 parent 80ef48b commit 5793ebc
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ func (p *Policy) addDefaultElementsWithoutAttrs() {
p.setOfElementsAllowedWithoutAttrs["ruby"] = struct{}{}
p.setOfElementsAllowedWithoutAttrs["s"] = struct{}{}
p.setOfElementsAllowedWithoutAttrs["samp"] = struct{}{}
p.setOfElementsAllowedWithoutAttrs["script"] = struct{}{}
p.setOfElementsAllowedWithoutAttrs["section"] = struct{}{}
p.setOfElementsAllowedWithoutAttrs["select"] = struct{}{}
p.setOfElementsAllowedWithoutAttrs["small"] = struct{}{}
Expand Down
3 changes: 1 addition & 2 deletions sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
case html.TextToken:

if !skipElementContent {
switch strings.ToLower(mostRecentlyStartedToken) {
switch mostRecentlyStartedToken {
case "script":
// not encouraged, but if a policy allows JavaScript we
// should not HTML escape it as that would break the output
Expand All @@ -235,7 +235,6 @@ func (p *Policy) sanitize(r io.Reader) *bytes.Buffer {
buff.WriteString(token.String())
}
}

default:
// A token that didn't exist in the html package when we wrote this
return &bytes.Buffer{}
Expand Down
40 changes: 40 additions & 0 deletions sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1644,3 +1644,43 @@ AAAASUVORK5CYII=" alt="">`
expected)
}
}

func TestIssue55ScriptTags(t *testing.T) {
p1 := NewPolicy()
p2 := UGCPolicy()
p3 := UGCPolicy().AllowElements("script")

in := `<SCRIPT>document.write('<h1><header/h1>')</SCRIPT>`
expected := ``
out := p1.Sanitize(in)
if out != expected {
t.Errorf(
"test failed;\ninput : %s\noutput : %s\nexpected: %s",
in,
out,
expected,
)
}

expected = ``
out = p2.Sanitize(in)
if out != expected {
t.Errorf(
"test failed;\ninput : %s\noutput : %s\nexpected: %s",
in,
out,
expected,
)
}

expected = `<script>document.write('<h1><header/h1>')</script>`
out = p3.Sanitize(in)
if out != expected {
t.Errorf(
"test failed;\ninput : %s\noutput : %s\nexpected: %s",
in,
out,
expected,
)
}
}

0 comments on commit 5793ebc

Please # to comment.