Skip to content

CLI: the fixes of #305's audit, and --verify on the OS trust store (stacked on #305) - #308

Open
bee-san wants to merge 11 commits into
mainfrom
pywhat-rs-cli-fixes
Open

bee-san wants to merge 11 commits into
mainfrom
pywhat-rs-cli-fixes

Conversation

@bee-san

@bee-san bee-san commented Nov 5, 2026

Copy link
Copy Markdown
Owner

The fixes for the audit of #305 (reports/audit-pywhat-rs-cli.md, MERGE_OK with ten non-blocking findings), in their own pull request as ciphey-lead decided, so that #305 didn't wait for them. Stacked on #305: the first four commits of this branch are #305's. Once #305 is on main, this branch is rebased onto it, and the diff below is what is left.

What changes

Closer to the Python CLI

  • F1, stdin: stdin is read when there are no inputs or only empty ones, like get_text()'s not any(value): producer | what "" searches stdin, as pywhat "" does.
  • F2, parser errors: an option without its value (what --rarity) or a flag with one (--json=1) prints only Error: Option '--rarity' requires an argument., as click 8.1 does: its parser raises these without a context. An invalid --map value still prints the usage line and the hint first, as click does there.
  • F3, markup errors: a --format string with a closing tag that closes nothing (--format '[b]%m[/i]') prints nothing and exits with 1, like Python, where rich raises its MarkupError. Rust says why on stderr (Error: closing tag '[/i]' at position 9 doesn't match any open tag); Python prints a traceback. Closing tags match the way rich matches them ([b]…[/bold], [bold red]…[/red bold], [/]).
  • F5, --verify: Google's answer is read the way json.loads() reads it (a UTF-8 BOM, UTF-16 and UTF-32, NaN and Infinity, integers of any size, the last of duplicate keys, the first detail that isn't an object ends the search). The request works like urlopen(..., timeout=10): 10 s to connect and for every read and write rather than for the whole request, 301/302/303/307 redirects followed (up to 10), and the common errors described in urllib's words (timed out, <urlopen error [Errno 111] Connection refused>, <urlopen error [Errno -2] Name or service not known>). Against the auditor's fake Google (verify_cmp.py), all 39 scenarios now give Python's answer (32 before).
  • F7, terminals: rich's TERM rules: a dumb terminal is checked before TERM is stripped, and the colour depth is what follows the last - (TERM=256color is 8-bit). On a dumb terminal the table is 80 columns wide unless COLUMNS and LINES are both set, as in rich. On a terminal, File: <location> is one styled span, as rich prints Python's markup.

F6, the root certificates: --verify trusts the operating system's root certificates through ureq's platform-verifier feature (rustls-platform-verifier 0.7.1, MIT OR Apache-2.0), with rustls on ring, like Python's urllib. webpki-roots (Mozilla's list, CDLA-Permissive-2.0) is no longer a dependency. rustls-platform-verifier depends on webpki-root-certs only for wasm32, where verify isn't built: cargo tree -i webpki-root-certs is empty for Linux, macOS and Windows. With SSL_CERT_FILE and SSL_CERT_DIR pointing nowhere, both CLIs fail the TLS handshake (could not ask Google (...)); with the system store, both get Google's answer.

F8, tests: the 20 of the auditor's 41 mutants that survived, rerun on this branch with the auditor's harness: 17 are killed now; 2 can't be applied, because the code they mutate was rewritten (D1 is F1's fix, V2 the old JSON handling); O7 (pretty cells not sanitized) is equivalent, as matches have no control characters left. The new tests: the parser's = values, empty values, unknown --opt=value and the order of the callbacks; a file signature without matches; matched text with markup in --format and the table; null exploits in the table; stdin's str.strip(); colour detection and control codes like rich's; [@…] meta tags; the 0.5 s delay of the bars (an in-memory terminal); the progress of every text of a file; any 2xx is valid; the request (User-Agent, redirects, a refused connection) against a local server; Python's describe_error() on 54 bodies. parity_cli.py now compares stderr too (unless Python prints a traceback), fails a known difference that stops differing, and checks that src/cli/help.txt is what.py's help (--update-help writes it).

F4, F9, docs: docs/cli.md lists the remaining differences: rich's width effects (only the table and the help depend on the width in Rust), output that is always UTF-8 (with U+FFFD for names that aren't), Ctrl-C, the colours of the table, and the root certificates.

F10, SonarCloud: main_command is split into the search, --stream and the complete output (SearchRun, StreamPrinter, complete_search), and print_raw/pretty_print into raw_match and description_cell; PoisonError::into_inner for the 7 closures; no redundant 'static.

Parity

$ PYWHAT_CLI=target/release/what python scripts/parity_cli.py
...
299 command lines (106 from tests/test_click.py): 10 known difference, 289 same

The known differences are #305's 9 (matched text and file names as markup, Python bug 4; emoji codes in --format; -v; python -m pywhat without arguments) and a tab in a file name in the table: rich counts it as 0 columns wide when it sizes the column and then wraps the cell, comfy-table shows it expanded. 26 command lines are new: the parser's values and errors, markup errors, empty inputs with stdin, stdin's whitespace, a null exploit in the table, a signature without matches, a tab in a file name.

A random differential of options and inputs against Python (random-differential.py) found no other difference: 1,795 command lines on this head (3 seeds), and 2,785 on the earlier revisions.

Commands and results

On this branch's head, rebased on main 0c78b02 (with #305's commits below it). Python 3.10.20, Rust 1.99.1, shared 64-core host.

Command Result
$CIPHEY_TC/gate.sh pywhat-rs-cli-ci.sh (fmt; clippy default, no-default and wasm; cargo test; --no-default-features; the wasm32 lib with default features; requirements.txt, the extension with maturin, the suite as nox runs it; mypy on the CLI's Python files on Python 3.8 and 3.10 for Linux, Windows and macOS; test_click against the binary; parity) LOCAL CI PASS
cargo test --locked lib 84 passed, tests/cli.rs 24 passed, examples 3, charset_only 2, golden 2, whole 2, doctests 5
pytest --cache-clear (nox's way: black, isort, flake8, mypy, the Rust engine) 2432 passed, 6 skipped
PYWHAT_CLI=target/release/what pytest -o addopts="" tests/test_click.py 144 passed, 9 skipped
PYWHAT_CLI=target/release/what python scripts/parity_cli.py 299 command lines, 289 same, 10 known differences
The auditor's verify_cmp.py against a build that reads Google's URL from an environment variable (scratch patch, not committed) 39 scenarios same, 0 different
The auditor's mrun.py on its 20 surviving mutants 17 killed, 2 not applicable, 1 equivalent
cargo tree -e normal --target {x86_64-unknown-linux-gnu,x86_64-pc-windows-msvc,aarch64-apple-darwin}, the crates that cli and verify add 42/44/43 crates: MIT, Apache-2.0, ISC, BSD-3-Clause; no webpki-roots, nothing CDLA
what --verify --format '%d' AIzaSyA0…0 (one real request) Verification: invalid, Google rejected the key (API_KEY_INVALID)

Evidence: /local/home/skerraut/herd-synth3/reports/pywhat-rs-cli-evidence/ (local-ci-fixes.log, verify-fake-google-next.txt, mutants-next.txt, licences-cli-*.txt, random-differential.*). The answer to each finding: reports/pywhat-rs-cli-audit-response.md.

Identifier::iter_identify_with_progress() calls a callback with a Progress
(the file, how many files and regex searches of the file are done, and a
regex that is searched for) when each regex search starts and ends, and once
a file is done, like the progress callback of Python's iter_identify(). For
texts over 4 KB the regexes run on rayon's threads, and so does the callback,
one call at a time. iter_identify() keeps its signature and output and runs
without a callback. The CLI's progress bars use it.
pywhat::cli::main(args) -> i32 (feature cli, default) runs the command line of
pywhat/what.py: the same options, defaults, help, errors, exit codes and
output. The arguments are parsed the way click 8.1 parses them (multi-letter
short options, clusters, unknown options as inputs), --help is click's
HelpFormatter and Python's textwrap, and raw, custom --format, --json and
--stream output are byte for byte Python's; --format pretty has the same cells
(comfy-table). Colours only on a terminal (NO_COLOR), progress bars (indicatif)
by progress.py's rules. --verify (feature verify, default) asks Google with
ureq/rustls, once per key, with GoogleAPIKeyVerifier's messages; the HTTP call
is injectable. --interactive needs the Python package. On wasm32 neither
feature adds anything.

scripts/parity_cli.py compares 272 command lines with python -m pywhat (every
case of tests/test_click.py, read from it): 0 differences beyond the 9 known
ones it lists. tests/conftest.py runs tests/test_click.py against $PYWHAT_CLI.
The Rust workflow runs both. docs/cli.md documents the command.
… urlopen; stdin when all inputs are empty; docs
…res without matches, null exploits, stdin strip, colours, control codes, meta tags, the 0.5 s delay, progress of every text, 2xx, the HTTP request
…aw and pretty_print split up; docs for the audit's findings
…form-verifier, ring), no bundled CA list (audit F6)
@sonarqubecloud

sonarqubecloud Bot commented Nov 5, 2026

Copy link
Copy Markdown

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.

1 participant