Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression with escaping internal assets #264

Merged
merged 1 commit into from Apr 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/viewer.js
Expand Up @@ -52,11 +52,11 @@ async function startServer(bundleStats, opts) {
app.use('/', (req, res) => {
res.render('viewer', {
mode: 'server',
get chartData() { return JSON.stringify(chartData) },
defaultSizes: JSON.stringify(defaultSizes),
get chartData() { return chartData },
defaultSizes,
enableWebSocket: true,
// Helpers
escapeScript
escapeJson
});
});

Expand Down Expand Up @@ -133,12 +133,12 @@ async function generateReport(bundleStats, opts) {
`${projectRoot}/views/viewer.ejs`,
{
mode: 'static',
chartData: JSON.stringify(chartData),
defaultSizes: JSON.stringify(defaultSizes),
chartData,
defaultSizes,
enableWebSocket: false,
// Helpers
assetContent: getAssetContent,
escapeScript
escapeJson
},
(err, reportHtml) => {
try {
Expand Down Expand Up @@ -180,10 +180,10 @@ function getAssetContent(filename) {
}

/**
* Escapes `<` characters in the string to safely use it in `<script>` tag.
* Escapes `<` characters in JSON to safely use it in `<script>` tag.
*/
function escapeScript(value) {
return String(value).replace(/</gu, '\\u003c');
function escapeJson(json) {
return JSON.stringify(json).replace(/</gu, '\\u003c');
}

function getChartData(analyzerOpts, ...args) {
Expand Down
2 changes: 1 addition & 1 deletion views/script.ejs
@@ -1,7 +1,7 @@
<% if (mode === 'static') { %>
<!-- <%= filename %> -->
<script>
<%- escapeScript(assetContent(filename)) %>
<%- assetContent(filename) %>
</script>
<% } else { %>
<script src="<%= filename %>"></script>
Expand Down
6 changes: 3 additions & 3 deletions views/viewer.ejs
Expand Up @@ -11,9 +11,9 @@
<body>
<div id="app"></div>
<script>
window.chartData = <%- escapeScript(chartData) %>;
window.defaultSizes = <%- escapeScript(defaultSizes) %>;
window.enableWebSocket = <%- escapeScript(enableWebSocket) %>;
window.chartData = <%- escapeJson(chartData) %>;
window.defaultSizes = <%- escapeJson(defaultSizes) %>;
window.enableWebSocket = <%- escapeJson(enableWebSocket) %>;
</script>
</body>
</html>