Skip to content

Commit d0f39c5

Browse files
Apply suggestion from @coderabbitai[bot]
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 4f85f55 commit d0f39c5

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/security/csp-config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ export interface CSPConfig {
1515
* Generate a secure nonce for CSP
1616
*/
1717
export function generateNonce(): string {
18+
// Prefer Web Crypto (Edge/Browser)
19+
const webCrypto = (globalThis as any).crypto;
20+
if (webCrypto?.getRandomValues) {
21+
const arr = new Uint8Array(16);
22+
webCrypto.getRandomValues(arr);
23+
// Base64 encode without Buffer dependency
24+
let binary = '';
25+
for (let i = 0; i < arr.length; i++) binary += String.fromCharCode(arr[i]);
26+
// btoa is available in Edge/Browser
27+
// @ts-ignore
28+
return typeof btoa === 'function' ? btoa(binary) : Buffer.from(arr).toString('base64');
29+
}
30+
// Node.js fallback
1831
return crypto.randomBytes(16).toString('base64');
1932
}
2033

0 commit comments

Comments
 (0)