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.

tests/test_gitenv.py:182

Show on the map →

Called by none

Calls _hostile_config_repo, whole_repo_scope

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

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_a_targets_own_git_config_cannot_run_commands → whole_repo_scope
REQ-DDB-PIPE-014catalog
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
lines 197–197sentinel = tmp_path / "pwned"lines 198–198root = _hostile_config_repo(tmp_path / "hostile", sentinel)lines 200–200scope.whole_repo_scope(root)lines 202–205assert 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" )

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.

tests/test_gitenv.py:85

Show on the map →

Called by none

Calls git_env

Checks

  • 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" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_git_env_drops_the_repointing_variables_and_keeps_the_rest → git_env
REQ-DDB-PIPE-014catalog
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
lines 96–96monkeypatch.setenv("GIT_DIR", "/somewhere/else/.git")lines 97–97monkeypatch.setenv("GIT_AUTHOR_NAME", "someone")lines 98–98env = gitenv.git_env()lines 100–100assert "GIT_DIR" not in envlines 101–101assert env.get("GIT_AUTHOR_NAME") == "someone", "a harmless git variable must survive"lines 102–102assert env.get("PATH") == os.environ.get("PATH"), "the rest of the environment is kept"lines 103–106assert 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" )

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.

tests/test_gitenv.py:213

Show on the map →

Called by none

Calls _hostile_config_repo, get_file_at_revision, get_staged_diff

Checks

  • assert not sentinel.exists(), ("the pre-commit gate ran a command the inspected repository's own .git/config chose" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_the_gate_git_path_cannot_run_the_trees_commands → get_staged_diff → run_git → git_argv
REQ-DDB-GUARD-023measuredtest_the_gate_git_path_cannot_run_the_trees_commands → get_staged_diff
REQ-DDB-PIPE-014catalog
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
lines 230–230from clew.guard.git import get_file_at_revision, get_staged_difflines 232–232sentinel = tmp_path / "gate-fsmonitor-ran"lines 233–233root = _hostile_config_repo(tmp_path / "hostile", sentinel)lines 234–234(root / "src.c").write_text("int main(void) { return 0; }\n", encoding="utf-8")lines 236–236original = Path.cwd()lines 237–237trylines 238–238os.chdir(root)lines 239–239get_staged_diff("src.c")lines 240–240get_file_at_revision("src.c", "HEAD")lines 244–246assert not sentinel.exists(), ( "the pre-commit gate ran a command the inspected repository's own .git/config chose" )

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.

tests/test_gitenv.py:109

Show on the map →

Called by none

Calls _as_if_inside_a_hook, _repo, whole_repo_scope

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

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_the_ignore_scope_answers_about_its_own_repo_inside_a_hook → whole_repo_scope
REQ-DDB-PIPE-014catalog
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
lines 121–121repo = _repo(tmp_path / "target")lines 122–122_as_if_inside_a_hook(monkeypatch, tmp_path / "foreign")lines 124–124excluded = {p.resolve() for p in scope.whole_repo_scope(repo).excludes}lines 125–128assert (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" )

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.

tests/test_gitenv.py:131

Show on the map →

Called by none

Calls _as_if_inside_a_hook, _repo, tracked_files

Checks

  • assert tracked is not None, "a real repository must not read as 'git unavailable'"
  • assert "kept.c" in tracked

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-CONFIG-001measuredtest_the_tracked_file_listing_answers_about_its_own_repo_inside_a_hook → tracked_files
REQ-DDB-PIPE-014catalog
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

Activity · 5 steps
lines 143–143repo = _repo(tmp_path / "target")lines 144–144_as_if_inside_a_hook(monkeypatch, tmp_path / "foreign")lines 146–146tracked = tracked_files(repo)lines 147–147assert tracked is not None, "a real repository must not read as 'git unavailable'"lines 148–148assert "kept.c" in tracked