Skip to content

Commit

Permalink
Use relative links for serving internal assets
Browse files Browse the repository at this point in the history
  • Loading branch information
th0r committed Apr 11, 2019
1 parent 3ce1b8c commit ed99c32
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/viewer.js
Expand Up @@ -14,6 +14,7 @@ const Logger = require('./Logger');
const analyzer = require('./analyzer');

const projectRoot = path.resolve(__dirname, '..');
const assetsRoot = path.join(projectRoot, 'public');

module.exports = {
startServer,
Expand Down Expand Up @@ -169,7 +170,13 @@ async function generateReport(bundleStats, opts) {
}

function getAssetContent(filename) {
return fs.readFileSync(`${projectRoot}/public/${filename}`, 'utf8');
const assetPath = path.join(assetsRoot, filename);

if (!assetPath.startsWith(assetsRoot)) {
throw new Error(`"${filename}" is outside of the assets root`);
}

return fs.readFileSync(assetPath, 'utf8');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion views/script.ejs
Expand Up @@ -4,5 +4,5 @@
<%- escapeScript(assetContent(filename)) %>
</script>
<% } else { %>
<script src="/<%= filename %>"></script>
<script src="<%= filename %>"></script>
<% } %>

0 comments on commit ed99c32

Please sign in to comment.