Skip to content

Regression: a budget stop miscounts excluded and duplicate candidates as skipped #361

Closed

Description

@TMHSDigital

Summary

When -max-queries or -max-duration stops admitting candidates, stopAdmitting splits the untested candidates into excluded and skipped. Both of its counting paths get multi-label wordlist entries wrong: the no-visited-set shortcut counts names that -exclude would have removed as skipped, and the visited-set branch can count one untested name twice. The skipped count and the verdict (complete vs degraded) are then wrong; the first case brings back the problem #238 fixed.

Where

  • internal/scan/dispatcher.go:221-226
  • internal/scan/dispatcher.go:254-292
  • internal/scan/dispatcher.go:43-56
  • internal/scan/dispatcher.go:350-356
  • internal/scan/dispatcher.go:221-252
  • internal/scan/dispatcher.go:337-349
  • internal/scan/dispatcher_test.go:128-166

Details

A budget stop counts excluded multi-label candidates as skipped. stopAdmitting takes the countUnbuilt shortcut whenever d.visited == nil and there are exclusions. visitedSet returns nil for every non-recursive scan, including one whose entries hold several labels (wordlist lines like dev.api, CT/Wayback names merged by passive.Merge, permutations; wordlist.Normalize keeps multi-label entries, reader.go:50). countUnbuilt assumes "every entry is a single label": it only checks subtreeExcluded(e.parent) and exact patterns split as <first label>.<parent> against the frontier parent. For entry a.dev under root example.com with -exclude '*.dev.example.com', next() (line 350) would call scope.excluded("a.dev.example.com") -> true, but countUnbuilt checks subtreeExcluded("example.com") -> false and finds no exact label, so the candidate is added to skipped. Same for an exact pattern a.dev.example.com (split to label a, parent dev.example.com, never a frontier parent in a non-recursive scan). The comment at 254-256 states the precondition the caller does not guarantee. Related: expand (367-381) counts all children of a parent found after the cap as skipped, though exact patterns among them would be counted excluded by countUnbuilt.

Untested cap branch of stopAdmitting counts a duplicate candidate twice. stopAdmitting's default branch (a recursive scan whose wordlist has a multi-label entry, so a visited set exists, #248/#301) is never executed by any test: a cross-package run (go test -coverpkg=./internal/scan ./internal/cli ./pkg/subenum ./internal/tui ./internal/scan) gives stopAdmitting 31.6%, all of it the first two cases. The branch checks each remaining candidate against d.visited but never adds it, while admission (next, 344-348) inserts every key it generates. So when the not-yet-generated remainder holds the same name twice (entry "a.b" under the root and entry "a" under a found b.example.com), the uncapped run tests it once and the capped run counts it as skipped twice.

default:
for _, e := range d.frontier[d.head:] {
for i := e.next; i < d.n; i++ {
...
if _, dup := d.visited[key]; dup {
continue
}
// key never added to d.visited here
if d.cfg.scope.excluded(name) { d.excluded++ } else { d.skipped++ }

A scratch test in a copy of the tree (entries b, x, a.b, a; Recursive; MaxQueries 2; b.example.com expanded) printed: "uncapped distinct candidates 7; capped admitted 2 ... skipped 6" -> admitted+skipped = 8, one more than the candidates that exist. TestCapCountsRecursiveRemainderWithoutBuildingIt (#301) covers only single-label entries, so it takes the countUnbuilt path.

Impact

Under -max-queries or -max-duration with -exclude, out-of-scope names that would have been excluded are reported as skipped. The skipped count and the "skipped N additional jobs" notice are inflated and, when every untested candidate was out of scope, the verdict drops from complete to degraded ("{max_queries} reached; N candidates not tested", verdict.go:44-47) -- the case #238 fixed, reintroduced for multi-label entries by the #301 shortcut.

With -max-queries or -max-duration on a recursive scan whose wordlist contains dotted entries (common: "dev.api", "mail.internal"), skipped_by_cap in -stats, the run-quality report and the MCP report is overstated by the number of duplicate names left, and tested+skipped exceeds the true candidate count. Small numbers, but it is the counter the #238/#301 work set out to make exact, and nothing guards it.

Steps to reproduce

Non-recursive scan, -simulate, wordlist www then a.dev, b.dev; -exclude '*.dev.<domain>' -max-queries 1. www is tested; the two *.dev entries are counted skipped (2) and excluded 0, so the verdict is degraded. Without the cap they would be excluded 2.

In internal/scan: d := testDispatcher(Config{Entries: []string{"b","x","a.b","a"}, Recursive: true, MaxQueries: 2}, 2); b, _ := d.next(); d.expand(job{domain: b.domain, depth: 1}); drain(d) -> d.skipped == 6; the same scan with MaxQueries 0 generates 7 candidates in all (2 admitted + 5 left).

Suggested fix

Use countUnbuilt only when every entry is a single label (compute that once in newDispatcher, as visitedSet already scans for a .), else fall back to the per-candidate walk; or make countUnbuilt split each exact/suffix pattern against <entry>.<parent> with multi-label entries in mind. Add a test with a multi-label entry, an exclusion and a cap.

In the default branch keep a local set (or insert into d.visited, which is discarded after the cap anyway) so each remaining name is counted once. Add a dispatcher test with multi-label entries and a cap that asserts admitted+skipped+excluded equals the uncapped run's candidate count, with and without an -exclude pattern hitting the duplicate name, and one where the duplicate is excluded under one parent spelling.

Acceptance criteria

  • Non-recursive scan with multi-label entries, -exclude and a cap counts the out-of-scope entries as excluded, not skipped, and keeps a complete verdict when nothing in scope was left
  • Recursive scan with dotted entries stopped by a cap reports tested + skipped + excluded equal to the candidate total
  • Tests for both branches of stopAdmitting

Related: #238 (closed as completed; the multi-label case of the same problem is back through the #301 shortcut), #301

Getting started

  • Relevant files: internal/scan/dispatcher.go, internal/scan/dispatcher_test.go
  • How to run tests: go test ./... (tests must stay hermetic: use -simulate, a lab zone or internal/dnstest, never a live domain)
  • Related docs: docs/DEVELOPER_GUIDE.md, docs/ARCHITECTURE.md

Filed by repo-audit/full-audit at 0f3b0b6. Severity: medium. Effort: S. audit-key: bug|internal/scan/dispatcher.go|stopAdmitting

Activity

  1. added
    bug
    area: scan
    severity: medium
    on Nov 16, 2026
  2. TMHSDigital commented on Nov 16, 2026

    @TMHSDigital
    OwnerAuthor

    Fixed in 87db011.

    stopAdmitting now uses the countUnbuilt shortcut only when every entry is a single label (computed once in newDispatcher); multi-label entries take the per-candidate walk, which now also adds each name to the visited set so a name the remainder would reach through two parents is counted once.

    Verified with two new dispatcher tests, both failing before the change and passing after: TestCapCountsExcludedMultiLabelEntries (flat scan, a.dev/b.dev with *.dev.example.com or an exact pattern, MaxQueries 1: counts match the uncapped run) and TestCapCountsRecursiveMultiLabelRemainderOnce (entries b, x, a.b, a, recursive, MaxQueries 2, with no exclusion, an exact one on the duplicate name, and a subtree one: admitted + skipped + excluded equals the uncapped candidate total). gofmt, go vet, golangci-lint v2.15.0 and go test ./... pass.

    Not changed: the related note that expand counts every child of a parent found after the cap as skipped (exact patterns among them, or duplicates in a multi-label recursive scan, are not split out). That path was outside this issue's acceptance criteria.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: scanbugseverity: medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions