Skip to content

Commit

Permalink
Merge pull request #5 from pawepaw/master
Browse files Browse the repository at this point in the history
Allowed all attributes for url check. Tests for src in img.
  • Loading branch information
cakkermans committed May 13, 2016
2 parents 18ff106 + 80d95f8 commit 557376f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
44 changes: 42 additions & 2 deletions Web.HtmlSanitizer.Tests/AttributeCheckTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AttributeCheckTests
/// Tests if obviously illegal URL's are caught while obviously legal ones are left alone.
/// </summary>
[Fact]
public void UrlCheckTest()
public void AHrefUrlCheckTest()
{

string result;
Expand All @@ -36,7 +36,7 @@ public void UrlCheckTest()
/// Regression test for checking if relative URL's are accepted.
/// </summary>
[Fact]
public void UrlCheckRelativeTest()
public void AHrefUrlCheckRelativeTest()
{

string result;
Expand All @@ -50,6 +50,46 @@ public void UrlCheckRelativeTest()
Assert.Equal(expected, result);
}



[Fact]
public void ImgSrcUrlCheckTest()
{

string result;
var sanitizer = new HtmlSanitizer();
sanitizer.Tag("img").CheckAttribute("src", HtmlSanitizerCheckType.Url);

// Test some illegal href
var inputIllegal = @"<img src=""javascript:alert('test')"">";
var expectedIllegal = @"";
result = sanitizer.Sanitize(inputIllegal);
Assert.Equal(expectedIllegal, result);

// Test a legal well formed url
var inputLegal = @"<img src=""http://www.google.com/a.png"">>";
result = sanitizer.Sanitize(inputLegal);
Assert.Equal(inputLegal, result);
}

/// <summary>
/// Regression test for checking if relative URL's are accepted.
/// </summary>
[Fact]
public void ImgSrcUrlCheckRelativeTest()
{

string result;
var sanitizer = new HtmlSanitizer();
sanitizer.Tag("img").CheckAttribute("src", HtmlSanitizerCheckType.Url);

// Test a relative url, which should pass.
var input = @"<img src=""../relative.png"">";
var expected = @"<img src=""../relative.png"">";
result = sanitizer.Sanitize(input);
Assert.Equal(expected, result);
}

/// <summary>
/// Tests if empty attributes are left alone.
/// </summary>
Expand Down
4 changes: 0 additions & 4 deletions Web.HtmlSanitizer/HtmlSanitizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ public static bool AttributeUrlCheck(HtmlAttribute attribute)
/// <returns></returns>
public static SanitizerOperation LinkHrefCheck(HtmlAttribute attribute)
{

if (attribute.Name != "href")
throw new ArgumentException("Expected href attribute.");

// Check the url. There's no use in keeping link tags without a link, so flatten the tag on failure.
if (!AttributeUrlCheck(attribute))
return SanitizerOperation.FlattenTag;
Expand Down

0 comments on commit 557376f

Please sign in to comment.