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

fix: escape foreign style tag content when serializing HTML5 (v1.15.x) #3350

Merged
merged 2 commits into from
Dec 2, 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA

---

## next / unreleased

### Fixed

* [CRuby] When serializing HTML5 documents, properly escape foreign content "style" elements. Normally, a "style" tag contains raw text that does not need entity-escaping, but when it appears in either SVG or MathML foreign content, the "style" tag is now correctly escaped when serialized. @flavorjones


## 1.15.6 / 2024-03-16

### Security
Expand Down
8 changes: 7 additions & 1 deletion ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -1853,13 +1853,19 @@ is_one_of(xmlNodePtr node, char const *const *tagnames, size_t num_tagnames)
if (name == NULL) { // fragments don't have a name
return false;
}

if (node->ns != NULL) {
// if the node has a namespace, it's in a foreign context and is not one of the HTML tags we're
// matching against.
return false;
}

for (size_t idx = 0; idx < num_tagnames; ++idx) {
if (!strcmp(name, tagnames[idx])) {
return true;
}
}
return false;

}

static void
Expand Down
16 changes: 16 additions & 0 deletions test/html5/test_serialize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -553,4 +553,20 @@ def test_serializing_html5_fragment
refute(fragment.send(:prepend_newline?))
assert_equal("<div>hello</div>goodbye", fragment.to_html)
end

describe "foreign content style tag serialization is escaped" do
it "with svg parent" do
input = %{<svg><style>&lt;img src>}
expected = %{<svg><style>&lt;img src&gt;</style></svg>}

assert_equal(expected, Nokogiri::HTML5.fragment(input).to_html)
end

it "with math parent" do
input = %{<math><style>&lt;img src>}
expected = %{<math><style>&lt;img src&gt;</style></math>}

assert_equal(expected, Nokogiri::HTML5.fragment(input).to_html)
end
end
end if Nokogiri.uses_gumbo?
Loading