test_the_hook_parses_only_what_it_names

Tests › tests/test_hook.py

passed Passed in the packaged run.

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

  1. source = HOOK_SOURCE.read_text(encoding="utf-8")
  2. tree = ast.parse(source)
6 more
  1. 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" )
  2. 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) }
  3. allowed = {"tool_input", "file_path", "path", "command", hook.DISABLE_ENV, hook._SESSION_ENV}
  4. allowed |= {hook._STATE_ENV, "XDG_STATE_HOME"}
  5. 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) ) ]
  6. 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

  1. —
→

Then

  1. ✓ read_keys is at most allowed
  2. ✓ read_keys contains "tool_input"
  3. ✓ guarded holds

tests/test_hook.py:249 · its file's page

What it checks (5)

  1. 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 attributed
  2. assert read_keys <= allowed, ( f"the hook reads event/environment keys it does not declare: {sorted(read_keys - allowed)}" ) not attributed
  3. assert "tool_input" in read_keys, "the extraction path is gone — this test would be vacuous" not attributed
  4. assert guarded, "the json.loads call is not inside a try block" not attributed
  5. assert "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)

requirementlinked byimplementers exercised
REQ-DDB-WIKI-054catalogunknown