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

Implement a special parse error closing <math> and <svg> elements #240

Merged
merged 1 commit into from
Nov 8, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/parser/tree_builder_simulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,29 @@ impl TreeBuilderSimulator {

if self.current_ns == Namespace::Html {
self.check_integration_point_exit(tag_name)
} else if self.current_ns == Namespace::Svg && tag_name == Tag::Svg
|| self.current_ns == Namespace::MathML && tag_name == Tag::Math
{
} else if self.should_leave_ns(tag_name) {
self.leave_ns()
} else {
TreeBuilderFeedback::None
}
}

fn should_leave_ns(&self, tag_name: LocalNameHash) -> bool {
if self.current_ns == Namespace::Svg && tag_name == Tag::Svg
|| self.current_ns == Namespace::MathML && tag_name == Tag::Math
{
return true;
}

if (self.current_ns == Namespace::Svg || self.current_ns == Namespace::MathML)
&& tag_is_one_of!(tag_name, [P, Br])
{
// 13.2.6.5
return true;
}
false
}

#[inline]
pub const fn current_ns(&self) -> Namespace {
self.current_ns
Expand Down
171 changes: 171 additions & 0 deletions src/rewritable_units/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,177 @@ mod tests {
});
}

#[test]
fn sanitizer_bypass1() {
let out = rewrite_element(
b"<math><style><img></style></math>
<textarea><!--</textarea><img>--></textarea>
<div><style><img></style></div>",
UTF_8,
"img",
|el| el.set_tag_name("TROUBLE").unwrap(),
);
assert_eq!(
out,
"<math><style><TROUBLE></style></math>
<textarea><!--</textarea><TROUBLE>--></textarea>
<div><style><img></style></div>"
);
}

#[test]
fn sanitizer_bypass2() {
let out = rewrite_element(
b"<svg><p><style><!--</style><img>--></style>
<math><p></p><style><!--</style><img src/onerror>--></style></math>",
UTF_8,
"img",
|el| el.set_tag_name("BINGO").unwrap(),
);
assert_eq!(
out,
"<svg><p><style><!--</style><BINGO>--></style>
<math><p></p><style><!--</style><BINGO src onerror>--></style></math>"
);
}

#[test]
fn noscript_mode() {
let out = rewrite_element(
br#"<noscript><p alt="</noscript><img>">"#,
UTF_8,
"img",
|el| el.set_tag_name("we-have-scripts").unwrap(),
);
assert_eq!(out, r#"<noscript><p alt="</noscript><we-have-scripts>">"#);
}

#[test]
fn parse_error_in_foreign_content() {
let out = rewrite_element(
br#"<svg></p><style><a id="</style><img>">"#,
UTF_8,
"img,a",
|el| el.set_tag_name("HIT").unwrap(),
);
assert_eq!(out, r#"<svg></p><style><a id="</style><HIT>">"#);
}

#[test]
fn parse_error_in_foreign_content2() {
let out = rewrite_element(
br#"<math><br><style><a id="</style><img>">
<math><font kolor/><style><a/></style><body><style><a/></style></math>
<math><font COLOR/><style><a/></style>"#,
UTF_8,
"img,a",
|el| el.set_tag_name("HIT").unwrap(),
);
assert_eq!(
out,
r#"<math><br><style><a id="</style><HIT>">
<math><font kolor/><style><HIT/></style><body><style><a/></style></math>
<math><font COLOR/><style><a/></style>"#
);
}

#[test]
fn nested_html_namespace() {
let out = rewrite_element(
br#"<math><mtext><br/></mtext><style><a id="</style><img>">"#,
UTF_8,
"img,a",
|el| el.set_tag_name("HIT").unwrap(),
);
assert_eq!(
out,
r#"<math><mtext><br/></mtext><style><HIT id="</style><img>">"#
);
}

#[test]
fn self_closing_script() {
let out = rewrite_element(
br#"<svG><sCript/><img></script></svg>"#,
UTF_8,
"img",
|el| el.set_tag_name("HIT").unwrap(),
);
assert_eq!(out, r#"<svG><sCript/><HIT></script></svg>"#);
}

#[test]
fn surprise_text_integration_point() {
let out = rewrite_element(
br#"<math><annotation-xml encoding="nope/html"><style><img></style></annotation-xml></math>
<math><annotation-xml encoding="text/HTML"><style><img></style></annotation-xml></math>"#,
UTF_8,
"img",
|el| el.set_tag_name("XML").unwrap(),
);
assert_eq!(
out,
r#"<math><annotation-xml encoding="nope/html"><style><XML></style></annotation-xml></math>
<math><annotation-xml encoding="text/HTML"><style><img></style></annotation-xml></math>"#
);
}

#[test]
fn foreignobject() {
let out = rewrite_element(
br#"<svg><annotation-xml><foreignobject><style><!--</style><p id="--><img>">"#,
UTF_8,
"p,img",
|el| el.set_tag_name("HIT").unwrap(),
);
assert_eq!(
out,
r#"<svg><annotation-xml><foreignobject><style><!--</style><HIT id="--><img>">"#
);
}

#[test]
fn foreignobject2() {
let out = rewrite_element(
br#"<svg><a><foreignobject><a><table><a></table><style><!--</style></svg><a id="-><img>">"#,
UTF_8,
"a,img",
|el| el.set_tag_name("A").unwrap(),
);
assert_eq!(
out,
r#"<svg><A><foreignobject><A><table><A></A><style><!--</style></A><A id="-><img>">"#
);
}

#[test]
fn math_parse_error() {
let out = rewrite_element(
br#"<math><p></p><style><!--</style><img src/onerror>--></style></math>"#,
UTF_8,
"a,img",
|el| el.set_tag_name("A").unwrap(),
);
assert_eq!(
out,
r#"<math><p></p><style><!--</style><A src onerror>--></style></math>"#
);
}

#[test]
fn roundtrip_impossible() {
let out = rewrite_element(
br#"<form><math><mtext></form><form><mglyph><style></math><img>"#,
UTF_8,
"style,img",
|el| el.set_tag_name("no-img-here").unwrap(),
);
assert_eq!(
out,
r#"<form><math><mtext></form><form><mglyph><no-img-here></math><img>"#
);
}

#[test]
fn forbidden_characters_in_tag_name() {
rewrite_element(b"<div>", UTF_8, "div", |el| {
Expand Down
21 changes: 21 additions & 0 deletions src/rewritable_units/tokens/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,27 @@ mod tests {
output
}

#[test]
fn sanitizer_bypass() {
// HTML allows comments to be closed in multiple invalid ways
let out = rewrite_comment(b"<!--><img> surprise-->", UTF_8, |c| {
c.set_text("comment closes early").unwrap();
});
assert_eq!("<!--comment closes early--><img> surprise-->", out);
let out = rewrite_comment(b"<!-- --!><p><!---></p>", UTF_8, |c| {
c.set_text("unusual ending").unwrap();
});
assert_eq!("<!--unusual ending--><p><!--unusual ending--></p>", out);
}

#[test]
fn sanitizer_bypass2() {
let out = rewrite_comment(b"<?xml >s<img src=x onerror=alert(1)> ?>", UTF_8, |c| {
c.set_text("pie is a lie!").unwrap()
});
assert_eq!("<!--pie is a lie!-->s<img src=x onerror=alert(1)> ?>", out);
}

#[test]
fn comment_closing_sequence_in_text() {
rewrite_comment(b"<!-- foo -->", UTF_8, |c| {
Expand Down
Loading