Files
zeekayandHanzo Dev 8467a88ca0 consent: the answer belongs to the person it is about
The endpoint was self-scoped and fail-closed, but the record it guarded is a
property on the user row, and other writers reach that row.

ONE WRITER. users.Update is a full-row write any org admin may perform on any
member, and its server-owned carry-forward list did not include the consent
record — so one request both FORGED an answer (by sending one) and DESTROYED a
real one (by sending a body with no properties, which is what a partial client
sends), silently and unaudited. It now carries the stored record, and only that
record: every other property still comes from the body, so the console's admin
properties editor keeps working. users.Create dropped nothing, so provisioning
an account could pre-grant training permission in the new member's name; a
create body's consent is now discarded the way a body's credential already was.
The one caller entitled to state an answer at create time is the signup screen,
where the person answers for themselves, and it says so through a seam that is
off the wire. update-preferences shallow-merged any key including this one, an
unvalidated and unaudited second writer of the record that most needs a single
one; it now refuses the key and says where to answer instead.

AN ANSWER YOU DID NOT SEND IS NOT AN ANSWER YOU CHANGED. The wire shape took a
plain bool and a plain string, so a screen saving one switch silently revoked
the other — {"training":"granted"} also said insights=false. Both fields are
pointers now and the record merges field-wise under the row lock, so absent
means untouched. The published description said it merged; now it does.

EVIDENCE. The audit row is what makes a grant demonstrable, and it was
best-effort: written after the fact, dropped on error, and only for a change to
the training answer. It now covers the whole record, commits on the SAME
transaction as the answer, and fails the request if it cannot be written — a
consent we cannot evidence is worth less than one we never claimed. Its action
is reserved, so the generic audit CRUD can no longer mint a grant nobody gave or
delete the row recording a refusal. The ingress address is dropped: behind
hanzoai/ingress it identified our own pod while still being personal data we
would owe a retention answer for.

The write half also refused nothing, so a value the read half normalizes away
could be stored for a later reader to guess at. Encoding an answer now validates
it, in the one place a Consent becomes bytes.

scripts/mutate.py lands the strict runner beside the table it scores: a mutant
counts as killed only if the anchor is unique, the tree builds, the named test
matches and then fails. The table grows 11 -> 20 rows covering the new guards.
20/20 killed. make test green, race-clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-04 23:45:43 -07:00

100 lines
3.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""Run this repo's mutation table (scripts/mutants.py) and score it STRICTLY.
scripts/mutate.py [name-substring-filter]
A mutant is KILLED only if all four hold: the anchor is found EXACTLY once, the
tree still COMPILES with the mutation applied, the named -run pattern matches at
least one test, and that test then FAILS. Anything else — anchor drift, a mutant
that does not build, a -run that matches nothing, a test that still passes — is a
hard FAILURE and is never counted as a kill.
That strictness is the whole point. A lenient runner scores an anchor that no
longer exists as a kill and reports a suite that tests nothing as green, which is
worse than no score at all: it is a false one. The BASELINE pass up front exists
for the same reason — if a named test does not pass and match on the pristine
tree, its "kill" below would be measuring the wrong thing.
Every mutation is reverted in a finally block, including on interrupt. Run it on
a clean tree: `git status --short` should be empty before and after.
"""
import os
import re
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
TABLE = ROOT / "scripts" / "mutants.py"
ONLY = sys.argv[1] if len(sys.argv) > 1 else ""
ns = {}
exec(compile(TABLE.read_text(), str(TABLE), "exec"), ns)
MUTANTS = [m for m in ns["MUTANTS"] if ONLY in m[0]]
if not MUTANTS:
sys.exit(f"no mutant matches {ONLY!r}")
env = dict(os.environ, PATH="/usr/local/go/bin:" + os.environ["PATH"])
def run(cmd):
return subprocess.run(cmd, cwd=ROOT, env=env, capture_output=True, text=True)
def matched(result):
"""How many tests the -run pattern actually reached."""
return len(re.findall(r"^=== RUN", result.stdout, re.M))
print("== baseline ==")
baseline_ok = True
for name, _edits, test, pkg in MUTANTS:
r = run(["go", "test", pkg, "-run", "^(" + test + ")$", "-count=1", "-v"])
ok = r.returncode == 0 and matched(r) > 0
baseline_ok &= ok
print(f" {'ok ' if ok else 'FAIL'} {test:<62} matched={matched(r)}")
if not baseline_ok:
print("BASELINE BROKEN — every score below is meaningless")
print("\n== mutants ==")
killed = failed = 0
for name, edits, test, pkg in MUTANTS:
originals = {}
drift = None
for path, old, new in edits:
p = ROOT / path
src = p.read_text()
originals[p] = src
n = src.count(old)
if n != 1:
drift = f"anchor occurs {n}x in {path}"
break
p.write_text(src.replace(old, new, 1))
try:
if drift:
print(f" FAIL {name}\n ANCHOR DRIFT: {drift}")
failed += 1
continue
b = run(["go", "build", "./..."])
if b.returncode != 0:
head = b.stderr.strip().splitlines()[:2]
print(f" FAIL {name}\n NO-COMPILE: {head}")
failed += 1
continue
r = run(["go", "test", pkg, "-run", "^(" + test + ")$", "-count=1", "-v"])
if matched(r) == 0:
print(f" FAIL {name}\n -run '{test}' MATCHED NOTHING")
failed += 1
elif r.returncode != 0:
print(f" KILL {name} ({test})")
killed += 1
else:
print(f" FAIL {name}\n SURVIVED: {test} still passes")
failed += 1
finally:
for p, src in originals.items():
p.write_text(src)
print(f"\nscore: {killed}/{len(MUTANTS)} killed, {failed} failed")
sys.exit(0 if killed == len(MUTANTS) and baseline_ok else 1)