Skip to content

Commit

Permalink
UW-31847 Switch to config driven nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
dgautsch committed Sep 7, 2023
1 parent d54d16a commit 41324f2
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,13 @@ function createRenderer(bundle, options, config) {
}, config.rendererOpts));
}

function addNonceToScriptTags(html, nonce) {
if (!nonce || !html) {
return html;
}
return html
.replace(/<script/g, `<script nonce="${nonce}"`)
.replace(/as="script">/g, `nonce="${nonce}" as="script">`);
}

function renderToString(config, context, res, cb) {
renderers[config.name].renderToString(context,
(err, html) => {
const nonceIncludedHTML = addNonceToScriptTags(html, res.locals.cspNonce);
if (err) {
config.errorHandler(err, res, cb);
} else {
res.send(nonceIncludedHTML);
res.send(html);
cb();
}
},
Expand Down Expand Up @@ -133,6 +123,11 @@ function render(config, clientManifest, req, res) {

res.setHeader('Content-Type', 'text/html');

if (res.locals.cspNonce) {
// eslint-disable-next-line no-param-reassign
config.nonce = res.locals.cspNonce;
}

// Render the appropriate Vue components into the renderer template
// using the server render logic in entry-server.js
const renderFn = config.stream ? renderToStream : renderToString;
Expand Down

0 comments on commit 41324f2

Please sign in to comment.