From 02a28f4ee9789ec7bb4e114640c2350147681f95 Mon Sep 17 00:00:00 2001 From: Weapie Date: Thu, 5 Nov 2026 17:02:48 +0100 Subject: [PATCH] Raise incoming mail and attachment limits to 50 MiB --- CHANGELOG.md | 1 + README.md | 2 +- server/content.js | 21 +++-- server/inbound-errors.js | 3 +- server/inbound.js | 5 +- server/mail-limits.js | 2 + server/service.js | 15 ++-- tests/inbound-resilience.test.js | 129 ++++++++++++++++++++++++++++++- 8 files changed, 160 insertions(+), 18 deletions(-) create mode 100644 server/mail-limits.js diff --git a/CHANGELOG.md b/CHANGELOG.md index d21ee68..b9c05da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Aktueller Stand +- Eingehende IMAP-Mails bis 50 MiB einschließlich MIME-Kodierung, mit passenden Speichergrenzen für Originalmail, HTML und eingehende Anhänge. Ausgehende Anhangsgrenzen bleiben bei 10 MiB je Datei und 20 MiB insgesamt. - Mails an mehrere Queues in An/Cc erzeugen je Queue eine eigene Kopie, mit queue-spezifischer Duplikaterkennung, Antwortzuordnung, Tags, Benachrichtigungen und optionalen Eingangsbestätigungen. Gemeinsame Postfächer werden vor dem Gelesen-Markieren vollständig verteilt. - Transaktionale Migration bestehender Message-IDs unter Erhalt von Originalen, Anhängen, Historie, Suchindizes und Verknüpfungen; Abrufprotokoll zählt zusätzliche Queue-Zuordnungen. - Robuster IMAP-Import: neue Mails werden auch bei mehr als 100 fehlerhaften Nachrichten weiter verarbeitet, persistente Wiederholungen mit wachsendem Abstand und getrennte Behandlung fehlgeschlagener Gelesen-Markierungen. diff --git a/README.md b/README.md index affe8b7..5ac9406 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ HTML und MIME-Originale werden erst seit diesem Update beim Empfang gespeichert. **Manuelles Ticket erstellen** erfasst Kundenname, E-Mail, Betreff und Beschreibung ohne vorherige E-Mail. Dafür sind Lesen und Antworten in der Queue erforderlich. Es entsteht keine automatische Kunden-Eingangsbestätigung. Wiederholtes Speichern desselben Auftrags erzeugt kein zweites Ticket. -Anhänge sind auf 20 Dateien, 10 MiB pro Datei und insgesamt 20 MiB pro ausgehender Nachricht begrenzt. Auch eine Antwort ausschließlich mit Anhang ist möglich. Für eingehende IMAP-Nachrichten gilt ein Größenlimit von 20 MiB; MIME-Kodierung und zusätzliche Providerlimits müssen berücksichtigt werden. +Anhänge sind auf 20 Dateien, 10 MiB pro Datei und insgesamt 20 MiB pro ausgehender Nachricht begrenzt. Auch eine Antwort ausschließlich mit Anhang ist möglich. Für eingehende IMAP-Nachrichten gilt ein Größenlimit von **50 MiB (52.428.800 Byte)** für die gesamte Originalmail einschließlich MIME-Kodierung. Eingehende Anhänge dürfen einzeln und zusammen bis zu 50 MiB umfassen, weiterhin höchstens 20 Dateien. Originalmail und HTML-Inhalt können bis zu 50 MiB gespeichert werden; die begrenzte HTML-Vorschau beeinflusst den Import nicht. Zusätzliche Providerlimits müssen berücksichtigt werden. Wegen Base64-Kodierung liegt die mögliche Nutzgröße von Anhängen innerhalb einer 50-MiB-Mail darunter. ### Status und Wiedervorlagen diff --git a/server/content.js b/server/content.js index f280775..3fc4a78 100644 --- a/server/content.js +++ b/server/content.js @@ -113,7 +113,10 @@ export function composeContent(body, signature, context) { : null, } } -export function normaliseAttachments(values = []) { +export function normaliseAttachments( + values = [], + { maxFileBytes = 10 * 1024 * 1024, maxTotalBytes = 20 * 1024 * 1024 } = {}, +) { if (!Array.isArray(values) || values.length > 20) throw new HttpError(400, 'Höchstens 20 Anhänge pro Nachricht.') let total = 0 @@ -121,17 +124,23 @@ export function normaliseAttachments(values = []) { if ( !a || typeof a.content !== 'string' || - a.content.length > 13981016 || + a.content.length > Math.ceil(maxFileBytes / 3) * 4 || a.content.length % 4 !== 0 || /[^a-z\d+/=]/i.test(a.content) ) - throw new HttpError(400, 'Ungültiger Anhang (maximal 10 MB pro Datei).') + throw new HttpError( + 400, + `Ungültiger Anhang (maximal ${maxFileBytes / 1024 / 1024} MiB pro Datei).`, + ) const content = Buffer.from(a.content, 'base64') - if (content.length > 10 * 1024 * 1024 || content.toString('base64') !== a.content) + if (content.length > maxFileBytes || content.toString('base64') !== a.content) throw new HttpError(413, 'Anhang ist zu groß oder ungültig.') total += content.length - if (total > 20 * 1024 * 1024) - throw new HttpError(413, 'Anhänge dürfen zusammen höchstens 20 MB groß sein.') + if (total > maxTotalBytes) + throw new HttpError( + 413, + `Anhänge dürfen zusammen höchstens ${maxTotalBytes / 1024 / 1024} MiB groß sein.`, + ) return { name: String(a.name || 'Anhang') .replace(/[\x00-\x1f\x7f/\\]/g, '_') diff --git a/server/inbound-errors.js b/server/inbound-errors.js index 7467ed1..f6f6be8 100644 --- a/server/inbound-errors.js +++ b/server/inbound-errors.js @@ -1,5 +1,6 @@ import { HttpError } from './service.js' import { validEmail } from './recipients.js' +import { INCOMING_MAIL_LIMIT_LABEL } from './mail-limits.js' const clean = (value, limit = 500) => String(value || '') @@ -41,7 +42,7 @@ export function mailFailure(error, stage, parsed) { const code = /^[a-z0-9_.-]{1,80}$/i.test(rawCode) ? rawCode : 'Error' let reason if (error.message === 'MessageTooLarge') - reason = 'Nachricht überschreitet das Importlimit von 20 MiB.' + reason = `Nachricht überschreitet das Importlimit von ${INCOMING_MAIL_LIMIT_LABEL}.` else if (error instanceof HttpError && error.status < 500) { reason = clean(error.message) if (error.message === 'Ungültige E-Mail.' && parsed) { diff --git a/server/inbound.js b/server/inbound.js index 9ad0f5f..9a0c90b 100644 --- a/server/inbound.js +++ b/server/inbound.js @@ -5,6 +5,7 @@ import { encryptSecret, decryptSecret } from './secrets.js' import { HttpError } from './service.js' import { now } from './db.js' import { mailIdentity, mailFailure, retryAt } from './inbound-errors.js' +import { INCOMING_MAIL_BYTES } from './mail-limits.js' export function nextFetch(config, at = new Date()) { if (!config.enabled) return null if (config.schedule.mode === 'interval') @@ -358,12 +359,12 @@ export class InboundWorker { meta = await client.fetchOne(uid, { size: true, envelope: true }, { uid: true }) if (!meta) continue size = meta.size - if (meta.size > 20 * 1024 * 1024) throw Error('MessageTooLarge') + if (meta.size > INCOMING_MAIL_BYTES) throw Error('MessageTooLarge') stage = 'download' const message = await client.fetchOne(uid, { source: true }, { uid: true }) if (!message) continue size = message.source.length - if (message.source.length > 20 * 1024 * 1024) throw Error('MessageTooLarge') + if (message.source.length > INCOMING_MAIL_BYTES) throw Error('MessageTooLarge') stage = 'parse' const p = await simpleParser(message.source, { keepCidLinks: true }) parsed = p diff --git a/server/mail-limits.js b/server/mail-limits.js new file mode 100644 index 0000000..e8fe21d --- /dev/null +++ b/server/mail-limits.js @@ -0,0 +1,2 @@ +export const INCOMING_MAIL_BYTES = 50 * 1024 * 1024 +export const INCOMING_MAIL_LIMIT_LABEL = '50 MiB' diff --git a/server/service.js b/server/service.js index 37cdc2d..7c24045 100644 --- a/server/service.js +++ b/server/service.js @@ -6,6 +6,7 @@ import { expandSignature } from './signature-placeholders.js' import { messageView } from './message-view.js' import { replyCopies, validEmail } from './recipients.js' import { searchQuery } from './search.js' +import { INCOMING_MAIL_BYTES, INCOMING_MAIL_LIMIT_LABEL } from './mail-limits.js' import { ticketPage, ticketCounts, visibleQueues, pagination, pageBounds } from './ticket-list.js' import { queueTags, @@ -621,14 +622,15 @@ export class Service { return this.tx(() => { if ( data.html != null && - (typeof data.html !== 'string' || data.html.length > 20 * 1024 * 1024) + (typeof data.html !== 'string' || + Buffer.byteLength(data.html, 'utf8') > INCOMING_MAIL_BYTES) ) - fail(400, 'Ungültiger HTML-Inhalt (maximal 20 MB).') + fail(400, `Ungültiger HTML-Inhalt (maximal ${INCOMING_MAIL_LIMIT_LABEL}).`) if ( data.source != null && - (!Buffer.isBuffer(data.source) || data.source.length > 20 * 1024 * 1024) + (!Buffer.isBuffer(data.source) || data.source.length > INCOMING_MAIL_BYTES) ) - fail(400, 'Ungültige Originalnachricht (maximal 20 MB).') + fail(400, `Ungültige Originalnachricht (maximal ${INCOMING_MAIL_LIMIT_LABEL}).`) if ( !data.messageId || data.messageId.length > 998 || @@ -754,7 +756,10 @@ export class Service { data.messageId, id, ) - for (const a of normaliseAttachments(data.attachments)) { + for (const a of normaliseAttachments(data.attachments, { + maxFileBytes: INCOMING_MAIL_BYTES, + maxTotalBytes: INCOMING_MAIL_BYTES, + })) { const aid = uid() this.run('INSERT INTO attachments VALUES(?,?,?,?,?)', aid, id, a.name, a.mime, a.content) if (a.contentId) this.run('INSERT INTO attachment_inline VALUES(?,?)', aid, a.contentId) diff --git a/tests/inbound-resilience.test.js b/tests/inbound-resilience.test.js index 3273f4d..110c461 100644 --- a/tests/inbound-resilience.test.js +++ b/tests/inbound-resilience.test.js @@ -8,6 +8,9 @@ import { Service } from '../server/service.js' import { readMailSettings, saveMailSettings } from '../server/mail-config.js' import { InboundWorker } from '../server/inbound.js' import { retryAt } from '../server/inbound-errors.js' +import { INCOMING_MAIL_BYTES } from '../server/mail-limits.js' +import { normaliseAttachments } from '../server/content.js' +import nodemailer from 'nodemailer' process.env.SMTP_ENCRYPTION_KEY = 'a9'.repeat(32) function setup(file = ':memory:') { @@ -67,6 +70,126 @@ const errors = (s) => .all("SELECT * FROM audits WHERE action='Nachricht konnte nicht importiert werden' ORDER BY id") .map((a) => JSON.parse(a.after_value)) +test('IMAP imports a complete MIME mail above 20 MiB with an attachment above the old 10 MiB limit', async () => { + const { db, s } = setup() + try { + const sent = await nodemailer + .createTransport({ streamTransport: true, buffer: true, newline: 'windows' }) + .sendMail({ + from: 'customer@example.org', + to: 'support@syncova.local', + messageId: '', + subject: 'Große Originalmail', + text: 'Bitte die Datei bearbeiten.', + attachments: [ + { + filename: 'gross.bin', + content: Buffer.alloc(19 * 1024 * 1024, 97), + contentType: 'application/octet-stream', + }, + ], + }) + const source = sent.message + assert(source.length > 20 * 1024 * 1024 && source.length < INCOMING_MAIL_BYTES) + const state = {} + const result = await new InboundWorker(s, { + clientFactory: fake(new Map([[1, source]]), state), + }).run('support') + assert.equal(result.imported, 1) + assert.equal(result.failed, 0) + assert.deepEqual(state.seen, [1]) + const message = s.get("SELECT id FROM messages WHERE message_id=''") + assert.equal( + s.get('SELECT length(source) n FROM message_originals WHERE message_id=?', message.id).n, + source.length, + ) + assert.equal( + s.get('SELECT length(content) n FROM attachments WHERE message_id=?', message.id).n, + 19 * 1024 * 1024, + ) + } finally { + db.close() + } +}) + +test('The 50 MiB metadata boundary is inclusive and oversized downloads are rejected even if metadata underreports size', async () => { + const { db, s } = setup() + try { + const messages = new Map([ + [1, mime(1)], + [2, Buffer.alloc(INCOMING_MAIL_BYTES + 1)], + [3, mime(3)], + ]) + const state = { sizes: { 1: INCOMING_MAIL_BYTES, 2: 100 } } + const result = await new InboundWorker(s, { clientFactory: fake(messages, state) }).run( + 'support', + ) + assert.equal(result.imported, 2) + assert.equal(result.failed, 1) + assert.deepEqual(state.seen, [1, 3]) + const [failure] = errors(s) + assert.equal(failure.code, 'MessageTooLarge') + assert.equal(failure.stage, 'download') + assert.match(failure.reason, /50 MiB/) + assert.equal(failure.size, INCOMING_MAIL_BYTES + 1) + } finally { + db.close() + } +}) + +test('Original storage accepts exactly 50 MiB and HTML above 20 MiB while refusing larger originals and HTML', () => { + const { db, s } = setup() + try { + const source = Buffer.alloc(INCOMING_MAIL_BYTES), + html = 'x'.repeat(21 * 1024 * 1024) + const incoming = { + messageId: '', + from: 'customer@example.org', + to: ['support@syncova.local'], + subject: 'Speicherlimit', + body: 'Beschreibung', + source, + html, + } + const id = s.ingest(incoming).ticket_id + const stored = s.get( + 'SELECT length(o.source) source_size,length(o.html) html_size FROM message_originals o JOIN messages m ON m.id=o.message_id WHERE m.ticket_id=?', + id, + ) + assert.equal(stored.source_size, INCOMING_MAIL_BYTES) + assert.equal(stored.html_size, html.length) + assert.throws( + () => + s.ingest({ + ...incoming, + messageId: '', + source: Buffer.alloc(INCOMING_MAIL_BYTES + 1), + html: null, + }), + /50 MiB/, + ) + assert.throws( + () => + s.ingest({ + ...incoming, + messageId: '', + source: null, + html: 'x'.repeat(INCOMING_MAIL_BYTES + 1), + }), + /50 MiB/, + ) + } finally { + db.close() + } +}) + +test('Outbound attachments retain their existing per-file and total limits', () => { + const large = { name: 'gross.bin', content: Buffer.alloc(11 * 1024 * 1024).toString('base64') } + assert.throws(() => normaliseAttachments([large]), /10 MiB/) + const small = { name: 'klein.bin', content: Buffer.alloc(8 * 1024 * 1024).toString('base64') } + assert.throws(() => normaliseAttachments([small, small, small]), /20 MiB/) +}) + test('A failed message does not stop later imports and logs usable identity, cause and retry time', async () => { const { db, s, admin } = setup() try { @@ -187,7 +310,7 @@ test('Oversize and failed downloads retain envelope identity without logging raw [3, mime(3)], ]) const state = { - sizes: { 1: 21 * 1024 * 1024 }, + sizes: { 1: 51 * 1024 * 1024 }, sourceErrors: { 2: Object.assign(new Error('raw-server-password=do-not-log'), { code: 'ECONNRESET' }), }, @@ -200,8 +323,8 @@ test('Oversize and failed downloads retain envelope identity without logging raw const [large, download] = errors(s) assert.equal(large.subject, 'Kopf 1') assert.equal(large.code, 'MessageTooLarge') - assert.equal(large.size, 21 * 1024 * 1024) - assert.match(large.reason, /20 MiB/) + assert.equal(large.size, 51 * 1024 * 1024) + assert.match(large.reason, /50 MiB/) assert(!state.requests.some((r) => r.uid === 1 && r.type === 'source')) assert.equal(download.subject, 'Kopf 2') assert.equal(download.stage, 'download')