سامانه صدور فاکتور و حسابداری
fix: support CSP-safe handler call sequences
davoodmehraban89 committed
Oct 2, 2026 at 01:21 UTC
8de5e2a384ed4736e369b68ba0b53bc9e871d7ea
1 file changed
+3
-2
scripts/build-pages.mjs
+3
-2
@@ -14,6 +14,7 @@ for(const dir of ['js','vendor'])fs.cpSync(path.join(root,dir),path.join(out,dir
14
15
const simpleCall=/^\s*(?:return\s+)?(?:window\.)?[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\s*\([^;]*\)\s*;?\s*$/;
16
const builderExpression=/^'\+[A-Za-z_$][\w$]*\+'$/;
17
+const simpleSequence=text=>text.split(';').map(x=>x.trim()).filter(Boolean).every(x=>simpleCall.test(x));
18
let dynamicHandlers=0;
19
for(const name of fs.readdirSync(path.join(out,'js'))){
20
if(!name.endsWith('.js'))continue;
@@ -22,7 +23,7 @@ for(const name of fs.readdirSync(path.join(out,'js'))){
23
source=source.replace(/\s(on[a-z]+)="([^"]*)"/gi,(_,attr,body)=>{
24
const event=attr.slice(2).toLowerCase();
25
const decoded=body.replace(/"/g,'"').replace(/'|'/g,"'").replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
25
- if(!simpleCall.test(decoded)&&!builderExpression.test(decoded))throw new Error(`Unsupported dynamic inline handler in ${name}: ${decoded}`);
26
+ if(!simpleSequence(decoded)&&!builderExpression.test(decoded))throw new Error(`Unsupported dynamic inline handler in ${name}: ${decoded}`);
27
dynamicHandlers++;
28
return ` data-finora-dynamic-${event}="${body}"`;
29
});
@@ -55,7 +56,7 @@ html=html.replace('</body>','<script src="js/generated-event-handlers.js"></scri
56
57
const generated=`// Generated at build time from legacy static 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`;
58
58
-const dynamic=`// CSP-safe dispatcher for simple handlers emitted by runtime renderers.\n(function(){\n const events=['click','change','input','submit','blur','focus','keydown','keyup'];\n function splitArgs(text){\n const out=[];let cur='',quote='',escape=false;\n for(const ch of text){\n if(escape){cur+=ch;escape=false;continue;}\n if(ch==='\\\\'){cur+=ch;escape=true;continue;}\n if(quote){cur+=ch;if(ch===quote)quote='';continue;}\n if(ch==='\\"'||ch==="'"){quote=ch;cur+=ch;continue;}\n if(ch===','){out.push(cur.trim());cur='';continue;}\n cur+=ch;\n }\n if(cur.trim()||text.trim())out.push(cur.trim());\n return out;\n }\n function arg(token,el,event){\n if(token==='')return undefined;\n if((token[0]==="'"&&token.at(-1)==="'")||(token[0]==='\\"'&&token.at(-1)==='\\"'))return token.slice(1,-1).replace(/\\\\(['\\"\\\\])/g,'$1');\n if(token==='this')return el;if(token==='event')return event;if(token==='this.value')return el.value;if(token==='this.checked')return el.checked;if(token==='event.target.value')return event.target?.value;\n if(token==='true')return true;if(token==='false')return false;if(token==='null')return null;if(token==='undefined')return undefined;\n if(/^-?\\d+(?:\\.\\d+)?$/.test(token))return Number(token);\n throw new Error('Unsupported CSP-safe handler argument: '+token);\n }\n function invoke(expression,el,event){\n const text=String(expression||'').trim().replace(/^return\\s+/,'').replace(/;$/,'');\n const m=text.match(/^((?:window\\.)?[A-Za-z_$][\\w$]*(?:\\.[A-Za-z_$][\\w$]*)*)\\s*\\((.*)\\)$/);\n if(!m)throw new Error('Unsupported CSP-safe handler: '+text);\n const parts=m[1].replace(/^window\\./,'').split('.');let owner=window;\n for(let i=0;i<parts.length-1;i++)owner=owner?.[parts[i]];\n const fn=owner?.[parts.at(-1)];if(typeof fn!=='function')throw new Error('Missing CSP-safe handler function: '+m[1]);\n return fn.apply(el,splitArgs(m[2]).map(x=>arg(x,el,event)));\n }\n for(const type of events){\n document.addEventListener(type,function(event){\n const el=event.target?.closest?.('[data-finora-dynamic-'+type+']');if(!el)return;\n const result=invoke(el.getAttribute('data-finora-dynamic-'+type),el,event);\n if(result===false){event.preventDefault();event.stopPropagation();}\n },type==='blur'||type==='focus');\n }\n})();\n`;
59
+const dynamic=`// CSP-safe dispatcher for simple handlers emitted by runtime renderers.\n(function(){\n const events=['click','change','input','submit','blur','focus','keydown','keyup'];\n function split(text,separator){\n const out=[];let cur='',quote='',escape=false;\n for(const ch of text){\n if(escape){cur+=ch;escape=false;continue;}\n if(ch==='\\\\'){cur+=ch;escape=true;continue;}\n if(quote){cur+=ch;if(ch===quote)quote='';continue;}\n if(ch==='\\"'||ch==="'"){quote=ch;cur+=ch;continue;}\n if(ch===separator){if(cur.trim())out.push(cur.trim());cur='';continue;}\n cur+=ch;\n }\n if(cur.trim())out.push(cur.trim());\n return out;\n }\n function arg(token,el,event){\n if(token==='')return undefined;\n if((token[0]==="'"&&token.at(-1)==="'")||(token[0]==='\\"'&&token.at(-1)==='\\"'))return token.slice(1,-1).replace(/\\\\(['\\"\\\\])/g,'$1');\n if(token==='this')return el;if(token==='event')return event;if(token==='this.value')return el.value;if(token==='this.checked')return el.checked;if(token==='event.target.value')return event.target?.value;\n if(token==='true')return true;if(token==='false')return false;if(token==='null')return null;if(token==='undefined')return undefined;\n if(/^-?\\d+(?:\\.\\d+)?$/.test(token))return Number(token);\n throw new Error('Unsupported CSP-safe handler argument: '+token);\n }\n function invokeOne(statement,el,event){\n const text=String(statement||'').trim().replace(/^return\\s+/,'');\n const m=text.match(/^((?:window\\.)?[A-Za-z_$][\\w$]*(?:\\.[A-Za-z_$][\\w$]*)*)\\s*\\((.*)\\)$/);\n if(!m)throw new Error('Unsupported CSP-safe handler: '+text);\n const parts=m[1].replace(/^window\\./,'').split('.');let owner=window;\n for(let i=0;i<parts.length-1;i++)owner=owner?.[parts[i]];\n const fn=owner?.[parts.at(-1)];if(typeof fn!=='function')throw new Error('Missing CSP-safe handler function: '+m[1]);\n return fn.apply(el,split(m[2],',').map(x=>arg(x,el,event)));\n }\n function invoke(expression,el,event){let result;for(const statement of split(String(expression||''),';'))result=invokeOne(statement,el,event);return result;}\n for(const type of events){\n document.addEventListener(type,function(event){\n const el=event.target?.closest?.('[data-finora-dynamic-'+type+']');if(!el)return;\n const result=invoke(el.getAttribute('data-finora-dynamic-'+type),el,event);\n if(result===false){event.preventDefault();event.stopPropagation();}\n },type==='blur'||type==='focus');\n }\n})();\n`;
60
61
if(/<script(?![^>]*\ssrc=)[^>]*>/i.test(html))throw new Error('Built page still contains inline script blocks');
62
if(/\son[a-z]+=/i.test(html))throw new Error('Built page still contains inline event handlers');