99
1010const fs = require ( 'fs' ) ;
1111const path = require ( 'path' ) ;
12- const crypto = require ( 'crypto ' ) ;
12+ const { getOrCreateNonce } = require ( './nonce-generator.cjs ' ) ;
1313
1414const distDir = path . join ( __dirname , '..' , 'dist' ) ;
1515const indexPath = path . join ( distDir , 'index.html' ) ;
@@ -18,8 +18,8 @@ const assetsDir = path.join(distDir, 'assets');
1818console . log ( '🚀 Starting Advanced Performance Optimization...' ) ;
1919
2020try {
21- // Generate unique nonce for CSP
22- const nonce = crypto . randomBytes ( 16 ) . toString ( 'base64' ) ;
21+ // Use shared nonce generator
22+ const nonce = getOrCreateNonce ( ) ;
2323
2424 // Read the index.html file
2525 let html = fs . readFileSync ( indexPath , 'utf8' ) ;
@@ -248,16 +248,16 @@ try {
248248 // 4. ENHANCED CSP WITH NONCE - Replace existing CSP if present
249249 const cspMeta = `<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'nonce-${ nonce } ' https://script.google.com https://script.googleusercontent.com; style-src 'self' 'nonce-${ nonce } ' 'unsafe-inline' https://fonts.googleapis.com; font-src 'self' https://fonts.gstatic.com; img-src 'self' data: https:; connect-src 'self' https://script.google.com https://script.googleusercontent.com; frame-src 'self' https://script.google.com; object-src 'none'; base-uri 'self'; form-action 'self';">` ;
250250
251- // Remove any existing CSP headers to prevent conflicts
252- html = html . replace ( / < m e t a [ ^ > ] * C o n t e n t - S e c u r i t y - P o l i c y [ ^ > ] * > / gi, '' ) ;
251+ // Remove any existing CSP headers to prevent conflicts - more comprehensive patterns
252+ html = html . replace ( / < m e t a [ ^ > ] * h t t p - e q u i v = [ " ' ] ? C o n t e n t - S e c u r i t y - P o l i c y [ " ' ] ? [ ^ > ] * > / gi, '' ) ;
253+ html = html . replace ( / < m e t a [ ^ > ] * c o n t e n t = [ " ' ] [ ^ " ' ] * C o n t e n t - S e c u r i t y - P o l i c y [ ^ " ' ] * [ " ' ] [ ^ > ] * > / gi, '' ) ;
253254
254255 // 5. ADDITIONAL SECURITY AND PERFORMANCE HEADERS
255256 const securityMeta = `
256257 <meta http-equiv="X-Content-Type-Options" content="nosniff">
257- <meta http-equiv="X-Frame-Options" content="SAMEORIGIN">
258258 <meta http-equiv="X-XSS-Protection" content="1; mode=block">
259259 <meta http-equiv="Referrer-Policy" content="strict-origin-when-cross-origin">
260- <meta http-equiv="Permissions-Policy" content="camera=(), microphone=(), geolocation=(), payment=()">
260+ <meta http-equiv="Permissions-Policy" content="camera=(), microphone=(), geolocation=(), payment=(), fullscreen=(self) ">
261261 ` ;
262262
263263 // 6. SERVICE WORKER REGISTRATION
@@ -278,6 +278,9 @@ try {
278278 // Remove existing critical CSS and replace with optimized version
279279 html = html . replace ( / < s t y l e [ ^ > ] * > [ \s \S ] * ?< \/ s t y l e > / g, '' ) ;
280280
281+ // Replace nonce placeholders with actual nonce
282+ html = html . replace ( / _ _ C S P _ N O N C E _ _ / g, nonce ) ;
283+
281284 // Find the head tag and insert optimized content
282285 const headEndIndex = html . indexOf ( '</head>' ) ;
283286 if ( headEndIndex !== - 1 ) {
0 commit comments