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: missing leading space before user-supplied value #73

Merged
merged 3 commits into from
Mar 11, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions site/_headers
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*
Content-Security-Policy: img-src 'self' blob: data:; script-src 'sha256-/Cb4VxgL2aVP0MVDvbP0DgEOUv+MeNQmZX4yXHkn/c0='
12 changes: 11 additions & 1 deletion site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@ <h1>Using CSP Nonces on Script Tags</h1>
);
</script>
<script>
var CHECKS_COUNT = 5;
// test for https://github.com/netlify/plugin-csp-nonce/issues/72 case
document.write(
"<scr" +
"ipt>" +
"document.write('<p>✅ User-defined script-src value is preserved!</p>');" +
"</scr" +
"ipt>",
);
</script>
<script>
var CHECKS_COUNT = 6;
if (document.getElementsByTagName("p").length === CHECKS_COUNT) {
document.write("<p>✅ Test suite has succeeded</p>");
} else {
Expand Down
4 changes: 3 additions & 1 deletion src/__csp-nonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ const handler = async (request: Request, context: Context) => {
const d = directive.trim();
// intentionally add trailing space to avoid mangling `script-src-elem`
if (d.startsWith("script-src ")) {
return d.replace("script-src ", scriptSrc);
// append with trailing space to include any user-supplied values
// https://github.com/netlify/plugin-csp-nonce/issues/72
return d.replace("script-src ", `${scriptSrc} `).trim();
}
// intentionally omit report-uri: theirs should take precedence
return d;
Expand Down