Skip to content

Commit a3a7602

Browse files
committed
Fix hex escape issue in style element
1 parent 0963175 commit a3a7602

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/HtmlSanitizer/HtmlSanitizer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ private void SanitizeStyleSheets(IHtmlDocument dom, string baseUrl)
743743
else i++;
744744
}
745745

746-
styleTag.InnerHtml = styleSheet.ToCss();
746+
styleTag.InnerHtml = styleSheet.ToCss().Replace("<", "\\3c");
747747
}
748748
}
749749

test/HtmlSanitizer.Tests/Tests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3201,6 +3201,19 @@ public void PreParsedDocumentWithContextTest()
32013201

32023202
Assert.Equal("<html><head></head><body><div>hi</div></body></html>", returnedDocument.ToHtml());
32033203
}
3204+
3205+
[Fact]
3206+
public void StyleByPassTest()
3207+
{
3208+
var sanitizer = new HtmlSanitizer();
3209+
3210+
sanitizer.AllowedTags.Add("style");
3211+
3212+
var html = "aaabc<style>x[x='\\3c /style>\\3c img src onerror=alert(1)>']{}</style>";
3213+
var sanitized = sanitizer.Sanitize(html, "http://www.example.com");
3214+
3215+
Assert.Equal("aaabc<style>x[x=\"\\3c/style>\\3cimg src onerror=alert(1)>\"] { }</style>", sanitized);
3216+
}
32043217
}
32053218
}
32063219

0 commit comments

Comments
 (0)