Skip to content

harden(pytest): close -p bare-relative + glued-form bypass (#49) - #59

Merged
thehoff merged 1 commit into
developfrom
harden/pytest-p-bypass-49
May 18, 2026
Merged

thehoff merged 1 commit into
developfrom
harden/pytest-p-bypass-49

Conversation

@thehoff

@thehoff thehoff commented May 18, 2026

Copy link
Copy Markdown
Owner

Summary

Closes the pytest -p bypass in two classes:

  1. Bare-relative paths (-p subdir/plugin.py) — shared looks_like_path required .////~//drive letter, so bare relative paths fell through unfiltered.
  2. Glued forms (-pVALUE, -p=VALUE) — argparse accepts both; the old a == "-p" exact match missed them.

New pytest_p_value_is_filesystem_path() rejects any value with /, \, or .py suffix. New pytest_p_value(a, next) recognises all 3 argparse-accepted shapes.

Pre-PR review fixes (in same commit)

  • Scope clarification — dotted module names like evil.payload can still resolve to a file via Python's import system when . is on sys.path (default for python -m pytest). Can't reject categorically — pytest_django.plugin etc. are legitimate. Documented as accepted residual risk in pytest_p_value_is_filesystem_path's doc-comment; remaining defense is the agent threat model.
  • Issue-ref correction — pyrbjvm_deny_message hardcodes #36 (umbrella). Added pyrbjvm_deny_message_with_issue variant; pytest -p deny 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:

Integration:

Updated pytest_minus_p_with_path_is_rejected to assert #49.

Verified

Closes #49.

`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.
@thehoff
thehoff merged commit b5442fe into develop May 18, 2026
3 checks passed
@thehoff
thehoff deleted the harden/pytest-p-bypass-49 branch May 18, 2026 12:47
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.

harden(pytest): -p <bare-relative-path> bypass via looks_like_path

1 participant