Skip to content

Commit 58eba27

Browse files
TMHSDigitalclaude
andcommitted
Stream TUI wordlist files and resume input digests (#411)
The TUI loaded a wordlist file with LoadWordlist, which holds every raw line in memory before preparing it, and fileDigest read each resume input whole before hashing it. The TUI now streams the file through EachLineOf into a Preparer, as the CLI does for -w, and fileDigest hashes the file through io.Copy. Digests are unchanged, so existing state files still resume; a test pins that against a whole-file SHA-256, and another covers the TUI file load. Fixes #411 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fc64cbf commit 58eba27

4 files changed

Lines changed: 60 additions & 8 deletions

File tree

‎internal/cli/resume.go‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"flag"
88
"fmt"
9+
"io"
910
"os"
1011
"slices"
1112
"sort"
@@ -392,12 +393,18 @@ func fileDigest(path string) (string, error) {
392393
if path == "" || path == "-" {
393394
return "", nil
394395
}
395-
data, err := os.ReadFile(path)
396+
// Streamed: a large wordlist is not read whole at Ctrl+C or on resume
397+
// (#411). The digest is the same as hashing the whole file at once.
398+
f, err := os.Open(path)
396399
if err != nil {
397400
return "", err
398401
}
399-
sum := sha256.Sum256(data)
400-
return hex.EncodeToString(sum[:]), nil
402+
defer func() { _ = f.Close() }()
403+
h := sha256.New()
404+
if _, err := io.Copy(h, f); err != nil {
405+
return "", err
406+
}
407+
return hex.EncodeToString(h.Sum(nil)), nil
401408
}
402409

403410
// inputDigests fingerprints the inputs a resume must find unchanged. That

‎internal/cli/resume_test.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cli
22

33
import (
4+
"crypto/sha256"
5+
"encoding/hex"
46
"encoding/json"
57
"fmt"
68
"maps"
@@ -285,6 +287,24 @@ func TestE2EResumeCountsRefoundNamesOnce(t *testing.T) {
285287
}
286288
}
287289

290+
// TestFileDigestMatchesWholeFileHash covers #411: the streamed digest equals
291+
// the whole-file SHA-256 earlier versions stored, so their states resume.
292+
func TestFileDigestMatchesWholeFileHash(t *testing.T) {
293+
content := strings.Repeat("www\nmail\napi.dev\n", 50_000) // larger than one copy buffer
294+
path := writeFile(t, "wl.txt", content)
295+
got, err := fileDigest(path)
296+
if err != nil {
297+
t.Fatal(err)
298+
}
299+
sum := sha256.Sum256([]byte(content))
300+
if want := hex.EncodeToString(sum[:]); got != want {
301+
t.Errorf("fileDigest = %s, want %s", got, want)
302+
}
303+
if got, err := fileDigest("-"); got != "" || err != nil {
304+
t.Errorf("fileDigest(stdin) = %q, %v; want no digest", got, err)
305+
}
306+
}
307+
288308
// TestWithoutRefoundDropsFlags covers #408: a refound name's takeover,
289309
// internal and partial flags were counted by the saved part and again by the
290310
// resumed pass, so they come off the resumed side with the name.

‎internal/tui/model.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,16 @@ func loadWordlistCmd(vals formValues, o Options) tea.Cmd {
208208
}
209209

210210
// loadWordlist reads the file named on the form, or the bundled list when the
211-
// field is empty, the way the CLI treats a missing -w (#288).
211+
// field is empty, the way the CLI treats a missing -w (#288). A file is
212+
// streamed, as the CLI streams -w, rather than held whole in memory (#411).
212213
func loadWordlist(vals formValues) (entries []string, skipped int, err error) {
214+
pr := wordlist.NewPreparer()
213215
if vals.wordlist != "" {
214-
entries, _, skipped, err = wordlist.LoadWordlist(vals.wordlist, vals.Domain)
215-
return entries, skipped, err
216+
err = wordlist.EachLineOf(vals.wordlist, pr.Add)
217+
} else {
218+
err = wordlist.EachLine(strings.NewReader(data.Subdomains5k), pr.Add)
216219
}
217-
pr := wordlist.NewPreparer()
218-
if err := wordlist.EachLine(strings.NewReader(data.Subdomains5k), pr.Add); err != nil {
220+
if err != nil {
219221
return nil, 0, err
220222
}
221223
entries, _, skipped = pr.Prepared().For(vals.Domain)

‎internal/tui/model_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,29 @@ func found(name string, records []dns.Record) output.Result {
2020
return output.Result{Result: scan.Result{Subdomain: name, Records: records}}
2121
}
2222

23+
// TestLoadWordlistFile covers #411: a wordlist file is streamed through the
24+
// same preparation as the CLI's -w: comments, duplicates and the target's own
25+
// suffix handled, invalid lines counted.
26+
func TestLoadWordlistFile(t *testing.T) {
27+
path := filepath.Join(t.TempDir(), "wl.txt")
28+
if err := os.WriteFile(path, []byte("# comment\nWWW\nwww\napi.example.com\nbad_label!\n"), 0o600); err != nil {
29+
t.Fatal(err)
30+
}
31+
vals := formValues{wordlist: path}
32+
vals.Domain = "example.com"
33+
entries, skipped, err := loadWordlist(vals)
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
if strings.Join(entries, ",") != "www,api" || skipped != 1 {
38+
t.Errorf("entries %v, skipped %d; want [www api] and 1", entries, skipped)
39+
}
40+
vals.wordlist = filepath.Join(t.TempDir(), "missing.txt")
41+
if _, _, err := loadWordlist(vals); err == nil {
42+
t.Error("a missing wordlist file was not an error")
43+
}
44+
}
45+
2346
// TestSummaryHasPoolResolvers covers #367: a scan with a resolver pool
2447
// reports each resolver's accounting in its summary, as the CLI's -r does,
2548
// and one without a pool reports none.

0 commit comments

Comments
 (0)