Skip to content

Commit e5de03d

Browse files
authored
Fix HTML tokenizer failing to close CDATA blocks on extra brackets (#2548)
When reading characters that end a CDATA block, encountering 3 or more right-square brackets before the final closing brace caused the state machine to improperly reset its tracking counter back to 0. The counter is now properly capped at 2 to correctly preserve the "CDATA section end state" transition limits as defined in the WHATWG parser specification. b/536201494
1 parent edcac84 commit e5de03d

3 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎pkgs/html/CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## 0.15.7-wip
22

3+
- Fix the tokenizer failing to close CDATA blocks when encountering extra brackets
34
- Require Dart `3.6`
45
- Added new `textContent` method to `Node` class that returns the text content of
56
the node.

‎pkgs/html/lib/src/tokenizer.dart‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,8 +1888,10 @@ class HtmlTokenizer implements Iterator<Token> {
18881888
data.add(ch);
18891889
// TODO(jmesserly): it'd be nice if we had an easier way to match the end,
18901890
// perhaps with a "peek" API.
1891-
if (ch == ']' && matchedEnd < 2) {
1892-
matchedEnd++;
1891+
if (ch == ']') {
1892+
if (matchedEnd < 2) {
1893+
matchedEnd++;
1894+
}
18931895
} else if (ch == '>' && matchedEnd == 2) {
18941896
// Remove "]]>" from the end.
18951897
data.removeLast();

‎pkgs/html/test/parser_feature_test.dart‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ import 'package:test/test.dart';
1515

1616
void main() {
1717
_testElementSpans();
18+
19+
test('CDATA parser differential avoids XSS payload hiding (b/536201494)', () {
20+
final payload = '<svg><![CDATA[x]]]><img src=1 onerror=alert(1)>]]></svg>';
21+
final document = parseFragment(payload);
22+
23+
final images = document.querySelectorAll('img');
24+
25+
// Check that <img> wasn't incorrectly swallowed as part of the CDATA block.
26+
expect(images, isNotEmpty);
27+
expect(images.first.attributes.containsKey('onerror'), isTrue);
28+
});
29+
1830
test('doctype is cloneable', () {
1931
final doc = parse('<!doctype HTML>');
2032
final doctype = doc.nodes[0] as DocumentType;

0 commit comments

Comments
 (0)