'use strict'; // Email to card (#794, #3948): the settings resolution, the email-to-card // mapping and the personal board addresses. // Run: node tests/emailToCard.test.cjs const { test } = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const { DEFAULT_PARTS, cleanParts, resolveEmailToCard, cardFromEmail, htmlToText, cleanInboundAdmin, inboundMailAdminView, createRateLimiter, } = require('../models/lib/emailToCard'); const { newBoardAddressToken, boardAddress, parseBoardAddress, recipientAddresses, subjectBoardToken, } = require('../server/lib/inboundBoardAddress'); const { fromProviderFields, fromParsedMail } = require('../server/lib/inboundEmailPayload'); const ROOT = path.join(__dirname, '..'); const read = file => fs.readFileSync(path.join(ROOT, file), 'utf8'); const { stripQuotedReply } = require('../server/lib/inboundEmailQuoteStrip'); const admin = { enabled: true, domain: 'boards.example.com' }; test('on only when the admin, the board and the member all allow it', () => { assert.equal(resolveEmailToCard({ admin, board: { enabled: true } }).enabled, true, 'member default is on'); assert.deepEqual( [ resolveEmailToCard({ admin: { enabled: false, domain: 'x' }, board: { enabled: true } }), resolveEmailToCard({ admin: { enabled: true }, board: { enabled: true } }), resolveEmailToCard({ admin, board: {} }), resolveEmailToCard({ admin, board: { enabled: true }, member: { enabled: false } }), ].map(r => [r.enabled, r.reason]), [[false, 'admin-off'], [false, 'admin-off'], [false, 'board-off'], [false, 'member-off']], ); // A member cannot turn on what the admin or the board has off (negative). assert.equal(resolveEmailToCard({ admin: {}, board: { enabled: true }, member: { enabled: true } }).enabled, false); assert.equal(resolveEmailToCard({ admin, board: { enabled: false }, member: { enabled: true } }).enabled, false); }); test('parts: member over board over defaults, unknown or invalid values dropped', () => { const r = resolveEmailToCard({ admin, board: { enabled: true, listId: 'L', swimlaneId: 'S', parts: { attachments: false, description: 'none' } }, member: { parts: { description: 'text', senderLine: false } }, }); assert.deepEqual(r.parts, { description: 'text', stripQuoted: true, attachments: false, senderLine: false }); assert.equal(r.listId, 'L'); assert.equal(r.swimlaneId, 'S'); assert.deepEqual(cleanParts({ description: 'html', attachments: 'yes', evil: true, stripQuoted: false }), { stripQuoted: false }); assert.deepEqual(cleanParts(null), {}); }); test('an email becomes a card: subject as title, stripped body, sender line, attachments', () => { const message = { subject: ' Fix the login ', text: 'Please fix it.\n\nOn Mon, Ann wrote:\n> old text\n', from: { name: 'Ann A', address: 'ann@example.com' }, attachments: [{ filename: 'a.txt', content: Buffer.from('x') }, { filename: '', content: Buffer.from('y') }], }; const card = cardFromEmail(message, DEFAULT_PARTS, { stripQuoted: stripQuotedReply }); assert.equal(card.title, 'Fix the login'); assert.equal(card.description, 'From: Ann A \n\nPlease fix it.'); assert.deepEqual(card.attachments.map(a => a.filename), ['a.txt'], 'nameless attachments are skipped'); const bare = cardFromEmail(message, { description: 'none', senderLine: false, attachments: false }); assert.equal(bare.description, ''); assert.deepEqual(bare.attachments, []); }); test('no subject: the first line; no text part: the HTML as text, never as markup', () => { assert.equal(cardFromEmail({ text: '\n\nFirst line\nsecond' }, { senderLine: false }).title, 'First line'); assert.equal(cardFromEmail({}, {}).title, 'Email'); const html = '

Hi & bye

Two
'; assert.equal(htmlToText(html), 'Hi & bye\nTwo'); const card = cardFromEmail({ subject: 's', html }, { senderLine: false }); assert.equal(card.description, 'Hi & bye\nTwo'); assert.doesNotMatch(card.description, /<|script|alert/); assert.equal(cardFromEmail({ subject: 'x'.repeat(400) }, {}).title.length, 255); }); test('CodeQL #570/#571/#581/#582: removing a tag never splices the text around it into a new one', () => { // One pass of each removal used to leave a whole tag or script element. for (const html of ['ipt>alert(1)', '<script>alert(1)</script>', 'xipt>alert(1)xipt>', '<<i>i>b>bold']) { const text = htmlToText(html); // No complete tag is left; a lone `<` with no `>` after it is not one. assert.doesNotMatch(text, /<[^>]*>/, `${JSON.stringify(html)} -> ${JSON.stringify(text)}`); } // CodeQL #581/#582: since the regular expressions were replaced by a // one-pass scanner, nothing is spliced together in the first place. In // these two, "" is ONE unknown tag (as a browser reads it), so // what follows is text, not a script element: it may stay, as inert text, // but no tag and no script or style element may come out. for (const html of ['ipt>alert(1)', 'le>p{}ok']) { const text = htmlToText(html); assert.doesNotMatch(text, /<[^>]*>|alert(1)b'), 'ab'); assert.equal(htmlToText('a