Skip to content

Commit d00a09a

Browse files
fix(wordlist-gen): parse flags interspersed and reject positionals (#120)
wordlist-gen called fs.Parse directly, which stops at the first non-flag argument. 'wordlist-gen acme.com -o out.txt' therefore silently ignored both acme.com and the -o after it and wrote ./wordlist.txt. Use the same interspersed parsing as main.go, reject leftover positionals with a -domain hint (exit 2), and return the usage code (2) for an invalid -combine value or an empty -o instead of 1.
1 parent de87d84 commit d00a09a

3 files changed

Lines changed: 119 additions & 5 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3838
- Lab mode can no longer send queries off the machine: `-r`, `-dns-server` or `-simulate` from a `SUBENUM_*` variable or the config file is refused with `-simulate-zone` just like the flag, naming where the value came from. Previously `SUBENUM_R` sent every lab query to the listed resolvers under a "no traffic leaves this machine" banner (#106).
3939
- Config and environment defaults: `-h`, `-version`, `-print-config` and `-tui` work despite a bad value; every bad value is reported at once; config values must be strings, numbers or booleans (`exclude` and `type` also take a list) instead of being stringified into values such as `<nil>`; `resume` is refused in the config file; `-attempts` and `-retries` from different sources resolve by precedence instead of conflicting; and `SUBENUM_PROGRESS` counts as an explicit `-progress` (#116).
4040
- An unreadable `-r` file exits 1 like every other unreadable input file (a malformed one is still a usage error, 2), and argument errors are reported before the simulation banner is printed or any resolver list is read (#120).
41+
- `tools/wordlist-gen` parses flags that appear after a positional the same way `subenum` does, instead of silently ignoring both: a leftover argument is now a usage error (2) that points at `-domain`, and an invalid `-combine` value or an empty `-o` exits 2 instead of 1 (#120).
4142
- A wildcard check whose probes time out or SERVFAIL is no longer read as "no wildcard". Probes are retried up to `-attempts`; if still inconclusive, the scan aborts (scans without filtering under `-force`, with a warning), and in recursive mode the branch is skipped and not cached. Previously a wildcard zone with flaky probes reported every wordlist entry as a hit (#47).
4243
- The dispatcher no longer does quadratic queue copying, so large wordlists scale linearly (simulated 2M entries: 19.4s down to 4.6s). Candidate names are generated lazily from the wordlist instead of being built up front, and a found parent is expanded in the dispatcher instead of pushing every child through a channel. `make bench` runs a 1M-entry benchmark (#49).
4344
- `-o` results files are written to a temporary file beside the target and renamed into place only once a scan finishes (including an interrupted one with partial results), in both the CLI and the TUI. A run that fails before scanning (preflight failure, wildcard abort, recursion refusal, Ctrl+C during preflight) no longer truncates an existing results file (#52).

‎tools/wordlist-gen.go‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,54 @@ func validateCombine(combine string) error {
140140
return nil
141141
}
142142

143+
// parseInterspersed parses flags that appear anywhere in args. The standard flag
144+
// package stops at the first non-flag argument, which silently dropped every flag
145+
// written after the domain (#29). Arguments after a "--" terminator are all
146+
// treated as positionals.
147+
func parseInterspersed(fs *flag.FlagSet, args []string) ([]string, error) {
148+
var positionals []string
149+
for {
150+
before := len(args)
151+
if err := fs.Parse(args); err != nil {
152+
return nil, err
153+
}
154+
rest := fs.Args()
155+
if len(rest) == 0 {
156+
return positionals, nil
157+
}
158+
consumed := args[:before-len(rest)]
159+
if endsWithTerminator(fs, consumed) {
160+
return append(positionals, rest...), nil
161+
}
162+
positionals = append(positionals, rest[0])
163+
args = rest[1:]
164+
}
165+
}
166+
167+
// endsWithTerminator reports whether the parsed arguments ended at a "--"
168+
// terminator, as opposed to "--" being the value of a flag such as "-o --".
169+
func endsWithTerminator(fs *flag.FlagSet, consumed []string) bool {
170+
n := len(consumed)
171+
if n == 0 || consumed[n-1] != "--" {
172+
return false
173+
}
174+
if n < 2 {
175+
return true
176+
}
177+
prev := consumed[n-2]
178+
if !strings.HasPrefix(prev, "-") || strings.Contains(prev, "=") {
179+
return true
180+
}
181+
fl := fs.Lookup(strings.TrimLeft(prev, "-"))
182+
if fl == nil {
183+
return true
184+
}
185+
if b, ok := fl.Value.(interface{ IsBoolFlag() bool }); ok && b.IsBoolFlag() {
186+
return true // bool flags take no separate value
187+
}
188+
return false // "--" was the value of the preceding flag
189+
}
190+
143191
func main() {
144192
os.Exit(run(os.Args[1:], os.Stdout, os.Stderr))
145193
}
@@ -153,20 +201,28 @@ func run(args []string, stdout, stderr io.Writer) int {
153201
addCommon := fs.Bool("common", true, "Add common subdomain prefixes")
154202
domainInfo := fs.String("domain", "", "Domain to extract potential subdomains from (e.g., company-name.com -> company, name)")
155203
verbose := fs.Bool("v", false, "Print every generated entry")
156-
if err := fs.Parse(args); err != nil {
204+
205+
positionals, err := parseInterspersed(fs, args)
206+
if err != nil {
157207
if errors.Is(err, flag.ErrHelp) {
158208
return 0
159209
}
160210
return 2
161211
}
162212

213+
// If any positionals remain, that's a usage error
214+
if len(positionals) > 0 {
215+
_, _ = fmt.Fprintf(stderr, "Error: unexpected argument %q; to take subdomains from a domain, pass -domain %s\n", positionals[0], positionals[0])
216+
return 2
217+
}
218+
163219
if *outputFile == "" {
164220
_, _ = fmt.Fprintln(stderr, "Error: output file cannot be empty")
165-
return 1
221+
return 2
166222
}
167223
if err := validateCombine(*combineWith); err != nil {
168224
_, _ = fmt.Fprintf(stderr, "Error: %v\n", err)
169-
return 1
225+
return 2
170226
}
171227

172228
words := generate(options{common: *addCommon, domain: *domainInfo, combine: *combineWith})

‎tools/wordlist-gen_test.go‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,65 @@ func TestRunGuards(t *testing.T) {
106106

107107
for _, bad := range []string{"shop!", "a b", "x.y"} {
108108
stderr.Reset()
109-
if code := run([]string{"-combine", "dev," + bad, "-o", filepath.Join(dir, "c.txt")}, io.Discard, &stderr); code != 1 || !strings.Contains(stderr.String(), "invalid -combine prefix") {
110-
t.Errorf("-combine %q: exit %d, stderr %q", bad, code, stderr.String())
109+
if code := run([]string{"-combine", "dev," + bad, "-o", filepath.Join(dir, "c.txt")}, io.Discard, &stderr); code != 2 || !strings.Contains(stderr.String(), "invalid -combine prefix") {
110+
t.Errorf("-combine %q: exit %d, stderr %q; want 2", bad, code, stderr.String())
111111
}
112112
}
113113
}
114+
115+
// TestRunRejectsPositionalAndAcceptsDomain tests issue #120: a leftover
116+
// positional is a usage error that points at -domain, while -domain itself
117+
// works and accepts flags written after it.
118+
func TestRunRejectsPositionalAndAcceptsDomain(t *testing.T) {
119+
dir := t.TempDir()
120+
out := filepath.Join(dir, "wl.txt")
121+
var stderr strings.Builder
122+
123+
// Without -domain, a positional should cause an error mentioning -domain
124+
if code := run([]string{"acme.com"}, io.Discard, &stderr); code != 2 || !strings.Contains(stderr.String(), "-domain") {
125+
t.Errorf("positional without -domain: exit %d, stderr %q; want 2 with -domain mentioned", code, stderr.String())
126+
}
127+
128+
// -domain takes acme.com as its value; -o (written after it) is still parsed.
129+
if code := run([]string{"-domain", "acme.com", "-o", out}, io.Discard, &stderr); code != 0 {
130+
t.Errorf("valid -domain with trailing flags: exit %d, stderr %q; want 0", code, stderr.String())
131+
}
132+
133+
// Check that the file was written
134+
content, err := os.ReadFile(out)
135+
if err != nil {
136+
t.Fatalf("could not read output file: %v", err)
137+
}
138+
if !strings.Contains(string(content), "www\n") {
139+
t.Errorf("expected 'www' in output; got %q", string(content))
140+
}
141+
}
142+
143+
// TestRunEmptyOutputFile tests issue #120: empty -o should exit 2.
144+
func TestRunEmptyOutputFile(t *testing.T) {
145+
var stderr strings.Builder
146+
if code := run([]string{"-o", ""}, io.Discard, &stderr); code != 2 || !strings.Contains(stderr.String(), "output file cannot be empty") {
147+
t.Errorf("empty -o: exit %d, stderr %q; want 2 with error message", code, stderr.String())
148+
}
149+
}
150+
151+
// TestRunInvalidCombinePrefix tests issue #120: invalid -combine should exit 2.
152+
func TestRunInvalidCombinePrefix(t *testing.T) {
153+
dir := t.TempDir()
154+
var stderr strings.Builder
155+
if code := run([]string{"-combine", "bad!.x", "-o", filepath.Join(dir, "c.txt")}, io.Discard, &stderr); code != 2 || !strings.Contains(stderr.String(), "invalid -combine prefix") {
156+
t.Errorf("invalid -combine prefix: exit %d, stderr %q; want 2 with error message", code, stderr.String())
157+
}
158+
}
159+
160+
// TestRunTerminator tests that -- terminator works correctly.
161+
func TestRunTerminator(t *testing.T) {
162+
dir := t.TempDir()
163+
out := filepath.Join(dir, "wl.txt")
164+
var stderr strings.Builder
165+
166+
// With --, all subsequent args are positionals and cause an error
167+
if code := run([]string{"-domain", "acme.com", "--", "-o", out}, io.Discard, &stderr); code != 2 || !strings.Contains(stderr.String(), "unexpected argument") {
168+
t.Errorf("-- terminator: exit %d, stderr %q; want 2", code, stderr.String())
169+
}
170+
}

0 commit comments

Comments
 (0)