سامانه صدور فاکتور و حسابداری

fix: preserve non-script CSP sources in Pages build

davoodmehraban89 committed Oct 2, 2026 at 01:09 UTC fba46de5c760b420a573c1fa884c889dc8bcfea8
1 file changed +9 -6
scripts/build-pages.mjs
+9 -6
@@ -14,10 +14,11 @@ for(const dir of ['js','vendor'])fs.cpSync(path.join(root,dir),path.join(out,dir
14
15 let html=fs.readFileSync(path.join(root,'index.html'),'utf8');
16 const handlers=[];
17 -html=html.replace(/\s(on(?:click|change|input|submit|blur|focus))="([^"]*)"/gi,(_,attr,body)=>{
17 +html=html.replace(/\s(on[a-z]+)="([^"]*)"/gi,(_,attr,body)=>{
18 const event=attr.slice(2).toLowerCase();
19 const id=handlers.length;
20 - handlers.push({id,event,body:body.replace(/"/g,'"').replace(/'/g,"'").replace(/&/g,'&')});
20 + const decoded=body.replace(/&quot;/g,'"').replace(/&#39;|&apos;/g,"'").replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&amp;/g,'&');
21 + handlers.push({id,event,body:decoded});
22 return ` data-finora-handler-${id}="${event}"`;
23 });
24
@@ -25,17 +26,19 @@ html=html.replace(
26 /<script\s+src="https:\/\/cdn\.jsdelivr\.net\/npm\/xlsx@0\.18\.5\/dist\/xlsx\.full\.min\.js"><\/script>/i,
27 '<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js" integrity="sha512-r22gChDnGvBylk90+2e/ycr3RVrDi8DIOkIGNhJlKfuyQM4tIRAI062MaV8sfjQKYVGjOBaZBOA87z+IhZE9DA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>'
28 );
28 -html=html.replace(/script-src 'self' 'unsafe-inline'/i,"script-src 'self' https://cdnjs.cloudflare.com");
29 -html=html.replace(/\shttps:\/\/cdn\.jsdelivr\.net(?=;)/g,'');
29 +html=html.replace(/script-src[^;]*/i,"script-src 'self' https://cdnjs.cloudflare.com");
30 +// frame-ancestors is ignored in meta-delivered CSP and generates a browser error;
31 +// clickjacking protection must be supplied as an HTTP response header by the edge/host.
32 html=html.replace(/;\s*frame-ancestors 'none'/i,'');
33 html=html.replace('</body>','<script src="js/generated-event-handlers.js"></script>\n</body>');
34
35 const generated=`// Generated at build time from legacy inline event attributes.\n(function(){\n${handlers.map(({id,event,body})=>` {\n const el=document.querySelector('[data-finora-handler-${id}]');\n if(el)el.addEventListener(${JSON.stringify(event)},function(event){\n const result=(function(event){${body}}).call(this,event);\n if(result===false){event.preventDefault();event.stopPropagation();}\n });\n }`).join('\n')}\n})();\n`;
36
37 if(/<script(?![^>]*\ssrc=)[^>]*>/i.test(html))throw new Error('Built page still contains inline script blocks');
36 -if(/\son(?:click|change|input|submit|blur|focus)=/i.test(html))throw new Error('Built page still contains inline event handlers');
38 +if(/\son[a-z]+=/i.test(html))throw new Error('Built page still contains inline event handlers');
39 if(/script-src[^;]*'unsafe-inline'/i.test(html))throw new Error('Built page still permits unsafe-inline scripts');
38 -if(/cdn\.(?:jsdelivr|cloudflare)\.com[^>]*><\/script>/i.test(html)&&!/<script[^>]+cdn\.(?:jsdelivr|cloudflare)\.com[^>]+integrity=/i.test(html))throw new Error('Built page contains CDN script without SRI');
40 +const externalScripts=[...html.matchAll(/<script[^>]+src="https:\/\/[^\"]+"[^>]*>/gi)].map(m=>m[0]);
41 +for(const tag of externalScripts)if(!/integrity=/i.test(tag))throw new Error('Built page contains external script without SRI: '+tag);
42
43 fs.writeFileSync(path.join(out,'index.html'),html);
44 fs.writeFileSync(path.join(out,'js','generated-event-handlers.js'),generated);