Skip to content

Commit 3ce1b8c

Browse files
authored
Merge pull request #262 from webpack-contrib/proper-js-escape
Properly escape embedded JS/JSON
2 parents 81ec8b3 + 5385936 commit 3ce1b8c

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ _Note: Gaps between patch versions are faulty, broken or test releases._
1414

1515
<!-- Add changelog entries for new changes under this section -->
1616

17+
* **Improvements**
18+
* Properly escape embedded JS/JSON ([#262](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/262))
19+
1720
* **Bug Fix**
1821
* Fix showing help message on `-h` flag ([#260](https://github.com/webpack-contrib/webpack-bundle-analyzer/pull/260), fixes [#239](https://github.com/webpack-contrib/webpack-bundle-analyzer/issues/239))
1922

src/viewer.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ async function startServer(bundleStats, opts) {
5353
mode: 'server',
5454
get chartData() { return JSON.stringify(chartData) },
5555
defaultSizes: JSON.stringify(defaultSizes),
56-
enableWebSocket: true
56+
enableWebSocket: true,
57+
// Helpers
58+
escapeScript
5759
});
5860
});
5961

@@ -131,9 +133,11 @@ async function generateReport(bundleStats, opts) {
131133
{
132134
mode: 'static',
133135
chartData: JSON.stringify(chartData),
134-
assetContent: getAssetContent,
135136
defaultSizes: JSON.stringify(defaultSizes),
136-
enableWebSocket: false
137+
enableWebSocket: false,
138+
// Helpers
139+
assetContent: getAssetContent,
140+
escapeScript
137141
},
138142
(err, reportHtml) => {
139143
try {
@@ -168,6 +172,13 @@ function getAssetContent(filename) {
168172
return fs.readFileSync(`${projectRoot}/public/${filename}`, 'utf8');
169173
}
170174

175+
/**
176+
* Escapes `<` characters in the string to safely use it in `<script>` tag.
177+
*/
178+
function escapeScript(value) {
179+
return String(value).replace(/</gu, '\\u003c');
180+
}
181+
171182
function getChartData(analyzerOpts, ...args) {
172183
let chartData;
173184
const {logger} = analyzerOpts;

views/script.ejs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<% if (mode === 'static') { %>
22
<!-- <%= filename %> -->
33
<script>
4-
<%- assetContent(filename) %>
4+
<%- escapeScript(assetContent(filename)) %>
55
</script>
66
<% } else { %>
77
<script src="/<%= filename %>"></script>

views/viewer.ejs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
<body>
1212
<div id="app"></div>
1313
<script>
14-
window.chartData = <%- chartData %>;
15-
window.defaultSizes = <%- defaultSizes %>;
16-
window.enableWebSocket = <%- enableWebSocket %>;
14+
window.chartData = <%- escapeScript(chartData) %>;
15+
window.defaultSizes = <%- escapeScript(defaultSizes) %>;
16+
window.enableWebSocket = <%- escapeScript(enableWebSocket) %>;
1717
</script>
1818
</body>
1919
</html>

0 commit comments

Comments
 (0)