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

feat: add CSP with nonce support #2478

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/server/HeadHtmlSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export default class HeadHtmlSupport {
const update = (obj, keys) => {
keys.sort();
for (const k of keys) {
if (k === 'nonce') {
// ignore nonce attribute, because it can change on every request
// eslint-disable-next-line no-continue
continue;
}

let v = obj[k];
if (v !== undefined) {
if (Array.isArray(v)) {
Expand Down
8 changes: 5 additions & 3 deletions src/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,21 @@ const utils = {
if (match) {
const { index } = match;
// eslint-disable-next-line no-param-reassign
const nonceMatch = body.match(/nonce="([a-zA-Z0-9+/=]+)"/);
const nonce = nonceMatch ? ` nonce="${nonceMatch[1]}"` : '';
let newbody = body.substring(0, index);
if (process.env.CODESPACES === 'true') {
newbody += `<script>
newbody += `<script${nonce}>
window.LiveReloadOptions = {
host: new URL(location.href).hostname.replace(/-[0-9]+\\.preview\\.app\\.github\\.dev/, '-35729.preview.app.github.dev'),
port: 443,
https: true,
};
</script>`;
} else {
newbody += `<script>window.LiveReloadOptions={port:${server.port},host:location.hostname,https:${server.scheme === 'https'}};</script>`;
newbody += `<script${nonce}>window.LiveReloadOptions={port:${server.port},host:location.hostname,https:${server.scheme === 'https'}};</script>`;
}
newbody += '<script src="/__internal__/livereload.js"></script>';
newbody += `<script${nonce} src="/__internal__/livereload.js"></script>`;
newbody += body.substring(index);
return newbody;
}
Expand Down
Loading