diff --git a/src/modules/mail/parser.cjs b/src/modules/mail/parser.cjs index 7ad415c..30791ad 100644 --- a/src/modules/mail/parser.cjs +++ b/src/modules/mail/parser.cjs @@ -5,10 +5,18 @@ const fail=message=>{throw Object.assign(new Error(message),{status:400});}; const escape=value=>String(value??'').replace(/[&<>"']/g,c=>({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c])); const VARIABLES=['产品线','订单号','日期','订单列表','订单明细','存放地址','类别名称']; function validateVariables(...parts){for(const part of parts)for(const token of String(part||'').matchAll(/{{([\s\S]*?)}}/g))if(!VARIABLES.includes(token[1].trim()))fail('模板含有不支持的变量,请使用:'+VARIABLES.map(x=>'{{'+x+'}}').join('、'));} +function safeLink(value){ + if(typeof value!=='string'||!/^https?:\/\/[^/?#]/i.test(value)||/[\s\p{Cc}\p{Cf}\\]/u.test(value))return false; + try{ + if(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}\\]/u.test(decodeURIComponent(value)))return false; + const url=new URL(value),authority=value.slice(value.indexOf('://')+3).split(/[/?#]/,1)[0]; + return ['http:','https:'].includes(url.protocol)&&!!url.hostname&&!url.username&&!url.password&&!authority.includes('@'); + }catch{return false;} +} function cleanHtml(html,attachments=[]){ let expandedBytes=Buffer.byteLength(html||''); const inline=new Map(attachments.filter(a=>a.cid && /^image\/(png|jpeg|gif|webp)$/.test(a.contentType)).map(a=>[a.cid,'data:'+a.contentType+';base64,'+a.content])); - return sanitize(html||'',{allowedTags:['p','br','div','span','b','strong','i','em','u','s','ul','ol','li','blockquote','pre','code','h1','h2','h3','h4','table','thead','tbody','tfoot','tr','th','td','hr','img'],allowedAttributes:{'*':['style'],td:['colspan','rowspan','style'],th:['colspan','rowspan','style'],img:['src','alt','width','height']},allowedStyles:{'*':{'color':[/^#[0-9a-f]{3,8}$/i,/^[a-z]+$/i],'background-color':[/^#[0-9a-f]{3,8}$/i,/^[a-z]+$/i],'text-align':[/^(left|right|center)$/],'font-weight':[/^(bold|normal|[1-9]00)$/],'font-size':[/^\d{1,2}(px|pt)$/],'border':[/^\dpx solid #[0-9a-f]{3,6}$/i],'padding':[/^\d{1,2}px$/]}},allowedSchemes:['data'],allowedSchemesByTag:{img:['data']},transformTags:{img:(tag,attrs)=>{const src=attrs.src?.startsWith('cid:')?(inline.get(attrs.src.slice(4))||''):attrs.src||'';expandedBytes+=src.length;if(expandedBytes>12*1024*1024)fail('内嵌图片展开超过 12 MB,请减少重复图片后重新发送。');return {tagName:'img',attribs:{...attrs,src}};}},exclusiveFilter:frame=>frame.tag==='img'&&!/^data:image\/(png|jpeg|gif|webp);base64,[A-Za-z0-9+/=]+$/.test(frame.attribs.src||'')}); + return sanitize(html||'',{allowedTags:['p','br','div','span','b','strong','i','em','u','s','ul','ol','li','blockquote','pre','code','h1','h2','h3','h4','table','thead','tbody','tfoot','tr','th','td','hr','img','a'],allowedAttributes:{'*':['style'],td:['colspan','rowspan','style'],th:['colspan','rowspan','style'],img:['src','alt','width','height'],a:['href','title']},allowedStyles:{'*':{'color':[/^#[0-9a-f]{3,8}$/i,/^[a-z]+$/i],'background-color':[/^#[0-9a-f]{3,8}$/i,/^[a-z]+$/i],'text-align':[/^(left|right|center)$/],'font-weight':[/^(bold|normal|[1-9]00)$/],'font-size':[/^\d{1,2}(px|pt)$/],'border':[/^\dpx solid #[0-9a-f]{3,6}$/i],'padding':[/^\d{1,2}px$/]}},allowedSchemes:['data'],allowedSchemesByTag:{img:['data'],a:['http','https']},allowProtocolRelative:false,transformTags:{a:(tag,attrs)=>{if(safeLink(attrs.href))return {tagName:'a',attribs:attrs};const {href,...rest}=attrs;return {tagName:'span',attribs:rest};},img:(tag,attrs)=>{const src=attrs.src?.startsWith('cid:')?(inline.get(attrs.src.slice(4))||''):attrs.src||'';expandedBytes+=src.length;if(expandedBytes>12*1024*1024)fail('内嵌图片展开超过 12 MB,请减少重复图片后重新发送。');return {tagName:'img',attribs:{...attrs,src}};}},exclusiveFilter:frame=>frame.tag==='img'&&!/^data:image\/(png|jpeg|gif|webp);base64,[A-Za-z0-9+/=]+$/.test(frame.attribs.src||'')}); } async function parseTemplate(raw,channel){ if(!Buffer.isBuffer(raw)||raw.length>MAX_MAIL)fail('邮件超过 10 MB 限制。'); diff --git a/tests/mail/huizhou-links.test.cjs b/tests/mail/huizhou-links.test.cjs new file mode 100644 index 0000000..2041a75 --- /dev/null +++ b/tests/mail/huizhou-links.test.cjs @@ -0,0 +1,25 @@ +'use strict'; +const test=require('node:test'),assert=require('node:assert/strict'); +const {parseTemplate,cleanHtml}=require('../../src/modules/mail/parser.cjs'); +const {extractTitle}=require('../../src/modules/order-rules/title.cjs'); +const {ordersForMode}=require('../../src/modules/order-rules/huizhou.cjs'); +const {buildBatch}=require('../../src/modules/order-rules/batch.cjs'); +const {applyBodyTemplate}=require('../../src/modules/order-rules/template.cjs'); +const channel={email:'orders@example.test',senders:['author@example.test']}; +const raw=html=>Buffer.from('From: author@example.test\r\nTo: orders@example.test\r\nSubject: [模板]惠州 {{订单号}}\r\nMIME-Version: 1.0\r\nContent-Type: text/html; charset=utf-8\r\n\r\n'+html); +test('imported business anchor survives real parsing and Hui rendering as a usable HTTPS link',async()=>{ + const parsed=await parseTemplate(raw('
{{订单明细}}