Skip to content

Commit 5b64741

Browse files
fix(html): handle NUL characters in CDATA sections
1 parent 351217f commit 5b64741

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

‎lib/html/syntax.js‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2872,13 +2872,22 @@ const tokenize = (input, pos = 0, callbacks = {}) => {
28722872
if (cc === CC_RIGHT_SQUARE_BRACKET) {
28732873
state = STATE_CDATA_SECTION_BRACKET;
28742874
pos++;
2875+
} else if (cc === CC_NULL) {
2876+
// U+0000 NULL is an unexpected-null-character parse error.
2877+
reportError("unexpected-null-character", pos, pos + 1, "warning");
2878+
pos++;
28752879
} else {
28762880
// Anything else
28772881
// Emit the current input character as a character token.
2878-
// Fast-forward to the next `]` (the only code point with its own
2879-
// arc) in one native scan.
2880-
pos = input.indexOf("]", pos + 1);
2881-
if (pos === -1) pos = len;
2882+
// Fast-forward to the next `]` or NUL, the code points with their
2883+
// own arcs.
2884+
pos++;
2885+
const nextBracket = input.indexOf("]", pos);
2886+
if (nextNul < pos) {
2887+
nextNul = input.indexOf("\0", pos);
2888+
if (nextNul === -1) nextNul = len;
2889+
}
2890+
pos = Math.min(nextBracket === -1 ? len : nextBracket, nextNul);
28822891
}
28832892
break;
28842893

@@ -8496,7 +8505,7 @@ const handleCommentToken = (input, start, end, dataStart, dataEnd) => {
84968505
const data = input.slice(dataStart, dataEnd).replace(/\r\n?/g, "\n");
84978506
if (data !== "") {
84988507
tok.type = TOKEN_CHAR;
8499-
tok.data = data;
8508+
tok.data = inputHasNul ? data.replace(/\0/g, "�") : data;
85008509
tok.start = start;
85018510
tok.end = end;
85028511
dispatch();

‎test/unitCases/HtmlSyntax.unittest.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8841,6 +8841,24 @@ describe("tokenize — content modes, CDATA and NUL arcs", () => {
88418841
{ code: "eof-in-cdata", slice: "", severity: "error" }
88428842
]);
88438843
});
8844+
8845+
it("reports and replaces NUL characters", () => {
8846+
const source = `<svg><![CDATA[a${NUL}b${NUL}c]]></svg>`;
8847+
expect(errorsOf(source, foreign)).toEqual([
8848+
{
8849+
code: "unexpected-null-character",
8850+
slice: NUL,
8851+
severity: "warning"
8852+
},
8853+
{
8854+
code: "unexpected-null-character",
8855+
slice: NUL,
8856+
severity: "warning"
8857+
}
8858+
]);
8859+
const svg = body(source)[0];
8860+
expect(/** @type {MatText} */ (svg.children[0]).data).toBe("a�b�c");
8861+
});
88448862
});
88458863

88468864
it("reports unexpected-null-character from each state that consumes one", () => {

0 commit comments

Comments
 (0)