Files
ely-eye/backend/tests/test_visibility_rule.py
T
ZacharyZhang-NY 83dfc14daa Let the server decide whose memory a caller may read
A holder is now somebody who can prove it. Schema v11 stores a sha256 of a
per-holder token beside the directory row, so a copied database cannot replay
it and the plaintext is shown exactly once. The operator token mints and
revokes those tokens; a holder token reads that holder's own memories and
nothing else; naming somebody else without the operator token is refused with
both ids in the message.

The visibility rule was leaking before this: browse had none at all, and the
search rules short-circuited to the whole library whenever no holder was
named. The rule is now written out in five copies with one parameter carried
four times, and a test compares the copies so they cannot drift — semgrep
rejects an interpolated constant, so the copies are not a choice.

Eyes carry three states on purpose. NULL means the whole library, which is
what a process that owns the store is entitled to; a holder id means that
holder; the empty string means nobody, which is what a tokenless HTTP caller
is. Making NULL mean nobody instead would have forced every internal caller —
CLI, proofs, the compiler, the bench harnesses — to name eyes, contradicting
the rule that a local process reads as its own trusted principal.

The capability card answers "can this store answer my kind of question" before
anything is sent. It is derived from what the store holds, so it cannot
promise a modality the store lacks, and it carries no memory text, so it is
safe to read before trusting the store. HTTP, CLI and one MCP tool all serve
it without a token.
2026-10-09 14:15:56 -04:00

47 lines
2.1 KiB
Python

"""The visibility rule is written out in five literal statements; this test keeps them one rule.
The SQL gate refuses a concatenated statement and a view cannot take a parameter, so the clause
is copied rather than composed. A copy that drifts is invisible to every other test — the search
leg, the browse page and the picture route would each keep passing while disagreeing about who
may see a memory.
"""
from __future__ import annotations
import re
from pathlib import Path
SOURCE_DIR = Path(__file__).resolve().parents[1] / "src" / "ely_eye"
FILES = ("browse.py", "store_search.py", "vector_index.py")
CLAUSE = re.compile(r"\(\? IS NULL[^()]*?holder_id = ''.*?WHERE value = \?[^)]*\)\)\)\)", re.DOTALL)
EXPECTED_COPIES = 5
def _normalize(clause: str) -> str:
"""Strip what the copies are allowed to differ in: whitespace, string-literal quotes, table aliases."""
return re.sub(r"\s+", "", clause).replace('"', "").replace("a.", "")
def test_every_copy_of_the_visibility_rule_agrees() -> None:
found: list[tuple[str, str]] = [
(name, _normalize(match.group(0)))
for name in FILES
for match in CLAUSE.finditer((SOURCE_DIR / name).read_text(encoding="utf-8"))
]
assert len(found) == EXPECTED_COPIES, [name for name, _ in found]
distinct = {clause for _, clause in found}
assert len(distinct) == 1, [name for name, clause in found if clause != next(iter(distinct))]
def test_the_rule_still_carries_all_three_states() -> None:
match = CLAUSE.search((SOURCE_DIR / "store_search.py").read_text(encoding="utf-8"))
assert match is not None, "the words leg no longer carries a visibility clause"
clause = _normalize(match.group(0))
assert "?ISNULL" in clause, "None still reads the whole library"
assert "holder_id=''" in clause and "$.scope')='everyone'" in clause, "unowned and public rows stay open to a stranger"
assert "?<>''" in clause, "the empty string must stay nobody rather than matching every holder"
assert "holder_id=?" in clause and "value=?" in clause, "the owner and the visible_to list are still checked"