test_no_run_time_value_can_reach_the_output

Tests › tests/test_hook.py

passed Passed in the packaged run.

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

  1. source = HOOK_SOURCE.read_text(encoding="utf-8")
  2. tree = ast.parse(source)
13 more
  1. payload_fn = next( node for node in ast.walk(tree) if isinstance(node, ast.FunctionDef) and node.name == "_payload_for" )
  2. substitutions = [ node for node in ast.walk(payload_fn) if isinstance(node, ast.BinOp) and isinstance(node.op, ast.Mod) ]
  3. safe_sources = {"int", "_safe_token", "_target_name"}
  4. extractor = next( node for node in ast.walk(tree) if isinstance(node, ast.FunctionDef) and node.name == "_target_name" )
  5. returns = [n for n in ast.walk(extractor) if isinstance(n, ast.Return)]
  6. 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" )
  7. bound: dict[str, set[str]] = {}
  8. 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)
  9. 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, ()))}" )
  10. names = {n.id for n in ast.walk(tree) if isinstance(n, ast.Name)}
  11. 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" )
  12. renderer = next( node for node in ast.walk(tree) if isinstance(node, ast.FunctionDef) and node.name == "_payload_for" )
  13. 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

  1. —
→

Then

  1. ✓ len(substitutions) equals 2
  2. ✓ returns holds
  3. ✓ [n for n in ast.walk(tree) if isinstance(n, ast.JoinedStr)] is false
  4. ✓ [n for n in ast.walk(tree) if isinstance(n, ast.Attribute) and n.attr == "format"] is false
  5. ✓ len(dumps) equals 1
  6. ✓ dumps[0].keywords is false
  7. ✓ source contains "count = int(pressure)"

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

What it checks (11)

  1. 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 attributed
  2. assert returns, "_target_name has no returns — this proof would be vacuous" not attributed
  3. 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" ) not attributed
  4. 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" ) not attributed
  5. 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, ()))}" ) not attributed
  6. assert 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 attributed
  7. assert 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 attributed
  8. 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" ) not attributed
  9. assert len(dumps) == 1, f"_payload_for must serialise exactly once; found {len(dumps)}" not attributed
  10. assert not dumps[0].keywords, ( "json.dumps must take no keywords here; ensure_ascii in particular must stay default" ) not attributed
  11. assert "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)

requirementlinked byimplementers exercised
REQ-DDB-WIKI-054catalogunknown