Skip to content

fix: replace subprocess which with shutil.which() in Process.exists()#524

Merged
kimocoder merged 2 commits into
kimocoder:masterfrom
vlkumiloslearning:fix/process-exists-false-positive
Jun 3, 2026
Merged

fix: replace subprocess which with shutil.which() in Process.exists()#524
kimocoder merged 2 commits into
kimocoder:masterfrom
vlkumiloslearning:fix/process-exists-false-positive

Conversation

@vlkumiloslearning

@vlkumiloslearning vlkumiloslearning commented Jun 2, 2026

Copy link
Copy Markdown

Summary

Process.exists() in wifite/util/process.py shelled out to the which binary via Popen to check if a tool is installed. This had three problems:

1. Operator precedence bug (original crash)

exist = not stdout == stderr == ''

Evaluated as not (stdout == stderr == ''). When which service fails, stdout is '' but stderr is 'service not found' — so the chain returns False, not FalseTrue, and wifite crashes trying to exec a missing binary.

2. Fragile on minimal systems
which is not present on Alpine, minimal Docker images, or some chroots. Popen(['which', ...]) raises an unhandled FileNotFoundError, crashing every exists() call site.

3. Inconsistent with the rest of the codebase
shutil is already imported in this file. _resolve_tool() in the same class uses shutil.which(). So does system_check.py and dependency.py. The subprocess approach was the only outlier.

Fix

Replace the entire subprocess block with a single in-process call:

exist = shutil.which(program) is not None

This is correct, portable, and consistent with every other tool-detection pattern in the codebase.

Test plan

  • Run wifite --kill on a system where service is not installed (e.g. Arch Linux) — should no longer crash
  • Verify tools that exist are still detected correctly (aircrack-ng, tshark, etc.)
  • Verify tools that don't exist return False correctly

vlkumiloslearning and others added 2 commits June 3, 2026 00:15
…or precedence bug

`not stdout == stderr == ''` is evaluated as `not (stdout == stderr == '')`
which returns True when stderr is non-empty (e.g. "service not found"),
causing wifite to believe the tool exists and then crash with FileNotFoundError.

Replace with `bool(stdout)` — which is non-empty only when `which` actually
found the binary.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The previous approach shelled out to the `which` binary via Popen, which
has three problems:
- `which` is not present on all systems (Alpine, minimal Docker images),
  causing an unhandled FileNotFoundError on every exists() call
- any non-path text on stdout (e.g. zsh built-in printing "foo not found"
  to stdout) produced a false positive
- spawning a subprocess for a PATH lookup is unnecessary overhead

shutil is already imported and shutil.which() is already used by
_resolve_tool() in the same class, as well as system_check.py and
dependency.py. Using it here is consistent and correct on all platforms.
@vlkumiloslearning vlkumiloslearning changed the title fix: Process.exists() false positive due to operator precedence bug fix: replace subprocess which with shutil.which() in Process.exists() Jun 2, 2026
@kimocoder

Copy link
Copy Markdown
Owner

/claude

@kimocoder kimocoder merged commit b085733 into kimocoder:master Jun 3, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants