// Stampwing — synthetic authentication diagnostics, reviewed 2026-11-09. // Run: node --test authentication-diagnostics.mjs (Node.js 22+). // Free to use, modify and redistribute, without warranty. No network or email. // This is a teaching model, not an SPF evaluator, DKIM verifier or DMARC engine. import assert from 'node:assert/strict'; import test from 'node:test'; // Each inner array is ONE TXT resource record, possibly split into strings. function spfRecordShape({ status, records }) { if (status !== 'NOERROR' && status !== 'NXDOMAIN') return 'lookup-error'; if (status === 'NXDOMAIN') return 'no-policy'; const policies = records.map((chunks) => chunks.join('')) .filter((record) => /^v=spf1(?: |$)/i.test(record)); return policies.length > 1 ? 'multiple-policies' : policies.length ? 'one-policy' : 'no-policy'; } const dnsCases = [ ['split strings', { status: 'NOERROR', records: [['v=spf1 ip4:192.0.2.1 ', '-all']] }, 'one-policy'], ['unrelated TXT', { status: 'NOERROR', records: [['site-verification=synthetic'], ['v=spf1 -all']] }, 'one-policy'], ['duplicate policies', { status: 'NOERROR', records: [['v=spf1 -all'], ['v=spf1 ~all']] }, 'multiple-policies'], ['empty answer', { status: 'NOERROR', records: [] }, 'no-policy'], ['missing name', { status: 'NXDOMAIN', records: [] }, 'no-policy'], ['resolver failure', { status: 'SERVFAIL', records: [] }, 'lookup-error'], ]; for (const [name, input, expected] of dnsCases) { test(`DNS: ${name}`, () => assert.equal(spfRecordShape(input), expected)); } // Alignment, policy discovery and signature results are supplied assumptions. // No suffix guessing, DNS Tree Walk or cryptographic verification occurs here. function explainAlignedPass({ trusted, policyResolved, spf, signatures }) { if (!trusted) return 'untrusted-report'; if (!policyResolved) return 'incomplete-evidence'; return [spf, ...signatures].some((result) => result.pass && result.aligned) ? 'aligned-pass' : 'no-aligned-pass'; } const good = { pass: true, aligned: true }; const unrelated = { pass: true, aligned: false }; const failed = { pass: false, aligned: true }; const authCases = [ ['forwarded; DKIM survives', failed, [good], true, true, 'aligned-pass'], ['SPF alone aligns', good, [failed], true, true, 'aligned-pass'], ['both pass for unrelated domains', unrelated, [unrelated], true, true, 'no-aligned-pass'], ['one of two signatures aligns', failed, [unrelated, good], true, true, 'aligned-pass'], ['modified signed content', failed, [failed], true, true, 'no-aligned-pass'], ['injected Authentication-Results', good, [good], false, true, 'untrusted-report'], ['policy lookup failed', good, [good], true, false, 'incomplete-evidence'], ]; for (const [name, spf, signatures, trusted, policyResolved, expected] of authCases) { test(`Receiver evidence: ${name}`, () => assert.equal( explainAlignedPass({ spf, signatures, trusted, policyResolved }), expected)); } // All passing tests mean the synthetic expectations hold, not that a real domain // authenticates. Consult RFC 7208 §§3.3/4.5, RFC 9989 §5.3, RFC 8601 §7.1.