Skip to content

Commit

Permalink
fix: mangled script-src-elem directive (#63)
Browse files Browse the repository at this point in the history
* fix: malformed script-src-elem directive

* fix: don't need this condition actually

* fix: simplify
  • Loading branch information
Jason Barry authored Jan 16, 2024
1 parent dc6e2a6 commit 3c3f5fb
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/__csp-nonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ const handler = async (request: Request, context: Context) => {
.map((directive) => {
// prepend our rules for any existing directives
const d = directive.trim();
if (d.startsWith("script-src")) {
return d.replace("script-src", scriptSrc);
// intentionally add trailing space to avoid mangling `script-src-elem`
if (d.startsWith("script-src ")) {
return d.replace("script-src ", scriptSrc);
}
// intentionally omit report-uri: theirs should take precedence
return d;
})
.filter(Boolean);
// push our rules if the directives don't exist yet
if (!directives.find((d) => d.startsWith("script-src"))) {
if (!directives.find((d) => d.startsWith("script-src "))) {
directives.push(scriptSrc);
}
if (!directives.find((d) => d.startsWith("report-uri"))) {
Expand Down

0 comments on commit 3c3f5fb

Please sign in to comment.