Skip to content

Commit

Permalink
refactor(secure-headers): refine secureHeadersNonce init (#3535)
Browse files Browse the repository at this point in the history
  • Loading branch information
imcotton authored Oct 22, 2024
1 parent f8664b0 commit 15938b4
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/middleware/secure-headers/secure-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`
}

Expand Down

0 comments on commit 15938b4

Please sign in to comment.