-
-
Notifications
You must be signed in to change notification settings - Fork 1
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
4a3e000
commit 8378376
Showing
4 changed files
with
101 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
coverage/ | ||
lib/ | ||
dist/ | ||
node_modules/ |
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,65 @@ | ||
import { | ||
getCSP, | ||
nonce, | ||
CSPDirectives, | ||
} from "csp-header"; | ||
import { type CSP, type CSPHeaderConfig } from "../types.js"; | ||
import { DEV_DEFAULT_CSP, PROD_DEFAULT_CSP } from "../defaults.js"; | ||
|
||
const cspNonceDirectives = [ | ||
"script-src", | ||
"style-src", | ||
"img-src", | ||
"font-src", | ||
"media-src", | ||
"object-src", | ||
"default-src", | ||
] as const; | ||
|
||
const DEFAULT_CSP: CSPHeaderConfig = { | ||
prod: { | ||
withNonce: true, | ||
value: PROD_DEFAULT_CSP, | ||
cspBlock: false, | ||
cspReportOnly: true, | ||
}, | ||
dev: { | ||
withNonce: true, | ||
value: DEV_DEFAULT_CSP, | ||
cspBlock: true, | ||
cspReportOnly: false, | ||
}, | ||
}; | ||
|
||
export const addNonceToDirectives = ( | ||
userDefinedCSP: CSP["value"], | ||
nonceString: string | ||
): CSP["value"] => { | ||
const csp: Partial<CSPDirectives> = { | ||
...DEFAULT_CSP.prod.value, | ||
...userDefinedCSP, | ||
}; | ||
|
||
cspNonceDirectives.forEach((directive) => { | ||
if (csp[directive] && Array.isArray(csp[directive])) { | ||
csp[directive].push(nonce(nonceString)); | ||
} | ||
}); | ||
|
||
return csp; | ||
}; | ||
|
||
export function generateCSP(cspOptions: CSP["value"], nonceString?: string) { | ||
const directives = nonceString ? addNonceToDirectives(cspOptions, nonceString) : cspOptions; | ||
|
||
if (Object.prototype.hasOwnProperty.call(directives,"report-uri")) { | ||
const reportUri = directives["report-uri"]; | ||
delete directives["report-uri"]; | ||
|
||
return getCSP({ directives, reportUri }) as string; | ||
} else { | ||
return getCSP({ | ||
directives, | ||
}) as string; | ||
} | ||
} |
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,15 @@ | ||
interface Params { | ||
includeSubDomains?: boolean; | ||
preload?: boolean; | ||
maxAge: number; | ||
} | ||
|
||
export function hsts({ | ||
includeSubDomains = true, | ||
preload = true, | ||
maxAge = 31536000 /* 1year */, | ||
}: Params) { | ||
return `max-age=${String(maxAge)};${includeSubDomains ? " includeSubDomains;" : ""}${ | ||
preload ? " preload" : "" | ||
}`; | ||
} |
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,20 @@ | ||
interface HardwarePermissions { | ||
camera?: string; | ||
microphone?: string; | ||
geolocation?: string; | ||
payment?: string; | ||
} | ||
|
||
export function permissionsPolicy(perms: HardwarePermissions) { | ||
const headerValue: string[] = []; | ||
|
||
for (const [key, value] of Object.entries(perms)) { | ||
if (typeof value === "string") { | ||
headerValue.push(`${key}=${value}`); | ||
} | ||
} | ||
|
||
// headerValue = ["camera=()", "microphone=()"] | ||
return headerValue.join(", "); | ||
// "camera=(), microfone=()" | ||
} |