emailverifier.dev
Posts

DNS TXT Domain Verification: A Replay-Safe Workflow for Email Platforms

| 5 min read | Usama Ejaz
A one-time challenge token moves into a scoped DNS TXT slot, through an exact-match gate, then branches to verified, mismatched, or pending outcomes.

To verify control of a sending domain, issue a single-use random challenge, ask the user to publish it in a service-specific DNS TXT record, then resolve that exact name and compare the complete TXT value. Bind the challenge to one account and one domain, give it an expiry, and consume it once.

A hypothetical record for example.com could look like this:

_sender-verify-challenge.example.com. 300 IN TXT \
  "v=sender1; token=3p0rQw9n6vYkH2F7mB8cT1xZaS4dE5uL"

The label and token above are illustrative. Your service should use its own stable, application-specific label.

What does the TXT record actually prove?

A matching answer proves that someone who could publish at that DNS name completed this challenge at the time you checked it. That is useful evidence of domain control. It is not proof of legal ownership, the operator's identity, mailbox ownership, sender safety, or future deliverability.

This boundary matters for email platforms. Domain control can justify enabling domain-scoped configuration. It should not silently approve a user, bypass an abuse review, or promise that mail will reach an inbox.

The mechanism is well established. ACME's DNS-01 challenge uses a designated TXT value to prove control of a domain. A current IETF working draft dated June 22, 2026, Domain Control Validation using DNS, recommends TXT records, application-specific underscore labels, and unique tokens. It is still an Internet-Draft, not a published RFC.

Bind one challenge to one account and one domain

I would generate 24 random bytes with a cryptographically secure generator and encode them as base64url. That gives 192 bits before encoding, above the 128-bit minimum used by ACME. The database row should contain the account ID, normalized domain, validation name, SHA-256 hash of the token, expiry, and a nullable used_at timestamp.

Only the plaintext token needs to reach the DNS administrator. Storing its hash reduces what a database leak can reveal. A new challenge should invalidate the previous one for the same account-domain pair.

Why the application-specific label? Publishing at _sender-verify-challenge.example.com scopes the proof to your service. Reusing a TXT record at the zone apex creates collisions with unrelated records and makes the intended privilege harder to see.

Treat DNS as a state machine, not a boolean

StateWhat the resolver observedApplication action
verifiedOne complete TXT value matches the live challengeConsume the challenge and grant the domain privilege in one transaction
pendingNXDOMAIN or no TXT data yetKeep the challenge open and let the user retry
mismatchTXT data exists, but no value matchesShow the expected name and value without declaring DNS broken
lookup_errorTimeout, SERVFAIL, or another resolver failureRetry with backoff; do not turn an infrastructure error into a domain verdict
expiredThe challenge deadline passedIssue a new token
consumedThe token was already usedReturn the existing domain state without granting anything again

DNS propagation is why pending deserves its own state. A resolver may cache the absence of a record, not just its presence. The June 2026 IETF draft notes that a verifier can wait as long as the zone's negative-caching TTL before a newly added record becomes visible.

TXT parsing has another small trap. DNS TXT RDATA can contain more than one character-string. Node's dnsPromises.resolveTxt() returns an array of records, and each record is an array of chunks. Join the chunks inside each record, then compare records separately. Joining the whole outer array can manufacture a value that DNS never returned.

A Node.js verifier with explicit outcomes

This helper classifies the DNS result. The caller still needs a database transaction that locks the challenge row, confirms it is unused and unexpired, sets used_at, and grants the domain privilege atomically.

import { createHash, timingSafeEqual } from 'node:crypto';

const hash = (value) =>
  createHash('sha256').update(value, 'utf8').digest();

export async function verifyDomainChallenge({
  challenge,
  resolveTxt,
  now = Date.now(),
}) {
  if (challenge.usedAt) return { state: 'consumed' };
  if (now >= Date.parse(challenge.expiresAt)) {
    return { state: 'expired' };
  }

  const name = `_sender-verify-challenge.${challenge.domain}`;
  let rows;

  try {
    rows = await resolveTxt(name);
  } catch (error) {
    if (error?.code === 'ENODATA' || error?.code === 'ENOTFOUND') {
      return { state: 'pending' };
    }
    return { state: 'lookup_error', reason: error?.code ?? 'UNKNOWN' };
  }

  const expected = Buffer.from(challenge.tokenHash, 'hex');

  for (const chunks of rows) {
    const value = chunks.join('');
    const match = /^v=sender1; token=([A-Za-z0-9_-]+)$/.exec(value);
    if (!match) continue;

    if (timingSafeEqual(hash(match[1]), expected)) {
      return { state: 'verified' };
    }
  }

  return { state: 'mismatch' };
}

For this article, I ran the helper against injected resolver responses for a chunked matching TXT record, no TXT data, a wrong token, a timeout, and an expired challenge. The five resulting states were verified, pending, mismatch, lookup_error, and expired.

When should the record be removed or checked again?

For a one-time challenge, the DNS record can be removed after verification. Leaving it in place should not recreate access because the token is consumed and future challenges use new values. Clear removal instructions prevent old validation records from becoming permanent DNS clutter.

Persistent verification is a different contract. If your product relies on the record continuing to exist, say how often you check it, what removal revokes, and how failures are handled. Reverification also makes sense after a domain transfer, an account recovery, or a sensitive change to sending configuration. There is no universal polling interval that fits every risk model.

A fair objection is that a token visible in DNS only proves a point in time. Correct. That is why the privilege, expiry, and reverification policy belong in the product design, not in the TXT string alone.

After domain control is established, sender authentication remains separate work. Use a dedicated email subdomain strategy, publish one coherent SPF policy, and rotate DKIM selectors without treating DNS verification as a substitute for any of them.

Continue reading