Skip to content

Commit e02abba

Browse files
fb55claude
andcommitted
fix(rewriting-stream): escape text outside raw text elements in emitText
`emitText` decided whether to skip escaping from the tokenizer's `lastStartTagName`, which end tags do not reset and which is already updated when the text before a start tag is flushed. Decoded text after `</script>`, before `<script>` or in an SVG `<style>` was written unescaped. `inRawText` is set from the tokenizer state when a start tag is emitted (the tokenizer has already switched for that element's content) and cleared by any end tag. The cases cover text around script and style elements and inside SVG. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 4bffbe6 commit e02abba

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

‎packages/parse5-html-rewriting-stream/lib/index.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { html, type Token } from 'parse5';
1+
import { TokenizerMode, type Token } from 'parse5';
22
import {
33
SAXParser,
44
type EndTag,
@@ -60,6 +60,9 @@ import { escapeText, escapeAttribute } from 'entities/escape';
6060
* ```
6161
*/
6262
export class RewritingStream extends SAXParser {
63+
/** Whether text is currently tokenized without decoding character references. */
64+
private inRawText = false;
65+
6366
/** Note: `sourceCodeLocationInfo` is always enabled. */
6467
constructor() {
6568
super({ sourceCodeLocationInfo: true });
@@ -82,10 +85,23 @@ export class RewritingStream extends SAXParser {
8285

8386
// Events
8487
protected override emitIfListenerExists(eventName: string, token: SaxToken): boolean {
88+
if (eventName === 'startTag') {
89+
// The tokenizer has already been switched to the state for this element's content.
90+
const { state } = this.tokenizer;
91+
this.inRawText =
92+
state === TokenizerMode.RAWTEXT ||
93+
state === TokenizerMode.SCRIPT_DATA ||
94+
state === TokenizerMode.PLAINTEXT;
95+
}
96+
8597
if (!super.emitIfListenerExists(eventName, token)) {
8698
this.emitRaw(this._getRawHtml(token.sourceCodeLocation!));
8799
}
88100

101+
if (eventName === 'endTag') {
102+
this.inRawText = false;
103+
}
104+
89105
// NOTE: don't skip new lines after `<pre>` and other tags,
90106
// otherwise we'll have incorrect raw data.
91107
this.parserFeedbackSimulator.skipNextNewLine = false;
@@ -136,12 +152,7 @@ export class RewritingStream extends SAXParser {
136152

137153
/** Emits a serialized text token into the output stream. */
138154
public emitText({ text }: Text): void {
139-
this.push(
140-
!this.parserFeedbackSimulator.inForeignContent &&
141-
html.hasUnescapedText(this.tokenizer.lastStartTagName, true)
142-
? text
143-
: escapeText(text),
144-
);
155+
this.push(this.inRawText ? text : escapeText(text));
145156
}
146157

147158
/** Emits a serialized comment token into the output stream. */

‎packages/parse5-html-rewriting-stream/test/rewriting-stream.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,4 +387,19 @@ describe('RewritingStream', () => {
387387
},
388388
}),
389389
);
390+
391+
it.each([
392+
'<div><script>a<b&&c</script>&lt;img src=x onerror=alert(1)&gt;</div>',
393+
'<style></style>&lt;img src=x onerror=alert(1)&gt;',
394+
'<p>&lt;img src=x onerror=alert(1)&gt;<script src="/app.js"></script></p>',
395+
'<svg><style>&lt;img src=x onerror=alert(1)&gt;</svg>',
396+
])('emits only the text of raw text elements without escaping: %s', (src) =>
397+
createRewriterTest({
398+
src,
399+
expected: src,
400+
assignTokenHandlers: (rewriter) => {
401+
rewriter.on('text', (token) => rewriter.emitText(token));
402+
},
403+
})(),
404+
);
390405
});

0 commit comments

Comments
 (0)