0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

refactor(secure-headers): refine secureHeadersNonce init (#3535)

This commit is contained in:
Cotton Hou 2024-10-22 09:31:58 +08:00 committed by GitHub
parent f8664b03a6
commit 15938b4896
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -132,13 +132,12 @@ const generateNonce = () => {
}
export const NONCE: ContentSecurityPolicyOptionHandler = (ctx) => {
const nonce =
ctx.get('secureHeadersNonce') ||
(() => {
const newNonce = generateNonce()
ctx.set('secureHeadersNonce', newNonce)
return newNonce
})()
const key = 'secureHeadersNonce'
const init = ctx.get(key)
const nonce = init || generateNonce()
if (init == null) {
ctx.set(key, nonce)
}
return `'nonce-${nonce}'`
}