Security: a valid DMARC or SPF of the domain's own isn't missing (#236) #237

Merged
jcoffey-dev merged 1 commits from fix/236-dns-different-not-missing into main 2026-11-18 16:04:38 +00:00
6 changed files with 184 additions and 32 deletions

No files matched your search

+18 -3
View File
@@ -66,12 +66,27 @@ async function lookup(name: string, type: string, signal?: AbortSignal): Promise
* and `missing` when there is nothing.
*/
export async function checkRecords(records: ZoneRecord[], signal?: AbortSignal): Promise<Map<ZoneRecord, LiveState>> {
const detailed = await checkRecordValues(records, signal);
return new Map([...detailed].map(([r, v]) => [r, v.state]));
}
/** A record's state, with what that name and type has published instead (normalized), for a caller that judges it. */
export interface LiveResult {
state: LiveState;
found: string[];
}
/** checkRecords, keeping the values found, so a `different` record can be told apart from a broken one (#236). */
export async function checkRecordValues(
records: ZoneRecord[],
signal?: AbortSignal,
): Promise<Map<ZoneRecord, LiveResult>> {
const groups = new Map<string, ZoneRecord[]>();
for (const r of records) {
const key = `${r.name.toLowerCase()}|${r.type}`;
groups.set(key, [...(groups.get(key) ?? []), r]);
}
const out = new Map<ZoneRecord, LiveState>();
const out = new Map<ZoneRecord, LiveResult>();
await Promise.all(
[...groups.values()].map(async (group) => {
const { name, type } = group[0];
@@ -79,10 +94,10 @@ export async function checkRecords(records: ZoneRecord[], signal?: AbortSignal):
const found = (await lookup(name, type, signal)).map((d) => normalizeValue(type, d));
for (const r of group) {
const want = normalizeValue(type, r.value);
out.set(r, found.includes(want) ? 'live' : found.length > 0 ? 'different' : 'missing');
out.set(r, { state: found.includes(want) ? 'live' : found.length > 0 ? 'different' : 'missing', found });
}
} catch {
for (const r of group) out.set(r, 'error');
for (const r of group) out.set(r, { state: 'error', found: [] });
}
}),
);
+47
View File
@@ -0,0 +1,47 @@
/*
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { describe, expect, it } from 'vitest';
import { coreState } from './facts';
const different = (...found: string[]) => ({ state: 'different' as const, found });
describe('coreState (#236)', () => {
it('a DMARC record of the domain’s own, with other report addresses and tags, is valid, not missing', () => {
expect(
coreState('dmarc', [
different('v=DMARC1; p=reject; rua=mailto:[email protected]; ruf=mailto:[email protected]; fo=1'),
]),
).toBe('valid');
});
it('a DMARC record without a policy is different', () => {
expect(coreState('dmarc', [different('v=DMARC1; rua=mailto:[email protected]')])).toBe('different');
});
it('an SPF with an extra include is valid, and other TXT records at the apex don’t count', () => {
expect(
coreState('spf', [different('google-site-verification=abc', 'v=spf1 mx include:_spf.example.net -all')]),
).toBe('valid');
});
it('two SPF records are different, and TXT records that aren’t SPF are missing', () => {
expect(coreState('spf', [different('v=spf1 mx -all', 'v=spf1 a ~all')])).toBe('different');
expect(coreState('spf', [different('google-site-verification=abc')])).toBe('missing');
});
it('an MX or DKIM that differs stays different; a failed lookup is an error, not missing', () => {
expect(coreState('mx', [different('10 mx.elsewhere.example')])).toBe('different');
expect(
coreState('dkim', [
{ state: 'live', found: [] },
{ state: 'missing', found: [] },
]),
).toBe('missing');
expect(coreState('dmarc', [{ state: 'error', found: [] }])).toBe('error');
expect(coreState('mx', [{ state: 'live', found: [] }])).toBe('live');
});
});
+50 -9
View File
@@ -10,7 +10,7 @@
*/
import { getAccountId, jmapRequest } from '@/services/jmap/client';
import { parseZone, type RecordKind, type ZoneRecord } from '@/features/dns/zone';
import { checkRecords } from '@/features/dns/liveCheck';
import { checkRecordValues, type LiveResult } from '@/features/dns/liveCheck';
import i18n from '@/i18n';
import { useAccountStore } from '@/stores/accountStore';
import { fetchReports, fetchSettings } from '@/features/deliverability/api';
@@ -25,7 +25,7 @@ export interface DomainFacts {
dkim: string;
certs: string;
/** The core records, and which are live in public DNS. */
checks: { kind: RecordKind; live: boolean }[];
checks: CoreCheck[];
/** inbuxa: DL-19, the domain's worst deliverability failure, if any. */
deliverability?: string;
}
@@ -53,15 +53,56 @@ function mode(value: unknown): string {
return ((value as { '@type'?: string } | undefined)?.['@type'] ?? 'Manual').toString();
}
/** Of the domain's core records, which are live. Kinds with no records (DKIM before keys exist) are left out. */
export async function coreChecks(zone: ZoneRecord[], domain: string): Promise<{ kind: RecordKind; live: boolean }[]> {
/**
* Where a core record stands in public DNS:
* - `live`: exactly what the server suggests is published;
* - `valid`: a DMARC or SPF record of the domain's own is published instead, and it is well formed (#236);
* - `different`: something else is published (MX elsewhere, another DKIM key, two SPF records);
* - `missing`: nothing is;
* - `error`: public DNS couldn't be asked.
*/
export type CoreState = 'live' | 'valid' | 'different' | 'missing' | 'error';
export interface CoreCheck {
kind: RecordKind;
state: CoreState;
/** Published and usable: `live` or `valid`. */
live: boolean;
}
const DMARC_RECORD = /^v=DMARC1\s*(;|$)/i;
const DMARC_POLICY = /;\s*p\s*=\s*(none|quarantine|reject)\s*(;|$)/i;
const SPF_RECORD = /^v=spf1(\s|$)/i;
/** One kind's state, from each of its suggested records' results. */
export function coreState(kind: RecordKind, results: LiveResult[]): CoreState {
if (results.some((r) => r.state === 'error')) return 'error';
if (results.every((r) => r.state === 'live')) return 'live';
if (kind === 'dmarc' || kind === 'spf') {
// The name may hold other TXT records (site verifications); only the ones of this kind count.
const own = [...new Set(results.flatMap((r) => r.found))].filter((v) =>
(kind === 'dmarc' ? DMARC_RECORD : SPF_RECORD).test(v),
);
if (own.length === 0) return 'missing';
// More than one is a permanent error for receivers, so only a single well-formed one passes.
if (own.length === 1 && (kind === 'spf' || DMARC_POLICY.test(own[0]))) return 'valid';
return 'different';
}
return results.some((r) => r.state === 'missing') ? 'missing' : 'different';
}
/** Of the domain's core records, where each kind stands. Kinds with no records (DKIM before keys exist) are left out. */
export async function coreChecks(zone: ZoneRecord[], domain: string): Promise<CoreCheck[]> {
const apex = domain.toLowerCase();
const core = zone.filter((r) => CORE_KINDS.includes(r.kind) && (r.kind !== 'spf' || r.name.toLowerCase() === apex));
const states = await checkRecords(core);
return CORE_KINDS.filter((k) => core.some((r) => r.kind === k)).map((kind) => ({
kind,
live: core.filter((r) => r.kind === kind).every((r) => states.get(r) === 'live'),
}));
const results = await checkRecordValues(core);
return CORE_KINDS.filter((k) => core.some((r) => r.kind === k)).map((kind) => {
const state = coreState(
kind,
core.filter((r) => r.kind === kind).map((r) => results.get(r) ?? { state: 'error', found: [] }),
);
return { kind, state, live: state === 'live' || state === 'valid' };
});
}
/** DL-19: the worst failing deliverability finding for a domain, for whoever may read the reports. */
+16
View File
@@ -147,6 +147,22 @@ describe('the checks (acceptance tests 1-8)', () => {
expect(dns[0].title).toContain('DKIM');
});
it('7b: a record that doesn’t match is named as such, not as missing (#236)', () => {
const domain = { id: 'd1', name: 'example.org', enabled: true, allowRelaying: false, dmarcPolicy: 'reject' };
const one = (missing: string[], different: string[]) =>
evaluate({ ...fresh(), domains: [{ ...domain, missing, different }] }, defaults).items.filter(
(i) => i.check === 'SS-13',
);
expect(one([], [])).toHaveLength(0);
const mx = one([], ['mx']);
expect(mx[0].title).toBe('example.org’s MX in public DNS doesn’t match this server');
expect(mx[0].why).toContain('somewhere else');
expect(mx[0].value).toEqual(['mx:different']);
const both = one(['dkim'], ['mx']);
expect(both[0].title).toBe('example.org is missing DKIM in public DNS, and its MX doesn’t match this server');
expect(one(['dkim'], [])[0].value).toEqual(['dkim']);
});
it('8: legacy protocols on point to the switch, with no Fix', () => {
const item = evaluate(fresh(), defaults).items.find((i) => i.check === 'SS-15');
expect(item?.action).toEqual({ kind: 'hardening' });
+43 -10
View File
@@ -82,6 +82,8 @@ export interface DomainSnapshot {
allowRelaying: boolean;
/** Core records missing from public DNS (SS-13), or null if not checked. */
missing: string[] | null;
/** Core records published with something else than the server's (an MX elsewhere, another DKIM key, a malformed SPF or DMARC), or null if not checked. A valid DMARC or SPF of the domain's own is neither (#236). */
different?: string[] | null;
/** The live DMARC policy (SS-18), or null if there's none or it wasn't checked. */
dmarcPolicy: string | null;
}
@@ -550,16 +552,21 @@ export function evaluate(s: Snapshot, defaults: Defaults): Result {
check('SS-13', 'Mail records in DNS', ['domains', 'dns'], () =>
(s.domains ?? [])
.filter((d) => d.enabled && d.missing && d.missing.length > 0)
.map((d) => ({
check: 'SS-13',
severity: 'important' as const,
subject: d.name,
value: [...(d.missing ?? [])].sort(),
title: `${d.name} is missing ${listWords((d.missing ?? []).map((k) => k.toUpperCase()))} in public DNS`,
why: 'Without them, other servers can’t find where to deliver, or can’t tell real mail from forged mail, so more of it lands in spam.',
action: { kind: 'review' as const, viewName: 'x:Domain', id: d.id },
})),
.filter((d) => d.enabled && ((d.missing?.length ?? 0) > 0 || (d.different?.length ?? 0) > 0))
.map((d) => {
const missing = [...(d.missing ?? [])].sort();
const different = [...(d.different ?? [])].sort();
return {
check: 'SS-13',
severity: 'important' as const,
subject: d.name,
// Missing kinds alone, as before, so an earlier acceptance still holds.
value: [...missing, ...different.map((k) => `${k}:different`)],
title: recordsTitle(d.name, missing, different),
why: recordsWhy(missing, different),
action: { kind: 'review' as const, viewName: 'x:Domain', id: d.id },
};
}),
);
check('SS-14', 'Certificates expiring within 30 days', ['certificates'], certItems(30 * DAY, 'important', 'SS-14'));
@@ -670,6 +677,32 @@ export function evaluate(s: Snapshot, defaults: Defaults): Result {
return { items: sortItems(items), passed };
}
const upper = (kinds: string[]) => listWords(kinds.map((k) => k.toUpperCase()));
/** SS-13's title: what is missing, what doesn't match, or both. */
function recordsTitle(name: string, missing: string[], different: string[]): string {
const match = `${different.length > 1 ? 'don’t' : 'doesn’t'} match this server`;
if (different.length === 0) return `${name} is missing ${upper(missing)} in public DNS`;
if (missing.length === 0) return `${name}’s ${upper(different)} in public DNS ${match}`;
return `${name} is missing ${upper(missing)} in public DNS, and its ${upper(different)} ${match}`;
}
const DIFFERENT_WHY: Record<string, string> = {
mx: 'Its MX sends other servers’ mail for it somewhere else.',
dkim: 'The published DKIM key isn’t the one the server signs with, so its signed mail fails DKIM.',
spf: 'Its SPF isn’t one valid record, so receivers treat it as an error.',
dmarc: 'Its DMARC record isn’t valid, so receivers ignore it.',
};
function recordsWhy(missing: string[], different: string[]): string {
const parts = missing.length
? [
'Without them, other servers can’t find where to deliver, or can’t tell real mail from forged mail, so more of it lands in spam.',
]
: [];
return [...parts, ...different.map((k) => DIFFERENT_WHY[k] ?? '')].filter(Boolean).join(' ');
}
function listWords(parts: string[]): string {
if (parts.length <= 1) return parts.join('');
return `${parts.slice(0, -1).join(', ')} and ${parts[parts.length - 1]}`;
+10 -10
View File
@@ -95,14 +95,13 @@ export function dmarcPolicyOf(txt: string[]): string | null {
return p ? p[1].toLowerCase() : null;
}
type DnsFacts = Pick<DomainSnapshot, 'missing' | 'different' | 'dmarcPolicy'>;
const DNS_TTL_MS = 60_000;
const dnsCache = new Map<string, { at: number; result: Promise<Pick<DomainSnapshot, 'missing' | 'dmarcPolicy'>> }>();
const dnsCache = new Map<string, { at: number; result: Promise<DnsFacts> }>();
/** A domain's DNS, kept for a minute as the hover cards keep theirs (SS-28), so a Fix doesn't ask again. */
function domainDns(
name: string,
zoneFile: string | undefined,
): Promise<Pick<DomainSnapshot, 'missing' | 'dmarcPolicy'>> {
function domainDns(name: string, zoneFile: string | undefined): Promise<DnsFacts> {
const key = `${name}\n${zoneFile ?? ''}`;
const hit = dnsCache.get(key);
if (hit && Date.now() - hit.at < DNS_TTL_MS) return hit.result;
@@ -112,13 +111,13 @@ function domainDns(
return result;
}
async function askDns(
name: string,
zoneFile: string | undefined,
): Promise<Pick<DomainSnapshot, 'missing' | 'dmarcPolicy'>> {
async function askDns(name: string, zoneFile: string | undefined): Promise<DnsFacts> {
const [checks, dmarc] = await Promise.all([coreChecks(parseZone(zoneFile), name), dohQuery(`_dmarc.${name}`, 'TXT')]);
// Every record's lookup failed: not checked, rather than passed (#236).
if (checks.length > 0 && checks.every((c) => c.state === 'error')) throw new Error('public DNS couldn’t be asked');
return {
missing: checks.filter((c) => !c.live).map((c) => c.kind),
missing: checks.filter((c) => c.state === 'missing').map((c) => c.kind),
different: checks.filter((c) => c.state === 'different').map((c) => c.kind),
dmarcPolicy: dmarcPolicyOf(dmarc.answer.filter((a) => a.type === 16).map((a) => a.data)),
};
}
@@ -214,6 +213,7 @@ export async function loadSnapshot(signal?: AbortSignal, critical = false): Prom
enabled,
allowRelaying: d.allowRelaying === true,
missing: dns?.missing ?? null,
different: dns?.different ?? null,
dmarcPolicy: dns?.dmarcPolicy ?? null,
};
}),