40 lines
4.9 KiB
JavaScript
40 lines
4.9 KiB
JavaScript
'use strict';
|
|
const test=require('node:test'),assert=require('node:assert/strict');
|
|
const {parseTemplate,cleanHtml,renderTemplate}=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('<p><a href="https://example.test/business">业务访问入口</a></p><p>{{订单明细}}</p><img src="https://tracker.example.test/pixel">'),channel);
|
|
const rule={id:'notice',version:1,categoryId:'notice',name:'通知',to:['recipient@example.test'],cc:[],attachPdf:false,huizhouEnabled:true,huizhouTemplateId:'hui-link'};
|
|
const title=extractTitle('订单号:O-100客户简称:普通客户业务模式:惠州数字毛利:绿标订单号:H-200客户简称:终端客户');
|
|
const order={id:'event',status:'pending',ruleSnapshot:rule,event:{orderNo:'O-100',receivedAt:'2026-10-19T01:00:00Z',brief:title.brief,titleOrders:title.orders},archive:{archivedAt:'2026-10-19T02:00:00Z',files:[{name:'event.pdf',path:'C:\\archive\\event.pdf',kind:'pdf',size:100,sha256:'a'.repeat(64)}]}};
|
|
const selected=ordersForMode([order],'huizhou'),snapshot=buildBatch(rule,selected,'2026-10-19T03:00:00Z',{notificationMode:'huizhou'});
|
|
const mail=applyBodyTemplate(snapshot,{...parsed,id:'hui-link',version:1,status:'active',notificationMode:'huizhou'},selected,20971520);
|
|
assert.match(mail.html,/<a href="https:\/\/example\.test\/business">业务访问入口<\/a>/);assert.match(mail.text,/https:\/\/example\.test\/business/);assert.match(mail.html,/H-200/);assert.doesNotMatch(mail.html,/O-100|tracker|<img/i);
|
|
});
|
|
test('sanitizer preserves absolute HTTP and HTTPS links but removes navigation side effects',()=>{
|
|
const html=cleanHtml('<a href="http://example.test/business" onclick="attack()" ping="https://tracker.test/ping" target="_blank" download>HTTP</a><a href="https://example.test/business?q=one&mode=hui" onmouseover="attack()">HTTPS</a>');
|
|
assert.match(html,/href="http:\/\/example\.test\/business"/);assert.match(html,/href="https:\/\/example\.test\/business\?q=one&mode=hui"/);assert.doesNotMatch(html,/onclick|onmouseover|ping=|target=|download|tracker|attack/i);
|
|
});
|
|
for(const href of ['javascript:alert(1)','javascript:alert(1)','data:text/html,hello','file:///C:/private','mailto:user@example.test','//example.test/business','/business','https://user:secret@example.test/business','https://user@example.test/business','https://@example.test/business','https://example.test/\nredirect',' https://example.test/business','https://example.test/%0aheader','https://example.test/%00','https:\\\\example.test\\business','https:///example.test/business'])test('sanitizer strips unsafe link target '+JSON.stringify(href),async()=>{
|
|
const parsed=await parseTemplate(raw('<a href="'+href+'" onfocus="attack()">业务访问入口</a>'),channel);assert.match(parsed.html,/业务访问入口/);assert.doesNotMatch(parsed.html,/href=|onfocus|attack/i);
|
|
});
|
|
for(const [href,value] of [
|
|
['{{产品线}}://example.test/business','javascript'],
|
|
['https://{{产品线}}','user:secret@example.test'],
|
|
['https://example.test/{{产品线}}','\\\\untrusted.test'],
|
|
['https://example.test/business?next={{产品线}}','x\nheader'],
|
|
['https://{{产品线}}','user:secret@example.test']
|
|
])test('import removes dynamic href before template substitution '+JSON.stringify(href),async()=>{
|
|
const parsed=await parseTemplate(raw('<a href="'+href+'">业务访问入口</a><p>产品线:{{产品线}}</p><p>订单:{{订单号}}</p>'),channel);
|
|
const rendered=renderTemplate(parsed,{'产品线':value,'订单号':'H-200'});assert.doesNotMatch(rendered.html,/href=/i);assert.match(rendered.html,/业务访问入口/);assert.match(rendered.html,/订单:H-200/);assert.doesNotMatch(rendered.html,/{{产品线}}/);assert.ok(rendered.text.includes(value));
|
|
});
|
|
test('literal percent-encoded business path stays clickable and body variables still render',async()=>{
|
|
const parsed=await parseTemplate(raw('<a href="https://example.test/business/%E6%83%A0%E5%B7%9E?label=one%20two">业务访问入口</a><p>{{产品线}} {{订单号}}</p>'),channel),rendered=renderTemplate(parsed,{'产品线':'机顶盒','订单号':'H-200'});
|
|
assert.match(rendered.html,/href="https:\/\/example\.test\/business\/%E6%83%A0%E5%B7%9E\?label=one%20two"/);assert.match(rendered.html,/机顶盒 H-200/);
|
|
});
|