Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle all different token types that the parser can emit (d'oh). #70

Merged
merged 1 commit into from
May 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions inline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ func TestRawHtmlTag(t *testing.T) {
"<iframe src=http://ha.ckers.org/scriptlet.html <",
// The hyperlink gets linkified, the <iframe> gets escaped
"<p>&lt;iframe src=<a href=\"http://ha.ckers.org/scriptlet.html\">http://ha.ckers.org/scriptlet.html</a> &lt;</p>\n",

// Additonal token types: SelfClosing, Comment, DocType.
"<br/>",
"<p><br></p>\n",

"<!-- Comment -->",
"<!-- Comment -->\n",

"<!DOCTYPE test>",
"<p>&lt;!DOCTYPE test&gt;</p>\n",
}
doTestsInlineParam(t, tests, 0, HTML_SKIP_STYLE|HTML_SANITIZE_OUTPUT)
}
Expand Down
9 changes: 8 additions & 1 deletion sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func sanitizeHtmlSafe(input []byte) []byte {
case html.TextToken:
// Text is written escaped.
wr.WriteString(tokenizer.Token().String())
case html.StartTagToken:
case html.SelfClosingTagToken, html.StartTagToken:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When HTML_SANITIZE_OUTPUT is turned on, self-closing tags like <hr /> become rewritten as <hr>. I know it's still valid HTML, but there's no reason to be changing them.

It can be fixed either by handling the html.SelfClosingTagToken case via the same path as html.EndTagToken on line 100, or by adding an if statement on line 96 that does wr.WriteString(" />") when t == html.SelfClosingTagToken.

// HTML tags are escaped unless whitelisted.
tag, hasAttributes := tokenizer.TagName()
tagName := string(tag)
Expand Down Expand Up @@ -105,7 +105,14 @@ func sanitizeHtmlSafe(input []byte) []byte {
} else {
wr.WriteString(html.EscapeString(string(tokenizer.Raw())))
}
case html.CommentToken:
// Comments are not really expected, but harmless.
wr.Write(tokenizer.Raw())
case html.DoctypeToken:
// Escape DOCTYPES, entities etc can be dangerous
wr.WriteString(html.EscapeString(string(tokenizer.Raw())))
default:
tokenizer.Token()
panic(fmt.Errorf("Unexpected token type %v", t))
}
}
Expand Down