Skip to content

Commit e5bec69

Browse files
TMHSDigitalclaude
andcommitted
perf(dns): resolve each takeover-hint CNAME target once per scan
Every result with a CNAME re-resolved its target for the takeover hint, so many names aliased to one CDN host spent a query and a rate slot each. dns.TakeoverCache memoizes the outcome per target for a scan, shares in-flight lookups, and drops inconclusive outcomes so a later name retries. Fixes #121 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3057e1e commit e5bec69

4 files changed

Lines changed: 124 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3030
- The minimum Go version is now 1.26 (`go 1.26.0` in go.mod), required by current `golang.org/x/sys` and `golang.org/x/text`; both were bumped and `govulncheck -show verbose` now reports no vulnerable modules. Dependabot also updates indirect modules, and CI lints with errorlint, nilerr, contextcheck, noctx, copyloopvar, usestdlibvars and revive, with errcheck exemptions narrowed to stdout prints (#83).
3131

3232
### Fixed
33+
- Takeover hints resolve each CNAME target once per scan instead of once per result, so hundreds of names aliased to the same CDN or SaaS host no longer spend a query and a `-rate` slot each; concurrent results for one target share the lookup, and an inconclusive one is retried by the next name (#121).
3334
- DoT connection reuse is accurate: a pooled connection the server already closed is detected before reuse instead of failing a query that had already been counted and paced (a server closing after each answer cost 19 dials for 10 lookups; now 10). Up to 128 connections are kept, matching the default `-t`, and a scan closes its idle DoT and DoH connections when it ends, so a program that scans repeatedly through `pkg/subenum` does not hold them open (#113).
3435
- DNS over HTTPS never follows a redirect, so a misconfigured endpoint or proxy cannot move the query elsewhere or downgrade it to cleartext `http://`; HTTP 429 and 503 count as `refused` (the rate-limit signal) instead of `other` (#114).
3536
- `-dns-server tls://[2001:db8::1]` (a bracketed IPv6 host without a port) is accepted; the validator and the dialer now share one parser. Lab zone CNAME targets are validated, and `CNAME @` points at the scanned domain instead of the bogus `@.<domain>` (#115).

‎internal/dns/takeover.go‎

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"net"
66
"strings"
7+
"sync"
8+
"sync/atomic"
79
"time"
810
)
911

@@ -65,17 +67,28 @@ func TakeoverProvider(target string) string {
6567
// nothing suspicious. The target is resolved with resolver; an inconclusive
6668
// lookup (timeout, SERVFAIL) is not called dangling.
6769
func TakeoverHint(ctx context.Context, resolver *net.Resolver, records []Record, timeout time.Duration, attempts int) string {
70+
target := cnameTarget(records)
71+
if target == "" {
72+
return ""
73+
}
74+
_, outcome := ResolveDomainWithRetry(ctx, resolver, target, timeout, nil, attempts, DefaultTypes)
75+
return takeoverMarker(target, outcome)
76+
}
77+
78+
// cnameTarget returns the CNAME target among records, or "".
79+
func cnameTarget(records []Record) string {
6880
target := ""
6981
for _, r := range records {
7082
if r.Type == "CNAME" {
7183
target = r.Value
7284
}
7385
}
74-
if target == "" {
75-
return ""
76-
}
86+
return target
87+
}
88+
89+
// takeoverMarker turns a CNAME target and its lookup outcome into a hint.
90+
func takeoverMarker(target string, outcome Outcome) string {
7791
provider := TakeoverProvider(target)
78-
_, outcome := ResolveDomainWithRetry(ctx, resolver, target, timeout, nil, attempts, DefaultTypes)
7992
switch {
8093
case outcome == OutcomeNXDomain && provider != "":
8194
return "dangling:" + provider
@@ -86,3 +99,65 @@ func TakeoverHint(ctx context.Context, resolver *net.Resolver, records []Record,
8699
}
87100
return ""
88101
}
102+
103+
// TakeoverCache is TakeoverHint with one lookup per CNAME target for the
104+
// life of the cache (a scan): many names aliased to the same CDN or SaaS
105+
// host cost one query and one rate slot, not one each (#121). Concurrent
106+
// callers for the same target share the lookup. An inconclusive outcome
107+
// (timeout, SERVFAIL, cancellation) is not kept, so a later name retries.
108+
type TakeoverCache struct {
109+
mu sync.Mutex
110+
entries map[string]*takeoverEntry
111+
lookups atomic.Int64
112+
}
113+
114+
type takeoverEntry struct {
115+
done chan struct{}
116+
outcome Outcome
117+
}
118+
119+
// Lookups reports how many target lookups the cache sent.
120+
func (c *TakeoverCache) Lookups() int64 { return c.lookups.Load() }
121+
122+
// Hint is TakeoverHint through the cache.
123+
func (c *TakeoverCache) Hint(ctx context.Context, resolver *net.Resolver, records []Record, timeout time.Duration, attempts int) string {
124+
target := cnameTarget(records)
125+
if target == "" {
126+
return ""
127+
}
128+
key := strings.ToLower(strings.TrimSuffix(target, "."))
129+
for {
130+
c.mu.Lock()
131+
if c.entries == nil {
132+
c.entries = map[string]*takeoverEntry{}
133+
}
134+
e, ok := c.entries[key]
135+
if !ok {
136+
e = &takeoverEntry{done: make(chan struct{})}
137+
c.entries[key] = e
138+
c.mu.Unlock()
139+
c.lookups.Add(1)
140+
_, e.outcome = ResolveDomainWithRetry(ctx, resolver, target, timeout, nil, attempts, DefaultTypes)
141+
if e.outcome != OutcomeFound && e.outcome != OutcomeNXDomain {
142+
c.mu.Lock()
143+
delete(c.entries, key) // inconclusive: let a later name retry
144+
c.mu.Unlock()
145+
}
146+
close(e.done)
147+
return takeoverMarker(target, e.outcome)
148+
}
149+
c.mu.Unlock()
150+
select {
151+
case <-e.done:
152+
case <-ctx.Done():
153+
return takeoverMarker(target, OutcomeCanceled)
154+
}
155+
if e.outcome == OutcomeFound || e.outcome == OutcomeNXDomain {
156+
return takeoverMarker(target, e.outcome)
157+
}
158+
// The shared lookup was inconclusive and dropped; try again.
159+
if ctx.Err() != nil {
160+
return takeoverMarker(target, OutcomeCanceled)
161+
}
162+
}
163+
}

‎internal/dns/takeover_test.go‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ package dns
22

33
import (
44
"context"
5+
"sync"
56
"testing"
67
"time"
8+
9+
"golang.org/x/net/dns/dnsmessage"
10+
11+
"github.com/TMHSDigital/subenum/internal/dnstest"
712
)
813

914
func TestTakeoverProvider(t *testing.T) {
@@ -49,3 +54,38 @@ func TestTakeoverHint(t *testing.T) {
4954
})
5055
}
5156
}
57+
58+
// TestTakeoverCache covers #121: many names aliased to one CNAME target cost
59+
// one lookup of it, even when they arrive at once, and the hints match
60+
// TakeoverHint's.
61+
func TestTakeoverCache(t *testing.T) {
62+
srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
63+
if q.Name == "acme.herokuapp.com" {
64+
return dnstest.Reply{A: []string{"192.0.2.20"}}
65+
}
66+
return dnstest.NXDomain
67+
})
68+
r := NewResolver(time.Second, srv.Addr)
69+
var c TakeoverCache
70+
targets := map[string]string{"gone.example.net": "dangling", "acme.herokuapp.com": "provider:heroku"}
71+
var wg sync.WaitGroup
72+
for i := 0; i < 50; i++ {
73+
for target, want := range targets {
74+
wg.Go(func() {
75+
recs := []Record{{Type: "CNAME", Value: target}}
76+
if got := c.Hint(context.Background(), r, recs, time.Second, 1); got != want {
77+
t.Errorf("Hint(%s) = %q, want %q", target, got, want)
78+
}
79+
})
80+
}
81+
}
82+
wg.Wait()
83+
if c.Lookups() != 2 {
84+
t.Errorf("cache sent %d target lookups, want 2", c.Lookups())
85+
}
86+
for target := range targets {
87+
if n := srv.QueriesFor(target, dnsmessage.TypeA); n != 1 {
88+
t.Errorf("server saw %d A queries for %s, want 1", n, target)
89+
}
90+
}
91+
}

‎internal/scan/runner.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ type Config struct {
6161
// Tests inject classified outcomes through this field without network I/O.
6262
resolveHook func(ctx context.Context, domain string) ([]dns.Record, dns.Outcome)
6363

64-
scope *scope // built from Exclude by Run
64+
scope *scope // built from Exclude by Run
65+
takeover *dns.TakeoverCache // one CNAME-target lookup per target per scan (#121)
6566
}
6667

6768
// logf returns the verbose logger for lookups, or nil when Verbose is off.
@@ -432,6 +433,7 @@ func Run(ctx context.Context, cfg Config, events chan<- Event) {
432433
// out of scope, or whose every candidate would be, is refused outright.
433434
sc := newScope(cfg.Exclude)
434435
cfg.scope = sc
436+
cfg.takeover = &dns.TakeoverCache{}
435437
if sc.excluded(cfg.Domain) || sc.subtreeExcluded(cfg.Domain) {
436438
events <- Event{Kind: EventError, Message: "refusing to scan " + cfg.Domain + ": it is out of scope (-exclude)"}
437439
return
@@ -810,7 +812,7 @@ func processJob(ctx context.Context, cfg Config, j job, maxDepth int, limiter *d
810812
// with the trusted resolver. Simulated records are never checked.
811813
takeover := ""
812814
if !cfg.Simulate && cfg.resolveHook == nil {
813-
takeover = dns.TakeoverHint(ctx, cfg.Resolver, records, cfg.Timeout, cfg.Attempts)
815+
takeover = cfg.takeover.Hint(ctx, cfg.Resolver, records, cfg.Timeout, cfg.Attempts)
814816
}
815817
if takeover != "" {
816818
st.takeover.Add(1)

0 commit comments

Comments
 (0)