-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75def2e
commit 591e584
Showing
6 changed files
with
107 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
type CSPDirective = | ||
| ( | ||
| "none" | ||
| "self" | ||
| "unsafe-inline" | ||
| "unsafe-hashes" | ||
| "unsafe-eval" | ||
| "strict-dynamic" | ||
| "blob:" | ||
| "https:" | ||
| "data:" | ||
| "mediastream:" | ||
| "nonce-" | ||
| "sha256-" | ||
)[] | ||
| string[] | ||
type CSPDirectiveWithWasm = CSPDirective | "wasm-unsafe-eval"[] | ||
|
||
type CSPPolicies = Partial<{ | ||
"script-src": CSPDirectiveWithWasm | ||
"connect-src": CSPDirectiveWithWasm | ||
"default-src": CSPDirective | ||
"style-src": CSPDirective | ||
"font-src": CSPDirective | ||
"frame-src": CSPDirective | ||
"frame-ancestors": "none"[] | string[] | ||
"img-src": CSPDirective | ||
"worker-src": CSPDirective | ||
"upgrade-insecure-requests": true | ||
}> | ||
|
||
export function generateCSP(policy: CSPPolicies): string { | ||
let _directives = [] | ||
|
||
const joinSpaces = (s: string[]) => s.join(" ") | ||
|
||
Object.entries(policy).forEach(([directive, values]) => { | ||
if (directive !== "upgrade-insecure-requests") { | ||
const parsedValues = joinSpaces( | ||
(values as string[]).map((value) => { | ||
if (value === "self") return `'self'` | ||
if (value === "none") return `'none'` | ||
if (value === "unsafe-inline") return `'unsafe-inline'` | ||
if (value === "unsafe-hashes") return `'unsafe-hashes'` | ||
if (value === "unsafe-eval") return `'unsafe-eval'` | ||
if (value.startsWith("nonce-") || value.startsWith("sha")) | ||
return `'${value}'` | ||
return value | ||
}) | ||
) | ||
|
||
_directives.push(`${directive} ${parsedValues};`) | ||
} | ||
if (directive == "upgrade-insecure-requests") { | ||
_directives.push(`upgrade-insecure-requests;`) | ||
} | ||
}) | ||
|
||
return joinSpaces(_directives) | ||
.replace(/(\s;\s)/g, "; ") | ||
.replace(/\s;/g, ";") | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { generateCSPString } from "./generateCSPString" | ||
import { generateCSP } from "./generateCSP" | ||
|
||
export { generateCSPString } | ||
export { generateCSP } |