test_security_surface.py¶
tests/test_security_surface.py
Properties every subprocess in this package must have, asserted over the SOURCE.
test_a_diff_through_the_hardened_argv_works
None test_security_surface.test_a_diff_through_the_hardened_argv_works(Path tmp_path)
git diff --cached through git_argv returns the patch.
tests/test_security_surface.py:194
Checks
assert out.returncode == 0, out.stderrassert "+two" in out.stdout
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_a_diff_through_the_hardened_argv_works → git_argv |
| REQ-DDB-PIPE-014 | catalog |
Source · 21 lines
def test_a_diff_through_the_hardened_argv_works(tmp_path: Path) -> None:
"""FOUND DOGFOODING (round 3, P6c): `-c diff.external=` is not "no external diff" — git runs
the EMPTY STRING as the diff program and dies ("cannot run : No such file or directory"), so
every `git diff` through `git_argv` failed and clew-guard's impact report was silently `[]`
for every adopter. A diff-family command now carries `--no-ext-diff`, which forbids any
external diff whatever the config says.
@brief git diff --cached through git_argv returns the patch.
"""
import subprocess
from clew.gitenv import git_argv
repo = _repo_with_change(tmp_path)
out = subprocess.run(
git_argv("diff", "--cached", "-U0"), cwd=repo, capture_output=True, text=True
)
assert out.returncode == 0, out.stderr
assert "+two" in out.stdout
Activity · 6 steps
test_a_module_with_an_indirect_argv_disarms_at_its_choke_point
None test_security_surface.test_a_module_with_an_indirect_argv_disarms_at_its_choke_point(str module, str choke_point)
Each indirect-argv module's choke point calls git_argv.
tests/test_security_surface.py:125
Checks
assert functions, f"{module} no longer defines {choke_point}; this exemption is stale"assert "git_argv" in called, (f"{module}:{choke_point} builds the argv that reaches subprocess and does not call " f"git_argv, so the inspected repository's own .git/config chooses a command git runs" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-014 | catalog |
Source · 32 lines
def test_a_module_with_an_indirect_argv_disarms_at_its_choke_point(
module: str, choke_point: str
) -> None:
"""THE OTHER HALF OF THE RULE ABOVE, which the argv test structurally cannot see. Where a
module builds a git argv and passes it to a RUNNER, the literal list is not the thing that
reaches `subprocess` — so the assertion has to move to the function every such call passes
through, and that function has to be named rather than inferred.
Asserted on the AST so the call is `git_argv(...)` and not a mention in a comment.
@brief Each indirect-argv module's choke point calls git_argv.
@version 1
"""
tree = ast.parse((PACKAGE / module).read_text(encoding="utf-8"))
functions = [
node
for node in ast.walk(tree)
if isinstance(node, ast.FunctionDef) and node.name == choke_point
]
assert functions, f"{module} no longer defines {choke_point}; this exemption is stale"
called = {
node.func.id
for node in ast.walk(functions[0])
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name)
}
assert "git_argv" in called, (
f"{module}:{choke_point} builds the argv that reaches subprocess and does not call "
f"git_argv, so the inspected repository's own .git/config chooses a command git runs"
)
@pytest.mark.parametrize("key", ["core.fsmonitor", "core.hooksPath", "diff.external"])test_a_planted_external_diff_never_runs
None test_security_surface.test_a_planted_external_diff_never_runs(Path tmp_path)
diff.external in the tree's own config: not executed, and the diff still answers.
tests/test_security_surface.py:215
Checks
assert out.returncode == 0, (args, out.stderr)assert not marker.exists(), "the tree's external diff program ran"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_a_planted_external_diff_never_runs → git_argv |
| REQ-DDB-PIPE-014 | catalog |
Source · 17 lines
def test_a_planted_external_diff_never_runs(tmp_path: Path) -> None:
"""@brief diff.external in the tree's own config: not executed, and the diff still answers."""
import subprocess
from clew.gitenv import git_argv
repo = _repo_with_change(tmp_path)
marker = tmp_path / "ran"
script = tmp_path / "ext.sh"
script.write_text(f"#!/bin/sh\ntouch {marker}\n")
script.chmod(0o755)
subprocess.run(["git", "config", "diff.external", str(script)], cwd=repo, check=True)
for args in (("diff", "--cached"), ("log", "-p", "-1"), ("show", "HEAD")):
out = subprocess.run(git_argv(*args), cwd=repo, capture_output=True, text=True)
assert out.returncode == 0, (args, out.stderr)
subprocess.run(git_argv("diff", "HEAD"), cwd=repo, capture_output=True, text=True)
assert not marker.exists(), "the tree's external diff program ran"1 more lines, not shown
Activity · 13 steps
test_every_git_invocation_disarms_the_inspected_trees_config
None test_security_surface.test_every_git_invocation_disarms_the_inspected_trees_config()
No call site builds a bare git argv.
tests/test_security_surface.py:90
Checks
assert not offenders, ("these git calls bypass gitenv.git_argv, so the inspected repository's own .git/config " f"can make git run a command of its choosing: {offenders}" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-014 | catalog |
Source · 35 lines
def test_every_git_invocation_disarms_the_inspected_trees_config() -> None:
"""THE RULE LIVES IN ONE PLACE AND THIS IS WHAT KEEPS IT THERE. `gitenv.git_argv` prepends
`-c core.fsmonitor=` and its siblings, which outrank the config file inside the tree being
inspected. A call site that builds its own `["git", …]` list reopens the execution path, and
it reopens it silently — the query still works, it just also runs the attacker's command.
Asserted on the argv's first element: a literal `"git"` is the defect, `git_argv(...)` is the
fix. A variable first element is reported too, since it cannot be shown safe from here.
@brief No call site builds a bare git argv.
@version 1
"""
offenders: list[str] = []
for path, call in _subprocess_calls():
if not call.args:
continue
argv = call.args[0]
if isinstance(argv, ast.Call):
func = argv.func
if isinstance(func, ast.Name) and func.id == "git_argv":
continue
if isinstance(argv, (ast.List, ast.Tuple)) and argv.elts:
first = argv.elts[0]
if isinstance(first, ast.Constant) and first.value == "git":
offenders.append(f"{path}:{call.lineno}")
elif isinstance(argv, ast.Constant) and argv.value == "git":
offenders.append(f"{path}:{call.lineno}")
assert not offenders, (
"these git calls bypass gitenv.git_argv, so the inspected repository's own .git/config "
f"can make git run a command of its choosing: {offenders}"
)
@pytest.mark.parametrize(("module", "choke_point"), sorted(_INDIRECT_ARGV.items()))test_every_subprocess_call_in_the_package_is_bounded
None test_security_surface.test_every_subprocess_call_in_the_package_is_bounded()
No subprocess.run in clew/ omits a timeout.
tests/test_security_surface.py:69
Checks
assert not unbounded, ("these subprocess.run calls carry no timeout, so a hostile or merely huge repository " f"can hang the build, the gate or the server indefinitely: {unbounded}" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-PIPE-014 | catalog |
Source · 21 lines
def test_every_subprocess_call_in_the_package_is_bounded() -> None:
"""A MISSING TIMEOUT IS A HANG SOMEONE ELSE PAYS FOR. The gate runs inside a developer's
commit; the MCP server runs for a session; both call git and cargo against trees they did not
write. `cargo metadata` on a hostile `Cargo.toml` and `git ls-files` on a huge tree were both
unbounded before the 1.1.0 audit.
@brief No subprocess.run in clew/ omits a timeout.
@version 1
"""
unbounded = [
f"{path}:{call.lineno}"
for path, call in _subprocess_calls()
if call.func.attr == "run" and not any(kw.arg == "timeout" for kw in call.keywords)
]
assert not unbounded, (
"these subprocess.run calls carry no timeout, so a hostile or merely huge repository "
f"can hang the build, the gate or the server indefinitely: {unbounded}"
)
Activity · 2 steps
test_the_disarmed_key_list_reaches_the_argv
None test_security_surface.test_the_disarmed_key_list_reaches_the_argv(str key)
git_argv emits each command-valued key, hooksPath pointed at /dev/null.
tests/test_security_surface.py:157
Checks
assert argv[0] == "git" and argv[-1] == "ls-files"assert expected in argv, f"{key} is not disarmed in {argv}"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_the_disarmed_key_list_reaches_the_argv → git_argv |
| REQ-DDB-PIPE-014 | catalog |
Source · 16 lines
def test_the_disarmed_key_list_reaches_the_argv(key: str) -> None:
"""The helper is only a defence if the keys actually land on the command line, and
`core.hooksPath` must be `/dev/null` rather than empty — an empty value sends git back to the
repository's own hooks directory.
@brief git_argv emits each command-valued key, hooksPath pointed at /dev/null.
@version 1
"""
from clew.gitenv import git_argv
argv = git_argv("ls-files")
assert argv[0] == "git" and argv[-1] == "ls-files"
expected = f"{key}=/dev/null" if key == "core.hooksPath" else f"{key}="
assert expected in argv, f"{key} is not disarmed in {argv}"