Add native Winlink attachment upload, reader indicators and verified storage

This commit is contained in:
alberto committed 2026-10-17 14:33:34 +02:00
1 parent 2b00b1cbbd
commit 5fde09823b
13 files changed
+223 -20

No files matched your search

+31
View File
@@ -0,0 +1,31 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {Readable} from 'node:stream';
import {readSubmission,outgoingForm,verifySavedAttachments,uploadLimits} from './mail-upload.mjs';
import {parseAttachments,b2Body,attachmentSummary} from './attachments.mjs';
async function request(form){const r=new Request('http://localhost/messages',{method:'POST',body:form}),stream=Readable.from([Buffer.from(await r.arrayBuffer())]);stream.headers={'content-type':r.headers.get('content-type')};return stream;}
function b2(body,files){return Buffer.concat([Buffer.from('MID: fixture\r\nBody: '+Buffer.byteLength(body)+'\r\n'+files.map(f=>'File: '+f.data.length+' '+f.name+'\r\n').join('')+'\r\n'+body+'\r\n'),...files.flatMap(f=>[f.data,Buffer.from('\r\n')])]);}
test('multipart upload reaches native BPQ fields with byte-exact binary and UTF8 files',async()=>{
const files=[{name:'image.jpg',data:Buffer.from([255,216,255,0,13,10,255])},{name:'note.txt',data:Buffer.from('Città e radio 📻')}];
const form=new FormData();for(const [k,v]of Object.entries({to:'RMS/[email protected]',subject:'Attachments',text:'Hello à',type:'P'}))form.set(k,v);for(const f of files)form.append('files',new Blob([f.data]),f.name);
const data=await readSubmission(await request(form));assert.equal(data.files.length,2);for(let i=0;i<files.length;i++)assert.deepEqual(data.files[i].data,files[i].data);
const native=outgoingForm(data);assert.equal(native.get('To'),data.to);assert.equal(native.get('Msg'),'Hello à');const nativeFiles=native.getAll('myFile[]');assert.equal(nativeFiles.length,2);for(let i=0;i<files.length;i++)assert.deepEqual(Buffer.from(await nativeFiles[i].arrayBuffer()),files[i].data);
const bytes=b2('Hello à',data.files);assert.equal(b2Body(bytes),'Hello à');assert.equal(verifySavedAttachments(bytes,data.files).length,2);assert.deepEqual(attachmentSummary(bytes),{attachmentCount:2,attachmentBytes:files.reduce((n,f)=>n+f.data.length,0)});
});
test('attachment count ignores plain text and requires a B2 header',()=>{assert.equal(attachmentSummary(Buffer.from('Body: 4\r\nFile: 3 test\r\n\r\ntext')).attachmentCount,0);});
test('upload rejects oversize files, too many files, duplicates and unsafe fields before contacting BPQ',async()=>{
const make=files=>{const f=new FormData();for(const [name,data]of files)f.append('files',new Blob([data]),name);return f;};
await assert.rejects(readSubmission(await request(make([['large.bin',Buffer.alloc(uploadLimits.bytes+1)]]))),/64 KB/);
await assert.rejects(readSubmission(await request(make(Array.from({length:11},(_,i)=>[i+'.txt','x'])))),/10 allegati/);
await assert.rejects(readSubmission(await request(make([['same.txt','a'],['SAME.txt','b']]))),/duplicati/);
const f=new FormData();f.set('to','a');f.append('to','b');await assert.rejects(readSubmission(await request(f)),/Campi/);
const req=Readable.from([Buffer.from(JSON.stringify({files:[{name:'bypass'}]}))]);req.headers={'content-type':'application/json'};await assert.rejects(readSubmission(req),/caricamento/);
});
test('filenames cannot inject headers, preserve paths, or silently collide after sanitisation',async()=>{
const form=new FormData();form.append('files',new Blob(['x']),'../../unsafe;name.txt');const data=await readSubmission(await request(form));assert.equal(data.files[0].name,'unsafe_name.txt');
const both=new FormData();both.append('files',new Blob(['x']),'one;two.txt');both.append('files',new Blob(['y']),'one_two.txt');await assert.rejects(readSubmission(await request(both)),/duplicati/);
});
test('saved message is rejected if BPQ drops, truncates or substitutes an attachment',()=>{
const files=[{name:'test.bin',data:Buffer.from([0,255,1])}];const bytes=b2('test',files);assert.deepEqual(parseAttachments(bytes)[0].data,files[0].data);assert.throws(()=>verifySavedAttachments(b2('test',[]),files),/allegati non corrispondono/);assert.throws(()=>verifySavedAttachments(b2('test',[{...files[0],data:Buffer.from([0,255,2])}]),files),/allegati non corrispondono/);assert.throws(()=>verifySavedAttachments(bytes.subarray(0,bytes.length-4),files),/incompleto/);
});