test_no_run_time_value_can_reach_the_output¶
Tests › tests/test_hook.py
What this test is for
Why it exists
THE WHOLE SECURITY ARGUMENT, AND IT HAS TO BE STRUCTURAL. A behavioural test can only show that the strings it tried were not echoed; it cannot show that no string would be. This reads the source and asserts the payload is a single literal with no interpolation, so there is no mechanism by which run-time data could be echoed at all.
Verifies REQ-DDB-WIKI-054
Given
source = HOOK_SOURCE.read_text(encoding="utf-8")tree = ast.parse(source)
13 more
payload_fn = next( node for node in ast.walk(tree) if isinstance(node, ast.FunctionDef) and node.name == "_payload_for" )substitutions = [ node for node in ast.walk(payload_fn) if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Mod) ]safe_sources = {"int", "_safe_token", "_target_name"}extractor = next( node for node in ast.walk(tree) if isinstance(node, ast.FunctionDef) and node.name == "_target_name" )returns = [n for n in ast.walk(extractor) if isinstance(n, ast.Return)]for node in returns: if node.value is None or ( isinstance(node.value, ast.Constant) and node.value.value is None ): continue assert ( isinstance(node.value, ast.Call) and isinstance(node.value.func, ast.Name) and node.value.func.id == "_safe_token" ), ( "every _target_name return must be None or _safe_token(...); otherwise a value can " "reach the note without passing the sanitiser" )bound: dict[str, set[str]] = {}for node in ast.walk(tree): if isinstance(node, ast.Assign) and isinstance(node.value, ast.Call): func = node.value.func name = func.id if isinstance(func, ast.Name) else None for target in node.targets: if isinstance(target, ast.Name) and name: bound.setdefault(target.id, set()).add(name)for sub in substitutions: operands = sub.right.elts if isinstance(sub.right, ast.Tuple) else [sub.right] for operand in operands: assert isinstance(operand, ast.Name), ( f"a %-application takes a non-name operand ({type(operand).__name__}); only a " f"name bound to int() or _safe_token() may be substituted" ) assert bound.get(operand.id, set()) <= safe_sources and bound.get(operand.id), ( f"{operand.id!r} is substituted into the note but is not bound from " f"{sorted(safe_sources)} — it is bound from {sorted(bound.get(operand.id, ()))}" )names = {n.id for n in ast.walk(tree) if isinstance(n, ast.Name)}for retired in ("_HEAD", "_TAIL"): assert retired not in names, ( f"{retired} is referenced again: the payload must be serialised by json.dumps over the " f"whole object, never assembled from string fragments" )renderer = next( node for node in ast.walk(tree) if isinstance(node, ast.FunctionDef) and node.name == "_payload_for" )dumps = [ node for node in ast.walk(renderer) if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute) and node.func.attr == "dumps" ]
When
- —
Then
- ✓ len(substitutions) equals 2
- ✓ returns holds
- ✓ [n for n in ast.walk(tree) if isinstance(n, ast.JoinedStr)] is false
- ✓ [n for n in ast.walk(tree) if isinstance(n, ast.Attribute) and n.attr == "format"] is false
- ✓ len(dumps) equals 1
- ✓ dumps[0].keywords is false
- ✓ source contains "count = int(pressure)"
tests/test_hook.py:104 · its file's page
What it checks (11)
assert len(substitutions) == 2, ( f"expected exactly 2 %-applications in the payload path; found {len(substitutions)}. A new " f"one means a value is being interpolated somewhere this test has not vetted." )not attributedassert returns, "_target_name has no returns — this proof would be vacuous"not attributedassert ( isinstance(node.value, ast.Call) and isinstance(node.value.func, ast.Name) and node.value.func.id == "_safe_token" ), ( "every _target_name return must be None or _safe_token(...); otherwise a value can " "reach the note without passing the sanitiser" )not attributedassert isinstance(operand, ast.Name), ( f"a %-application takes a non-name operand ({type(operand).__name__}); only a " f"name bound to int() or _safe_token() may be substituted" )not attributedassert bound.get(operand.id, set()) <= safe_sources and bound.get(operand.id), ( f"{operand.id!r} is substituted into the note but is not bound from " f"{sorted(safe_sources)} — it is bound from {sorted(bound.get(operand.id, ()))}" )not attributedassert not [n for n in ast.walk(tree) if isinstance(n, ast.JoinedStr)], ( "an f-string in the hook could interpolate run-time data outside the vetted path" )not attributedassert not [n for n in ast.walk(tree) if isinstance(n, ast.Attribute) and n.attr == "format"], ( "`.format()` accepts arbitrary objects, including strings off stdin" )not attributedassert retired not in names, ( f"{retired} is referenced again: the payload must be serialised by json.dumps over the " f"whole object, never assembled from string fragments" )not attributedassert len(dumps) == 1, f"_payload_for must serialise exactly once; found {len(dumps)}"not attributedassert not dumps[0].keywords, ( "json.dumps must take no keywords here; ensure_ascii in particular must stay default" )not attributedassert "count = int(pressure)" in source, ( "the substituted count must be coerced with int(); without it a corrupted tally could " "carry text into the payload" )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 |