Skip to content

Commit 037fc69

Browse files
TMHSDigitalclaude
andcommitted
fix(pkg/subenum): Scan succeeds for a completed NoAbort scan (#149)
Under NoAbort the reliability guard only warns, yet Scan turned that KindError into a returned error even though the scan ran to the end. Ignore the error when NoAbort is set, EventDone was not Stopped and the scan was not aborted, matching the CLI's rule. Run still sends the warning. Documents the behaviour and adds a test with a resolver that refuses half the names. Fixes #149 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LpkeXSPwrzd7ZCuX3no7gD
1 parent c8134c6 commit 037fc69

3 files changed

Lines changed: 70 additions & 2 deletions

File tree

‎docs/library.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Read the channel until it closes, or cancel `ctx`. Breaking out of the loop with
7676
| `MaxQueries` | unlimited | Cap on candidate names tested. |
7777
| `Exclude` | none | Out-of-scope names and `*.parent` branches, never queried. |
7878
| `Force` | off | Scan a wildcard zone, dropping answers that match its fingerprint. |
79-
| `NoAbort` | off | Keep going when more than a fifth of lookups fail. |
79+
| `NoAbort` | off | Keep going when more than a fifth of lookups fail. `Run` still sends the warning as a `KindError` event; `Scan` returns no error for a scan that finished. |
8080
| `Simulate`, `HitRate`, `Seed` | off, `15`, random | Synthetic results with no DNS traffic, for tests and demos. |
8181

8282
## Accounting

‎pkg/subenum/subenum.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ type Config struct {
112112
// NoAbort keeps scanning when more than a fifth of lookups fail.
113113
// Without it the scan stops early and Stats.Aborted is set, because a
114114
// resolver that is failing cannot be distinguished from a domain whose
115-
// names do not exist.
115+
// names do not exist. With it, Run still sends the reliability
116+
// warning as a KindError event, but the scan finishes and Scan
117+
// returns no error for it.
116118
NoAbort bool
117119

118120
// Simulate invents results instead of sending DNS queries, for demos,
@@ -213,6 +215,8 @@ const (
213215
// the scan: the domain is out of scope, the resolver failed a
214216
// preflight check, the domain is a wildcard zone and Force is not
215217
// set, or the reliability guard gave up. Few or no results follow.
218+
// The exception is the reliability guard under Config.NoAbort: its
219+
// warning arrives as a KindError, and the scan then runs to the end.
216220
KindError
217221
// KindDone is always the last event, and reports Event.Stats. It
218222
// arrives whether the scan completed, never started, was stopped by
@@ -407,6 +411,7 @@ func Scan(ctx context.Context, cfg Config) ([]Result, Stats, error) {
407411
var results []Result
408412
var stats Stats
409413
var failure string
414+
completed := false
410415
for ev := range events {
411416
switch ev.Kind {
412417
case KindResult:
@@ -417,8 +422,15 @@ func Scan(ctx context.Context, cfg Config) ([]Result, Stats, error) {
417422
}
418423
case KindDone:
419424
stats = ev.Stats
425+
completed = !ev.Stopped
420426
}
421427
}
428+
// Under NoAbort the reliability guard only warns: it is the one error
429+
// after which the scan still runs to completion, so a completed scan
430+
// that was not aborted is a success.
431+
if cfg.NoAbort && completed && !stats.Aborted {
432+
failure = ""
433+
}
422434
switch {
423435
case ctx.Err() != nil:
424436
return results, stats, ctx.Err()

‎pkg/subenum/subenum_test.go‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package subenum_test
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"runtime"
78
"slices"
89
"strings"
@@ -392,6 +393,61 @@ func TestRateLimitIsApplied(t *testing.T) {
392393
}
393394
}
394395

396+
// TestNoAbortScanSucceedsDespiteFailingLookups covers #149: under NoAbort
397+
// the reliability guard only warns, so a completed Scan returns no error,
398+
// while Run still reports the warning.
399+
func TestNoAbortScanSucceedsDespiteFailingLookups(t *testing.T) {
400+
srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply {
401+
if strings.HasPrefix(q.Name, "bad") {
402+
return dnstest.Refused
403+
}
404+
return dnstest.NXDomain
405+
})
406+
words := make([]string, 0, 300)
407+
for i := range 300 {
408+
if i%2 == 0 {
409+
words = append(words, fmt.Sprintf("bad%d", i))
410+
} else {
411+
words = append(words, fmt.Sprintf("ok%d", i))
412+
}
413+
}
414+
cfg := subenum.Config{
415+
Domain: "lab.example",
416+
Words: words,
417+
Resolver: srv.Addr,
418+
Timeout: 2 * time.Second,
419+
Attempts: 1,
420+
NoAbort: true,
421+
}
422+
results, stats, err := subenum.Scan(context.Background(), cfg)
423+
if err != nil {
424+
t.Fatalf("Scan with NoAbort returned %v, want nil", err)
425+
}
426+
if stats.Aborted || stats.Total() != int64(len(words)) {
427+
t.Fatalf("stats = %+v, want a complete scan of %d names", stats, len(words))
428+
}
429+
if len(results) != 0 {
430+
t.Errorf("results = %v, want none", names(results))
431+
}
432+
433+
events, err := subenum.Run(context.Background(), cfg)
434+
if err != nil {
435+
t.Fatal(err)
436+
}
437+
warned := false
438+
for ev := range events {
439+
warned = warned || ev.Kind == subenum.KindError
440+
}
441+
if !warned {
442+
t.Error("Run sent no KindError warning from the reliability guard")
443+
}
444+
445+
cfg.NoAbort = false
446+
if _, stats, err := subenum.Scan(context.Background(), cfg); err == nil || !stats.Aborted {
447+
t.Errorf("without NoAbort: err = %v, Aborted = %v; want an error and an aborted scan", err, stats.Aborted)
448+
}
449+
}
450+
395451
// TestBreakAndCancelLeaksNothing covers #112: the idiomatic
396452
// `for ev := range events { break }` with a deferred cancel leaves no
397453
// goroutine of the scan behind, even though the channel is never drained.

0 commit comments

Comments
 (0)