File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed
Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,19 @@ export interface CSPConfig {
1515 * Generate a secure nonce for CSP
1616 */
1717export 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
You can’t perform that action at this time.
0 commit comments