diff --git a/src/checks/mail-auth.mjs b/src/checks/mail-auth.mjs index add95c8..31ab567 100644 --- a/src/checks/mail-auth.mjs +++ b/src/checks/mail-auth.mjs @@ -871,7 +871,55 @@ export async function check(input, { dns: resolver, lookup, now = Date.now(), ca void cache; // caching is the router's job; this module always reports fromCache: false const dnsClient = pickResolver(resolver || lookup); const gathered = await gather(host, dnsClient); - return analyseMailAuth(gathered, { now }); + return applyHostRole(analyseMailAuth(gathered, { now }), host, gathered); +} + +// --------------------------------------------------------------------------- +// Host role: a subdomain with no mail setup is normal, not a broken domain +// --------------------------------------------------------------------------- + +const SECOND_LEVEL = new Set(['com', 'net', 'org', 'edu', 'gov', 'mil', 'name', 'biz', 'sch', 'co', 'ac', 'or', 'ne', 'go', 'web']); + +/** The registrable domain: tools.hoelee.com → hoelee.com, shop.example.com.my → example.com.my. */ +export function registrableApex(host) { + const labels = String(host).split('.'); + if (labels.length <= 2) return host; + const tld = labels[labels.length - 1]; + const sld = labels[labels.length - 2]; + if (tld.length === 2 && SECOND_LEVEL.has(sld)) return labels.slice(-3).join('.'); + return labels.slice(-2).join('.'); +} + +/** + * tools.hoelee.com has no MX and no SPF because it only serves a website — the mail comes from + * hoelee.com. Reporting that as "anyone can spoof this domain" is a false accusation against a + * perfectly ordinary setup, so a subdomain with no mail surface at all reads as OK with a pointer + * to the domain that actually sends the mail. + */ +export function applyHostRole(result, host, gathered) { + const apex = registrableApex(host); + if (apex === host) return result; + if (gathered?.fatal) return result; + const publishesNothing = + gathered.spfRecords.length === 0 && + !gathered.mx.nullMx && + gathered.mx.records.length === 0 && + !gathered.dmarc.record && + !gathered.dkim.some((entry) => entry.key); + if (!publishesNothing) return result; + return { + ...result, + state: 'ok', + summary: `This host does not send or receive email of its own. Email for ${apex} is what matters — check ${apex}.`, + findings: [ + { + level: 'info', + code: 'subdomain_no_mail', + message: `${host} publishes no mail servers and no email records, which is normal for a subdomain that only hosts a website or an app.`, + fix: `Check ${apex} for SPF, DKIM and DMARC — that is the domain your mail is actually sent from.` + } + ] + }; } export const checkMailAuth = check; diff --git a/src/lib/net-guard.mjs b/src/lib/net-guard.mjs index 26114c1..4c5b77c 100644 --- a/src/lib/net-guard.mjs +++ b/src/lib/net-guard.mjs @@ -242,7 +242,9 @@ export async function resolvePublic(host, { lookup, timeoutMs = 4000 } = {}) { throw new NetGuardError('private_address', 'That domain points at a private address.'); } } - return addresses; + // Prefer IPv4. On a home connection with a broken or slow IPv6 route, dialling the AAAA answer + // first turns a 40 ms check into a timeout, and the visitor reads "skipped" when the site is fine. + return [...addresses].sort((a, b) => (a.family === 4 ? 0 : 1) - (b.family === 4 ? 0 : 1)); } function normaliseAnswers(answers) { diff --git a/test/host-role.test.mjs b/test/host-role.test.mjs new file mode 100644 index 0000000..99fe202 --- /dev/null +++ b/test/host-role.test.mjs @@ -0,0 +1,67 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { registrableApex, applyHostRole, analyseMailAuth } from '../src/checks/mail-auth.mjs'; +import { resolvePublic } from '../src/lib/net-guard.mjs'; + +test('registrableApex keeps a Malaysian second-level suffix intact', () => { + assert.equal(registrableApex('example.com.my'), 'example.com.my'); + assert.equal(registrableApex('www.example.com.my'), 'example.com.my'); + assert.equal(registrableApex('tools.hoelee.com'), 'hoelee.com'); + assert.equal(registrableApex('hoelee.com'), 'hoelee.com'); + assert.equal(registrableApex('a.b.example.org.my'), 'example.org.my'); + assert.equal(registrableApex('localhost'), 'localhost'); +}); + +const NOTHING = { + domain: 'tools.example.com', + fatal: null, + spfRecords: [], + dmarc: { checked: true, code: null, record: null, parsed: null }, + dkim: [{ selector: 'default', status: 'none', key: false }], + mx: { checked: true, code: null, records: [], nullMx: false, addresses: [] } +}; + +function envelopeOf(state, findings) { + return { id: 'mail-auth', state, summary: 'x', evidence: [], findings, checkedAt: new Date(0).toISOString(), fromCache: false }; +} + +test('a subdomain that publishes no mail reads as OK, pointing at the domain that does', () => { + const analysed = analyseMailAuth(NOTHING, { now: 0 }); + assert.equal(analysed.state, 'fail', 'the raw analysis does accuse a bare subdomain'); + const fixed = applyHostRole(analysed, 'tools.example.com', NOTHING); + assert.equal(fixed.state, 'ok'); + assert.equal(fixed.findings.length, 1); + assert.equal(fixed.findings[0].code, 'subdomain_no_mail'); + assert.match(fixed.summary, /example\.com/); + assert.match(fixed.summary, /does not send or receive email/); +}); + +test('a subdomain that does publish SPF keeps its real verdict', () => { + const gathered = { ...NOTHING, spfRecords: ['v=spf1 include:_spf.example.com -all'] }; + const analysed = analyseMailAuth(gathered, { now: 0 }); + const out = applyHostRole(analysed, 'mail.example.com', gathered); + assert.equal(out.state, analysed.state); + assert.equal(out.findings[0].code === 'subdomain_no_mail', false); +}); + +test('the apex itself is never downgraded', () => { + const analysed = analyseMailAuth(NOTHING, { now: 0 }); + const out = applyHostRole(analysed, 'example.com', NOTHING); + assert.equal(out.state, 'fail'); +}); + +test('a null MX subdomain is a deliberate setup, not "publishes nothing"', () => { + const gathered = { ...NOTHING, mx: { checked: true, code: null, records: [], nullMx: true, addresses: [] } }; + const out = applyHostRole(analyseMailAuth(gathered, { now: 0 }), 'sub.example.com', gathered); + assert.equal(out.findings.some((f) => f.code === 'subdomain_no_mail'), false); +}); + +test('resolvePublic hands back IPv4 first so a broken IPv6 route cannot stall a check', async () => { + const lookup = async () => [ + { address: '2606:4700:4700::1111', family: 6 }, + { address: '93.184.216.34', family: 4 } + ]; + const out = await resolvePublic('example.com', { lookup }); + assert.equal(out[0].family, 4); + assert.equal(out[1].family, 6); +}); diff --git a/test/http.test.mjs b/test/http.test.mjs new file mode 100644 index 0000000..7f083d9 --- /dev/null +++ b/test/http.test.mjs @@ -0,0 +1,361 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; + +import { + analyseHttp, + checkHttp, + auditHeaders, + parseHead, + buildPreview, + detectBlock, + worstLevel, + BLOCKED_MESSAGE +} from '../src/checks/http.mjs'; +import { NetGuardError } from '../src/lib/net-guard.mjs'; + +// Fixtures are plain objects shaped exactly like net-guard's fetchFollowing() output. +// No server is started, no socket is opened, nothing is resolved. +const NOW = Date.UTC(2026, 0, 15, 12, 0, 0); + +const SECURE_HEADERS = { + 'strict-transport-security': 'max-age=31536000; includeSubDomains', + 'content-security-policy': "default-src 'self'; frame-ancestors 'none'", + 'x-content-type-options': 'nosniff', + 'x-frame-options': 'DENY', + 'referrer-policy': 'strict-origin-when-cross-origin', + 'permissions-policy': 'camera=(), microphone=()', + 'content-type': 'text/html; charset=utf-8' +}; + +const OG_BODY = `
+ + + + + + + +hello
`; + +function hop(status, url, location = null) { + return { url, status, location, totalMs: 40 }; +} + +function page(hops, { headers = {}, body = '' } = {}) { + const last = hops[hops.length - 1]; + return { + hops, + final: { status: last.status, headers, body, url: last.url, truncated: false, tls: { authorized: true }, timing: { totalMs: 90 } }, + body, + headers, + truncated: false + }; +} + +function codes(analysis) { + return analysis.findings.map((finding) => finding.code); +} + +function byCode(analysis, code) { + return analysis.findings.find((finding) => finding.code === code); +} + +// --- clean page ----------------------------------------------------------------------------- + +test('a clean https page with every header and an og:image is ok', () => { + const analysis = analyseHttp(page([hop(200, 'https://www.hoelee.com/check/')], { headers: SECURE_HEADERS, body: OG_BODY }), { + host: 'www.hoelee.com', + now: NOW + }); + assert.equal(analysis.state, 'ok'); + assert.deepEqual(analysis.findings.filter((finding) => finding.level !== 'ok'), []); + assert.equal(analysis.redirects.count, 0); + assert.equal(analysis.preview.title, 'Hoelee Check'); + assert.equal(analysis.preview.hasImage, true); + assert.equal(analysis.preview.imageIsAbsolute, true); +}); + +test('http → https is recorded as a win', () => { + const hops = [hop(301, 'http://example.com/', 'https://example.com/'), hop(200, 'https://example.com/')]; + const analysis = analyseHttp(page(hops, { headers: SECURE_HEADERS, body: OG_BODY }), { host: 'example.com', now: NOW }); + assert.equal(analysis.state, 'ok'); + assert.ok(codes(analysis).includes('http_upgraded')); + assert.equal(byCode(analysis, 'http_upgraded').level, 'ok'); + assert.equal(analysis.redirects.count, 1); + assert.equal(analysis.redirects.finalUrl, 'https://example.com/'); +}); + +test('a page served only over http is a failure', () => { + const analysis = analyseHttp(page([hop(200, 'http://example.com/')], { headers: {}, body: OG_BODY }), { host: 'example.com', now: NOW }); + assert.equal(analysis.state, 'fail'); + assert.ok(codes(analysis).includes('http_no_https')); +}); + +// --- redirect chain ------------------------------------------------------------------------- + +test('six requests is more hops than the limit and fails', () => { + const hops = [ + hop(301, 'https://example.com/a', 'https://example.com/b'), + hop(301, 'https://example.com/b', 'https://example.com/c'), + hop(301, 'https://example.com/c', 'https://example.com/d'), + hop(301, 'https://example.com/d', 'https://example.com/e'), + hop(301, 'https://example.com/e', 'https://example.com/f'), + hop(200, 'https://example.com/f') + ]; + const analysis = analyseHttp(page(hops, { headers: SECURE_HEADERS, body: OG_BODY }), { host: 'example.com', now: NOW }); + assert.equal(analysis.state, 'fail'); + assert.ok(codes(analysis).includes('http_too_many_hops')); + assert.equal(analysis.redirects.count, 5); +}); + +test('a redirect loop fails and names the address it loops on', () => { + const hops = [ + hop(301, 'https://example.com/a', 'https://example.com/b'), + hop(301, 'https://example.com/b', 'https://example.com/a') + ]; + const analysis = analyseHttp(page(hops, { headers: SECURE_HEADERS, body: OG_BODY }), { host: 'example.com', now: NOW }); + assert.equal(analysis.state, 'fail'); + const loop = byCode(analysis, 'http_redirect_loop'); + assert.ok(loop); + assert.match(loop.message, /loops back/i); +}); + +test('http → https → http ping-pong fails as a downgrade', () => { + const hops = [ + hop(301, 'http://example.com/', 'https://example.com/'), + hop(301, 'https://example.com/', 'http://example.com/old'), + hop(200, 'http://example.com/old') + ]; + const analysis = analyseHttp(page(hops, { headers: {}, body: OG_BODY }), { host: 'example.com', now: NOW }); + assert.equal(analysis.state, 'fail'); + assert.ok(codes(analysis).includes('http_downgrade')); + assert.ok(!codes(analysis).includes('http_upgraded')); +}); + +test('a 404 on the final hop fails with the real status code', () => { + const analysis = analyseHttp(page([hop(404, 'https://example.com/missing')], { headers: SECURE_HEADERS, body: '