test_gitenv.py¶
tests/test_gitenv.py
Every git subprocess this package runs must ignore the hook environment it inherits.
test_a_targets_own_git_config_cannot_run_commands
None test_gitenv.test_a_targets_own_git_config_cannot_run_commands(Path tmp_path)
A target repository's own git config must not run commands on clew's git calls.
Checks
assert not sentinel.exists(), ("the target repository's own .git/config executed a command during scope " "derivation — cloning a repo and indexing it is arbitrary code execution" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_a_targets_own_git_config_cannot_run_commands → whole_repo_scope |
| REQ-DDB-PIPE-014 | catalog |
Source · 26 lines
def test_a_targets_own_git_config_cannot_run_commands(tmp_path: Path) -> None:
"""THE SHORTEST EXECUTION PATH IN THE PRODUCT, found by an adversarial audit of the
1.1.0 branch and reproduced twice under git 2.34.1. `core.fsmonitor` is executed by git
on `ls-files`, so every one of this package's git queries runs code chosen by the tree
it was pointed at — during an index build AND during the pre-commit gate. `gitenv`
already strips the variables a hook exports; this is the orthogonal channel, config
INSIDE the tree, which no environment scrub can reach.
THE DEFENCE IS ARGV, NOT ENV: `-c core.fsmonitor=` on the command line outranks the
tree's config file. Asserted through `whole_repo_scope`, the first git call any build
makes, rather than against the helper, so a call site that forgets the helper fails here.
@brief A planted `core.fsmonitor` never executes during scope derivation.
@version 1
"""
sentinel = tmp_path / "pwned"
root = _hostile_config_repo(tmp_path / "hostile", sentinel)
scope.whole_repo_scope(root)
assert not sentinel.exists(), (
"the target repository's own .git/config executed a command during scope "
"derivation — cloning a repo and indexing it is arbitrary code execution"
)
Activity · 4 steps
test_git_env_drops_the_repointing_variables_and_keeps_the_rest
None test_gitenv.test_git_env_drops_the_repointing_variables_and_keeps_the_rest(pytest.MonkeyPatch monkeypatch)
`git_env` removes exactly the overrides and nothing else.
Checks
assert "GIT_DIR" not in envassert env.get("GIT_AUTHOR_NAME") == "someone", "a harmless git variable must survive"assert env.get("PATH") == os.environ.get("PATH"), "the rest of the environment is kept"assert os.environ.get("GIT_DIR") == "/somewhere/else/.git", ("the scrub must return a COPY — mutating os.environ would change how the HOST " "process's own git commands behave" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_git_env_drops_the_repointing_variables_and_keeps_the_rest → git_env |
| REQ-DDB-PIPE-014 | catalog |
Source · 24 lines
def test_git_env_drops_the_repointing_variables_and_keeps_the_rest(
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The scrub is NAMED, not a `GIT_*` prefix sweep. `GIT_AUTHOR_NAME` and `GIT_EDITOR`
are exported to hooks too and are harmless; removing an unknown variable by prefix is
how a scrub starts breaking things nobody asked it to touch.
@brief `git_env` removes exactly the overrides and nothing else.
@return None.
@version 1
"""
monkeypatch.setenv("GIT_DIR", "/somewhere/else/.git")
monkeypatch.setenv("GIT_AUTHOR_NAME", "someone")
env = gitenv.git_env()
assert "GIT_DIR" not in env
assert env.get("GIT_AUTHOR_NAME") == "someone", "a harmless git variable must survive"
assert env.get("PATH") == os.environ.get("PATH"), "the rest of the environment is kept"
assert os.environ.get("GIT_DIR") == "/somewhere/else/.git", (
"the scrub must return a COPY — mutating os.environ would change how the HOST "
"process's own git commands behave"
)
Activity · 7 steps
test_the_gate_git_path_cannot_run_the_trees_commands
None test_gitenv.test_the_gate_git_path_cannot_run_the_trees_commands(Path tmp_path)
The pre-commit gate's own git calls disarm the tree's config too.
Checks
assert not sentinel.exists(), ("the pre-commit gate ran a command the inspected repository's own .git/config chose" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_the_gate_git_path_cannot_run_the_trees_commands → get_staged_diff → run_git → git_argv |
| REQ-DDB-GUARD-023 | measured | test_the_gate_git_path_cannot_run_the_trees_commands → get_staged_diff |
| REQ-DDB-PIPE-014 | catalog |
Source · 34 lines
def test_the_gate_git_path_cannot_run_the_trees_commands(tmp_path: Path) -> None:
"""THE SECOND HALF OF C1, MISSED BY THE FIRST PASS AND FOUND BY A TEST FOR SOMETHING ELSE.
`clew.guard` deliberately shares no code with the pipeline, so it has its OWN git runner —
`run_git(["git", …])` through an injectable `_default_run_command`. Routing the pipeline's
five call sites through `git_argv` left all six of the gate's untouched, and the gate is
the WORSE context: it runs on a developer's machine, as that developer, on whatever branch
they have checked out to review.
`tests/test_security_surface.py` did not catch it because the argv reaching
`subprocess.run` there is a VARIABLE, and that test only recognised a literal. It now
names this module as the one indirection it accepts, and asserts the disarming happens at
the choke point — which is the only way a structural test can cover an indirect argv.
@brief A planted `core.fsmonitor` never executes during a gate git call.
@return None.
@version 1
"""
from clew.guard.git import get_file_at_revision, get_staged_diff
sentinel = tmp_path / "gate-fsmonitor-ran"
root = _hostile_config_repo(tmp_path / "hostile", sentinel)
(root / "src.c").write_text("int main(void) { return 0; }\n", encoding="utf-8")
original = Path.cwd()
try:
os.chdir(root)
get_staged_diff("src.c")
get_file_at_revision("src.c", "HEAD")
finally:
os.chdir(original)
assert not sentinel.exists(), (
"the pre-commit gate ran a command the inspected repository's own .git/config chose"
)1 more lines, not shown
Activity · 10 steps
test_the_ignore_scope_answers_about_its_own_repo_inside_a_hook
None test_gitenv.test_the_ignore_scope_answers_about_its_own_repo_inside_a_hook(Path tmp_path, pytest.MonkeyPatch monkeypatch)
`.gitignore` exclusions are read from the target repo, not the hook's.
Checks
assert (repo / "ignored").resolve() in excluded, ("the ignored directory belongs to THIS repo's .gitignore; a hook's GIT_DIR must " "not decide what this index excludes" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_the_ignore_scope_answers_about_its_own_repo_inside_a_hook → whole_repo_scope |
| REQ-DDB-PIPE-014 | catalog |
Source · 22 lines
def test_the_ignore_scope_answers_about_its_own_repo_inside_a_hook(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""`git -C <root>` reads as "definitely this repository" and is not: an absolute
`GIT_DIR` outranks it. Under the unfixed code this returned the OTHER repository's
ignore list, so the scope excluded paths that do not exist here and admitted the ones
that should have been excluded.
@brief `.gitignore` exclusions are read from the target repo, not the hook's.
@return None.
@version 1
"""
repo = _repo(tmp_path / "target")
_as_if_inside_a_hook(monkeypatch, tmp_path / "foreign")
excluded = {p.resolve() for p in scope.whole_repo_scope(repo).excludes}
assert (repo / "ignored").resolve() in excluded, (
"the ignored directory belongs to THIS repo's .gitignore; a hook's GIT_DIR must "
"not decide what this index excludes"
)
Activity · 4 steps
test_the_tracked_file_listing_answers_about_its_own_repo_inside_a_hook
None test_gitenv.test_the_tracked_file_listing_answers_about_its_own_repo_inside_a_hook(Path tmp_path, pytest.MonkeyPatch monkeypatch)
`tracked_files` lists the target repo's files, not the hook's.
Checks
assert tracked is not None, "a real repository must not read as 'git unavailable'"assert "kept.c" in tracked
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_the_tracked_file_listing_answers_about_its_own_repo_inside_a_hook → tracked_files |
| REQ-DDB-PIPE-014 | catalog |
Source · 20 lines
def test_the_tracked_file_listing_answers_about_its_own_repo_inside_a_hook(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""`cwd=<repo_root>` is overridden by an absolute `GIT_DIR` just as `-C` is, and the
consequence is worse here because the result is used as a MEMBERSHIP test: every file
in the target repository reads as untracked, so `propose_declaration` reports a
correctly-indexed repo as indexing nothing it tracks.
@brief `tracked_files` lists the target repo's files, not the hook's.
@return None.
@version 1
"""
repo = _repo(tmp_path / "target")
_as_if_inside_a_hook(monkeypatch, tmp_path / "foreign")
tracked = tracked_files(repo)
assert tracked is not None, "a real repository must not read as 'git unavailable'"
assert "kept.c" in tracked