785 lines
29 KiB
Go
785 lines
29 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"net"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/miekg/dns"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
dto "github.com/prometheus/client_model/go"
|
|
)
|
|
|
|
func newCookieQuery(name string, qtype uint16, clientCookie []byte) *dns.Msg {
|
|
q := newQuery(name, qtype, dns.ClassINET)
|
|
q.SetEdns0(1232, false)
|
|
opt := q.IsEdns0()
|
|
opt.Option = append(opt.Option, &dns.EDNS0_COOKIE{
|
|
Code: dns.EDNS0COOKIE,
|
|
Cookie: hex.EncodeToString(clientCookie),
|
|
})
|
|
return q
|
|
}
|
|
|
|
func responseCookie(m *dns.Msg) *dns.EDNS0_COOKIE {
|
|
opt := m.IsEdns0()
|
|
if opt == nil {
|
|
return nil
|
|
}
|
|
for _, o := range opt.Option {
|
|
if c, ok := o.(*dns.EDNS0_COOKIE); ok {
|
|
return c
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func decodeCookie(t *testing.T, c *dns.EDNS0_COOKIE) (client, server []byte) {
|
|
t.Helper()
|
|
raw, err := hex.DecodeString(c.Cookie)
|
|
if err != nil {
|
|
t.Fatalf("COOKIE option is not valid hex: %v", err)
|
|
}
|
|
if len(raw) < clientCookieLen {
|
|
t.Fatalf("COOKIE option too short: %d bytes", len(raw))
|
|
}
|
|
return raw[:clientCookieLen], raw[clientCookieLen:]
|
|
}
|
|
|
|
func mustEqual(t *testing.T, what string, got, want []byte) {
|
|
t.Helper()
|
|
if !bytes.Equal(got, want) {
|
|
t.Errorf("%s = %x, want %x", what, got, want)
|
|
}
|
|
}
|
|
|
|
// Official SipHash-2-4 reference vectors (key 00 01 02 ... 0f, inputs
|
|
// of increasing length), from the reference implementation test suite.
|
|
var siphashRefVectors = [][]byte{
|
|
{0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0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xe5},
|
|
{0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95},
|
|
}
|
|
|
|
func TestSipHash24ReferenceVectors(t *testing.T) {
|
|
var key [16]byte
|
|
for i := range key {
|
|
key[i] = byte(i)
|
|
}
|
|
var in [64]byte
|
|
for i := range in {
|
|
in[i] = byte(i)
|
|
}
|
|
for i, want := range siphashRefVectors {
|
|
got := siphash24(key[:], in[:i])
|
|
if got != binary.LittleEndian.Uint64(want) {
|
|
t.Errorf("siphash24(key, in[:%d]) = %016x, want %016x", i, got, binary.LittleEndian.Uint64(want))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMakeServerCookie(t *testing.T) {
|
|
now := time.Unix(1700000000, 0)
|
|
clientCookie := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
|
|
sc := makeServerCookie("192.0.2.10", clientCookie, now)
|
|
if len(sc) != serverCookieLen {
|
|
t.Fatalf("server cookie length = %d, want %d", len(sc), serverCookieLen)
|
|
}
|
|
if sc[0] != cookieVersion {
|
|
t.Errorf("server cookie version = %d, want %d", sc[0], cookieVersion)
|
|
}
|
|
if ts := binary.BigEndian.Uint32(sc[4:8]); ts != uint32(now.Unix()) {
|
|
t.Errorf("server cookie timestamp = %d, want %d (seconds since epoch)", ts, now.Unix())
|
|
}
|
|
if sc[1] != 0 || sc[2] != 0 || sc[3] != 0 {
|
|
t.Errorf("server cookie reserved bytes = %x, want 000000", sc[1:4])
|
|
}
|
|
|
|
// Bound to the client address.
|
|
if other := makeServerCookie("192.0.2.11", clientCookie, now); bytes.Equal(sc, other) {
|
|
t.Error("server cookie must differ for different client IPs")
|
|
}
|
|
// Bound to the client cookie.
|
|
if other := makeServerCookie("192.0.2.10", []byte{1, 2, 3, 4, 5, 6, 7, 8}, now); bytes.Equal(sc, other) {
|
|
t.Error("server cookie must differ for different client cookies")
|
|
}
|
|
// Changes over time.
|
|
if other := makeServerCookie("192.0.2.10", clientCookie, now.Add(2*time.Minute)); bytes.Equal(sc, other) {
|
|
t.Error("server cookie must change over time")
|
|
}
|
|
// Works for IPv6 clients too.
|
|
if other := makeServerCookie("2001:db8::1", clientCookie, now); len(other) != serverCookieLen {
|
|
t.Errorf("IPv6 server cookie length = %d, want %d", len(other), serverCookieLen)
|
|
}
|
|
}
|
|
|
|
func TestValidateServerCookie(t *testing.T) {
|
|
now := time.Now()
|
|
clientCookie := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
|
|
sc := makeServerCookie("192.0.2.10", clientCookie, now)
|
|
if !validateServerCookie("192.0.2.10", clientCookie, sc, now) {
|
|
t.Error("freshly minted server cookie must validate")
|
|
}
|
|
if validateServerCookie("192.0.2.11", clientCookie, sc, now) {
|
|
t.Error("server cookie must not validate for a different client IP")
|
|
}
|
|
if validateServerCookie("192.0.2.10", []byte{1, 2, 3, 4, 5, 6, 7, 8}, sc, now) {
|
|
t.Error("server cookie must not validate for a different client cookie")
|
|
}
|
|
if validateServerCookie("192.0.2.10", clientCookie, sc[:15], now) {
|
|
t.Error("server cookie with wrong length must not validate")
|
|
}
|
|
|
|
// Skew tolerance: up to one hour in the past is accepted.
|
|
old := makeServerCookie("192.0.2.10", clientCookie, now.Add(-30*time.Minute))
|
|
if !validateServerCookie("192.0.2.10", clientCookie, old, now) {
|
|
t.Error("server cookie 30 minutes old must validate (skew tolerance)")
|
|
}
|
|
tooOld := makeServerCookie("192.0.2.10", clientCookie, now.Add(-2*time.Hour))
|
|
if validateServerCookie("192.0.2.10", clientCookie, tooOld, now) {
|
|
t.Error("server cookie 2 hours old must not validate")
|
|
}
|
|
// Small future skew is tolerated, larger future skew is not.
|
|
future := makeServerCookie("192.0.2.10", clientCookie, now.Add(2*time.Minute))
|
|
if !validateServerCookie("192.0.2.10", clientCookie, future, now) {
|
|
t.Error("server cookie 2 minutes in the future must validate (skew tolerance)")
|
|
}
|
|
tooFarFuture := makeServerCookie("192.0.2.10", clientCookie, now.Add(10*time.Minute))
|
|
if validateServerCookie("192.0.2.10", clientCookie, tooFarFuture, now) {
|
|
t.Error("server cookie 10 minutes in the future must not validate")
|
|
}
|
|
|
|
// Rotation: cookies minted before a rotation stay valid (previous
|
|
// secret is retained), cookies from two rotations ago do not.
|
|
preRotation := makeServerCookie("192.0.2.10", clientCookie, now)
|
|
rotateCookieSecrets()
|
|
if !validateServerCookie("192.0.2.10", clientCookie, preRotation, now) {
|
|
t.Error("server cookie minted before secret rotation must still validate")
|
|
}
|
|
rotateCookieSecrets()
|
|
if validateServerCookie("192.0.2.10", clientCookie, preRotation, now) {
|
|
t.Error("server cookie minted two rotations ago must not validate")
|
|
}
|
|
}
|
|
|
|
// TestRFC9018Vectors pins the server cookie construction against the test
|
|
// vectors of RFC 9018 Appendix A.
|
|
func TestRFC9018Vectors(t *testing.T) {
|
|
setSecrets := func(current, previous [16]byte) (restore func()) {
|
|
cookieSecrets.Lock()
|
|
oldCurrent, oldPrevious := cookieSecrets.current, cookieSecrets.previous
|
|
cookieSecrets.current, cookieSecrets.previous = current, previous
|
|
cookieSecrets.Unlock()
|
|
return func() {
|
|
cookieSecrets.Lock()
|
|
cookieSecrets.current, cookieSecrets.previous = oldCurrent, oldPrevious
|
|
cookieSecrets.Unlock()
|
|
}
|
|
}
|
|
secret := func(hexSecret string) [16]byte {
|
|
raw, err := hex.DecodeString(hexSecret)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var s [16]byte
|
|
copy(s[:], raw)
|
|
return s
|
|
}
|
|
cookie := func(hexCookie string) []byte {
|
|
raw, err := hex.DecodeString(hexCookie)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return raw
|
|
}
|
|
|
|
// A.1 / A.2: IPv4 client 198.51.100.100.
|
|
restore := setSecrets(secret("e5e973e5a6b2a43f48e7dc849e37bfcf"), secret("0000000000000000000000000000000000"))
|
|
defer restore()
|
|
clientCookie := cookie("2464c4abcf10c957")
|
|
|
|
sc := makeServerCookie("198.51.100.100", clientCookie, time.Unix(1559731985, 0))
|
|
if want := cookie("010000005cf79f111f8130c3eee29480"); !bytes.Equal(sc, want) {
|
|
t.Errorf("A.1 server cookie = %x, want %x", sc, want)
|
|
}
|
|
if !validateServerCookie("198.51.100.100", clientCookie, sc, time.Unix(1559731985, 0)) {
|
|
t.Error("A.1 server cookie must validate")
|
|
}
|
|
|
|
sc = makeServerCookie("198.51.100.100", clientCookie, time.Unix(1559734385, 0))
|
|
if want := cookie("010000005cf7a871d4a564a1442aca77"); !bytes.Equal(sc, want) {
|
|
t.Errorf("A.2 server cookie = %x, want %x", sc, want)
|
|
}
|
|
|
|
// A.3: another IPv4 client, 203.0.113.203.
|
|
clientCookie = cookie("fc93fc62807ddb86")
|
|
sc = makeServerCookie("203.0.113.203", clientCookie, time.Unix(1559734700, 0))
|
|
if want := cookie("010000005cf7a9acf73a7810aca2381e"); !bytes.Equal(sc, want) {
|
|
t.Errorf("A.3 server cookie = %x, want %x", sc, want)
|
|
}
|
|
|
|
// A.4: IPv6 client with a rolled-over secret: the previous secret
|
|
// still validates the cookie the client presents, the new secret is
|
|
// used for minting.
|
|
restore()
|
|
restore = setSecrets(secret("445536bcd2513298075a5d379663c962"), secret("dd3bdf9344b678b185a6f5cb60fca715"))
|
|
defer restore()
|
|
|
|
clientCookie = cookie("22681ab97d52c298")
|
|
presented := cookie("010000005cf7c57926556bd0934c72f8")
|
|
now := time.Unix(1559741961, 0)
|
|
if !validateServerCookie("2001:db8:220:1:59de:d0f4:8769:82b8", clientCookie, presented, now) {
|
|
t.Error("A.4 server cookie minted with the previous secret must still validate")
|
|
}
|
|
sc = makeServerCookie("2001:db8:220:1:59de:d0f4:8769:82b8", clientCookie, now)
|
|
if want := cookie("010000005cf7c609a6bb79d16625507a"); !bytes.Equal(sc, want) {
|
|
t.Errorf("A.4 server cookie = %x, want %x", sc, want)
|
|
}
|
|
}
|
|
|
|
func TestApplyEDNSEchoesClientCookie(t *testing.T) {
|
|
clientCookie := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
q := newCookieQuery("example.test", dns.TypeA, clientCookie)
|
|
|
|
// Response without any OPT record.
|
|
r := new(dns.Msg)
|
|
r.SetReply(q)
|
|
applyEDNS(q, r, "192.0.2.10")
|
|
|
|
c := responseCookie(r)
|
|
if c == nil {
|
|
t.Fatal("response has no COOKIE option")
|
|
}
|
|
gotClient, gotServer := decodeCookie(t, c)
|
|
mustEqual(t, "echoed client cookie", gotClient, clientCookie)
|
|
if len(gotServer) != serverCookieLen {
|
|
t.Errorf("server cookie length = %d, want %d", len(gotServer), serverCookieLen)
|
|
}
|
|
if !validateServerCookie("192.0.2.10", clientCookie, gotServer, time.Now()) {
|
|
t.Error("response server cookie does not validate for the querying client")
|
|
}
|
|
if r.IsEdns0().UDPSize() != serverUDPSize {
|
|
t.Errorf("response OPT UDP size = %d, want %d (responder's payload size)", r.IsEdns0().UDPSize(), serverUDPSize)
|
|
}
|
|
}
|
|
|
|
func TestApplyEDNSReplacesUpstreamServerCookie(t *testing.T) {
|
|
clientCookie := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
upstreamServer := []byte{0x01, 0x00, 0x00, 0x00, 0x6a, 0xcb, 0x73, 0x63, 0xa3, 0x6c, 0x6a, 0x41, 0x62, 0xa3, 0x69, 0x6b}
|
|
q := newCookieQuery("example.test", dns.TypeA, clientCookie)
|
|
|
|
// Response as relayed from upstream: echoes our client cookie and
|
|
// carries the upstream server cookie, which must not be relayed.
|
|
r := new(dns.Msg)
|
|
r.SetReply(q)
|
|
r.SetEdns0(1232, false)
|
|
upstreamCookie := append(append([]byte{}, clientCookie...), upstreamServer...)
|
|
r.IsEdns0().Option = append(r.IsEdns0().Option, &dns.EDNS0_COOKIE{
|
|
Code: dns.EDNS0COOKIE,
|
|
Cookie: hex.EncodeToString(upstreamCookie),
|
|
})
|
|
|
|
applyEDNS(q, r, "192.0.2.10")
|
|
|
|
c := responseCookie(r)
|
|
if c == nil {
|
|
t.Fatal("response has no COOKIE option")
|
|
}
|
|
gotClient, gotServer := decodeCookie(t, c)
|
|
mustEqual(t, "echoed client cookie", gotClient, clientCookie)
|
|
if bytes.Equal(gotServer, upstreamServer) {
|
|
t.Error("upstream server cookie must not be relayed to the client")
|
|
}
|
|
if len(gotServer) != serverCookieLen {
|
|
t.Errorf("server cookie length = %d, want %d", len(gotServer), serverCookieLen)
|
|
}
|
|
if !validateServerCookie("192.0.2.10", clientCookie, gotServer, time.Now()) {
|
|
t.Error("response server cookie does not validate for the querying client")
|
|
}
|
|
}
|
|
|
|
func TestApplyEDNSNoCookieInQuery(t *testing.T) {
|
|
// Query with EDNS but without a COOKIE option: any COOKIE option must
|
|
// be stripped from the response, and the OPT record must be rebuilt
|
|
// for the responder (RFC 6891 sections 6.1.1 and 6.2.4).
|
|
q := newQuery("example.test", dns.TypeA, dns.ClassINET)
|
|
q.SetEdns0(1232, false)
|
|
q.IsEdns0().SetDo(true)
|
|
|
|
r := new(dns.Msg)
|
|
r.SetReply(q)
|
|
r.SetEdns0(512, false)
|
|
r.IsEdns0().Option = append(r.IsEdns0().Option, &dns.EDNS0_COOKIE{
|
|
Code: dns.EDNS0COOKIE,
|
|
Cookie: hex.EncodeToString([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}),
|
|
})
|
|
applyEDNS(q, r, "192.0.2.10")
|
|
if c := responseCookie(r); c != nil {
|
|
t.Errorf("response must not carry a COOKIE option, got %s", c.Cookie)
|
|
}
|
|
if r.IsEdns0() == nil {
|
|
t.Fatal("response to an EDNS query must carry an OPT record (RFC 6891 section 6.1.1)")
|
|
}
|
|
if size := r.IsEdns0().UDPSize(); size != serverUDPSize {
|
|
t.Errorf("response OPT UDP size = %d, want %d (responder's payload size)", size, serverUDPSize)
|
|
}
|
|
if !r.IsEdns0().Do() {
|
|
t.Error("response OPT must echo the query's DO bit")
|
|
}
|
|
|
|
// Query without EDNS at all: the response must not carry an OPT
|
|
// record (RFC 6891 section 7), even one present in a cached or
|
|
// upstream response.
|
|
q2 := newQuery("example.test", dns.TypeA, dns.ClassINET)
|
|
r2 := new(dns.Msg)
|
|
r2.SetReply(q2)
|
|
r2.SetEdns0(1232, false)
|
|
r2.IsEdns0().Option = append(r2.IsEdns0().Option, &dns.EDNS0_COOKIE{
|
|
Code: dns.EDNS0COOKIE,
|
|
Cookie: hex.EncodeToString([]byte{1, 2, 3, 4, 5, 6, 7, 8}),
|
|
})
|
|
applyEDNS(q2, r2, "192.0.2.10")
|
|
if r2.IsEdns0() != nil {
|
|
t.Errorf("response to a non-EDNS query must not carry an OPT record, got udp: %d", r2.IsEdns0().UDPSize())
|
|
}
|
|
}
|
|
|
|
func counterValue(t *testing.T, c prometheus.Counter) float64 {
|
|
t.Helper()
|
|
var m dto.Metric
|
|
if err := c.Write(&m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return m.GetCounter().GetValue()
|
|
}
|
|
|
|
func TestApplyEDNSCountsServerCookieValidation(t *testing.T) {
|
|
clientCookie := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
serverCookie := makeServerCookie("192.0.2.10", clientCookie, time.Now())
|
|
|
|
validBefore := counterValue(t, cookieChecks.WithLabelValues("valid"))
|
|
invalidBefore := counterValue(t, cookieChecks.WithLabelValues("invalid"))
|
|
absentBefore := counterValue(t, cookieChecks.WithLabelValues("absent"))
|
|
|
|
// A client presenting a valid server cookie (echoed back per RFC 7873
|
|
// section 5.2.2).
|
|
q := newCookieQuery("metric.test", dns.TypeA, append(append([]byte{}, clientCookie...), serverCookie...))
|
|
r := new(dns.Msg)
|
|
r.SetReply(q)
|
|
applyEDNS(q, r, "192.0.2.10")
|
|
if got := counterValue(t, cookieChecks.WithLabelValues("valid")); got != validBefore+1 {
|
|
t.Errorf("valid cookie checks = %v, want %v", got, validBefore+1)
|
|
}
|
|
|
|
// A client presenting a bogus server cookie.
|
|
bogus := append(append([]byte{}, clientCookie...), make([]byte, serverCookieLen)...)
|
|
q = newCookieQuery("metric.test", dns.TypeA, bogus)
|
|
r = new(dns.Msg)
|
|
r.SetReply(q)
|
|
applyEDNS(q, r, "192.0.2.10")
|
|
if got := counterValue(t, cookieChecks.WithLabelValues("invalid")); got != invalidBefore+1 {
|
|
t.Errorf("invalid cookie checks = %v, want %v", got, invalidBefore+1)
|
|
}
|
|
|
|
// A client sending a client cookie only.
|
|
q = newCookieQuery("metric.test", dns.TypeA, clientCookie)
|
|
r = new(dns.Msg)
|
|
r.SetReply(q)
|
|
applyEDNS(q, r, "192.0.2.10")
|
|
if got := counterValue(t, cookieChecks.WithLabelValues("absent")); got != absentBefore+1 {
|
|
t.Errorf("absent cookie checks = %v, want %v", got, absentBefore+1)
|
|
}
|
|
}
|
|
|
|
func TestHandleQueryCookieFirstAndRepeat(t *testing.T) {
|
|
resetMaps()
|
|
qcache.Flush()
|
|
hostsMap["cookie.test"] = []string{"192.0.2.10"}
|
|
|
|
clientCookie1 := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
clientCookie2 := []byte{0xde, 0xad, 0xbe, 0xef, 0x00, 0x11, 0x22, 0x33}
|
|
|
|
// First query from client 1 (cache miss, answered locally).
|
|
w1 := &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
|
|
q1 := newCookieQuery("cookie.test", dns.TypeA, clientCookie1)
|
|
handleQuery(w1, q1)
|
|
if w1.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
c1 := responseCookie(w1.msg)
|
|
if c1 == nil {
|
|
t.Fatal("first response has no COOKIE option")
|
|
}
|
|
gotClient, gotServer := decodeCookie(t, c1)
|
|
mustEqual(t, "first response client cookie", gotClient, clientCookie1)
|
|
if len(gotServer) != serverCookieLen {
|
|
t.Fatalf("first response server cookie length = %d, want %d", len(gotServer), serverCookieLen)
|
|
}
|
|
if len(w1.msg.Answer) != 1 {
|
|
t.Fatalf("first response answers = %d, want 1", len(w1.msg.Answer))
|
|
}
|
|
|
|
// The cached entry must not have been polluted with client 1's cookie.
|
|
if cached, found := qcache.Get(cacheKey("cookie.test", "IN", "A")); found {
|
|
if c := responseCookie(cached.(*dns.Msg)); c != nil {
|
|
t.Errorf("cached entry carries a COOKIE option (%s), want none", c.Cookie)
|
|
}
|
|
} else {
|
|
t.Fatal("response was not cached")
|
|
}
|
|
|
|
// Repeat query from a different client (different address and
|
|
// different client cookie): cache hit, cookie must be rewritten.
|
|
w2 := &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.99"), Port: 5300}}
|
|
q2 := newCookieQuery("cookie.test", dns.TypeA, clientCookie2)
|
|
q2.Id = 4321
|
|
handleQuery(w2, q2)
|
|
if w2.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w2.msg.Id != 4321 {
|
|
t.Errorf("cached response ID = %d, want 4321", w2.msg.Id)
|
|
}
|
|
c2 := responseCookie(w2.msg)
|
|
if c2 == nil {
|
|
t.Fatal("cached response has no COOKIE option")
|
|
}
|
|
gotClient2, gotServer2 := decodeCookie(t, c2)
|
|
mustEqual(t, "cached response client cookie", gotClient2, clientCookie2)
|
|
if bytes.Equal(gotServer2, gotServer) {
|
|
t.Error("cached response must carry a fresh server cookie for the new client")
|
|
}
|
|
if !validateServerCookie("192.0.2.99", clientCookie2, gotServer2, time.Now()) {
|
|
t.Error("cached response server cookie does not validate for the new client")
|
|
}
|
|
if len(w2.msg.Answer) != 1 {
|
|
t.Errorf("cached response answers = %d, want 1", len(w2.msg.Answer))
|
|
}
|
|
}
|
|
|
|
func TestHandleQueryCookieCacheHitNXDOMAIN(t *testing.T) {
|
|
resetMaps()
|
|
qcache.Flush()
|
|
|
|
// Simulate a cached negative response that still carries the COOKIE
|
|
// option of the original upstream exchange.
|
|
oldClientCookie := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
|
|
upstreamServer := []byte{0x01, 0x00, 0x00, 0x00, 0x6a, 0xcb, 0x73, 0x63, 0xa3, 0x6c, 0x6a, 0x41, 0x62, 0xa3, 0x69, 0x6b}
|
|
cached := new(dns.Msg)
|
|
cached.SetReply(newQuery("nx.cookie.test", dns.TypeTXT, dns.ClassINET))
|
|
cached.Rcode = dns.RcodeNameError
|
|
cached.Ns = []dns.RR{&dns.SOA{
|
|
Hdr: dns.RR_Header{Name: "cookie.test.", Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 300},
|
|
Ns: "ns.cookie.test.",
|
|
Mbox: "hostmaster.cookie.test.",
|
|
Serial: 1,
|
|
Refresh: 3600,
|
|
Retry: 600,
|
|
Expire: 86400,
|
|
Minttl: 300,
|
|
}}
|
|
cached.SetEdns0(1232, false)
|
|
cachedCookie := append(append([]byte{}, oldClientCookie...), upstreamServer...)
|
|
cached.IsEdns0().Option = append(cached.IsEdns0().Option, &dns.EDNS0_COOKIE{
|
|
Code: dns.EDNS0COOKIE,
|
|
Cookie: hex.EncodeToString(cachedCookie),
|
|
})
|
|
qcache.Set(cacheKey("nx.cookie.test", "IN", "TXT"), cached, cacheTTL())
|
|
|
|
// New client, new client cookie: the cached NXDOMAIN must be served
|
|
// with a cookie rebuilt for this query.
|
|
clientCookie := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
w := &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
|
|
q := newCookieQuery("nx.cookie.test", dns.TypeTXT, clientCookie)
|
|
q.Id = 777
|
|
handleQuery(w, q)
|
|
|
|
if w.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w.msg.Rcode != dns.RcodeNameError {
|
|
t.Errorf("rcode = %d, want NXDOMAIN", w.msg.Rcode)
|
|
}
|
|
if w.msg.Id != 777 {
|
|
t.Errorf("response ID = %d, want 777", w.msg.Id)
|
|
}
|
|
if len(w.msg.Ns) != 1 {
|
|
t.Errorf("authority records = %d, want 1 (SOA preserved)", len(w.msg.Ns))
|
|
}
|
|
c := responseCookie(w.msg)
|
|
if c == nil {
|
|
t.Fatal("cached NXDOMAIN response has no COOKIE option")
|
|
}
|
|
gotClient, gotServer := decodeCookie(t, c)
|
|
mustEqual(t, "cached NXDOMAIN client cookie", gotClient, clientCookie)
|
|
if bytes.Equal(gotServer, upstreamServer) {
|
|
t.Error("cached NXDOMAIN response must not relay the upstream server cookie")
|
|
}
|
|
if !validateServerCookie("192.0.2.10", clientCookie, gotServer, time.Now()) {
|
|
t.Error("cached NXDOMAIN server cookie does not validate for the current client")
|
|
}
|
|
|
|
// The cached entry itself must be untouched.
|
|
if c := responseCookie(cached); c == nil || c.Cookie != hex.EncodeToString(cachedCookie) {
|
|
t.Errorf("cached entry COOKIE mutated: %v", c)
|
|
}
|
|
}
|
|
|
|
func TestHandleQueryCookieAbsentOnCacheHit(t *testing.T) {
|
|
resetMaps()
|
|
qcache.Flush()
|
|
hostsMap["plain.test"] = []string{"192.0.2.10"}
|
|
|
|
// Prime the cache with a cookie-carrying entry.
|
|
clientCookie := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
cached := new(dns.Msg)
|
|
cached.SetReply(newQuery("plain.test", dns.TypeA, dns.ClassINET))
|
|
cached.Answer = []dns.RR{&dns.A{
|
|
Hdr: dns.RR_Header{Name: "plain.test.", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 300},
|
|
A: net.ParseIP("192.0.2.10"),
|
|
}}
|
|
cached.SetEdns0(1232, false)
|
|
cached.IsEdns0().Option = append(cached.IsEdns0().Option, &dns.EDNS0_COOKIE{
|
|
Code: dns.EDNS0COOKIE,
|
|
Cookie: hex.EncodeToString(append(append([]byte{}, clientCookie...), makeServerCookie("192.0.2.10", clientCookie, time.Now())...)),
|
|
})
|
|
qcache.Set(cacheKey("plain.test", "IN", "A"), cached, cacheTTL())
|
|
|
|
// A client sending no EDNS/COOKIE at all must get no OPT record back,
|
|
// even on a cache hit whose entry carries one.
|
|
w := &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
|
|
handleQuery(w, newQuery("plain.test", dns.TypeA, dns.ClassINET))
|
|
if w.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if c := responseCookie(w.msg); c != nil {
|
|
t.Errorf("response to cookie-less query carries COOKIE option %s", c.Cookie)
|
|
}
|
|
if w.msg.IsEdns0() != nil {
|
|
t.Error("response to a non-EDNS query must not carry an OPT record (RFC 6891 section 7)")
|
|
}
|
|
if len(w.msg.Answer) != 1 {
|
|
t.Errorf("answers = %d, want 1", len(w.msg.Answer))
|
|
}
|
|
}
|
|
|
|
func TestHandleQueryCookieBlocklistAndServfail(t *testing.T) {
|
|
resetMaps()
|
|
qcache.Flush()
|
|
blocklistMap["blocked.cookie.test"] = true
|
|
|
|
clientCookie := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
|
|
// Locally generated REFUSED answer.
|
|
w := &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
|
|
handleQuery(w, newCookieQuery("blocked.cookie.test", dns.TypeA, clientCookie))
|
|
if w.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w.msg.Rcode != dns.RcodeRefused {
|
|
t.Errorf("rcode = %d, want REFUSED", w.msg.Rcode)
|
|
}
|
|
c := responseCookie(w.msg)
|
|
if c == nil {
|
|
t.Fatal("REFUSED response has no COOKIE option")
|
|
}
|
|
gotClient, gotServer := decodeCookie(t, c)
|
|
mustEqual(t, "REFUSED response client cookie", gotClient, clientCookie)
|
|
if !validateServerCookie("192.0.2.10", clientCookie, gotServer, time.Now()) {
|
|
t.Error("REFUSED response server cookie does not validate")
|
|
}
|
|
|
|
// Locally generated SERVFAIL answer (writeResponse path on error).
|
|
q := newCookieQuery("error.cookie.test", dns.TypeA, clientCookie)
|
|
r := new(dns.Msg)
|
|
r.SetRcode(q, dns.RcodeServerFailure)
|
|
applyEDNS(q, r, "192.0.2.10")
|
|
c = responseCookie(r)
|
|
if c == nil {
|
|
t.Fatal("SERVFAIL response has no COOKIE option")
|
|
}
|
|
gotClient, _ = decodeCookie(t, c)
|
|
mustEqual(t, "SERVFAIL response client cookie", gotClient, clientCookie)
|
|
}
|
|
|
|
// newProbeQuery builds a question-less query (QDCOUNT=0) carrying a
|
|
// COOKIE option, as used for the RFC 7873 section 5.4 server-cookie
|
|
// learning probe.
|
|
func newProbeQuery(clientCookie, serverCookie []byte) *dns.Msg {
|
|
q := new(dns.Msg)
|
|
q.RecursionDesired = true
|
|
q.SetEdns0(1232, false)
|
|
cookie := append(append([]byte{}, clientCookie...), serverCookie...)
|
|
q.IsEdns0().Option = append(q.IsEdns0().Option, &dns.EDNS0_COOKIE{
|
|
Code: dns.EDNS0COOKIE,
|
|
Cookie: hex.EncodeToString(cookie),
|
|
})
|
|
return q
|
|
}
|
|
|
|
func TestHandleQueryMalformedCookie(t *testing.T) {
|
|
resetMaps()
|
|
qcache.Flush()
|
|
hostsMap["cookie.test"] = []string{"192.0.2.10"}
|
|
|
|
// RFC 7873 section 5.2.2: valid COOKIE option lengths are 8 and 16 to
|
|
// 40 inclusive; anything else must be answered with FORMERR.
|
|
for _, length := range []int{4, 12, 41} {
|
|
w := &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
|
|
handleQuery(w, newCookieQuery("cookie.test", dns.TypeA, make([]byte, length)))
|
|
if w.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w.msg.Rcode != dns.RcodeFormatError {
|
|
t.Errorf("COOKIE option of %d bytes: rcode = %d, want FORMERR", length, w.msg.Rcode)
|
|
}
|
|
if c := responseCookie(w.msg); c != nil {
|
|
t.Errorf("COOKIE option of %d bytes: FORMERR response carries a COOKIE option (%s)", length, c.Cookie)
|
|
}
|
|
}
|
|
|
|
// Valid lengths are answered normally, with the client cookie echoed
|
|
// and a fresh server cookie minted.
|
|
for _, length := range []int{8, 16, 40} {
|
|
w := &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
|
|
handleQuery(w, newCookieQuery("cookie.test", dns.TypeA, make([]byte, length)))
|
|
if w.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w.msg.Rcode != dns.RcodeSuccess {
|
|
t.Errorf("COOKIE option of %d bytes: rcode = %d, want NOERROR", length, w.msg.Rcode)
|
|
}
|
|
if len(w.msg.Answer) != 1 {
|
|
t.Errorf("COOKIE option of %d bytes: answers = %d, want 1", length, len(w.msg.Answer))
|
|
}
|
|
if c := responseCookie(w.msg); c == nil {
|
|
t.Errorf("COOKIE option of %d bytes: response has no COOKIE option", length)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHandleQueryCookieProbe(t *testing.T) {
|
|
resetMaps()
|
|
qcache.Flush()
|
|
|
|
clientCookie := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
serverCookie := makeServerCookie("192.0.2.10", clientCookie, time.Now())
|
|
|
|
// Probe with a client cookie only: NOERROR with a fresh server
|
|
// cookie (RFC 7873 section 5.4).
|
|
w := &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
|
|
handleQuery(w, newProbeQuery(clientCookie, nil))
|
|
if w.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w.msg.Rcode != dns.RcodeSuccess {
|
|
t.Errorf("rcode = %d, want NOERROR for a client-cookie-only probe", w.msg.Rcode)
|
|
}
|
|
if len(w.msg.Question) != 0 || len(w.msg.Answer) != 0 {
|
|
t.Error("probe response must have an empty question and answer section")
|
|
}
|
|
c := responseCookie(w.msg)
|
|
if c == nil {
|
|
t.Fatal("probe response has no COOKIE option")
|
|
}
|
|
gotClient, gotServer := decodeCookie(t, c)
|
|
mustEqual(t, "probe response client cookie", gotClient, clientCookie)
|
|
if !validateServerCookie("192.0.2.10", clientCookie, gotServer, time.Now()) {
|
|
t.Error("probe response server cookie does not validate")
|
|
}
|
|
|
|
// Probe re-establishing a valid server cookie: NOERROR.
|
|
w = &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
|
|
handleQuery(w, newProbeQuery(clientCookie, serverCookie))
|
|
if w.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w.msg.Rcode != dns.RcodeSuccess {
|
|
t.Errorf("rcode = %d, want NOERROR for a probe with a valid server cookie", w.msg.Rcode)
|
|
}
|
|
|
|
// Probe re-establishing an invalid server cookie: BADCOOKIE, with a
|
|
// fresh server cookie so the client can retry (RFC 7873 sections 5.4
|
|
// and 5.3).
|
|
w = &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
|
|
handleQuery(w, newProbeQuery(clientCookie, make([]byte, serverCookieLen)))
|
|
if w.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w.msg.Rcode != dns.RcodeBadCookie {
|
|
t.Errorf("rcode = %d, want BADCOOKIE for a probe with an invalid server cookie", w.msg.Rcode)
|
|
}
|
|
c = responseCookie(w.msg)
|
|
if c == nil {
|
|
t.Fatal("BADCOOKIE response has no COOKIE option")
|
|
}
|
|
gotClient, _ = decodeCookie(t, c)
|
|
mustEqual(t, "BADCOOKIE response client cookie", gotClient, clientCookie)
|
|
}
|