domains: verify the zone before a fresh answer (#724, Astra R2)
The cached LookupNS follows a CNAME and drops the owner of the NS records, so it can name the wrong zone. A zone is now used only after the server about to be asked answers, with authority, an IN SOA owned by exactly that name; the A answer and the in-zone CNAME test both use that verified zone. A level the cache knows to be an alias is skipped without asking anyone. Two servers per zone, 4 s per lookup, so a zone of silent servers leaves time for the next. Special-use names (.internal sidecars) are no longer eligible in freshUpgrade, so they take no share of the 64-lookup pass budget. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
Zone verified by its own SOA before any fresh answer; aliased levels skipped; special-use names out of the budget.
8 files changed,
+301
−55
docs/superpowers/notes/census-proposal-dns-fresh-lookup-724.md+4 −4internal/domains/freshdns.go+96 −17internal/domains/freshdns_stamp_test.go+32 −0internal/domains/freshdns_test.go+153 −20internal/domains/reconcile.go+4 −3internal/host/agent.go+3 −2internal/provision/testdata/plan14-root-reads-chain.tsv+1 −1internal/provision/testdata/plan14-root-reads-dns-fresh-lookup-724.tsv+8 −8
CI passed — run details
diff --git a/docs/superpowers/notes/census-proposal-dns-fresh-lookup-724.md b/docs/superpowers/notes/census-proposal-dns-fresh-lookup-724.md| index 361181038..451736ef1 100644 |
| --- a/docs/superpowers/notes/census-proposal-dns-fresh-lookup-724.md |
| +++ b/docs/superpowers/notes/census-proposal-dns-fresh-lookup-724.md |
| @@ -4,8 +4,8 @@ Ruling `census-ruling-dns-fresh-lookup-724`. The independent reviewer approves o |
| | unit | change | read sites | what changed | |
| |---|---|---|---| |
| -| `internal/host/agent.go` `domainsTickConfig` | body | unchanged (2) | wires ResolveFresh to a FreshResolver over net.DefaultResolver (#724) | |
| -| `internal/domains/reconcile.go` `StampHealth` | sites | +registry:Alias, +registry:Host, +whole:Route ×3, +whole:RouteAlias ×3 | section 4 now computes every cached verdict first, then freshUpgrade gives up to 64 randomly ordered non-apps-domain names whose verdict is "" or "elsewhere" an uncached lookup (30 s pass budget) that may only upgrade to "pointing", then stamps; the extra sites are the indexed loops over the same rows (#724) | |
| +| `internal/host/agent.go` `domainsTickConfig` | body | unchanged (2) | wires ResolveFresh to a FreshResolver over net.DefaultResolver (LookupNS, LookupHost, LookupCNAME) (#724) | |
| +| `internal/domains/reconcile.go` `StampHealth` | sites | +registry:Alias, +registry:Host, +whole:Route ×3, +whole:RouteAlias ×3 | section 4 now computes every cached verdict first, then freshUpgrade gives up to 64 randomly ordered names that could have a public zone (not under the apps domain, not special-use like .internal) and whose verdict is "" or "elsewhere" an uncached lookup (30 s pass budget) that may only upgrade to "pointing", then stamps; the extra sites are the indexed loops over the same rows (#724) | |
| Envelopes re-pinned: `internal/domains`, `internal/host`. |
| @@ -18,8 +18,8 @@ Recorded dispositions, which each change must still satisfy: |
| New dispositions, which census-propose wrote and the reviewer checks against the unit's reads and the diff: |
| -- new `internal/domains/freshdns.go:FreshResolver.ask`: reads +file:Read; written legacy, accepted-legacy, frozen |
| +- new `internal/domains/freshdns.go:FreshResolver.exchange`: reads +file:Read; written legacy, accepted-legacy, frozen |
| -Seal: `2774b43ac5970e4340339b859204bce5eb9f83d4c2551f0984e919a2853d0216` → `44917f6a61196f613c10e47a4778fb800d1314f369119bd6e50a79bdea1d4a83`. |
| +Seal: `2774b43ac5970e4340339b859204bce5eb9f83d4c2551f0984e919a2853d0216` → `800e4282621795e0aed7d14d0b77396af0ebac977975d3262b9e732771771269`. |
| Overlay: `internal/provision/testdata/plan14-root-reads-dns-fresh-lookup-724.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/internal/domains/freshdns.go b/internal/domains/freshdns.go| index 96483fb5a..b05641a30 100644 |
| --- a/internal/domains/freshdns.go |
| +++ b/internal/domains/freshdns.go |
| @@ -36,12 +36,16 @@ import ( |
| type FreshResolver struct { |
| LookupNS func(ctx context.Context, name string) ([]*net.NS, error) |
| LookupHost func(ctx context.Context, host string) ([]string, error) |
| + // LookupCNAME is the cached resolver's canonical name, used only to |
| + // skip a level that is an alias; nil skips that shortcut (verifyZone |
| + // still refuses a wrong zone). |
| + LookupCNAME func(ctx context.Context, host string) (string, error) |
| // Dial opens the UDP socket to a nameserver that has already passed |
| // publicNameserver; nil is a net.Dialer. A seam for tests, which point |
| // a public test address at a loopback server. |
| Dial func(ctx context.Context, network, address string) (net.Conn, error) |
| - // Timeout bounds one whole Resolve; 0 is 3 s, the cached seam's own |
| - // budget (internal/host resolveHost). |
| + // Timeout bounds one whole Resolve; 0 is 4 s: two silent servers at |
| + // AttemptTimeout each, then a verified zone's two queries. |
| Timeout time.Duration |
| // AttemptTimeout bounds one nameserver inside it; 0 is 1 s, so a |
| // silent server leaves time for the next. |
| @@ -61,8 +65,9 @@ type FreshResolver struct { |
| var errNoAuthority = errors.New("fresh dns: no authoritative answer") |
| const ( |
| - // freshMaxServers bounds the nameserver addresses tried per zone. |
| - freshMaxServers = 3 |
| + // freshMaxServers bounds the nameserver addresses tried per zone, so a |
| + // zone of silent servers leaves time for the next zone up. |
| + freshMaxServers = 2 |
| // freshMaxZones bounds the enclosing zones tried, nearest first. More |
| // than one, because a cached CNAME at the name can make the nearest NS |
| // set belong to the CNAME target's zone, which then refuses. |
| @@ -82,13 +87,21 @@ var freshSkipTLDs = map[string]bool{ |
| "onion": true, "arpa": true, "alt": true, "home": true, "lan": true, |
| } |
| +// freshPublicName reports whether name (lower case, no trailing dot) could |
| +// live in a public zone at all: more than one label, and not under a |
| +// special-use top-level name. Resolve refuses the rest, and freshUpgrade |
| +// leaves them out before they can take a share of a pass's budget. |
| +func freshPublicName(name string) bool { |
| + dot := strings.LastIndex(name, ".") |
| + return dot > 0 && !freshSkipTLDs[name[dot+1:]] |
| +} |
| + |
| // Resolve returns host's addresses as its zone's nameservers give them now. |
| // An authoritative "no such name" is a *net.DNSError with IsNotFound, the |
| // same shape the cached resolver gives, so callers classify both alike. |
| func (r *FreshResolver) Resolve(host string) ([]string, error) { |
| name := strings.ToLower(strings.TrimSuffix(host, ".")) |
| - dot := strings.LastIndex(name, ".") |
| - if dot <= 0 || freshSkipTLDs[name[dot+1:]] { |
| + if !freshPublicName(name) { |
| return nil, fmt.Errorf("%w: %s has no public zone", errNoAuthority, name) |
| } |
| now := time.Now |
| @@ -114,7 +127,7 @@ func (r *FreshResolver) Resolve(host string) ([]string, error) { |
| timeout := r.Timeout |
| if timeout == 0 { |
| - timeout = 3 * time.Second |
| + timeout = 4 * time.Second |
| } |
| ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| defer cancel() |
| @@ -148,13 +161,26 @@ func isNotFound(err error) bool { |
| // resolve walks the enclosing zones of name, nearest first, and asks each |
| // zone's nameservers until one gives a definitive authoritative answer (the |
| -// addresses, or "no such name"). A zone whose servers all refuse, answer |
| -// without authority, or stay silent passes the question up to the next. |
| -// It stops below the top level: a TLD's servers only refer back down. |
| +// addresses, or "no such name"). It stops below the top level: a TLD's |
| +// servers only refer back down. |
| +// |
| +// A zone is only used once it is VERIFIED: the server about to be asked |
| +// answers, with authority, an SOA record owned by exactly that name. The |
| +// cached LookupNS cannot be trusted to name the zone on its own, because an |
| +// NS query follows a CNAME and Go drops the owner of the records it returns: |
| +// a cached "shop.example.com CNAME old-provider.net" hands back |
| +// old-provider.net's servers as if they were shop.example.com's. Verifying |
| +// at the server keeps every fresh answer, and the in-zone test on a CNAME |
| +// target, tied to the zone that really holds the name. A level the cached |
| +// resolver already knows to be an alias is skipped without asking anyone, |
| +// since a CNAME cannot sit at a zone apex. |
| func (r *FreshResolver) resolve(ctx context.Context, name string) ([]string, error) { |
| last := fmt.Errorf("%w: no zone found for %s", errNoAuthority, name) |
| zones := 0 |
| for zone := name; strings.Contains(zone, "."); zone = zone[strings.Index(zone, ".")+1:] { |
| + if r.aliased(ctx, zone) { |
| + continue |
| + } |
| nss, err := r.LookupNS(ctx, zone) |
| if err != nil || len(nss) == 0 { |
| if ctx.Err() != nil { |
| @@ -171,6 +197,13 @@ func (r *FreshResolver) resolve(ctx context.Context, name string) ([]string, err |
| continue |
| } |
| for _, s := range servers { |
| + if err := r.verifyZone(ctx, s, zone); err != nil { |
| + last = err |
| + if ctx.Err() != nil { |
| + return nil, last |
| + } |
| + continue |
| + } |
| ips, err := r.ask(ctx, s, name, zone) |
| if err == nil || isNotFound(err) { |
| return ips, err |
| @@ -184,6 +217,19 @@ func (r *FreshResolver) resolve(ctx context.Context, name string) ([]string, err |
| return nil, last |
| } |
| +// aliased reports whether the cached resolver says zone is a CNAME to some |
| +// other name. Only a definite answer counts; a failed lookup is not one. |
| +func (r *FreshResolver) aliased(ctx context.Context, zone string) bool { |
| + if r.LookupCNAME == nil { |
| + return false |
| + } |
| + canon, err := r.LookupCNAME(ctx, zone) |
| + if err != nil || canon == "" { |
| + return false |
| + } |
| + return !strings.EqualFold(strings.TrimSuffix(canon, "."), zone) |
| +} |
| + |
| // nameserverAddrs resolves the NS hosts (in random order) through the |
| // cached resolver and keeps up to freshMaxServers addresses that pass |
| // publicNameserver, IPv4 first: a box without a working IPv6 route would |
| @@ -252,12 +298,10 @@ func publicNameserver(ip netip.Addr) bool { |
| return true |
| } |
| -// ask sends one non-recursive IN A query for name to server and reads the |
| -// authoritative reply to exactly that question. A CNAME chain in the reply |
| -// is followed; if it ends outside zone with no address in the reply, the |
| -// target goes to the cached LookupHost. A chain that loops, runs past |
| -// freshMaxCNAMEs, or ends inside zone with no address is not an answer. |
| -func (r *FreshResolver) ask(ctx context.Context, server netip.AddrPort, name, zone string) ([]string, error) { |
| +// exchange sends one non-recursive IN query of type qtype for name to |
| +// server and returns the reply to exactly that question, which must carry |
| +// authority and not be truncated. The RCODE is the caller's to read. |
| +func (r *FreshResolver) exchange(ctx context.Context, server netip.AddrPort, name string, qtype dnsmessage.Type) (*dnsmessage.Message, error) { |
| if !publicNameserver(server.Addr()) { |
| return nil, fmt.Errorf("%w: refusing non-public nameserver %s", errNoAuthority, server) |
| } |
| @@ -274,7 +318,7 @@ func (r *FreshResolver) ask(ctx context.Context, server netip.AddrPort, name, zo |
| return nil, err |
| } |
| id := uint16(rand.Uint32()) |
| - question := dnsmessage.Question{Name: qname, Type: dnsmessage.TypeA, Class: dnsmessage.ClassINET} |
| + question := dnsmessage.Question{Name: qname, Type: qtype, Class: dnsmessage.ClassINET} |
| query := dnsmessage.Message{Header: dnsmessage.Header{ID: id}, Questions: []dnsmessage.Question{question}} |
| packed, err := query.Pack() |
| if err != nil { |
| @@ -316,6 +360,41 @@ func (r *FreshResolver) ask(ctx context.Context, server netip.AddrPort, name, zo |
| if reply.Truncated { |
| return nil, fmt.Errorf("%w: truncated reply from %s", errNoAuthority, server) |
| } |
| + return &reply, nil |
| +} |
| + |
| +// verifyZone checks that server holds zone: an authoritative answer with an |
| +// IN SOA record owned by exactly zone. A server for some other zone (the |
| +// CNAME target's, say) refuses, answers without authority, or puts its own |
| +// apex's SOA in the authority section, and fails this. |
| +func (r *FreshResolver) verifyZone(ctx context.Context, server netip.AddrPort, zone string) error { |
| + reply, err := r.exchange(ctx, server, zone, dnsmessage.TypeSOA) |
| + if err != nil { |
| + return err |
| + } |
| + if reply.RCode == dnsmessage.RCodeSuccess { |
| + for _, rr := range reply.Answers { |
| + if _, ok := rr.Body.(*dnsmessage.SOAResource); ok && rr.Header.Class == dnsmessage.ClassINET && |
| + strings.EqualFold(rr.Header.Name.String(), zone+".") { |
| + return nil |
| + } |
| + } |
| + } |
| + return fmt.Errorf("%w: %s does not hold zone %s", errNoAuthority, server, zone) |
| +} |
| + |
| +// ask sends one non-recursive IN A query for name to server, which |
| +// verifyZone has shown holds zone, and reads the answer. A CNAME chain in |
| +// the reply is followed; if it ends outside zone with no address in the |
| +// reply, the target goes to the cached LookupHost. A chain that loops, runs |
| +// past freshMaxCNAMEs, or ends inside zone with no address is not an |
| +// answer. |
| +func (r *FreshResolver) ask(ctx context.Context, server netip.AddrPort, name, zone string) ([]string, error) { |
| + reply, err := r.exchange(ctx, server, name, dnsmessage.TypeA) |
| + if err != nil { |
| + return nil, err |
| + } |
| + fqdn := name + "." |
| switch reply.RCode { |
| case dnsmessage.RCodeSuccess: |
| case dnsmessage.RCodeNameError: |
diff --git a/internal/domains/freshdns_stamp_test.go b/internal/domains/freshdns_stamp_test.go| index e6100fa41..bc663c777 100644 |
| --- a/internal/domains/freshdns_stamp_test.go |
| +++ b/internal/domains/freshdns_stamp_test.go |
| @@ -3,6 +3,7 @@ package domains_test |
| import ( |
| "context" |
| "errors" |
| + "fmt" |
| "net" |
| "testing" |
| "time" |
| @@ -216,3 +217,34 @@ func TestStampHealthFreshLookupsAreBoundedAndFair(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// Astra R2 P2-3: names that can never get a fresh lookup (sidecar hosts |
| +// under .internal) take no share of the budget, so a customer alias is |
| +// asked every pass however many of them there are. |
| +func TestStampHealthSpecialUseNamesTakeNoBudget(t *testing.T) { |
| + defer domains.SetFreshPassMax(1)() |
| + routes := []registry.Route{{Host: "app.acct.oncarlos.test", Kind: "service", Addr: "10.0.0.1:1", Status: "ready"}} |
| + for i := 0; i < 80; i++ { |
| + routes = append(routes, registry.Route{Host: fmt.Sprintf("bot%d.app.acct.sidecar.internal", i), Kind: "service", Addr: "10.0.0.1:1", Status: "ready"}) |
| + } |
| + reg := regWith(t, routes...) |
| + if err := reg.UpsertAlias(registry.RouteAlias{Alias: "shop.example.com", Host: "app.acct.oncarlos.test", Account: "acct", App: "app"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + var calls []string |
| + cfg := freshCfg(func(h string) ([]string, error) { |
| + if h == "app.acct.oncarlos.test" { |
| + return pointingAt(h) |
| + } |
| + return nxdomain(h) |
| + }, nxdomain, &calls) |
| + for pass := 0; pass < 5; pass++ { |
| + calls = nil |
| + if err := domains.StampHealth(context.Background(), reg, cfg, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + if len(calls) != 1 || calls[0] != "shop.example.com" { |
| + t.Fatalf("pass %d fresh lookups = %v, want [shop.example.com]", pass, calls) |
| + } |
| + } |
| +} |
diff --git a/internal/domains/freshdns_test.go b/internal/domains/freshdns_test.go| index 0ab23bae0..7d6fd2cd9 100644 |
| --- a/internal/domains/freshdns_test.go |
| +++ b/internal/domains/freshdns_test.go |
| @@ -16,15 +16,27 @@ import ( |
| "github.com/carlosframework/platform/internal/domains" |
| ) |
| -// fakeAuthority is a nameserver on loopback. answer builds the reply to |
| -// each query; returning false drops the query (a silent nameserver). |
| +// fakeAuthority is a nameserver on loopback for zone. It answers SOA |
| +// queries itself, as a real server for zone would: the apex's SOA with |
| +// authority, an authoritative NODATA inside the zone, REFUSED outside it. |
| +// An empty zone hands SOA queries to answer too. answer builds the reply |
| +// to every other query; returning false drops the |
| +// query. A nil answer is a silent server that drops everything. |
| type fakeAuthority struct { |
| addr string |
| queries atomic.Int32 |
| - asked atomic.Value // last question name |
| + asked atomic.Value // last A question name |
| } |
| -func startAuthority(t *testing.T, answer func(q dnsmessage.Question, reply *dnsmessage.Message) bool) *fakeAuthority { |
| +func soaRecord(zone string) dnsmessage.Resource { |
| + return dnsmessage.Resource{ |
| + Header: dnsmessage.ResourceHeader{Name: dnsmessage.MustNewName(zone + "."), Type: dnsmessage.TypeSOA, Class: dnsmessage.ClassINET, TTL: 300}, |
| + Body: &dnsmessage.SOAResource{NS: dnsmessage.MustNewName("ns1." + zone + "."), MBox: dnsmessage.MustNewName("hostmaster." + zone + "."), |
| + Serial: 1, Refresh: 3600, Retry: 600, Expire: 86400, MinTTL: 1800}, |
| + } |
| +} |
| + |
| +func startAuthority(t *testing.T, zone string, answer func(q dnsmessage.Question, reply *dnsmessage.Message) bool) *fakeAuthority { |
| t.Helper() |
| pc, err := net.ListenPacket("udp", "127.0.0.1:0") |
| if err != nil { |
| @@ -44,11 +56,29 @@ func startAuthority(t *testing.T, answer func(q dnsmessage.Question, reply *dnsm |
| continue |
| } |
| fa.queries.Add(1) |
| - fa.asked.Store(q.Questions[0].Name.String()) |
| - reply := dnsmessage.Message{Header: dnsmessage.Header{ID: q.ID, Response: true}, Questions: q.Questions} |
| - if !answer(q.Questions[0], &reply) { |
| + if answer == nil { |
| continue |
| } |
| + qq := q.Questions[0] |
| + reply := dnsmessage.Message{Header: dnsmessage.Header{ID: q.ID, Response: true}, Questions: q.Questions} |
| + if qq.Type == dnsmessage.TypeSOA && zone != "" { |
| + qn := strings.TrimSuffix(qq.Name.String(), ".") |
| + switch { |
| + case qn == zone: |
| + reply.Authoritative = true |
| + reply.Answers = []dnsmessage.Resource{soaRecord(zone)} |
| + case strings.HasSuffix(qn, "."+zone): |
| + reply.Authoritative = true |
| + reply.Authorities = []dnsmessage.Resource{soaRecord(zone)} |
| + default: |
| + reply.RCode = dnsmessage.RCodeRefused |
| + } |
| + } else { |
| + fa.asked.Store(qq.Name.String()) |
| + if !answer(qq, &reply) { |
| + continue |
| + } |
| + } |
| out, err := reply.Pack() |
| if err != nil { |
| t.Errorf("pack reply: %v", err) |
| @@ -92,6 +122,7 @@ type world struct { |
| ns map[string][]string // zone -> NS hosts |
| hosts map[string][]string // name -> addresses (the cache) |
| routes map[string]*fakeAuthority // "ip:53" -> server |
| + cnames map[string]string // name -> cached canonical name |
| mu sync.Mutex |
| dialed []string |
| } |
| @@ -115,6 +146,12 @@ func (w *world) resolver(now *time.Time) *domains.FreshResolver { |
| } |
| return nil, errNotFound |
| }, |
| + LookupCNAME: func(_ context.Context, host string) (string, error) { |
| + if c, ok := w.cnames[host]; ok { |
| + return c, nil |
| + } |
| + return "", errNotFound |
| + }, |
| Dial: func(ctx context.Context, network, address string) (net.Conn, error) { |
| w.mu.Lock() |
| w.dialed = append(w.dialed, address) |
| @@ -147,7 +184,7 @@ func oneZone(fa *fakeAuthority) *world { |
| func clock() *time.Time { t := time.Unix(1000, 0); return &t } |
| func TestFreshResolverReadsTheAuthoritativeA(t *testing.T) { |
| - fa := startAuthority(t, authoritative(aRecord("shop.example.com.", edgeIP))) |
| + fa := startAuthority(t, "example.com", authoritative(aRecord("shop.example.com.", edgeIP))) |
| ips, err := oneZone(fa).resolver(clock()).Resolve("shop.example.com") |
| if err != nil || len(ips) != 1 || ips[0] != edgeIP { |
| t.Fatalf("Resolve = %v, %v; want [%s]", ips, err, edgeIP) |
| @@ -160,7 +197,7 @@ func TestFreshResolverReadsTheAuthoritativeA(t *testing.T) { |
| func TestFreshResolverFollowsAnOutOfZoneCNAME(t *testing.T) { |
| // The usual custom-domain setup: the customer's zone holds only the |
| // CNAME, and the target is ours. |
| - fa := startAuthority(t, authoritative(cnameRecord("shop.example.com.", "edge.oncarlos.test."))) |
| + fa := startAuthority(t, "example.com", authoritative(cnameRecord("shop.example.com.", "edge.oncarlos.test."))) |
| ips, err := oneZone(fa).resolver(clock()).Resolve("shop.example.com") |
| if err != nil || len(ips) != 1 || ips[0] != edgeIP { |
| t.Fatalf("Resolve = %v, %v; want [%s] via the CNAME target", ips, err, edgeIP) |
| @@ -168,7 +205,7 @@ func TestFreshResolverFollowsAnOutOfZoneCNAME(t *testing.T) { |
| } |
| func TestFreshResolverReportsAuthoritativeNXDOMAIN(t *testing.T) { |
| - fa := startAuthority(t, func(_ dnsmessage.Question, r *dnsmessage.Message) bool { |
| + fa := startAuthority(t, "example.com", func(_ dnsmessage.Question, r *dnsmessage.Message) bool { |
| r.Authoritative = true |
| r.RCode = dnsmessage.RCodeNameError |
| return true |
| @@ -181,7 +218,7 @@ func TestFreshResolverReportsAuthoritativeNXDOMAIN(t *testing.T) { |
| } |
| func TestFreshResolverIgnoresNonAuthoritativeAnswers(t *testing.T) { |
| - fa := startAuthority(t, func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| + fa := startAuthority(t, "example.com", func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| r.Answers = []dnsmessage.Resource{aRecord(q.Name.String(), edgeIP)} |
| return true |
| }) |
| @@ -191,7 +228,7 @@ func TestFreshResolverIgnoresNonAuthoritativeAnswers(t *testing.T) { |
| } |
| func TestFreshResolverRejectsAnswersForOtherNames(t *testing.T) { |
| - fa := startAuthority(t, authoritative(aRecord("other.example.com.", edgeIP))) |
| + fa := startAuthority(t, "example.com", authoritative(aRecord("other.example.com.", edgeIP))) |
| if ips, err := oneZone(fa).resolver(clock()).Resolve("shop.example.com"); err == nil { |
| t.Fatalf("Resolve = %v, want an error: the answer names another host", ips) |
| } |
| @@ -208,7 +245,7 @@ func TestFreshResolverRejectsRepliesToOtherQuestions(t *testing.T) { |
| } |
| for name, mangle := range cases { |
| t.Run(name, func(t *testing.T) { |
| - fa := startAuthority(t, func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| + fa := startAuthority(t, "example.com", func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| r.Authoritative = true |
| r.Answers = []dnsmessage.Resource{aRecord(q.Name.String(), edgeIP)} |
| mangle(r) |
| @@ -254,8 +291,8 @@ func TestFreshResolverNeverDialsANonPublicNameserver(t *testing.T) { |
| // Astra R1 P2-3: a silent server costs one attempt, not the whole lookup. |
| func TestFreshResolverMovesPastASilentServer(t *testing.T) { |
| - silent := startAuthority(t, func(dnsmessage.Question, *dnsmessage.Message) bool { return false }) |
| - healthy := startAuthority(t, authoritative(aRecord("shop.example.com.", edgeIP))) |
| + silent := startAuthority(t, "example.com", nil) |
| + healthy := startAuthority(t, "example.com", authoritative(aRecord("shop.example.com.", edgeIP))) |
| w := &world{ |
| ns: map[string][]string{"example.com": {"ns1.example.com."}}, |
| hosts: map[string][]string{"ns1.example.com": {"192.0.2.1", "192.0.2.2"}}, |
| @@ -271,11 +308,11 @@ func TestFreshResolverMovesPastASilentServer(t *testing.T) { |
| // CNAME target's zone. Its refusal must pass the question up to the zone |
| // that really holds the name. |
| func TestFreshResolverClimbsPastAWrongZone(t *testing.T) { |
| - wrong := startAuthority(t, func(_ dnsmessage.Question, r *dnsmessage.Message) bool { |
| + wrong := startAuthority(t, "oldprovider.net", func(_ dnsmessage.Question, r *dnsmessage.Message) bool { |
| r.RCode = dnsmessage.RCodeRefused |
| return true |
| }) |
| - right := startAuthority(t, authoritative(aRecord("shop.example.com.", edgeIP))) |
| + right := startAuthority(t, "example.com", authoritative(aRecord("shop.example.com.", edgeIP))) |
| w := &world{ |
| ns: map[string][]string{ |
| "shop.example.com": {"ns.oldprovider.net."}, |
| @@ -306,7 +343,7 @@ func TestFreshResolverRefusesBrokenCNAMEChains(t *testing.T) { |
| } |
| for name, rrs := range cases { |
| t.Run(name, func(t *testing.T) { |
| - fa := startAuthority(t, authoritative(rrs...)) |
| + fa := startAuthority(t, "example.com", authoritative(rrs...)) |
| w := oneZone(fa) |
| w.hosts["www.example.com"] = []string{edgeIP} // stale cache |
| w.hosts["a.example.com"] = []string{edgeIP} |
| @@ -323,14 +360,14 @@ func TestFreshResolverRefusesBrokenCNAMEChains(t *testing.T) { |
| prev = next |
| } |
| long = append(long, aRecord(prev, edgeIP)) |
| - fa := startAuthority(t, authoritative(long...)) |
| + fa := startAuthority(t, "example.com", authoritative(long...)) |
| if ips, err := oneZone(fa).resolver(clock()).Resolve("shop.example.com"); err == nil { |
| t.Fatalf("Resolve over a 12-hop chain = %v, want an error", ips) |
| } |
| } |
| func TestFreshResolverBacksOffAfterAFailure(t *testing.T) { |
| - fa := startAuthority(t, func(dnsmessage.Question, *dnsmessage.Message) bool { return false }) |
| + fa := startAuthority(t, "example.com", nil) |
| now := clock() |
| r := oneZone(fa).resolver(now) |
| if _, err := r.Resolve("shop.example.com"); err == nil { |
| @@ -395,3 +432,99 @@ func TestFreshResolverSkipsSpecialUseNames(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// Astra R2 P2-1: a cached CNAME at the name hands LookupNS the target's |
| +// servers. A server that is not the name's authority must never supply the |
| +// answer, even when it marks its reply authoritative. |
| +func TestFreshResolverNeverTrustsAWrongZoneServer(t *testing.T) { |
| + wrong := startAuthority(t, "oldprovider.net", authoritative(aRecord("shop.example.com.", edgeIP))) |
| + right := startAuthority(t, "example.com", func(_ dnsmessage.Question, r *dnsmessage.Message) bool { |
| + r.Authoritative = true |
| + r.RCode = dnsmessage.RCodeNameError |
| + return true |
| + }) |
| + w := &world{ |
| + ns: map[string][]string{ |
| + "shop.example.com": {"ns.oldprovider.net."}, |
| + "example.com": {"ns1.example.com."}, |
| + }, |
| + hosts: map[string][]string{ |
| + "ns.oldprovider.net": {"192.0.2.9"}, |
| + "ns1.example.com": {"192.0.2.53"}, |
| + }, |
| + routes: map[string]*fakeAuthority{"192.0.2.9:53": wrong, "192.0.2.53:53": right}, |
| + } |
| + ips, err := w.resolver(clock()).Resolve("shop.example.com") |
| + if !isNotFoundErr(err) { |
| + t.Fatalf("Resolve = %v, %v; want the real zone's NXDOMAIN, not the wrong zone's address", ips, err) |
| + } |
| +} |
| + |
| +// A level the cache already knows to be an alias is not a zone: its |
| +// (silent, here) servers are never dialled, so they cannot use up the time. |
| +func TestFreshResolverSkipsAnAliasedLevel(t *testing.T) { |
| + silent := startAuthority(t, "oldprovider.net", nil) |
| + right := startAuthority(t, "example.com", authoritative(aRecord("shop.example.com.", edgeIP))) |
| + w := &world{ |
| + ns: map[string][]string{ |
| + "shop.example.com": {"ns.oldprovider.net."}, |
| + "example.com": {"ns1.example.com."}, |
| + }, |
| + hosts: map[string][]string{ |
| + "ns.oldprovider.net": {"192.0.2.9", "192.0.2.10"}, |
| + "ns1.example.com": {"192.0.2.53"}, |
| + }, |
| + routes: map[string]*fakeAuthority{"192.0.2.9:53": silent, "192.0.2.10:53": silent, "192.0.2.53:53": right}, |
| + cnames: map[string]string{"shop.example.com": "old-provider.net."}, |
| + } |
| + ips, err := w.resolver(clock()).Resolve("shop.example.com") |
| + if err != nil || len(ips) != 1 || ips[0] != edgeIP { |
| + t.Fatalf("Resolve = %v, %v; want [%s]", ips, err, edgeIP) |
| + } |
| + for _, d := range w.dialed { |
| + if d != "192.0.2.53:53" { |
| + t.Fatalf("dialled %v, want only the real zone's server", w.dialed) |
| + } |
| + } |
| +} |
| + |
| +// Astra R2 P2-2: with a cached "shop.example.com CNAME example.com", |
| +// LookupNS(shop.example.com) returns example.com's own servers. The zone |
| +// must still come out as example.com, so an in-zone target with no |
| +// address is refused rather than filled in from a stale cache. |
| +func TestFreshResolverInZoneTestUsesTheVerifiedZone(t *testing.T) { |
| + fa := startAuthority(t, "example.com", authoritative(cnameRecord("shop.example.com.", "www.example.com."))) |
| + w := oneZone(fa) |
| + w.ns["shop.example.com"] = []string{"ns1.example.com."} |
| + w.hosts["www.example.com"] = []string{edgeIP} // stale cache |
| + if ips, err := w.resolver(clock()).Resolve("shop.example.com"); err == nil { |
| + t.Fatalf("Resolve = %v, want an error: the in-zone target has no address", ips) |
| + } |
| +} |
| + |
| +func isNotFoundErr(err error) bool { |
| + var dnsErr *net.DNSError |
| + return errors.As(err, &dnsErr) && dnsErr.IsNotFound |
| +} |
| + |
| +// A sloppy server puts its own apex's SOA in the answer section whatever |
| +// name it is asked about. Only an SOA owned by the asked name verifies a |
| +// zone, or shop.example.com would pass as a zone and www.example.com would |
| +// look out-of-zone, open to the stale cache. |
| +func TestFreshResolverVerifiesTheSOAOwner(t *testing.T) { |
| + fa := startAuthority(t, "", func(q dnsmessage.Question, r *dnsmessage.Message) bool { |
| + r.Authoritative = true |
| + if q.Type == dnsmessage.TypeSOA { |
| + r.Answers = []dnsmessage.Resource{soaRecord("example.com")} |
| + return true |
| + } |
| + r.Answers = []dnsmessage.Resource{cnameRecord("shop.example.com.", "www.example.com.")} |
| + return true |
| + }) |
| + w := oneZone(fa) |
| + w.ns["shop.example.com"] = []string{"ns1.example.com."} |
| + w.hosts["www.example.com"] = []string{edgeIP} // stale cache |
| + if ips, err := w.resolver(clock()).Resolve("shop.example.com"); err == nil { |
| + t.Fatalf("Resolve = %v, want an error", ips) |
| + } |
| +} |
diff --git a/internal/domains/reconcile.go b/internal/domains/reconcile.go| index 6256a10fe..61f120601 100644 |
| --- a/internal/domains/reconcile.go |
| +++ b/internal/domains/reconcile.go |
| @@ -619,8 +619,9 @@ var ( |
| // verdict as it was, so this path can never unpublish anything or turn an |
| // answer into "unresolved". A cached lookup that FAILED is left alone too: |
| // that is our resolver's fault, handled by section 4's hold-last-verdict |
| -// rule. Names under the apps domain are the platform's own zone and are |
| -// never asked twice. |
| +// rule. Names under the apps domain are the platform's own zone, and names |
| +// with no possible public zone (sidecar hosts under .internal) have nothing |
| +// to ask; neither is eligible, so neither takes a share of the budget. |
| // |
| // Eligible names are visited in a random order and the pass stops after |
| // freshPassMax lookups or freshPassTime, so a large backlog is worked |
| @@ -636,7 +637,7 @@ func freshUpgrade(cfg Config, edge map[string]bool, candidates []freshCandidate) |
| continue |
| } |
| h := strings.ToLower(strings.TrimSuffix(c.host, ".")) |
| - if apps != "" && (h == apps || strings.HasSuffix(h, "."+apps)) { |
| + if !freshPublicName(h) || (apps != "" && (h == apps || strings.HasSuffix(h, "."+apps))) { |
| continue |
| } |
| eligible = append(eligible, c) |
diff --git a/internal/host/agent.go b/internal/host/agent.go| index d0010704e..dbf3a47e0 100644 |
| --- a/internal/host/agent.go |
| +++ b/internal/host/agent.go |
| @@ -3792,8 +3792,9 @@ func domainsTickConfig(getenv func(string) string, publicIP string) (domains.Con |
| // cached answer says does not point here yet. The cached resolver |
| // still finds the zone's nameservers and our own CNAME target. |
| ResolveFresh: (&domains.FreshResolver{ |
| - LookupNS: net.DefaultResolver.LookupNS, |
| - LookupHost: net.DefaultResolver.LookupHost, |
| + LookupNS: net.DefaultResolver.LookupNS, |
| + LookupHost: net.DefaultResolver.LookupHost, |
| + LookupCNAME: net.DefaultResolver.LookupCNAME, |
| }).Resolve, |
| } |
| // EdgeIPs is deliberately left nil here — a Config out of this function |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 1ebac8060..30d99d395 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -134,4 +134,4 @@ plan14-root-reads-managed-fleets-2b1.tsv d467adec3d86ae220afe98269aa226e87adba1b |
| plan14-root-reads-design-console-secrets.tsv ba2d479f7176757f8c8a4e251c498854a2ae092509ff77d9786f79d10c503c19 |
| plan14-root-reads-edge-page-cache-1.tsv 2774b43ac5970e4340339b859204bce5eb9f83d4c2551f0984e919a2853d0216 |
| plan14-root-reads-cli-default-console-carloku.tsv 2774b43ac5970e4340339b859204bce5eb9f83d4c2551f0984e919a2853d0216 |
| -plan14-root-reads-dns-fresh-lookup-724.tsv 44917f6a61196f613c10e47a4778fb800d1314f369119bd6e50a79bdea1d4a83 |
| +plan14-root-reads-dns-fresh-lookup-724.tsv 800e4282621795e0aed7d14d0b77396af0ebac977975d3262b9e732771771269 |
diff --git a/internal/provision/testdata/plan14-root-reads-dns-fresh-lookup-724.tsv b/internal/provision/testdata/plan14-root-reads-dns-fresh-lookup-724.tsv| index 32e00286c..e5b55a000 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-dns-fresh-lookup-724.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-dns-fresh-lookup-724.tsv |
| @@ -1,11 +1,11 @@ |
| # Census overlay dns-fresh-lookup-724, written by census-propose (#369). census-ruling-dns-fresh-lookup-724, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-dns-fresh-lookup-724.md. |
| # 1 body, 1 sites, 2 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| # 1 new dispositions: 0 reads re-frozen at their disposition with the claim, 1 new units frozen accepted-legacy, 0 quiet units retired while they read nothing, 0 other. |
| -@legacy 5d3238530e5a92db8e4c656224087ad2a623211170b712dc3a812f55d33f0c52 internal/host/agent.go domainsTickConfig legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-dns-fresh-lookup-724;basis=wires ResolveFresh to a FreshResolver over net.DefaultResolver (#724) data-literal@469,data-literal@510 b5843939ae5cfaf543c55c0806fef21bfb4008c7ebbec67baf22480b9084fc39 |
| -@legacy 90cc3630ea0c6facb5e3682b51f73d757ead11cce170561755296f6da2a8d7da internal/domains/freshdns.go FreshResolver.ask legacy accepted-legacy accepted-legacy;rev3=census-ruling-dns-fresh-lookup-724;basis=new: one UDP DNS query to a zone nameserver that passed publicNameserver (public unicast only); the Read is the socket reply, not a file (#724) file:Read@1195 b2d3cbe408c1542ff517b243bf777e2b0ab0272f3f4e0bd857e45d37c6009f4b |
| -@legacy 790551ba893a635c8579f6e8ee7fd5b61db9d0e0a05887f1827e53900ee1444e internal/domains/reconcile.go StampHealth legacy accepted-legacy accepted-legacy;rev3=J1;basis=root-route;rev3=census-ruling-dns-fresh-lookup-724;basis=section 4 now computes every cached verdict first, then freshUpgrade gives up to 64 randomly ordered non-apps-domain names whose verdict is "" or "elsewhere" an uncached lookup (30 s pass budget) that may only upgrade to "pointing", then stamps; the extra sites are the indexed loops over the same rows (#724) registry:Alias@3097,registry:Alias@3348,registry:Alias@3864,registry:Alias@3954,registry:Alias@4388,registry:Alias@4452,registry:Alias@4540,registry:DNSStatus@3513,registry:DNSStatus@3788,registry:Host@2955,registry:Host@3234,registry:Host@3585,registry:Host@3669,registry:Host@4157,registry:Host@4216,registry:Host@4298,whole:Route@199,whole:Route@236,whole:Route@2871,whole:Route@2902,whole:Route@3174,whole:Route@3441,whole:Route@4111,whole:RouteAlias@3012,whole:RouteAlias@3043,whole:RouteAlias@3287,whole:RouteAlias@3715,whole:RouteAlias@4341,whole:RouteAlias@780 46715bba1e8b5451f7c5383a425f1b7cace565acaaa9fab4a4405562d7d710f5 |
| -internal/host/agent.go domainsTickConfig legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-dns-fresh-lookup-724;basis=wires ResolveFresh to a FreshResolver over net.DefaultResolver (#724) data-literal@469,data-literal@510 b5843939ae5cfaf543c55c0806fef21bfb4008c7ebbec67baf22480b9084fc39 |
| -internal/domains/freshdns.go FreshResolver.ask legacy accepted-legacy accepted-legacy;rev3=census-ruling-dns-fresh-lookup-724;basis=new: one UDP DNS query to a zone nameserver that passed publicNameserver (public unicast only); the Read is the socket reply, not a file (#724) file:Read@1195 b2d3cbe408c1542ff517b243bf777e2b0ab0272f3f4e0bd857e45d37c6009f4b |
| -internal/domains/reconcile.go StampHealth legacy accepted-legacy accepted-legacy;rev3=J1;basis=root-route;rev3=census-ruling-dns-fresh-lookup-724;basis=section 4 now computes every cached verdict first, then freshUpgrade gives up to 64 randomly ordered non-apps-domain names whose verdict is "" or "elsewhere" an uncached lookup (30 s pass budget) that may only upgrade to "pointing", then stamps; the extra sites are the indexed loops over the same rows (#724) registry:Alias@3097,registry:Alias@3348,registry:Alias@3864,registry:Alias@3954,registry:Alias@4388,registry:Alias@4452,registry:Alias@4540,registry:DNSStatus@3513,registry:DNSStatus@3788,registry:Host@2955,registry:Host@3234,registry:Host@3585,registry:Host@3669,registry:Host@4157,registry:Host@4216,registry:Host@4298,whole:Route@199,whole:Route@236,whole:Route@2871,whole:Route@2902,whole:Route@3174,whole:Route@3441,whole:Route@4111,whole:RouteAlias@3012,whole:RouteAlias@3043,whole:RouteAlias@3287,whole:RouteAlias@3715,whole:RouteAlias@4341,whole:RouteAlias@780 46715bba1e8b5451f7c5383a425f1b7cace565acaaa9fab4a4405562d7d710f5 |
| -@coverage internal/domains f3bd9a67d2a297169e1be17811cf2e948e446fe16caf940604baf8c0058596be |
| -@coverage internal/host 2ebf59ee154f59349da7887e5675ab5bd0d732ae8748685d8d2f5ecb4e9d7cdf |
| +@legacy fb048df97af767c79c1bd5495c5338df8716ecbacef15197f6588dc84e595482 internal/host/agent.go domainsTickConfig legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-dns-fresh-lookup-724;basis=wires ResolveFresh to a FreshResolver over net.DefaultResolver (LookupNS, LookupHost, LookupCNAME) (#724) data-literal@469,data-literal@510 015ea39a31e76d50ee6f573cec2f2cbf915c729accfd017ec99fd8b7299ac2e5 |
| +@legacy 5d4d1b5fe6d590e9dad9840c14878453d4dc94daf08f726c398b518a1df6c650 internal/domains/freshdns.go FreshResolver.exchange legacy accepted-legacy accepted-legacy;rev3=census-ruling-dns-fresh-lookup-724;basis=new: one non-recursive IN query over UDP to a nameserver that passed publicNameserver (public unicast only), 1 s per attempt; the Read is the socket reply, not a file (#724) file:Read@1217 42780e6af3180514c8d83fefff0aa84069a8e4db9ec49ee8bfa404edce0ee21b |
| +@legacy cbfa6a628884ab6c9f95a5e174caa9a44fa526bde29a4f36bfd0686514b8b913 internal/domains/reconcile.go StampHealth legacy accepted-legacy accepted-legacy;rev3=J1;basis=root-route;rev3=census-ruling-dns-fresh-lookup-724;basis=section 4 now computes every cached verdict first, then freshUpgrade gives up to 64 randomly ordered names that could have a public zone (not under the apps domain, not special-use like .internal) and whose verdict is "" or "elsewhere" an uncached lookup (30 s pass budget) that may only upgrade to "pointing", then stamps; the extra sites are the indexed loops over the same rows (#724) registry:Alias@3097,registry:Alias@3348,registry:Alias@3864,registry:Alias@3954,registry:Alias@4388,registry:Alias@4452,registry:Alias@4540,registry:DNSStatus@3513,registry:DNSStatus@3788,registry:Host@2955,registry:Host@3234,registry:Host@3585,registry:Host@3669,registry:Host@4157,registry:Host@4216,registry:Host@4298,whole:Route@199,whole:Route@236,whole:Route@2871,whole:Route@2902,whole:Route@3174,whole:Route@3441,whole:Route@4111,whole:RouteAlias@3012,whole:RouteAlias@3043,whole:RouteAlias@3287,whole:RouteAlias@3715,whole:RouteAlias@4341,whole:RouteAlias@780 46715bba1e8b5451f7c5383a425f1b7cace565acaaa9fab4a4405562d7d710f5 |
| +internal/host/agent.go domainsTickConfig legacy accepted-legacy accepted-legacy;rev3=J4;basis=root-input;rev3=census-ruling-dns-fresh-lookup-724;basis=wires ResolveFresh to a FreshResolver over net.DefaultResolver (LookupNS, LookupHost, LookupCNAME) (#724) data-literal@469,data-literal@510 015ea39a31e76d50ee6f573cec2f2cbf915c729accfd017ec99fd8b7299ac2e5 |
| +internal/domains/freshdns.go FreshResolver.exchange legacy accepted-legacy accepted-legacy;rev3=census-ruling-dns-fresh-lookup-724;basis=new: one non-recursive IN query over UDP to a nameserver that passed publicNameserver (public unicast only), 1 s per attempt; the Read is the socket reply, not a file (#724) file:Read@1217 42780e6af3180514c8d83fefff0aa84069a8e4db9ec49ee8bfa404edce0ee21b |
| +internal/domains/reconcile.go StampHealth legacy accepted-legacy accepted-legacy;rev3=J1;basis=root-route;rev3=census-ruling-dns-fresh-lookup-724;basis=section 4 now computes every cached verdict first, then freshUpgrade gives up to 64 randomly ordered names that could have a public zone (not under the apps domain, not special-use like .internal) and whose verdict is "" or "elsewhere" an uncached lookup (30 s pass budget) that may only upgrade to "pointing", then stamps; the extra sites are the indexed loops over the same rows (#724) registry:Alias@3097,registry:Alias@3348,registry:Alias@3864,registry:Alias@3954,registry:Alias@4388,registry:Alias@4452,registry:Alias@4540,registry:DNSStatus@3513,registry:DNSStatus@3788,registry:Host@2955,registry:Host@3234,registry:Host@3585,registry:Host@3669,registry:Host@4157,registry:Host@4216,registry:Host@4298,whole:Route@199,whole:Route@236,whole:Route@2871,whole:Route@2902,whole:Route@3174,whole:Route@3441,whole:Route@4111,whole:RouteAlias@3012,whole:RouteAlias@3043,whole:RouteAlias@3287,whole:RouteAlias@3715,whole:RouteAlias@4341,whole:RouteAlias@780 46715bba1e8b5451f7c5383a425f1b7cace565acaaa9fab4a4405562d7d710f5 |
| +@coverage internal/domains 8c13a19a1f3c0ce734679577bf1d8aeb9e62b07877dff9c5d3bd04104f51aa94 |
| +@coverage internal/host 161141914cb12cc84dfcc503804f23d20e6e2da025744d3ef8f3dd62d2ca0f89 |