-
-
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.
refactor: rearrange some dependencies, preparing for adapter(s)
- Loading branch information
1 parent
edf676c
commit 17140ce
Showing
3 changed files
with
101 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,76 @@ | ||
import { | ||
getCSP, | ||
nonce, | ||
CSPDirectives, | ||
} from "csp-header"; | ||
import { type CSP, type CSPHeaderConfig } from "../types.js"; | ||
import { getCSP, nonce, CSPDirectives } from "csp-header"; | ||
import { SecHeaders, 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", | ||
"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, | ||
}, | ||
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 | ||
userDefinedCSP: CSP["value"], | ||
nonceString: string, | ||
): CSP["value"] => { | ||
const csp: Partial<CSPDirectives> = { | ||
...DEFAULT_CSP.prod.value, | ||
...userDefinedCSP, | ||
}; | ||
const csp: Partial<CSPDirectives> = { | ||
...DEFAULT_CSP.prod.value, | ||
...userDefinedCSP, | ||
}; | ||
|
||
cspNonceDirectives.forEach((directive) => { | ||
if (csp[directive] && Array.isArray(csp[directive])) { | ||
csp[directive].push(nonce(nonceString)); | ||
} | ||
}); | ||
cspNonceDirectives.forEach((directive) => { | ||
if (csp[directive] && Array.isArray(csp[directive])) { | ||
csp[directive].push(nonce(nonceString)); | ||
} | ||
}); | ||
|
||
return csp; | ||
return csp; | ||
}; | ||
|
||
export function generateCSP(cspOptions: CSP["value"], nonceString?: string) { | ||
const isDev = process.env.NODE_ENV === "development"; | ||
const directives = nonceString && !isDev ? addNonceToDirectives(cspOptions, nonceString) : cspOptions; | ||
const isDev = process.env.NODE_ENV === "development"; | ||
const directives = | ||
nonceString && !isDev | ||
? addNonceToDirectives(cspOptions, nonceString) | ||
: cspOptions; | ||
|
||
if (Object.prototype.hasOwnProperty.call(directives,"report-uri")) { | ||
const reportUri = directives["report-uri"]; | ||
delete directives["report-uri"]; | ||
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; | ||
} | ||
return getCSP({ directives, reportUri }) as string; | ||
} else { | ||
return getCSP({ | ||
directives, | ||
}) as string; | ||
} | ||
} | ||
|
||
export const chooseCSP = (settings: SecHeaders) => { | ||
if (!settings.csp) { | ||
return; | ||
} | ||
if (process.env.NODE_ENV === "development") { | ||
return settings.csp.dev || settings.csp.prod; | ||
} else { | ||
return settings.csp.prod; | ||
} | ||
}; |
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