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

Improve HTML validation #3192

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,6 @@ public class NarrativeHtmlSanitizer : INarrativeHtmlSanitizer
"xmlns",
};

private static readonly ISet<string> Src = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"#",
"data:",
"http:",
"https:",
};

// Obvious invalid structural parsing errors to report
private static readonly ISet<HtmlParseError> RaiseErrorTypes = new HashSet<HtmlParseError>
{
Expand Down Expand Up @@ -180,9 +172,11 @@ public IEnumerable<string> Validate(string html)
var dom = parser.ParseDocument(string.Format(HtmlTemplate, html));

// Report parsing errors
if (errors.Any())
var htmlParseErrors = errors.Where(x => RaiseErrorTypes.Contains((HtmlParseError)x.Code)).ToList();

if (htmlParseErrors.Any())
{
foreach (var error in errors.Where(x => RaiseErrorTypes.Contains((HtmlParseError)x.Code)))
foreach (var error in htmlParseErrors)
{
yield return string.Format(Core.Resources.IllegalHtmlParsingError, error.Message, error.Position.Line, error.Position.Column);
}
Expand Down Expand Up @@ -289,14 +283,6 @@ private static void ValidateAttributes(IElement element, Action<IElement, IAttr>
{
onInvalidAttr(element, attr);
}

if (string.Equals("src", attr.Name, StringComparison.OrdinalIgnoreCase))
{
if (!Src.Any(x => attr.Value.StartsWith(x, StringComparison.OrdinalIgnoreCase)))
{
onInvalidAttr(element, attr);
}
}
Comment on lines -293 to -299
Copy link
Member

Choose a reason for hiding this comment

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

Can we add a few examples to the tests of what this now supports? (i.e. the IG image example) Are there any negative test cases we should add?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think there are any negatives to add. @michael-wilson-au added a negative test as part of the first PR, but my change only removes a restriction. I'll add some examples of what is supported.

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public void Validate(string code)
[InlineData("<div>text</div><?xml version=\"1.0\" encoding=\"UTF-8\"?>div>")]
[InlineData("<not_real>Example!</not_real>")]
[InlineData("<?not_real><div>Example!</div>")]
public void GivenHtmlWithEmptyDiv_WhenSanitizingHtml_ThenAValidationErrorIsReturned(string val)
[InlineData("<div><!-- Comment ended with a dash. This error should be ignored ---><not_real>This tag should return validation error</not_real></div>")]
public void GivenInvalidNarrativeHtml_WhenSanitizingHtml_ThenAValidationErrorIsReturned(string val)
{
var results = _sanitizer.Validate(val);

Expand Down