Skip to content

Commit

Permalink
fix: missing leading space before user-supplied value (#73)
Browse files Browse the repository at this point in the history
* fix: missing leading space before user-supplied value

* fix: _headers syntax

* fix: use base64 for _headers test
  • Loading branch information
Jason Barry authored Mar 11, 2024
1 parent dda7286 commit f30f961
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
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

0 comments on commit f30f961

Please sign in to comment.