Skip to content

Commit e1ab6f2

Browse files
authored
fix(html): escape a text run the tag after it would fuse with (#22086)
* fix(html): escape a text run the tag after it would fuse with A text node ending in `</` was emitted as its source bytes, so the end tag printed after it met that `</` as a bogus comment — which runs to the next `>` and takes the text with it. The tail test named only a trailing `<`; a `</` fuses one character earlier. Reachable through htmlMinify, `<iframe srcdoc>` included, where escaped input came back corrupted. Costs one charCodeAt, only where the tail is `/`. * chore(html): fold the second @import of ../Chunk into the first `lint:code` fails on `webpack/no-duplicate-import-tag` for it, which stops the `&&` chain before every later stage runs.
1 parent 0c8d9d9 commit e1ab6f2

8 files changed

Lines changed: 112 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Escape a text run ending in `</`, which the tag after it made a comment of.

‎lib/html/syntax.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9044,6 +9044,18 @@ const _hasOpenReference = (s) => {
90449044
return true;
90459045
};
90469046

9047+
/**
9048+
* Whether the tag printed after this text would fuse with its tail: a `<` runs
9049+
* into the next tag's name, and a `</` meets that tag's `<` as a bogus comment,
9050+
* which consumes to the next `>` and takes the text with it.
9051+
* @param {string} s source text
9052+
* @returns {boolean} true when the text cannot be emitted as written
9053+
*/
9054+
const _tailFusesWithNextTag = (s) => {
9055+
const tail = s.charCodeAt(s.length - 1);
9056+
return tail === 60 || (tail === 47 && s.charCodeAt(s.length - 2) === 60);
9057+
};
9058+
90479059
/**
90489060
* Whether the `&` at `i` would be consumed as a character reference when the
90499061
* text is read back (§13.2.5.72): only before `#` or a name the table holds.
@@ -12437,7 +12449,7 @@ const printer = (path, writer) => {
1243712449
// without changing length — so it is excluded by name.
1243812450
if (
1243912451
raw.length === data.length &&
12440-
raw.charCodeAt(raw.length - 1) !== 60 &&
12452+
!_tailFusesWithNextTag(raw) &&
1244112453
!(inputHasCr && raw.includes("\r")) &&
1244212454
!_hasOpenReference(raw)
1244312455
) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases html minimize-text-ending-in-tag-open minimize-text-ending-in-tag-open should compile 1`] = `
4+
"<!doctype html><head><title>text ending in tag open</title></head><body>
5+
<iframe srcdoc=\\"<div>a&amp;lt;/</div>\\"></iframe>
6+
<div>b&lt;/</div></body></html>"
7+
`;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases html minimize-text-ending-in-tag-open minimize-text-ending-in-tag-open should compile 1`] = `
4+
"<!doctype html><head><title>text ending in tag open</title></head><body>
5+
<iframe srcdoc=\\"<div>a&amp;lt;/</div>\\"></iframe>
6+
<div>b&lt;/</div></body></html>"
7+
`;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "./page.html";
2+
3+
// A text run ending in `</` fuses with the end tag printed after it into a
4+
// bogus comment, which drops the text from the document.
5+
it("should keep text a following end tag would fuse with", () => {
6+
expect(true).toBe(true);
7+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head><title>text ending in tag open</title></head>
4+
<body>
5+
<iframe srcdoc="<div>a&lt;/"></iframe>
6+
<div>b</
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
module.exports = {
7+
findBundle(_i, options) {
8+
const files = fs.readdirSync(options.output.path);
9+
return files.includes("main.js") ? ["./main.js"] : undefined;
10+
},
11+
afterExecute(options) {
12+
const html = fs.readFileSync(
13+
path.join(options.output.path, "page.html"),
14+
"utf8"
15+
);
16+
expect(html).toMatchSnapshot();
17+
18+
// `</` against the `<` of the tag after it opens a bogus comment, which
19+
// runs to the next `>` and takes the text with it.
20+
expect(html).not.toContain("</<");
21+
expect(html).toContain("<div>b&lt;/</div>");
22+
// A level further in: the attribute escapes the `&` the inner document
23+
// wrote, so the iframe decodes `&lt;` and parses the text back.
24+
expect(html).toContain('srcdoc="<div>a&amp;lt;/</div>"');
25+
}
26+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
3+
const MinimizerPlugin = require("minimizer-webpack-plugin");
4+
const webpack = require("../../../../");
5+
6+
const { htmlMinify } = webpack.html.syntax;
7+
8+
/** @type {import("../../../../").Configuration} */
9+
module.exports = {
10+
target: "web",
11+
mode: "production",
12+
output: {
13+
filename: "[name].js",
14+
pathinfo: false
15+
},
16+
module: {
17+
generator: {
18+
html: {
19+
extract: true
20+
}
21+
}
22+
},
23+
optimization: {
24+
minimize: true,
25+
minimizer: [
26+
{
27+
apply: (compiler) => {
28+
new MinimizerPlugin({
29+
test: /\.(?:[cm]?js|html)(\?.*)?$/i,
30+
parallel: false,
31+
minify: [MinimizerPlugin.terserMinify, htmlMinify],
32+
minimizerOptions: [{}, {}]
33+
}).apply(/** @type {EXPECTED_ANY} */ (compiler));
34+
}
35+
}
36+
]
37+
},
38+
experiments: {
39+
html: true
40+
}
41+
};

0 commit comments

Comments
 (0)