test_the_hook_parses_only_what_it_names¶
Tests › tests/test_hook.py
What this test is for
Why it exists
REPLACES `test_the_hook_does_not_parse_its_input`, WHICH BECAME FALSE BY DESIGN IN 1.0.10. That test forbade `import json` outright, on the reasoning that a parser is a decision tree over untrusted input and this component needed nothing from it. It needs one thing now — the basename of the file just inspected — because a note that cannot say what you just did was measured changing nothing.
Verifies REQ-DDB-WIKI-054
Given
source = HOOK_SOURCE.read_text(encoding="utf-8")tree = ast.parse(source)
6 more
for quoted in ('"tool_response"', "'tool_response'"): assert quoted not in source, ( f"{quoted} appears as a string literal: the response field is the one bulk " f"repository-authored blob in the event and must never be read" )read_keys = { node.args[0].value for node in ast.walk(tree) if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and node.func.attr == "get" and node.args and isinstance(node.args[0], ast.Constant) and isinstance(node.args[0].value, str) }allowed = {"tool_input", "file_path", "path", "command", hook.DISABLE_ENV, hook._SESSION_ENV}allowed |= {hook._STATE_ENV, "XDG_STATE_HOME"}guarded = [ node for node in ast.walk(tree) if isinstance(node, ast.Try) and any( isinstance(inner, ast.Call) and isinstance(inner.func, ast.Attribute) and inner.func.attr == "loads" for inner in ast.walk(node) ) ]for node in guarded: names = {h.type.id for h in node.handlers if isinstance(h.type, ast.Name)} assert "Exception" in names, ( f"the parse must catch Exception, not {sorted(names)} — RecursionError is a " f"RuntimeError and would escape a narrower tuple" )
When
- —
Then
- ✓ read_keys is at most allowed
- ✓ read_keys contains "tool_input"
- ✓ guarded holds
tests/test_hook.py:249 · its file's page
What it checks (5)
assert quoted not in source, ( f"{quoted} appears as a string literal: the response field is the one bulk " f"repository-authored blob in the event and must never be read" )not attributedassert read_keys <= allowed, ( f"the hook reads event/environment keys it does not declare: {sorted(read_keys - allowed)}" )not attributedassert "tool_input" in read_keys, "the extraction path is gone — this test would be vacuous"not attributedassert guarded, "the json.loads call is not inside a try block"not attributedassert "Exception" in names, ( f"the parse must catch Exception, not {sorted(names)} — RecursionError is a " f"RuntimeError and would escape a narrower tuple" )not attributed
What it reaches
Each row runs from this test, through the calls it makes, to the code a requirement names. If that code breaks, the test's checks are what should catch it. Reaching is not proving: a check only proves what it observes. No mutation records were given, so these rows say what the test REACHES.
Requirements it verifies (1)
| requirement | linked by | implementers exercised |
|---|---|---|
| REQ-DDB-WIKI-054 | catalog | unknown |