fix EDNS COOKIE (RFC 7873/9018) handling: terminate cookies at the resolver #2

Merged
marvin merged 3 commits from fix/edns-cookie-handling into main 2026-10-11 19:20:44 +00:00
Owner

Cached responses were served verbatim, replaying the COOKIE option of the
original upstream exchange to clients with a different client cookie, which
made dig report 'Client COOKIE mismatch' on repeat queries. Upstream server
cookies were also relayed downstream.

  • Synthesize server cookies ourselves (RFC 9018: SipHash-2-4 over client
    cookie, version, timestamp and client IP, keyed with a rotating server
    secret, 1h rotation with previous-secret retention and timestamp skew
    tolerance)
  • Echo the current query's client cookie unchanged in every response
  • Strip/rebuild the COOKIE option on cache hits, including negative
    (NXDOMAIN) responses, and never relay the upstream server cookie
  • Respond without a COOKIE option when the query has none
  • Keep cached entries pristine (store a copy) so per-client rewriting never
    mutates a cached message
  • Add unit and integration regression tests
Cached responses were served verbatim, replaying the COOKIE option of the original upstream exchange to clients with a different client cookie, which made dig report 'Client COOKIE mismatch' on repeat queries. Upstream server cookies were also relayed downstream. - Synthesize server cookies ourselves (RFC 9018: SipHash-2-4 over client cookie, version, timestamp and client IP, keyed with a rotating server secret, 1h rotation with previous-secret retention and timestamp skew tolerance) - Echo the current query's client cookie unchanged in every response - Strip/rebuild the COOKIE option on cache hits, including negative (NXDOMAIN) responses, and never relay the upstream server cookie - Respond without a COOKIE option when the query has none - Keep cached entries pristine (store a copy) so per-client rewriting never mutates a cached message - Add unit and integration regression tests
tchivert added 1 commit 2026-10-11 15:10:37 +00:00
Cached responses were served verbatim, replaying the COOKIE option of the
original upstream exchange to clients with a different client cookie, which
made dig report 'Client COOKIE mismatch' on repeat queries. Upstream server
cookies were also relayed downstream.

- Synthesize server cookies ourselves (RFC 9018: SipHash-2-4 over client
  cookie, version, timestamp and client IP, keyed with a rotating server
  secret, 1h rotation with previous-secret retention and timestamp skew
  tolerance)
- Echo the current query's client cookie unchanged in every response
- Strip/rebuild the COOKIE option on cache hits, including negative
  (NXDOMAIN) responses, and never relay the upstream server cookie
- Respond without a COOKIE option when the query has none
- Keep cached entries pristine (store a copy) so per-client rewriting never
  mutates a cached message
- Add unit and integration regression tests
tchivert added 1 commit 2026-10-11 15:51:29 +00:00
Response handling:
- Clear the DNSSEC AD bit in all responses (RFC 6840 section 5.8): rdns
  forwards but does not validate, so it must not relay the upstream AD bit
- Truncate UDP responses exceeding the client's advertised payload size
  (RFC 6891 section 6.2.5): set TC and let clients retry over TCP; the
  COOKIE option survives truncation. Server UDP buffer raised to 4096
- Decrement TTLs of cached responses by the time spent in the cache
  (positive and negative answers); -t remains the cache lifetime source
  of truth. OPT pseudo-records are left untouched
- Reject upstream responses whose question section does not match the
  query, then try the next nameserver (anti-spoofing)
- Echo the current query's question section on cache hits and forwarded
  responses (0x20 case), and stop mutating the client's message when
  adding EDNS for the upstream exchange
- Advertise 1232 bytes to upstream when the client sends no EDNS, and
  gate DoT connection error logging behind -l

Cookies:
- Make server cookies fully RFC 9018-compliant, pinned against the RFC
  Appendix A test vectors: version 1, second-granularity timestamps,
  little-endian hash sub-field, RFC 1982 serial-number skew checks,
  reserved sub-field hashed as received on validation
- Strip COOKIE options before caching instead of on every cache hit
- Count server cookies presented by clients in the
  rdns_cookie_checks_total metric (valid/invalid/absent), wiring
  validateServerCookie into the serving path
- Response OPT advertises the responder's payload size (RFC 6891)

Packaging and CI:
- package.yml: fix the deb Description/Homepage copy-paste errors,
  install the binary to /usr/local/bin (matching the systemd unit),
  register /etc/rdns conffiles, ship the systemd unit and the
  logrotate/rsyslog configs, add Installed-Size
- New test.yml workflow running go vet and go test -race on pull
  requests; image/release pushes stay tied to main/tags
- Stop tracking the generated conf/blocklist.txt (gitignore it,
  generate with utils/blocklist_update)
- README: fix the project description, document cookie handling, the
  AD-bit and truncation behavior, TTL decrementing, and utils/

Tests:
- Mock-upstream integration tests: end-to-end cookie rewrite and AD
  clearing, question-mismatch rejection, TCP retry on truncation, TC
  truncation per client buffer, TTL decrement on cache serve, question
  case echo, cache entry stored without COOKIE
- Unit tests: all 64 SipHash-2-4 reference vectors, RFC 9018 Appendix A
  cookie vectors, cookie validation metric

Upstream ports are overridable (upstreamDNSPort/upstreamDoTPort) so
tests can point the resolver at a mock nameserver.
tchivert added 1 commit 2026-10-11 17:22:43 +00:00
close remaining RFC 7873/6891 spec gaps from review
test / test (pull_request) Successful in 10s
2a3e20d8d7
Malformed COOKIE options (RFC 7873 section 5.2.2): option lengths other
than 8 and 16-40 are now answered with FORMERR and never get a COOKIE
in the response; getQueryCookie classifies absent/valid/malformed.

Server-cookie learning probes (RFC 7873 section 5.4): question-less
queries carrying a COOKIE option are answered NOERROR with a freshly
minted server cookie, or BADCOOKIE when re-establishing an invalid
server cookie. miekg's ServeMux refuses question-less queries, so the
servers now use handleQuery directly as their handler, and
msgAcceptFunc lets QDCOUNT=0 QUERY requests through.

EDNS/OPT handling (RFC 6891): the upstream OPT record is no longer
cached or forwarded (sections 6.1.1 and 6.2.1) — it is removed before
caching and rebuilt per query. Responses to EDNS queries always carry
an OPT record advertising the responder's payload size and echoing
the query's DO bit (sections 6.1.1 and 6.2.4); responses to non-EDNS
queries never carry one (section 7). applyCookie is now applyEDNS.

Tests: malformed-COOKIE FORMERR for lengths 4/12/41 and normal
answers for 8/16/40, cookie probe (NOERROR/BADCOOKIE) at the handler
level and through a real miekg server over UDP (independent raw-socket
verification included), msgAcceptFunc unit test, OPT-never-cached and
OPT-rebuild assertions, non-EDNS responses carry no OPT.

README documents the new EDNS and COOKIE behaviors.
marvin merged commit 59f7ec83ad into main 2026-10-11 19:20:44 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: tchivert/rdns-go#2