fix(tests): hardened-YAML guard skips a tree-root virtualenv by path segment (#2890) - #2895
Merged
Merged
Conversation
…segment (#2890) `test_no_service_parses_yaml_without_the_shared_loader` excluded virtualenvs with `"/venv/" in rel`, where `rel` is RELATIVE to the scanned root — so a venv at the root of that tree (`src/backend/venv/`, i.e. where one actually lives) yields `venv/lib/...` with no leading slash, never matches, and every vendored `safe_load` in `site-packages` is reported as an unguarded parse on a clean checkout. Only a venv nested a directory deep was ever excluded. The scan is now `_bare_safe_load_offenders(roots)`, pure over the filesystem, with the exclusion matched on `path.relative_to(root).parts` against `_VENDORED_PARTS` ({venv, .venv, site-packages, node_modules, __pycache__}) — position-independent, and a directory merely NAMED like one (`my-venv-tool/`) is still scanned. `git ls-files` was rejected: it would hide an untracked first-party file from the pre-commit scan. Proven both ways on the real tree: a planted `src/backend/venv/lib/python3.12/site-packages/starlette/mod.py` with a bare parse no longer fails the guard, and a planted `services/_tmp_bare.py` still does. The new fixture tests pin both directions across root/nested/.venv/ node_modules/__pycache__ placements. The other 34 `rglob("*.py")` guards under tests/unit were audited with the same planted venv: none false-fires. Fixes #2890 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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
test_no_service_parses_yaml_without_the_shared_loaderexcluded virtualenvs with"/venv/" in relon a path relative to the scanned root, so a venv at the root of the tree (src/backend/venv/— where one actually lives) yieldedvenv/lib/..., never matched, and vendoredstarlette/uvicornsafe_loadcalls were reported as unguarded parses on a clean checkout._bare_safe_load_offenders(roots)whose exclusion matchespath.relative_to(root).partsagainst_VENDORED_PARTS = {venv, .venv, site-packages, node_modules, __pycache__}— position-independent; a directory merely named like one (my-venv-tool/) is still scanned.git ls-files(the issue's alternative) rejected: it would hide an untracked first-party file from the pre-commit scan.Acceptance criteria
venv/,.venv/) + proven on the real tree with a plantedsrc/backend/venv/.../starlette/mod.pyyaml.safe_loadin first-party code — plantedservices/_tmp_bare.py→ red; fixture test assertssvc.pyis the only offender beside a vendored twintest_2890_exclusion_is_segment_based_not_substring_based.venv,site-packages,node_modules(and__pycache__) covered, root and nestedSibling audit
The other 34
rglob("*.py")guards undertests/unit/were run with the same planted root venv (baresafe_load, a SETNX lock, a 404 raise): 683 passed, only this guard false-fired.test_1920already excludesvenv/viastartswith.Test Plan
cd tests && pytest unit/test_ent314_hardened_yaml.py unit/test_1965_agent_server_safe_yaml.py -v— 43 passedlint_sys_modules.py— no new violationsFixes #2890
🤖 Generated with Claude Code