We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 4f85f55 commit d0f39c5Copy full SHA for d0f39c5
1 file changed
lib/security/csp-config.ts
@@ -15,6 +15,19 @@ export interface CSPConfig {
15
* Generate a secure nonce for CSP
16
*/
17
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
31
return crypto.randomBytes(16).toString('base64');
32
}
33
0 commit comments