Skip to content

Commit

Permalink
Force input HTML into body
Browse files Browse the repository at this point in the history
  • Loading branch information
mganss committed Feb 11, 2016
1 parent a36f4c8 commit 24d3767
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
46 changes: 42 additions & 4 deletions HtmlSanitizer.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ public void ImageHalfOpenHtmlXSSTest()
string actual = sanitizer.Sanitize(htmlFragment);

// Assert
string expected = "";
string expected = "<img>";
Assert.That(actual, Is.EqualTo(expected).IgnoreCase);
}

Expand All @@ -493,13 +493,12 @@ public void ImageDoubleOpenAngleBracketXSSTest()
// Arrange
var sanitizer = new HtmlSanitizer();


// Act
string htmlFragment = "<image src=http://ha.ckers.org/scriptlet.html <";
string actual = sanitizer.Sanitize(htmlFragment);

// Assert
string expected = "";
string expected = "<img src=\"http://ha.ckers.org/scriptlet.html\">";
Assert.That(actual, Is.EqualTo(expected).IgnoreCase);
}

Expand Down Expand Up @@ -976,7 +975,7 @@ public void XmlNamespaceXSSTest()


// Act
string htmlFragment = "<HTML xmlns:xss> <?import namespace=\"xss\" implementation=\"http://ha.ckers.org/xss.htc\"> <xss:xss>XSS</xss:xss></HTML>";
string htmlFragment = "<HTML xmlns:xss><?import namespace=\"xss\" implementation=\"http://ha.ckers.org/xss.htc\"><xss:xss>XSS</xss:xss></HTML>";
string actual = sanitizer.Sanitize(htmlFragment);

// Assert
Expand Down Expand Up @@ -2449,6 +2448,45 @@ public void RemoveEventForNotAllowedUrlAtStyle()

Assert.That(actual, Is.EqualTo(RemoveReason.NotAllowedUrlValue));
}

[Test]
public void RemoveEventForNotAllowedTag_ScriptTag()
{
RemoveReason? actual = null;
var s = new HtmlSanitizer();
s.RemovingTag += (sender, args) =>
{
actual = args.Reason;
};
s.Sanitize("<script>alert('Hello world!')</script>");
Assert.That(actual, Is.EqualTo(RemoveReason.NotAllowedTag));
}

[Test]
public void RemoveEventForNotAllowedTag_StyleTag()
{
RemoveReason? actual = null;
var s = new HtmlSanitizer();
s.RemovingTag += (sender, args) =>
{
actual = args.Reason;
};
s.Sanitize("<style> body {background-color:lightgrey;}</style>");
Assert.That(actual, Is.EqualTo(RemoveReason.NotAllowedTag));
}

[Test]
public void RemoveEventForNotAllowedTag_ScriptTagAndSpan()
{
RemoveReason? actual = null;
var s = new HtmlSanitizer();
s.RemovingTag += (sender, args) =>
{
actual = args.Reason;
};
s.Sanitize("<span>Hi</span><script>alert('Hello world!')</script>");
Assert.That(actual, Is.EqualTo(RemoveReason.NotAllowedTag));
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion HtmlSanitizer/HtmlSanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public string Sanitize(string html, string baseUrl = "", IMarkupFormatter output
IsToleratingInvalidConstraints = true,
IsToleratingInvalidValues = true
}));
var dom = parser.Parse(html);
var dom = parser.Parse("<body>" + html + "</body>");

// remove non-whitelisted tags
foreach (var tag in dom.Body.QuerySelectorAll("*").Where(t => !IsAllowedTag(t)).ToList())
Expand Down

0 comments on commit 24d3767

Please sign in to comment.