Repository navigation
harden(pytest): close -p bare-relative + glued-form bypass (#49) - #59
Merged
Merged
Conversation
`check_forbidden_pytest_args` only caught `-p VALUE` (space form) and only when `looks_like_path(value)` returned true. Two bypass classes: 1. **Bare-relative path** -- the shared looks_like_path requires `./`, `/`, `~/`, `.\`, or a drive letter. `subdir/plugin.py` had no leading marker and fell through. Pytest loads it as a file regardless. 2. **Glued -p forms** -- argparse accepts `-pVALUE` and `-p=VALUE` in addition to `-p VALUE`. The old `a == "-p"` exact match missed both. ## Fix New `pytest_p_value_is_filesystem_path` (pytest-specific, stricter than shared `looks_like_path`): - Anything `looks_like_path` catches - Any value containing `/` or `\` (closes bypass #1) - Any value ending in `.py` (a dotted module name never ends in `.py`) New `pytest_p_value(a, next)` helper recognises: - `-p VALUE` -- consumes next - `-pVALUE` (glued) - `-p=VALUE` (equals) ## Deny message now cites #49 (review P3) Pre-PR review caught that `pyrbjvm_deny_message` hardcodes `(issue #36)` (the umbrella). New `pyrbjvm_deny_message_with_issue(...)` variant lets the `-p` deny cite #49 directly so operators land on the right tracker entry. Other pyrbjvmdotnet denies continue to cite #36. ## Accepted residual risk (documented) `pytest -p evil.payload` resolves through Python's import system. When pytest is invoked as `python -m pytest`, `.` is on sys.path and the attacker can plant `evil/payload.py` in cwd. We can't reject dotted names categorically (`pytest_django.plugin` etc. are legitimate). Documented in `pytest_p_value_is_filesystem_path`'s doc-comment as accepted residual risk; remaining defense is the agent threat model. ## Pinning tests (4 new unit + 4 new integration) Unit (src/core/utils.rs): - pytest_rejects_p_bare_relative_path (the main #49 bypass) - pytest_rejects_p_glued_and_equals_forms (the -pVALUE / -p=VALUE pair) - pytest_p_deny_cites_issue_49 (P3 pin: -p cites #49, --rootdir #36) - pytest_allows_p_with_module_name (extended negative coverage incl. dotted module names + glued legitimate forms) Integration (tests/runtime_hardening.rs): - pytest_p_bare_relative_path_is_rejected (asserts #49 in stderr) - pytest_p_glued_form_is_rejected - pytest_p_bare_py_extension_is_rejected - pytest_p_legitimate_module_names_still_allowed Updated existing `pytest_minus_p_with_path_is_rejected` to assert #49. ## Verified - cargo build clean - cargo test --bin contextcrawler: 2156 pass, 0 fail - All 8 integration suites green (runtime_hardening 16 pass, was 12) Closes #49.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes the pytest
-pbypass in two classes:-p subdir/plugin.py) — sharedlooks_like_pathrequired.////~//drive letter, so bare relative paths fell through unfiltered.-pVALUE,-p=VALUE) — argparse accepts both; the olda == "-p"exact match missed them.New
pytest_p_value_is_filesystem_path()rejects any value with/,\, or.pysuffix. Newpytest_p_value(a, next)recognises all 3 argparse-accepted shapes.Pre-PR review fixes (in same commit)
evil.payloadcan still resolve to a file via Python's import system when.is onsys.path(default forpython -m pytest). Can't reject categorically —pytest_django.pluginetc. are legitimate. Documented as accepted residual risk inpytest_p_value_is_filesystem_path's doc-comment; remaining defense is the agent threat model.pyrbjvm_deny_messagehardcodes#36(umbrella). Addedpyrbjvm_deny_message_with_issuevariant; pytest-pdeny now cites harden(pytest): -p <bare-relative-path> bypass via looks_like_path #49 directly so operators land on the right tracker entry.Pinning tests (4 new unit + 4 new integration; 1 updated)
Unit:
pytest_rejects_p_bare_relative_path(main harden(pytest): -p <bare-relative-path> bypass via looks_like_path #49 bypass)pytest_rejects_p_glued_and_equals_forms(-pVALUE / -p=VALUE)pytest_p_deny_cites_issue_49(P3 pin: -p cites harden(pytest): -p <bare-relative-path> bypass via looks_like_path #49, --rootdir cites umbrella harden(python/ruby/jvm/dotnet): strip PYTHONPATH/RUBYOPT/JAVA_TOOL_OPTIONS/DOTNET_STARTUP_HOOKS #36)pytest_allows_p_with_module_name(extended negatives incl. dotted module names + glued legitimate forms)Integration:
pytest_p_bare_relative_path_is_rejected(asserts harden(pytest): -p <bare-relative-path> bypass via looks_like_path #49 in stderr)pytest_p_glued_form_is_rejectedpytest_p_bare_py_extension_is_rejectedpytest_p_legitimate_module_names_still_allowedUpdated
pytest_minus_p_with_path_is_rejectedto assert #49.Verified
cargo buildcleancargo test --bin contextcrawler— 2156 pass / 0 failpytest -p subdir/plugin.pyandpytest -pevil.pyboth deny with harden(pytest): -p <bare-relative-path> bypass via looks_like_path #49Closes #49.