From 8d0e09d103274a8d972ff97464c7f6181940f081 Mon Sep 17 00:00:00 2001 From: Beehtrix Date: Thu, 3 Dec 2026 23:38:37 +0000 Subject: [PATCH] fix: label Follow Records results as records in the TUI and count records_found (#494) --- internal/tui/follow_test.go | 98 +++++++++++++++++++++++++++++++++++++ internal/tui/form.go | 2 +- internal/tui/model.go | 11 ++++- 3 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 internal/tui/follow_test.go diff --git a/internal/tui/follow_test.go b/internal/tui/follow_test.go new file mode 100644 index 0000000..e1a2ec4 --- /dev/null +++ b/internal/tui/follow_test.go @@ -0,0 +1,98 @@ +package tui + +import ( + "encoding/json" + "os" + "path/filepath" + "slices" + "strings" + "testing" + + tea "charm.land/bubbletea/v2" + + "github.com/TMHSDigital/subenum/internal/candidates" + "github.com/TMHSDigital/subenum/internal/dnstest" + "github.com/TMHSDigital/subenum/internal/output" + "github.com/TMHSDigital/subenum/internal/scan" +) + +// TestFollowRecordsSources covers #494: a name a found name's records +// named is saved with source records and counted as records_found, with +// Follow Records alone and with Permute on. +func TestFollowRecordsSources(t *testing.T) { + redirectConfig(t) + srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply { + switch q.Name { + case "example.com": + return dnstest.Reply{} + case "app.example.com": + return dnstest.Reply{CNAME: "lb-7.int.example.com", A: []string{"192.0.2.7"}} + case "lb-7.int.example.com": + return dnstest.Reply{A: []string{"192.0.2.7"}} + } + return dnstest.NXDomain + }) + for _, permute := range []bool{false, true} { + dir := t.TempDir() + wl, out := filepath.Join(dir, "wl.txt"), filepath.Join(dir, "out.jsonl") + if err := os.WriteFile(wl, []byte("app\n"), 0o644); err != nil { + t.Fatal(err) + } + m := New(Options{Version: "test"}) + sized, _ := m.Update(tea.WindowSizeMsg{Width: 120, Height: 40}) + m = sized.(Model) + for f, v := range map[int]string{ + fieldDomain: "example.com", fieldWordlist: wl, fieldDNSServer: srv.Addr, + fieldTypes: "A,CNAME", fieldOutput: out, fieldFormat: "jsonl", + } { + m.form.inputs[f].SetValue(v) + } + m.form.toggles[fieldFollowRecords], m.form.toggles[fieldPermute] = true, permute + next, load := m.Update(keyCtrlR) + m = next.(Model) + if m.form.err != "" || load == nil { + t.Fatalf("permute %v: ctrl+r: error %q, load started %v", permute, m.form.err, load != nil) + } + next, listen := m.Update(load()) + m = drive(t, next.(Model), listen) + data, err := os.ReadFile(out) + if err != nil { + t.Fatal(err) + } + lines := strings.Split(strings.TrimSpace(string(data)), "\n") + sources := map[string][]string{} + for _, line := range lines[:len(lines)-1] { + var r output.Result + if err := json.Unmarshal([]byte(line), &r); err != nil { + t.Fatalf("result line %q: %v", line, err) + } + sources[r.Subdomain] = r.Sources + } + var summary output.Summary + if err := json.Unmarshal([]byte(lines[len(lines)-1]), &summary); err != nil || len(summary.Targets) != 1 { + t.Fatalf("no report (err %v): %s", err, lines[len(lines)-1]) + } + if got := sources["lb-7.int.example.com"]; !slices.Equal(got, candidates.FromRecords) { + t.Errorf("permute %v: sources of lb-7.int = %v, want [records] (results: %v)", permute, got, sources) + } + if got := sources["app.example.com"]; !slices.Equal(got, candidates.FromWordlist) { + t.Errorf("permute %v: sources of app = %v, want [wordlist]", permute, got) + } + if got := summary.Targets[0].RecordsFound; got != 1 { + t.Errorf("permute %v: records_found = %d, want 1", permute, got) + } + } +} + +// TestRecordFollowedInLaterPass covers #494 for a followed result found +// after the main pass: it keeps source records, not the pass's own. +func TestRecordFollowedInLaterPass(t *testing.T) { + for _, stage := range []scanStage{stagePermute, stagePTR} { + m := Model{scanView: newScanViewModel(120, 40, false), foundBy: map[string]int{}} + m.scanView.stage = stage + m.record(scan.Result{Subdomain: "lb-7.int.example.com"}, true) + if m.foundBy[candidates.Records] != 1 || len(m.foundBy) != 1 { + t.Errorf("stage %v: foundBy %v, want only records", stage, m.foundBy) + } + } +} diff --git a/internal/tui/form.go b/internal/tui/form.go index dae594b..32263b3 100644 --- a/internal/tui/form.go +++ b/internal/tui/form.go @@ -757,5 +757,5 @@ func (v formValues) zoneLookup() bool { return v.axfr || v.nsecWalk } // attributed reports whether results say what proposed them: something // other than the wordlist can, as on the command line (#274, #418). func (v formValues) attributed() bool { - return len(v.sources) > 0 || v.permute || v.zoneLookup() || v.srv || v.ptr + return len(v.sources) > 0 || v.permute || v.zoneLookup() || v.srv || v.ptr || v.FollowRecords } diff --git a/internal/tui/model.go b/internal/tui/model.go index d73409f..ab0e295 100644 --- a/internal/tui/model.go +++ b/internal/tui/model.go @@ -557,7 +557,7 @@ func (m Model) updateScan(msg tea.Msg) (tea.Model, tea.Cmd) { m.finishOutput(ev.Event) return m, svCmd case ev.Kind == scan.EventResult: - m.record(ev.Result) + m.record(ev.Result, ev.Followed) } // A kind this view does not know yet must not stall the scan: the // listener is re-armed for it like any other event (#165). @@ -567,9 +567,15 @@ func (m Model) updateScan(msg tea.Msg) (tea.Model, tea.Cmd) { // record keeps a result for the permutation pass's seeds and writes it to // the output file, saying what proposed it when the scan has sources or a // permutation pass (#274). -func (m *Model) record(r scan.Result) { +func (m *Model) record(r scan.Result, followed bool) { res := output.Result{Result: r} switch stage := m.scanView.stage; { + case followed: + res.Sources = candidates.FromRecords // a found name's record named it (#432, #494) + res.Permutation = stage == stagePermute + if stage != stagePTR && stage != stagePermute && m.vals.permute { + m.names = append(m.names, r.Subdomain) + } case stage == stagePTR: res.Sources = candidates.FromPTR case stage == stagePermute: @@ -940,6 +946,7 @@ func (m *Model) summary(final scan.Event) output.Summary { m.scanView.perm.summarize(&t, m.scanView.aborted, m.scanView.errText) m.scanView.ptrp.summarize(&t, m.scanView.aborted, m.scanView.errText) t.PTRFound = m.foundBy[candidates.PTR] + t.RecordsFound = m.foundBy[candidates.Records] // As the CLI's, with the resolver pool's accounting (#367). return report.Build(report.Run{ Version: m.opts.Version,