سامانه صدور فاکتور و حسابداری
build: create hardened gated Pages artifact
davoodmehraban89 committed
Oct 2, 2026 at 01:09 UTC
54715ffe88055f058526e10ee53991e02bf0efbd
1 file changed
+42
scripts/build-pages.mjs
new
+42
@@ -0,0 +1,42 @@
1
+import fs from 'node:fs';
2
+import path from 'node:path';
3
+
4
+const root=process.cwd();
5
+const out=path.join(root,'_site');
6
+fs.rmSync(out,{recursive:true,force:true});
7
+fs.mkdirSync(out,{recursive:true});
8
+
9
+for(const name of ['manifest.json','CNAME']){
10
+ const src=path.join(root,name);
11
+ if(fs.existsSync(src))fs.copyFileSync(src,path.join(out,name));
12
+}
13
+for(const dir of ['js','vendor'])fs.cpSync(path.join(root,dir),path.join(out,dir),{recursive:true});
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)=>{
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,'&')});
21
+ return ` data-finora-handler-${id}="${event}"`;
22
+});
23
+
24
+html=html.replace(
25
+ /<script\s+src="https:\/\/cdn\.jsdelivr\.net\/npm\/xlsx@0\.18\.5\/dist\/xlsx\.full\.min\.js"><\/script>/i,
26
+ '<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>'
27
+);
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,'');
30
+html=html.replace(/;\s*frame-ancestors 'none'/i,'');
31
+html=html.replace('</body>','<script src="js/generated-event-handlers.js"></script>\n</body>');
32
+
33
+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`;
34
+
35
+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');
37
+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');
39
+
40
+fs.writeFileSync(path.join(out,'index.html'),html);
41
+fs.writeFileSync(path.join(out,'js','generated-event-handlers.js'),generated);
42
+console.log(`Pages artifact built: ${handlers.length} inline handlers externalized; diag.html intentionally excluded.`);