fix: retain safe business access links in mail templates

This commit is contained in:
abaishu0730 committed 2026-10-21 16:01:15 +08:00
1 parent d669a7b28e
commit 2fe5a3fe5d
2 files changed
+34 -1

No files matched your search

+25
View File
@@ -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('<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&amp;mode=hui" onmouseover="attack()">HTTPS</a>');
assert.match(html,/href="http:\/\/example\.test\/business"/);assert.match(html,/href="https:\/\/example\.test\/business\?q=one&amp;mode=hui"/);assert.doesNotMatch(html,/onclick|onmouseover|ping=|target=|download|tracker|attack/i);
});
for(const href of ['javascript:alert(1)','jav&#x61;script: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);
});