SPF, DMARC and DNSSEC from a static page: DoH JSON, resolver fallback, Null MX and why we trust the AD bit
Our DNS records tool has no backend. The browser talks DNS-over-HTTPS directly to public resolvers.
Here's the implementation, plus the shortcuts we knowingly took. Snippets are lightly simplified from the production code.
Wire format vs JSON
RFC 8484 DoH speaks application/dns-message (binary DNS wire format). Both Cloudflare and Google also expose a non-standard JSON API (application/dns-json). For a dependency-free static page, JSON wins: no packet encoder, no name compression parser, just fetch.
async function ask(name, type) {
const urls = [
'https://cloudflare-dns.com/dns-query?name=' + encodeURIComponent(name) + '&type=' + type,
'https://dns.google/resolve?name=' + encodeURIComponent(name) + '&type=' + type,
];
for (const url of urls) {
try {
const ctrl = typeof AbortController === 'function' ? new AbortController() : null;
const timer = setTimeout(() => ctrl && ctrl.abort(), 7000);
const res = await fetch(url, { headers: { accept: 'application/dns-json' }, signal: ctrl?.signal });
clearTimeout(timer);
if (res.ok) return await res.json();
} catch (e) { /* fall through to next resolver */ }
}
return null; // both resolvers failed or timed out
}
Design notes:
- Sequential fallback, not a race.
Promise.anywould be faster, but it doubles third-party requests for every lookup. Privacy tool, privacy defaults. - 7-second per-resolver timeout via
AbortController. Without it, a hanging connection on a captive portal blocks the whole UI. nullmeans "unknown", which the UI treats differently from "NXDOMAIN" or "no records".
Fan-out
const TYPES = ['A', 'AAAA', 'CNAME', 'MX', 'TXT', 'NS', 'CAA'];
const answers = await Promise.all(TYPES.map((t) => ask(name, t)));
const dmarc = await ask('_dmarc.' + name, 'TXT');
const dnskey = await ask(name, 'DNSKEY');
Seven parallel queries, then DMARC and DNSKEY. DMARC lives at the _dmarc subdomain (RFC 7489 ยง6.1), which is why it's a separate lookup and why "I checked TXT and there's no DMARC" is such a common false negative.
Parsing mail policy
const spf = (found.TXT || []).filter((r) => /v=spf1/i.test(r.data));
const dmarcList = (dmarc?.Answer || []).filter((r) => r.type === 16 && /v=DMARC1/i.test(r.data));
const mx = found.MX || [];
const nullMx = mx.length === 1 && /^\d+\s+.?$/.test(String(mx[0].data).trim()); // RFC 7505
const hasMail = mx.length > 0 && !nullMx;
Null MX (RFC 7505) is a single MX with preference 0 and target ., meaning "this domain accepts no mail". Treating it as "has MX" would be wrong, so it gets its own branch.
DNSSEC: outsourcing validation to the resolver
const signed = !!(dnskey && dnskey.Answer && dnskey.Answer.some((r) => r.type === 48) // 48 = DNSKEY
&& dnskey.AD); // Authenticated Data bit
We don't validate the chain of trust ourselves. We check that the zone publishes DNSKEY records and that the resolver set the AD bit, i.e. the resolver claims it validated the response. That's trusting the resolver, over HTTPS, which is the standard stub-resolver posture (RFC 4035 ยง4.9.3), but it's worth saying out loud.
Known shortcuts
- SPF isn't evaluated. We show the record. We don't recurse into
include:/redirect=, so we can't tell you if you blow the 10-DNS-lookup limit (RFC 7208 ยง4.6.4). - TXT chunking. Long TXT records are split into 255-byte character-strings. The JSON APIs present them quoted and concatenated differently; we display what the resolver returns.
- DMARC tags aren't parsed beyond detection.
p=,sp=,pct=are shown raw. (Fun fact: plenty of big domains havep=rejectwithsp=none, meaning subdomains are wide open.) - Resolver-dependent views. Split-horizon DNS or geo-steered answers can differ between Cloudflare and Google. Whichever answers first is what you see.
Disclosure: I work on 404 VPN. The DNS records tool is free and runs entirely in your browser: https://404vpn.io/en/tools/dns-records. If you know a clean way to do RFC 7208 lookup counting client-side without hammering resolvers, I'm all ears.
Comments
No comments yet. Start the discussion.