// Offline tests for mail-transport: a table-driven fake resolver, a route-table fake request and a // fixed clock. Nothing here resolves a name, opens a socket, or reads the real time. // // The assertions that matter most are the wording ones. A TXT record on its own protects nothing, // a blocked policy fetch is not a finding, and the absence of all three records is the normal // state of a Malaysian SME — each of those is pinned below. import test from 'node:test'; import assert from 'node:assert/strict'; import { checkMailTransport, analyseMailTransport, parseMtaStsTxt, parseMtaStsPolicy, parseTlsRptTxt, parseBimiTxt, parseTags, policyUrl, buildSummary, worstLevel, registrableApex, REQUEST_BUDGET, POLICY_TIMEOUT_MS, POLICY_MAX_BYTES, MTA_STS_NAME, TLS_RPT_NAME, BIMI_NAME } from '../src/checks/mail-transport.mjs'; import { NetGuardError } from '../src/lib/net-guard.mjs'; // 10 Nov 2026 00:00 UTC — every checkedAt below is this instant. const NOW = Date.UTC(2026, 10, 10, 0, 0, 0); const ISO = '2026-11-10T00:00:00.000Z'; const POLICY_TEXT = 'version: STSv1\nmode: enforce\nmax_age: 86400\nmx: mx.zoho.com\nmx: mx2.zoho.com\n'; const POLICY_URL = 'https://mta-sts.example.com/.well-known/mta-sts.txt'; // --------------------------------------------------------------------------- // fakes // --------------------------------------------------------------------------- /** TXT answers arrive as arrays of chunk arrays, exactly like node:dns. */ function tableDns(table = {}, { fail = {} } = {}) { const calls = []; const resolveTxt = async (name) => { calls.push(name); if (name in fail) throw Object.assign(new Error(`${fail[name]} ${name}`), { code: fail[name] }); if (!(name in table)) throw Object.assign(new Error(`ENODATA ${name}`), { code: 'ENODATA' }); const value = table[name]; if (value instanceof Error) throw value; return value; }; resolveTxt.calls = calls; return { resolveTxt }; } function routeRequest(routes = {}) { const calls = []; const fn = async (url, options = {}) => { calls.push({ url, options }); const route = routes[url]; if (route === undefined) throw new NetGuardError('dns_nxdomain', `no route for ${url}`); if (route instanceof Error) throw route; return { status: 200, headers: {}, body: '', url, ...route }; }; fn.calls = calls; return fn; } const txt = (record) => [[record]]; function gatheredFixture(overrides = {}) { return { domain: 'example.com', mtaSts: { checked: true, present: false, valid: false, record: null, id: null, count: 0 }, tlsRpt: { checked: true, present: false, valid: false, rua: [], record: null }, bimi: { checked: true, present: false, valid: false, location: null, authority: null, record: null }, dmarc: { checked: true, record: null, parsed: null }, policy: { attempted: false, ok: false, kind: 'skipped' }, fatal: null, ...overrides }; } function policyOk(text = POLICY_TEXT) { return { attempted: true, ok: true, kind: 'ok', status: 200, text, parsed: parseMtaStsPolicy(text) }; } const codes = (result) => result.findings.map((finding) => finding.code); const levels = (result) => result.findings.map((finding) => finding.level); // --------------------------------------------------------------------------- // record parsing // --------------------------------------------------------------------------- test('an MTA-STS TXT record is read for its id and nothing more', () => { const parsed = parseMtaStsTxt(['v=STSv1; id=20240101T000000']); assert.equal(parsed.present, true); assert.equal(parsed.valid, true); assert.equal(parsed.id, '20240101T000000'); // The TXT says nothing about enforcement; the mode lives in the policy file. assert.equal(Object.prototype.hasOwnProperty.call(parsed, 'mode'), false); }); test('a TXT record that is not v=STSv1 is present but not valid', () => { const parsed = parseMtaStsTxt(['v=STSv1x; id=abc']); assert.equal(parsed.present, true); assert.equal(parsed.valid, false); assert.equal(parseMtaStsTxt([]).present, false); }); test('a TXT record split across 255-byte chunks is joined before parsing', async () => { // node:dns returns chunks; joining them is what makes an id that straddles the boundary readable. const dns = tableDns({ [`_mta-sts.example.com`]: [['v=STSv1; id=20240101T00', '0000']] }); const result = await checkMailTransport('example.com', { dns, request: routeRequest({}), now: NOW }); const row = result.evidence.find((item) => item.label === 'MTA-STS record'); assert.match(row.value, /id=20240101T000000/); }); test('the policy file is parsed for mode, max_age and mx patterns', () => { const parsed = parseMtaStsPolicy(POLICY_TEXT); assert.equal(parsed.isPolicy, true); assert.equal(parsed.valid, true); assert.equal(parsed.mode, 'enforce'); assert.equal(parsed.maxAge, 86400); assert.deepEqual(parsed.mx, ['mx.zoho.com', 'mx2.zoho.com']); }); test('a CRLF policy with comments and a trailing dot on mx still parses', () => { const parsed = parseMtaStsPolicy('# generated\r\nversion: STSv1\r\nmode: TESTING\r\nmax_age: 604800\r\nmx: MX.Example.COM.\r\n'); assert.equal(parsed.mode, 'testing'); assert.equal(parsed.maxAge, 604800); assert.deepEqual(parsed.mx, ['mx.example.com']); }); test('a body with no version: STSv1 is not a policy at all', () => { const parsed = parseMtaStsPolicy('Welcome to nginx'); assert.equal(parsed.isPolicy, false); assert.equal(parsed.valid, false); }); test('a policy missing max_age or mx is not valid', () => { assert.equal(parseMtaStsPolicy('version: STSv1\nmode: enforce\nmx: mx.example.com\n').valid, false); assert.equal(parseMtaStsPolicy('version: STSv1\nmode: enforce\nmax_age: 86400\n').valid, false); }); test('a policy with an unknown mode is not valid', () => { const parsed = parseMtaStsPolicy('version: STSv1\nmode: strict\nmax_age: 86400\nmx: mx.example.com\n'); assert.equal(parsed.mode, 'strict'); assert.equal(parsed.modeKnown, false); assert.equal(parsed.valid, false); }); test('TLS-RPT keeps every rua address and needs at least one', () => { const parsed = parseTlsRptTxt(['v=TLSRPTv1; rua=mailto:tls@example.com,mailto:ops@example.com']); assert.equal(parsed.valid, true); assert.deepEqual(parsed.rua, ['mailto:tls@example.com', 'mailto:ops@example.com']); const noRua = parseTlsRptTxt(['v=TLSRPTv1;']); assert.equal(noRua.present, true); assert.equal(noRua.valid, false); assert.deepEqual(noRua.rua, []); }); test('BIMI is read for its logo and its optional certificate', () => { const full = parseBimiTxt(['v=BIMI1; l=https://example.com/logo.svg; a=https://example.com/vmc.pem']); assert.equal(full.valid, true); assert.equal(full.location, 'https://example.com/logo.svg'); assert.equal(full.authority, 'https://example.com/vmc.pem'); const noLogo = parseBimiTxt(['v=BIMI1; l=']); assert.equal(noLogo.present, true); assert.equal(noLogo.valid, false); assert.equal(noLogo.location, null); }); test('the tag splitter ignores empty keys and keeps the last value', () => { assert.deepEqual(parseTags('v=BIMI1; l=https://a/b.svg; =junk; l=https://c/d.svg'), { v: 'BIMI1', l: 'https://c/d.svg' }); }); // --------------------------------------------------------------------------- // absence is normal // --------------------------------------------------------------------------- test('no records at all is info, not a fault: one row, state ok', () => { const result = analyseMailTransport(gatheredFixture(), { now: NOW }); assert.equal(result.state, 'ok'); assert.deepEqual(codes(result), ['mail_transport_none']); assert.equal(levels(result)[0], 'info'); assert.match(result.summary, /normal for a small business/); assert.match(result.summary, /MTA-STS is the one worth adding first/); // Three separate "missing" rows would read as three faults. assert.equal(result.findings.length, 1); }); test('the absence row is an opportunity, never a verdict', () => { const result = analyseMailTransport(gatheredFixture(), { now: NOW }); const text = JSON.stringify(result); assert.doesNotMatch(text, /insecure|broken|fail|vulnerable|at risk/i); }); test('when MTA-STS is absent but TLS-RPT is published, the summary says what is missing', () => { const result = analyseMailTransport(gatheredFixture({ tlsRpt: { checked: true, present: true, valid: true, rua: ['mailto:tls@example.com'], record: 'v=TLSRPTv1; rua=mailto:tls@example.com' } }), { now: NOW }); assert.equal(result.state, 'ok'); assert.ok(codes(result).includes('mta_sts_absent')); assert.ok(codes(result).includes('tls_rpt_published')); assert.match(result.summary, /publishes TLS-RPT but no MTA-STS record/); }); // --------------------------------------------------------------------------- // MTA-STS: the TXT is a promise, the policy file is the fact // --------------------------------------------------------------------------- test('a TXT record alone never claims protection', () => { // The record is valid but the policy file was not read at all. const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: { attempted: false, ok: false, kind: 'skipped' } }), { now: NOW }); assert.equal(codes(result).includes('mta_sts_mode_enforce'), false); assert.doesNotMatch(JSON.stringify(result.findings), /is protected|protects you|is enforcing/i); assert.match(result.evidence[0].note, /only points at the policy file/); }); test('mode=enforce is reported only after the policy file was read, and names the file', () => { const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: policyOk() }), { now: NOW }); assert.equal(result.state, 'ok'); assert.deepEqual(codes(result).filter((code) => code.startsWith('mta_sts')), ['mta_sts_mode_enforce']); const finding = result.findings.find((item) => item.code === 'mta_sts_mode_enforce'); assert.match(finding.message, /policy file at https:\/\/mta-sts\.example\.com\/\.well-known\/mta-sts\.txt/); assert.equal(result.evidence.find((item) => item.label === 'MTA-STS policy').value, 'mode=enforce, max_age=86400'); }); test('mode=testing is progress, not failure', () => { const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: policyOk('version: STSv1\nmode: testing\nmax_age: 86400\nmx: mx.example.com\n') }), { now: NOW }); assert.equal(result.state, 'warn'); const finding = result.findings.find((item) => item.code === 'mta_sts_mode_testing'); assert.equal(finding.level, 'warn'); assert.match(finding.message, /published but not enforced yet/); assert.match(finding.message, /still deliver/); assert.doesNotMatch(finding.message, /insecure|is broken|has failed/i); assert.equal(result.findings.some((item) => item.level === 'fail'), false); assert.match(result.summary, /progress rather than protection/); }); test('mode=none is info: published, but nothing is protected', () => { const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: policyOk('version: STSv1\nmode: none\nmax_age: 86400\nmx: mx.example.com\n') }), { now: NOW }); assert.equal(result.state, 'ok'); const finding = result.findings.find((item) => item.code === 'mta_sts_mode_none'); assert.equal(finding.level, 'info'); assert.match(finding.message, /nothing is protected/); }); test('a TXT promise with a 404 policy is a broken promise, named with the URL', () => { const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: { attempted: true, ok: false, kind: 'missing', code: 'http_404', status: 404, url: POLICY_URL } }), { now: NOW }); assert.equal(result.state, 'warn'); const finding = result.findings.find((item) => item.code === 'mta_sts_policy_missing'); assert.equal(finding.level, 'warn'); assert.match(finding.message, /https:\/\/mta-sts\.example\.com\/\.well-known\/mta-sts\.txt/); assert.match(finding.message, /not there \(HTTP 404\)/); assert.match(finding.message, /no policy/); assert.match(result.summary, /promises a policy/); }); test('a policy URL that answers 200 without version: STSv1 is a distinct finding', () => { const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: { attempted: true, ok: false, kind: 'missing', code: 'not_a_policy', status: 200, url: POLICY_URL } }), { now: NOW }); assert.equal(result.state, 'warn'); assert.ok(codes(result).includes('mta_sts_policy_not_a_policy')); const finding = result.findings.find((item) => item.code === 'mta_sts_policy_not_a_policy'); assert.match(finding.message, /version: STSv1/); }); test('a policy that is missing max_age or mx is never reported as enforcing', () => { const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: policyOk('version: STSv1\nmode: enforce\n') }), { now: NOW }); assert.equal(result.state, 'warn'); assert.equal(codes(result).includes('mta_sts_mode_enforce'), false); const finding = result.findings.find((item) => item.code === 'mta_sts_policy_malformed'); assert.match(finding.message, /missing a numeric max_age and an mx: line/); assert.match(finding.message, /even though it says mode=enforce/); }); test('a malformed _mta-sts TXT record is a warning, and no fetch is attempted', async () => { const dns = tableDns({ '_mta-sts.example.com': txt('v=STSv1x; id=abc') }); const request = routeRequest({}); const result = await checkMailTransport('example.com', { dns, request, now: NOW }); assert.equal(result.state, 'warn'); assert.ok(codes(result).includes('mta_sts_txt_malformed')); assert.equal(request.calls.length, 0); }); test('an unreadable _mta-sts record says unknown, never "none is published"', () => { const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: false, code: 'ESERVFAIL', present: false, valid: false }, tlsRpt: { checked: true, present: true, valid: true, rua: ['mailto:tls@example.com'] } }), { now: NOW }); assert.equal(result.state, 'not-evaluated'); assert.ok(codes(result).includes('mta_sts_not_checked')); assert.doesNotMatch(result.summary, /No MTA-STS/); assert.match(result.summary, /unknown rather than missing/); assert.match(result.summary, /\(ESERVFAIL\)/); }); test('a summary never mentions BIMI when no BIMI record exists', () => { const result = analyseMailTransport(gatheredFixture({ tlsRpt: { checked: true, present: true, valid: true, rua: ['mailto:tls@example.com'] }, dmarc: { checked: true, record: 'v=DMARC1; p=none;', parsed: { valid: true, p: 'none', pct: 100 } } }), { now: NOW }); assert.doesNotMatch(result.summary, /BIMI/); assert.match(result.summary, /publishes TLS-RPT but no MTA-STS record/); }); // --------------------------------------------------------------------------- // not-evaluated: a blocked fetch is not a finding // --------------------------------------------------------------------------- test('a policy fetch that times out is not-evaluated with the reason code, and no warn/fail', () => { const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: { attempted: true, ok: false, kind: 'not-read', code: 'timeout', url: POLICY_URL } }), { now: NOW }); assert.equal(result.state, 'not-evaluated'); assert.equal(result.findings.some((finding) => finding.level === 'warn' || finding.level === 'fail'), false); const finding = result.findings.find((item) => item.code === 'mta_sts_policy_not_read'); assert.equal(finding.level, 'info'); assert.match(finding.message, /timeout/); assert.match(result.summary, /not a verdict/); assert.match(result.summary, /could not be read from here \(timeout\)/); }); test('a policy fetch refused by a firewall (403) is not-evaluated, never a broken promise', () => { const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: { attempted: true, ok: false, kind: 'not-read', code: 'http_403', status: 403, url: POLICY_URL } }), { now: NOW }); assert.equal(result.state, 'not-evaluated'); assert.equal(codes(result).includes('mta_sts_policy_missing'), false); assert.ok(codes(result).includes('mta_sts_policy_not_read')); assert.equal(result.evidence.find((item) => item.label === 'MTA-STS policy').value, 'not read (http_403)'); }); test('not-evaluated wins over a warning from another record', () => { const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: { attempted: true, ok: false, kind: 'not-read', code: 'timeout', url: POLICY_URL }, tlsRpt: { checked: true, present: true, valid: false, rua: [], record: 'v=TLSRPTv1;' } }), { now: NOW }); assert.equal(result.state, 'not-evaluated'); assert.ok(codes(result).includes('tls_rpt_no_rua')); }); test('a SERVFAIL on every record is not-evaluated with the code in the sentence', () => { const result = analyseMailTransport(gatheredFixture({ mtaSts: { checked: false, code: 'ESERVFAIL', present: false, valid: false }, tlsRpt: { checked: false, code: 'ESERVFAIL', present: false, valid: false }, bimi: { checked: false, code: 'ESERVFAIL', present: false, valid: false }, fatal: { code: 'ESERVFAIL', transient: true } }), { now: NOW }); assert.equal(result.state, 'not-evaluated'); assert.deepEqual(codes(result), ['dns_unavailable']); assert.equal(levels(result)[0], 'info'); assert.match(result.summary, /\(ESERVFAIL\)/); assert.match(result.findings[0].message, /not a finding against the domain/); }); test('a resolver that refuses is not a statement about the domain', async () => { const dns = tableDns({}, { fail: { '_mta-sts.example.com': 'EREFUSED', '_smtp._tls.example.com': 'EREFUSED', 'default._bimi.example.com': 'EREFUSED' } }); const result = await checkMailTransport('example.com', { dns, request: routeRequest({}), now: NOW }); assert.equal(result.state, 'not-evaluated'); assert.match(result.summary, /EREFUSED/); assert.equal(result.findings.every((finding) => finding.level === 'info'), true); }); test('a policy host that does not resolve is a broken promise, not a blocked fetch', async () => { const dns = tableDns({ '_mta-sts.example.com': txt('v=STSv1; id=abc') }); const request = routeRequest({ [POLICY_URL]: new NetGuardError('dns_nxdomain', 'That domain did not resolve.') }); const result = await checkMailTransport('example.com', { dns, request, now: NOW }); assert.equal(result.state, 'warn'); assert.ok(codes(result).includes('mta_sts_policy_missing')); assert.match(result.summary, /does not resolve/); // One attempt, never a retry: the budget is one request. assert.equal(request.calls.length, 1); }); test('a policy fetch that times out is not-evaluated end to end, and is not retried', async () => { const dns = tableDns({ '_mta-sts.example.com': txt('v=STSv1; id=abc') }); const request = routeRequest({ [POLICY_URL]: new NetGuardError('timeout', 'That took too long.') }); const result = await checkMailTransport('example.com', { dns, request, now: NOW }); assert.equal(result.state, 'not-evaluated'); assert.ok(codes(result).includes('mta_sts_policy_not_read')); assert.equal(codes(result).includes('mta_sts_policy_missing'), false); assert.equal(request.calls.length, 1); }); test('a policy URL that redirects is a broken promise: the policy must be served in place', async () => { const dns = tableDns({ '_mta-sts.example.com': txt('v=STSv1; id=abc') }); const request = routeRequest({ [POLICY_URL]: { status: 301, headers: { location: 'https://example.com/policy.txt' }, body: '' } }); const result = await checkMailTransport('example.com', { dns, request, now: NOW }); assert.equal(result.state, 'warn'); assert.ok(codes(result).includes('mta_sts_policy_missing')); assert.match(result.summary, /redirected \(HTTP 301\)/); // The redirect is not followed: one request, ever. assert.equal(request.calls.length, 1); }); // --------------------------------------------------------------------------- // TLS-RPT and BIMI // --------------------------------------------------------------------------- test('TLS-RPT publishes a reporting address', () => { const result = analyseMailTransport(gatheredFixture({ tlsRpt: { checked: true, present: true, valid: true, rua: ['mailto:tls@example.com'], record: 'v=TLSRPTv1; rua=mailto:tls@example.com' } }), { now: NOW }); assert.equal(result.state, 'ok'); const finding = result.findings.find((item) => item.code === 'tls_rpt_published'); assert.equal(finding.level, 'ok'); assert.match(finding.message, /mailto:tls@example\.com/); }); test('TLS-RPT with no rua address has nowhere to send reports', () => { const result = analyseMailTransport(gatheredFixture({ tlsRpt: { checked: true, present: true, valid: false, rua: [], record: 'v=TLSRPTv1;' } }), { now: NOW }); assert.equal(result.state, 'warn'); assert.ok(codes(result).includes('tls_rpt_no_rua')); }); test('BIMI with a logo but DMARC p=none is reported as ignored, not as configured', () => { const result = analyseMailTransport(gatheredFixture({ bimi: { checked: true, present: true, valid: true, location: 'https://example.com/logo.svg', authority: null, record: 'v=BIMI1; l=https://example.com/logo.svg' }, dmarc: { checked: true, record: 'v=DMARC1; p=none;', parsed: { valid: true, p: 'none', pct: 100 } } }), { now: NOW }); const finding = result.findings.find((item) => item.code === 'bimi_ignored_dmarc_weak'); assert.equal(finding.level, 'info'); assert.match(finding.message, /ignore BIMI unless the DMARC policy is quarantine or reject/); assert.match(finding.message, /p=none/); assert.equal(codes(result).includes('bimi_published'), false); assert.doesNotMatch(JSON.stringify(result.findings), /BIMI is configured|logo will show/i); }); test('BIMI counts when DMARC is enforced, and a missing VMC is named', () => { const result = analyseMailTransport(gatheredFixture({ bimi: { checked: true, present: true, valid: true, location: 'https://example.com/logo.svg', authority: null, record: 'v=BIMI1; l=https://example.com/logo.svg' }, dmarc: { checked: true, record: 'v=DMARC1; p=reject;', parsed: { valid: true, p: 'reject', pct: 100 } } }), { now: NOW }); assert.equal(result.state, 'ok'); assert.ok(codes(result).includes('bimi_published')); const vmc = result.findings.find((item) => item.code === 'bimi_no_vmc'); assert.equal(vmc.level, 'info'); assert.match(vmc.message, /Gmail and Yahoo/); }); test('BIMI with pct<100 is not enforcement', () => { const result = analyseMailTransport(gatheredFixture({ bimi: { checked: true, present: true, valid: true, location: 'https://example.com/logo.svg', authority: 'https://example.com/vmc.pem' }, dmarc: { checked: true, record: 'v=DMARC1; p=quarantine; pct=50;', parsed: { valid: true, p: 'quarantine', pct: 50 } } }), { now: NOW }); assert.ok(codes(result).includes('bimi_ignored_dmarc_weak')); assert.equal(codes(result).includes('bimi_published'), false); }); test('BIMI with an unreadable DMARC record says unknown rather than judging it', () => { const result = analyseMailTransport(gatheredFixture({ bimi: { checked: true, present: true, valid: true, location: 'https://example.com/logo.svg', authority: null }, dmarc: { checked: false, code: 'ETIMEOUT', record: null, parsed: null } }), { now: NOW }); assert.ok(codes(result).includes('bimi_dmarc_unknown')); assert.equal(codes(result).includes('bimi_ignored_dmarc_weak'), false); }); // --------------------------------------------------------------------------- // envelope + wording // --------------------------------------------------------------------------- test('the envelope is exactly the agreed shape, with an ISO checkedAt', () => { const result = analyseMailTransport(gatheredFixture(), { now: NOW }); assert.deepEqual(Object.keys(result), ['id', 'state', 'summary', 'evidence', 'findings', 'checkedAt', 'fromCache']); assert.equal(result.id, 'mail-transport'); assert.equal(result.checkedAt, ISO); assert.equal(result.fromCache, false); assert.equal(typeof result.checkedAt, 'string'); assert.ok(['ok', 'warn', 'fail', 'not-evaluated'].includes(result.state)); for (const item of result.evidence) { assert.equal(typeof item.label, 'string'); assert.equal(typeof item.value, 'string'); // `note` is optional, so when present it must be a real string — never note: undefined. if ('note' in item) assert.equal(typeof item.note, 'string'); assert.deepEqual(Object.keys(item).filter((key) => !['label', 'value', 'note'].includes(key)), []); } for (const finding of result.findings) { assert.ok(['ok', 'info', 'warn', 'fail'].includes(finding.level)); assert.equal(typeof finding.code, 'string'); assert.equal(typeof finding.message, 'string'); if ('fix' in finding) assert.equal(typeof finding.fix, 'string'); } }); test('no score, no delivery promise, no "your email is insecure"', () => { const fixtures = [ gatheredFixture(), gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: policyOk() }), gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: { attempted: true, ok: false, kind: 'missing', code: 'http_404', status: 404 } }), gatheredFixture({ bimi: { checked: true, present: true, valid: true, location: 'https://example.com/logo.svg' }, dmarc: { checked: true, record: 'v=DMARC1; p=none;', parsed: { valid: true, p: 'none', pct: 100 } } }) ]; for (const fixture of fixtures) { const text = JSON.stringify(analyseMailTransport(fixture, { now: NOW })); assert.doesNotMatch(text, /\bscore\b|\d+\s*\/\s*100|inbox placement|delivery rate|guarantee|insecure/i); } }); test('every summary is one sentence', () => { const fixtures = [ gatheredFixture(), gatheredFixture({ tlsRpt: { checked: true, present: true, valid: true, rua: ['mailto:t@example.com'] } }), gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: policyOk() }), gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: policyOk('version: STSv1\nmode: testing\nmax_age: 86400\nmx: mx.example.com\n') }), gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=abc', id: 'abc', count: 1 }, policy: { attempted: true, ok: false, kind: 'not-read', code: 'timeout' } }), gatheredFixture({ fatal: { code: 'ESERVFAIL' } }) ]; for (const fixture of fixtures) { const summary = analyseMailTransport(fixture, { now: NOW }).summary; const stops = summary.match(/[.!?](\s|$)/g) || []; assert.equal(stops.length, 1, `more than one sentence: ${summary}`); } }); test('worstLevel maps info to ok and keeps warn/fail', () => { assert.equal(worstLevel([]), 'ok'); assert.equal(worstLevel([{ level: 'info' }, { level: 'ok' }]), 'ok'); assert.equal(worstLevel([{ level: 'info' }, { level: 'warn' }]), 'warn'); assert.equal(worstLevel([{ level: 'warn' }, { level: 'fail' }]), 'fail'); }); // --------------------------------------------------------------------------- // check(): the request budget and the host role // --------------------------------------------------------------------------- test('the policy fetch is the only request, and only when a valid TXT exists', async () => { assert.equal(REQUEST_BUDGET, 1); assert.equal(POLICY_TIMEOUT_MS, 3000); assert.equal(POLICY_MAX_BYTES, 32 * 1024); assert.equal(policyUrl('example.com'), POLICY_URL); const withRecord = routeRequest({ [POLICY_URL]: { status: 200, body: POLICY_TEXT } }); await checkMailTransport('example.com', { dns: tableDns({ '_mta-sts.example.com': txt('v=STSv1; id=abc') }), request: withRecord, now: NOW }); assert.equal(withRecord.calls.length, 1); assert.equal(withRecord.calls[0].url, POLICY_URL); // The policy is not a page: an explicit Accept is passed, with the caps the module promises. assert.match(withRecord.calls[0].options.accept, /^text\/plain/); assert.equal(withRecord.calls[0].options.timeoutMs, POLICY_TIMEOUT_MS); assert.equal(withRecord.calls[0].options.maxBytes, POLICY_MAX_BYTES); assert.equal(withRecord.calls[0].options.method, 'GET'); const withoutRecord = routeRequest({}); await checkMailTransport('example.com', { dns: tableDns({}), request: withoutRecord, now: NOW }); assert.equal(withoutRecord.calls.length, 0); }); test('the DNS seam is asked for the four names, and nothing else', async () => { const dns = tableDns({}); await checkMailTransport('example.com', { dns, request: routeRequest({}), now: NOW }); assert.deepEqual(dns.resolveTxt.calls.sort(), [ '_dmarc.example.com', '_mta-sts.example.com', '_smtp._tls.example.com', 'default._bimi.example.com' ].sort()); assert.equal(MTA_STS_NAME, '_mta-sts'); assert.equal(TLS_RPT_NAME, '_smtp._tls'); assert.equal(BIMI_NAME, 'default._bimi'); }); test('a full read through check() produces mode=enforce from the served file', async () => { const dns = tableDns({ '_mta-sts.example.com': txt('v=STSv1; id=abc'), '_smtp._tls.example.com': txt('v=TLSRPTv1; rua=mailto:tls@example.com'), 'default._bimi.example.com': txt('v=BIMI1; l=https://example.com/logo.svg'), '_dmarc.example.com': txt('v=DMARC1; p=reject; rua=mailto:d@example.com') }); const request = routeRequest({ [POLICY_URL]: { status: 200, body: POLICY_TEXT } }); const result = await checkMailTransport('example.com', { dns, request, now: NOW }); assert.equal(result.state, 'ok'); assert.equal(result.checkedAt, ISO); assert.deepEqual(codes(result).sort(), ['bimi_no_vmc', 'bimi_published', 'mta_sts_mode_enforce', 'tls_rpt_published']); assert.equal(request.calls.length, 1); }); test('a 403 on the policy file leaves the card not-evaluated, and other records still report', async () => { const dns = tableDns({ '_mta-sts.example.com': txt('v=STSv1; id=abc'), '_smtp._tls.example.com': txt('v=TLSRPTv1; rua=mailto:tls@example.com') }); const request = routeRequest({ [POLICY_URL]: { status: 403, body: 'Forbidden' } }); const result = await checkMailTransport('example.com', { dns, request, now: NOW }); assert.equal(result.state, 'not-evaluated'); assert.ok(codes(result).includes('mta_sts_policy_not_read')); assert.ok(codes(result).includes('tls_rpt_published')); }); test('a subdomain that publishes nothing is a website, not a broken domain', async () => { const dns = tableDns({}); const request = routeRequest({}); const result = await checkMailTransport('tools.example.com', { dns, request, now: NOW }); assert.equal(result.state, 'ok'); assert.deepEqual(codes(result), ['subdomain_no_mail']); assert.equal(result.findings[0].level, 'info'); assert.match(result.summary, /example\.com/); assert.match(result.findings[0].message, /normal for a subdomain/); assert.equal(request.calls.length, 0); }); test('a subdomain that does publish a policy is judged in place', async () => { const dns = tableDns({ '_mta-sts.shop.example.com': txt('v=STSv1; id=abc') }); const request = routeRequest({ 'https://mta-sts.shop.example.com/.well-known/mta-sts.txt': { status: 200, body: POLICY_TEXT } }); const result = await checkMailTransport('shop.example.com', { dns, request, now: NOW }); assert.equal(result.state, 'ok'); assert.ok(codes(result).includes('mta_sts_mode_enforce')); assert.equal(codes(result).includes('subdomain_no_mail'), false); assert.equal(request.calls[0].url, 'https://mta-sts.shop.example.com/.well-known/mta-sts.txt'); }); test('the registrable apex keeps .com.my and .co.uk second levels intact', () => { assert.equal(registrableApex('tools.hoelee.com'), 'hoelee.com'); assert.equal(registrableApex('shop.example.com.my'), 'example.com.my'); assert.equal(registrableApex('mail.example.co.uk'), 'example.co.uk'); assert.equal(registrableApex('example.com'), 'example.com'); }); test('an injected resolver object works through the lookup alias too', async () => { const dns = tableDns({ '_mta-sts.example.com': txt('v=STSv1; id=abc') }); const request = routeRequest({ [POLICY_URL]: { status: 200, body: POLICY_TEXT } }); const result = await checkMailTransport('example.com', { lookup: dns, request, now: NOW }); assert.ok(codes(result).includes('mta_sts_mode_enforce')); }); test('bad input fails without touching DNS', async () => { const dns = tableDns({}); const result = await checkMailTransport('not a domain', { dns, request: routeRequest({}), now: NOW }); assert.equal(result.state, 'fail'); assert.deepEqual(codes(result), ['bad_input']); assert.equal(dns.resolveTxt.calls.length, 0); }); test('buildSummary has a sentence for the enforce case that names the file, not the TXT', () => { const summary = buildSummary({ domain: 'example.com', mtaSts: { present: true, valid: true }, policy: { ok: true, kind: 'ok', parsed: { mode: 'enforce' } }, tlsRpt: { present: false }, bimi: { present: false }, dmarcEnforced: false, fatal: null }); assert.match(summary, /policy file says mode=enforce/); assert.doesNotMatch(summary, /TXT record (protects|is enforcing)/); }); test('a policy file cut off at the size limit is not judged, because half a policy reads like a whole one', () => { // The header lines arrive before the mx: list, so a truncated file still parses. Claiming // mode=enforce from it would promise protection the sender does not get. const result = analyseMailTransport( gatheredFixture({ mtaSts: { checked: true, present: true, valid: true, record: 'v=STSv1; id=20261110T000000', id: '20261110T000000', count: 1 }, policy: { ...policyOk(), truncated: true } }), { now: NOW } ); assert.ok(codes(result).includes('mta_sts_policy_truncated')); assert.ok(!codes(result).includes('mta_sts_mode_enforce')); assert.equal(result.findings.find((finding) => finding.code === 'mta_sts_policy_truncated').level, 'info'); assert.equal(result.state, 'not-evaluated'); });