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

fix: preserve return-false semantics in CSP dispatcher

davoodmehraban89 committed Oct 2, 2026 at 01:23 UTC a42c4fd08dcd9390de11ef468fda5a99e7448db5
1 file changed +2 -2
scripts/build-pages.mjs
+2 -2
@@ -15,7 +15,7 @@ for(const dir of ['js','vendor'])fs.cpSync(path.join(root,dir),path.join(out,dir
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 assignmentBuilder=/^[A-Za-z_$][\w$]*\['\+[A-Za-z_$][\w$]*\+'\]\.[A-Za-z_$][\w$]*=this\.(?:value|checked)$/;
18 -const simpleSequence=text=>text.split(';').map(x=>x.trim()).filter(Boolean).every(x=>simpleCall.test(x));
18 +const simpleSequence=text=>text.split(';').map(x=>x.trim()).filter(Boolean).every(x=>simpleCall.test(x)||/^return\s+(?:false|true)$/.test(x));
19 let dynamicHandlers=0;
20 for(const name of fs.readdirSync(path.join(out,'js'))){
21 if(!name.endsWith('.js'))continue;
@@ -57,7 +57,7 @@ html=html.replace('</body>','<script src="js/generated-event-handlers.js"></scri
57
58 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`;
59
60 -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 assign=text.match(/^([A-Za-z_$][\\w$]*)\\[(\\d+)\\]\\.([A-Za-z_$][\\w$]*)=(this\\.(?:value|checked))$/);\n if(assign){const target=window[assign[1]]?.[Number(assign[2])];if(!target)throw new Error('Missing CSP-safe assignment target: '+assign[1]+'['+assign[2]+']');target[assign[3]]=assign[4]==='this.checked'?el.checked:el.value;return target[assign[3]];}\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 +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 raw=String(statement||'').trim();if(raw==='return false')return false;if(raw==='return true')return true;\n const text=raw.replace(/^return\\s+/,'');\n const assign=text.match(/^([A-Za-z_$][\\w$]*)\\[(\\d+)\\]\\.([A-Za-z_$][\\w$]*)=(this\\.(?:value|checked))$/);\n if(assign){const target=window[assign[1]]?.[Number(assign[2])];if(!target)throw new Error('Missing CSP-safe assignment target: '+assign[1]+'['+assign[2]+']');target[assign[3]]=assign[4]==='this.checked'?el.checked:el.value;return target[assign[3]];}\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);if(result===false)break;}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`;
61
62 if(/<script(?![^>]*\ssrc=)[^>]*>/i.test(html))throw new Error('Built page still contains inline script blocks');
63 if(/\son[a-z]+=/i.test(html))throw new Error('Built page still contains inline event handlers');