diff --git a/docs/requirements/019-reveal-withheld-diff.md b/docs/requirements/019-reveal-withheld-diff.md new file mode 100644 index 0000000..0494510 --- /dev/null +++ b/docs/requirements/019-reveal-withheld-diff.md @@ -0,0 +1,110 @@ +# 019 - Reveal withheld diff content + +## User Story + +As the operator, I want a diff file that hits a secret rule to show with only +the hits masked, and the hits shown after a passkey approval per file or per +item, so that I can review every change while no secret reaches the hub page +unasked. + +## Decisions (operator-confirmed 2026-11-17; do not re-litigate) + +1. **Mask, not withhold**: the client masks only each match of a MEDUSA + secret rule with `••••`, and every PEM armour block from its BEGIN to its + END line; the rest of the patch shows. A file for which no secret rule + loads stays withheld whole (fail closed), but revealable. Rejected: + withholding the whole file by default (hid `tests/test_hub_view.py` over + `tokens = {...}`). +2. **Passkey approval, two scopes**: an eye button per masked file reveals + that file; one button per item reveals every masked file of the review or + ask. The passkey signs `reveal`, kind, id and the paths. Rejected: a plain + confirm click (anyone with page access could reveal). +3. **Sealed store**: the client sends the full patches of the masked files + with the request, kept in a sealed file per item + (`.-sealed.json`, mode 0600), never in the snapshot, the stream + or `/api/diffs`; the hub deletes it once the item is decided or expires. + Rejected: keeping it with the review (the secret stays on disk longer); + the hub reading git (the hub runs no git by design). +4. **Both kinds**: review diffs (`approval.py`, `tree_diff`) and ask diffs + (`permission_hook.py`, `bound_diff`). +5. **Audit**: every reveal logs an approval-stream event with kind, id and + paths, never the content. +6. **Page memory only**: revealed text stays in page memory until the item + leaves Waits on Operator; never in `localStorage` or the view store. +7. **Timing**: implemented with the other open requirements before the one + final suite plus all e2e on master (operator order 2026-11-17); per + bundle only the MEDUSA scan runs. Rejected: waiting for the 018 commit + (it waits for that same final run). + +## Approach + +```mermaid +flowchart TD + subgraph P["🧭 0 · Preparation"] + P1["📋 Write requirement"] --> P2{"✅ Operator approves?"} + P2 -- "✏️ corrections" --> P1 + end + P2 -- "🤖 yes, after 018" --> A1 + subgraph A["🔐 A · Client masking and sealing"] + A1["🎭 Mask hit spans and armour blocks, seal full patches"] --> A2{"🧪 Unit green?"} + A2 -- "🔴 no: fix" --> A1 + end + A2 -- "🟢 yes" --> B1 + subgraph B["🗄️ B · Hub sealed store and reveal"] + B1["🔏 Sealed file, signed reveal per file or item, delete on decision"] --> B2{"🧪 Unit green?"} + B2 -- "🔴 no: fix" --> B1 + end + B2 -- "🟢 yes" --> C1 + subgraph C["👁️ C · Page"] + C1["🖱️ Eye per file, reveal all per item"] --> C2{"🧪 E2E green?"} + C2 -- "🔴 no: fix" --> C1 + C2 -- "🟢 yes" --> CF{"💬 Operator feedback on screenshots?"} + CF -- "✏️ changes" --> C1 + end + CF -- "👍 fits" --> D1["🏁 Review, commit with operator OK, hand-over"] +``` + +## Acceptance Criteria + +### Client + +- [ ] On a file holding `tokens = {"chart": "tokens"}` and an unrelated line, + `tree_diff` and `bound_diff` give a patch where only the match reads + `••••`, note `masked`, and the unrelated line is unchanged. +- [ ] A PEM block masks every line from BEGIN to END; without loadable rules + every text file keeps the note `content withheld` and an empty patch. +- [ ] The request carries the full patches of the masked or withheld files + apart from `diff`; `diff` itself never holds a masked span in clear. + +### Hub + +- [ ] The sealed file exists with mode 0600 next to the item, is absent from + `/api/diffs//`, the snapshot and the stream, and is deleted + after the grant, the denial or the expiry. +- [ ] `POST /api/diffs///reveal` answers the patches only for a + pinned passkey's unexpired, unreplayed signature over `reveal`, kind, + id and exactly the requested paths; a wrong path, a replay, a missing + proof or a decided item answers 4xx without content. +- [ ] Every reveal adds one approval event with kind, id and paths and no + patch text. + +### Page + +- [ ] A masked file shows its patch with the `••••` spans and an eye button; + an item with 2 or more masked files shows one reveal button; after the + passkey the clear patch replaces the masked one. +- [ ] Revealed text is never written to `localStorage` or the view store and + is gone once the item leaves Waits on Operator. + +### Tests + +- [ ] Unit: span masking (rule match, armour block, no rules), sealing apart + from `diff`, sealed file mode and deletion per decision path, reveal + signature (per file, per item, wrong path, replay, decided item), audit + event without content. +- [ ] Integration: `approval.py` and the permission hook send masked `diff` + plus the sealed patches to a test hub; a signed reveal returns the + clear patch. +- [ ] E2E: Chromium and Firefox at 1920, 1366 and 390: the masked file with + its eye button, the per-item reveal, the clear patch after the virtual + passkey, no CSP violation; screenshots for the checkpoint. diff --git a/docs/requirements/README.md b/docs/requirements/README.md index 6ad7575..8a2f051 100644 --- a/docs/requirements/README.md +++ b/docs/requirements/README.md @@ -22,4 +22,5 @@ skill defines the format. - [016 - Agents Controls in the View Row](016-agents-controls-row.md) - [017 - Legend Toggle](017-legend-toggle.md) - [018 - Gibberlink](018-gibberlink.md) +- [019 - Reveal withheld diff content](019-reveal-withheld-diff.md) - [021 - TokenEconomizer by default](021-tokeneconomizer-by-default.md) diff --git a/orchestrator/client/approval.py b/orchestrator/client/approval.py index 96dea40..cb7b722 100755 --- a/orchestrator/client/approval.py +++ b/orchestrator/client/approval.py @@ -15,9 +15,10 @@ with the repository, worktree, branch and tree as git prints them here, the notes file's text (refused past NOTES_LIMIT characters), and the diff of the tree git write-tree prints against HEAD - (staged_diff.tree_diff, content withheld per the MEDUSA rules under - , by default those of the pipx install); without the diff when - the body would pass DIFF_BODY_LIMIT. + (staged_diff.tree_diff, secrets masked per the MEDUSA rules under + , by default those of the pipx install) with the sealed clear + patches of its masked files apart; without either when the body + would pass DIFF_BODY_LIMIT. Returns: pin: exit code 0 and the pinned credential on stdout; 1 and the reason @@ -89,14 +90,14 @@ def request_review(args: argparse.Namespace) -> str: item = staged(cwd, "0" * 16) try: notes = args.notes.read_text(encoding="utf-8") - diff = staged_diff.tree_diff(cwd, item["tree"], args.rules or staged_diff.rules_root()) + diff, sealed = staged_diff.tree_diff(cwd, item["tree"], args.rules or staged_diff.rules_root()) except (OSError, ValueError, staged_diff.DiffError) as error: raise Refused(f"the review cannot be prepared: {error}") from error if len(notes) > NOTES_LIMIT: raise Refused(f"the notes hold {len(notes)} characters, the hub takes at most {NOTES_LIMIT}") arguments = {key: item[key] for key in ("repo", "worktree", "branch", "tree")} arguments.update(title=args.title, page=str(args.page), notes=notes, gate=args.gate, score=args.score, agent=args.agent) - body = review_body({**arguments, "diff": diff}) + body = review_body({**arguments, "diff": diff, "sealed": sealed}) if len(body) > DIFF_BODY_LIMIT: print(f"approval: the review goes without its diff, its {len(body)} bytes pass the hub's {DIFF_BODY_LIMIT}", file=sys.stderr) body = review_body(arguments) diff --git a/orchestrator/client/hub_limits.py b/orchestrator/client/hub_limits.py index 215eba0..0d864bc 100644 --- a/orchestrator/client/hub_limits.py +++ b/orchestrator/client/hub_limits.py @@ -9,12 +9,21 @@ NOTES_LIMIT: characters of review notes the hub takes; the client refuses longer notes instead of cutting them. WITHHELD: the note of a diff entry that ships without its patch because - its content may hold a secret. + no secret rule loads to mask it. + MASKED: the note of a diff entry whose patch reads MASK for every secret + rule match and every armour block line. + MASK: what stands for a masked span in a patch. + SEALED_LIMIT: bytes of JSON-encoded clear patch text of the masked and + withheld files one request carries apart from its diff, for a + signed reveal. """ DIFF_LIMIT = 2 << 20 DIFF_BODY_LIMIT = 4 << 20 +SEALED_LIMIT = 1 << 20 FILE_CAP = 5000 PATH_LIMIT = 4096 NOTES_LIMIT = 100_000 WITHHELD = "content withheld" +MASKED = "masked" +MASK = "••••" diff --git a/orchestrator/client/permission_hook.py b/orchestrator/client/permission_hook.py index 4270ed0..3677c4b 100755 --- a/orchestrator/client/permission_hook.py +++ b/orchestrator/client/permission_hook.py @@ -18,9 +18,10 @@ Returns: takes or a hub that is down, so Claude Code shows its own dialog. The exit code is 0. An ask for a Bash git commit of exactly the staged index (staged_diff.commit_of) carries the diff staged_diff.py - builds of the tree its directory stages, content withheld per the - MEDUSA rules of the pipx install; none when it cannot be built or - the body would pass DIFF_BODY_LIMIT. An allow of such an ask turns + builds of the tree its directory stages, secrets masked per the + MEDUSA rules of the pipx install, and the sealed clear patches of + its masked files apart; neither when it cannot be built or the body + would pass DIFF_BODY_LIMIT. An allow of such an ask turns into a deny with CHANGED when what the commit records (staged_diff.binding) differs right before answering from the preview's, and with UNBOUND when either cannot be read. Any other @@ -88,8 +89,9 @@ def unapprovable(ask: dict) -> bool: def commit_diff(ask: dict, rules: Path | None) -> tuple[str | None, dict]: """Returns: For a commit ask, what the commit records (staged_diff.binding, None - when it cannot be read) and {"diff": the preview of exactly that}, - {} when it cannot be built; None and {} for any other ask. + when it cannot be read) and {"diff": the preview of exactly that, + "sealed": the clear patches of its masked files}, {} when it cannot + be built; None and {} for any other ask. """ target = commit_target(ask) if target is None: @@ -100,7 +102,8 @@ def commit_diff(ask: dict, rules: Path | None) -> tuple[str | None, dict]: print(f"kvb-orchestrator: the ask goes without its diff and an allow of it turns into a deny: {error}", file=sys.stderr) return None, {} try: - return pinned, {"diff": staged_diff.bound_diff(target, pinned, rules)} + diff, sealed = staged_diff.bound_diff(target, pinned, rules) + return pinned, {"diff": diff, "sealed": sealed} except FAILURES as error: print(f"kvb-orchestrator: the ask goes without its diff: {error}", file=sys.stderr) return pinned, {} diff --git a/orchestrator/client/staged_diff.py b/orchestrator/client/staged_diff.py index 36930d6..5b5a019 100644 --- a/orchestrator/client/staged_diff.py +++ b/orchestrator/client/staged_diff.py @@ -6,19 +6,24 @@ pinned: for bound_diff, what binding returned for the same cwd. Returns: - review_diff, tree_diff, bound_diff: the review diff of requirement 003 - of the staged tree against HEAD (git diff --cached), one {path, - status, mode, additions, deletions, patch, note} per changed path, - sorted by path, status A, M, D or B (binary); path is the file name - with bytes that are no UTF-8 written as backslash escapes; mode the - git file mode, "old => new" when it changed. patch holds the unified - hunks; it is empty for a binary file, a gitlink, a side over - BLOB_LIMIT bytes (counted 0/0), a path sandbox_paths denies (its - FLOOR and the settings' deny lists) and every file past DIFF_LIMIT - bytes of JSON-encoded patch text. A file whose old or new content - holds an ARMOUR line or matches a MEDUSA secret rule for its name, - and every file when no secret rule pattern loads, ships no patch, - only its counts and the note WITHHELD. The diff fails when more + review_diff, tree_diff, bound_diff: (diff, sealed). diff is the review + diff of requirement 003 of the staged tree against HEAD (git diff + --cached), one {path, status, mode, additions, deletions, patch, + note} per changed path, sorted by path, status A, M, D or B + (binary); path is the file name with bytes that are no UTF-8 + written as backslash escapes; mode the git file mode, "old => new" + when it changed. patch holds the unified hunks; it is empty for a + binary file, a gitlink, a side over BLOB_LIMIT bytes (counted 0/0), + a path sandbox_paths denies (its FLOOR and the settings' deny lists) + and every file past DIFF_LIMIT bytes of JSON-encoded patch text. In + a shown line every match of a MEDUSA secret rule for the file's name + and every line from an ARMOUR line through its ARMOUR_END line reads + MASK, and the file carries the note MASKED; when no secret rule + pattern loads, every text file ships no patch, only its counts and + the note WITHHELD. sealed maps the path of each MASKED or WITHHELD + file to its clear patch, while their JSON-encoded text stays within + SEALED_LIMIT bytes; diff never holds a masked span in clear. The + diff fails when more than FILE_CAP paths changed or one is longer than PATH_LIMIT, a settings file sandbox_paths reads is unreadable, the working tree lies under a denied path, HEAD exists but its tree cannot be listed, @@ -46,9 +51,11 @@ is read. from __future__ import annotations +import bisect import difflib import fnmatch import hashlib +import itertools import json import os import re @@ -63,6 +70,9 @@ DIFF_LIMIT = hub_limits.DIFF_LIMIT FILE_CAP = hub_limits.FILE_CAP PATH_LIMIT = hub_limits.PATH_LIMIT WITHHELD = hub_limits.WITHHELD +MASKED = hub_limits.MASKED +MASK = hub_limits.MASK +SEALED_LIMIT = hub_limits.SEALED_LIMIT BLOB_LIMIT = 2 << 20 BINARY_PROBE = 8000 CONTEXT = 3 @@ -100,8 +110,11 @@ SHORT_ARGS = set("mFcCtuSU") KEPT_VARS = {"GIT_EDITOR", "GIT_SEQUENCE_EDITOR", "GIT_PAGER", "GIT_TERMINAL_PROMPT", "GIT_AUTHOR_NAME", "GIT_AUTHOR_EMAIL", "GIT_AUTHOR_DATE", "GIT_COMMITTER_NAME", "GIT_COMMITTER_EMAIL", "GIT_COMMITTER_DATE"} ASSIGNMENT = re.compile(r"([A-Za-z_][A-Za-z0-9_]*)=.*") ARMOUR = re.compile(r"-----BEGIN [A-Z0-9 ]+-----") +ARMOUR_END = re.compile(r"-----END [A-Z0-9 ]+-----") +HUNK = re.compile(r"@@ -([0-9]+)(?:,[0-9]+)? \+([0-9]+)(?:,[0-9]+)? @@") Patterns = list[re.Pattern] Rules = list[tuple[tuple[str, ...], Patterns]] +Spans = list[list[tuple[int, int]]] class DiffError(Exception): @@ -200,7 +213,7 @@ def binding(cwd: Path) -> str: return hashlib.sha256(staged(toplevel(cwd))).hexdigest() -def bound_diff(cwd: Path, pinned: str, rules: Path | None) -> list[dict]: +def bound_diff(cwd: Path, pinned: str, rules: Path | None) -> tuple[list[dict], dict[str, str]]: index = staged(toplevel(cwd)) if hashlib.sha256(index).hexdigest() != pinned: raise DiffError("the staged tree changed while its preview was read") @@ -325,32 +338,114 @@ def rule_patterns(path: str, rules: Rules) -> Patterns: return [pattern for globs, found in rules if any(fnmatch.fnmatchcase(name, glob.lower()) for glob in globs) for pattern in found] -def withheld(path: str, texts: tuple[str, str], rules: Rules | None) -> bool: - if not rules or not any(patterns for _, patterns in rules): - return True - patterns = [ARMOUR, *rule_patterns(path, rules)] - return any(pattern.search(text) for text in texts for pattern in patterns) +def any_pattern(rules: Rules | None) -> bool: + return bool(rules) and any(patterns for _, patterns in rules) -def entry(head: dict, before: bytes | None, after: bytes | None, rules: Rules | None) -> tuple[dict, list[str]]: +def armour_lines(lines: list[str]) -> set[int]: + """Returns: + The numbers (from 0) of every line from an ARMOUR line through its + ARMOUR_END line, through the last line when no end follows. + """ + found, inside = set(), False + for number, line in enumerate(lines): + inside = inside or ARMOUR.search(line) is not None + if inside: + found.add(number) + inside = ARMOUR_END.search(line) is None + return found + + +def hit_spans(text: str, patterns: Patterns) -> Spans: + """Returns: + Per line of text (text_lines), the [start, end) columns that a match + of a pattern covers, the whole line inside an armour block. + """ + lines = text_lines(text) + spans: Spans = [[] for _ in lines] + starts = list(itertools.accumulate((len(line) + 1 for line in lines), initial=0)) + for number in armour_lines(lines): + spans[number].append((0, len(lines[number]))) + for pattern in patterns: + for match in pattern.finditer(text): + for number in range(bisect.bisect_right(starts, match.start()) - 1, len(lines)): + if starts[number] >= match.end(): + break + begin, end = max(match.start(), starts[number]) - starts[number], min(match.end(), starts[number] + len(lines[number])) - starts[number] + if end > begin: + spans[number].append((begin, end)) + return spans + + +def merged(spans: list[tuple[int, int]]) -> list[tuple[int, int]]: + found: list[tuple[int, int]] = [] + for begin, end in sorted(spans): + if found and begin <= found[-1][1]: + found[-1] = (found[-1][0], max(found[-1][1], end)) + else: + found.append((begin, end)) + return found + + +def masked(line: str, spans: list[tuple[int, int]]) -> str: + parts, at = [], 0 + for begin, end in merged(spans): + parts += [line[at:begin], MASK] + at = end + return "".join([*parts, line[at:]]) + + +def masked_patch(lines: list[str], old: Spans, new: Spans) -> list[str]: + """Args: + lines: unified diff lines from the first hunk header on. + old, new: hit_spans() of the old and the new text. + + Returns: + The lines with every span of their old or new line masked; a + context line takes the spans of both sides. + """ + found, before, after = [], 0, 0 + for line in lines: + hunk = HUNK.match(line) + if hunk: + before, after = int(hunk.group(1)) - 1, int(hunk.group(2)) - 1 + found.append(line) + continue + sign, spans = line[:1], [] + if sign in " -": + spans += old[before] + before += 1 + if sign in " +": + spans += new[after] + after += 1 + found.append(sign + masked(line[1:], spans) if spans else line) + return found + + +def entry(head: dict, before: bytes | None, after: bytes | None, rules: Rules | None) -> tuple[dict, list[str], list[str] | None]: """Args: head: the entry's path, status and mode. Returns: - The diff entry without its patch, and the patch lines; no lines for - a binary side, one over BLOB_LIMIT or content withheld. + The diff entry without its patch, the patch lines to show, and the + clear lines when they differ from those shown (masked or withheld), + else None; no lines for a binary side or one over BLOB_LIMIT. """ found = {**head, "additions": 0, "deletions": 0, "note": ""} if before is None or after is None: - return found, [] + return found, [], None if b"\0" in before[:BINARY_PROBE] or b"\0" in after[:BINARY_PROBE]: - return {**found, "status": "B"}, [] + return {**found, "status": "B"}, [], None texts = (before.decode("utf-8", "replace"), after.decode("utf-8", "replace")) lines = list(difflib.unified_diff(*map(text_lines, texts), lineterm="", n=CONTEXT))[2:] found.update(additions=sum(1 for line in lines if line.startswith("+")), deletions=sum(1 for line in lines if line.startswith("-"))) - if withheld(head["path"], texts, rules): - return {**found, "note": WITHHELD}, [] - return found, lines + if not any_pattern(rules): + return {**found, "note": WITHHELD}, [], lines or None + patterns = rule_patterns(head["path"], rules) + shown = masked_patch(lines, *(hit_spans(text, patterns) for text in texts)) + if shown == lines: + return found, lines, None + return {**found, "note": MASKED}, shown, lines def content_of(side: tuple[str, str] | None, gitlink: bool, contents: dict[str, bytes | None]) -> bytes | None: @@ -359,11 +454,11 @@ def content_of(side: tuple[str, str] | None, gitlink: bool, contents: dict[str, return None if gitlink else contents.get(side[1]) -def review_diff(cwd: Path, rules: Path | None) -> list[dict]: +def review_diff(cwd: Path, rules: Path | None) -> tuple[list[dict], dict[str, str]]: return built(cwd, sides(cwd, None, None), rules) -def tree_diff(cwd: Path, tree: str, rules: Path | None) -> list[dict]: +def tree_diff(cwd: Path, tree: str, rules: Path | None) -> tuple[list[dict], dict[str, str]]: return built(cwd, sides(cwd, tree, None), rules) @@ -380,7 +475,7 @@ def shown_path(path: str) -> str: return shown -def path_entry(path: str, before: tuple[str, str] | None, after: tuple[str, str] | None, contents: dict[str, bytes | None], rules: Rules | None) -> tuple[dict, list[str]]: +def path_entry(path: str, before: tuple[str, str] | None, after: tuple[str, str] | None, contents: dict[str, bytes | None], rules: Rules | None) -> tuple[dict, list[str], list[str] | None]: status = "A" if before is None else "D" if after is None else "M" gitlink = any(side is not None and side[0] == GITLINK for side in (before, after)) modes = list(dict.fromkeys(side[0] for side in (before, after) if side is not None)) @@ -388,7 +483,11 @@ def path_entry(path: str, before: tuple[str, str] | None, after: tuple[str, str] return entry(head, content_of(before, gitlink, contents), content_of(after, gitlink, contents), rules) -def built(cwd: Path, found_sides: tuple[dict, dict, dict[str, bytes | None]], rules: Path | None) -> list[dict]: +def encoded_size(text: str) -> int: + return len(json.dumps(text, ensure_ascii=False).encode("utf-8")) + + +def built(cwd: Path, found_sides: tuple[dict, dict, dict[str, bytes | None]], rules: Path | None) -> tuple[list[dict], dict[str, str]]: old, new, contents = found_sides changed = sorted(path for path in {*old, *new} if old.get(path) != new.get(path)) if len(changed) > FILE_CAP: @@ -397,18 +496,21 @@ def built(cwd: Path, found_sides: tuple[dict, dict, dict[str, bytes | None]], ru shown_path(path) fetched(cwd, old, new, changed, contents) secrets = secret_rules(rules) - diff, spent, full = [], 0, False + diff, sealed, spent, kept, full = [], {}, 0, 0, False for path in changed: - found, lines = path_entry(path, old.get(path), new.get(path), contents, secrets) + found, lines, clear = path_entry(path, old.get(path), new.get(path), contents, secrets) patch = "\n".join(lines) - size = len(json.dumps(patch, ensure_ascii=False).encode("utf-8")) + size = encoded_size(patch) full = full or spent + size > DIFF_LIMIT if full: patch = "" else: spent += size diff.append({**found, "patch": patch}) - return diff + if clear is not None and kept + encoded_size("\n".join(clear)) <= SEALED_LIMIT: + sealed[found["path"]] = "\n".join(clear) + kept += encoded_size(sealed[found["path"]]) + return diff, sealed def names_commit(command: str) -> bool: diff --git a/orchestrator/kvb_orchestrator/asking.py b/orchestrator/kvb_orchestrator/asking.py index 6bed78d..7a2acd2 100644 --- a/orchestrator/kvb_orchestrator/asking.py +++ b/orchestrator/kvb_orchestrator/asking.py @@ -3,7 +3,7 @@ from __future__ import annotations import time from .choosing import EVERYONE, answer_text -from .diffs import store_diff, valid_diff +from .diffs import sealed_file, store_diff, store_sealed, valid_diff, valid_sealed from .errors import Conflict, Missing, ToolError from .fields import ( COMMAND_LIMIT, @@ -65,16 +65,20 @@ class Asking: def ask(self, body: bytes) -> dict: payload = decode(body) diff = valid_diff(payload.pop("diff")) if "diff" in payload else None + sealed = valid_sealed(payload.pop("sealed"), diff) if "sealed" in payload else {} expect(payload, ("tool", *ASK_TEXTS, "timeout")) values = {name: bounded(payload, name, limit) for name, limit in ASK_TEXTS.items()} visible(values) lifetime = seconds(payload, "timeout", ASK_LIFETIME) created = time.time() fields = {**values, "tool": field(payload, "tool").strip(), "status": "pending", "decided": None, "proof": None, "diff_files": None if diff is None else len(diff)} - ask_id = self.asks.fresh() - if diff is not None: - store_diff(self.reviews.pages, "ask", ask_id, diff) - record = self.asks.insert(ask_id, {**fields, "created_ts": created, "expires": created + lifetime}) + with self.asks.changed: + ask_id = self.asks.fresh() + if diff is not None: + store_diff(self.reviews.pages, "ask", ask_id, diff) + if sealed: + store_sealed(self.reviews.pages, "ask", ask_id, sealed) + record = self.asks.insert(ask_id, {**fields, "created_ts": created, "expires": created + lifetime}) self.automate("ask", record) return {"id": record["id"], "status": record["status"]} @@ -85,7 +89,10 @@ class Asking: raise Missing(ask_id) left = max(record["expires"] - time.time(), 0.0) record = self.asks.wait(ask_id, min(wait or 0.0, WAIT_CAP, left)) - return ask_state(record, time.time()) + state = ask_state(record, time.time()) + if state["status"] == "expired": + sealed_file(self.reviews.pages, "ask", ask_id).unlink(missing_ok=True) + return state def settle(self, kind_name: str, item_id: str, verb: str, body: bytes) -> dict: if kind_name == "asks" and verb in ANSWERS: diff --git a/orchestrator/kvb_orchestrator/diffs.py b/orchestrator/kvb_orchestrator/diffs.py index b801ae8..c22e95b 100644 --- a/orchestrator/kvb_orchestrator/diffs.py +++ b/orchestrator/kvb_orchestrator/diffs.py @@ -10,13 +10,16 @@ from .signed import limits DIFF_LIMIT = limits.DIFF_LIMIT DIFF_BODY_LIMIT = limits.DIFF_BODY_LIMIT +SEALED_LIMIT = limits.SEALED_LIMIT FILE_CAP = limits.FILE_CAP PATH_LIMIT = limits.PATH_LIMIT STATUSES = ("A", "M", "D", "R", "B") DIFF_KEYS = ("path", "status", "mode", "additions", "deletions", "patch", "note") -NOTES = ("", limits.WITHHELD) +HIDDEN = (limits.WITHHELD, limits.MASKED) +NOTES = ("", *HIDDEN) MODE = re.compile(r"[0-7]{6}(?: => [0-7]{6})?") DIFF_SUFFIX = {"review": ".review-diff.json", "ask": ".ask-diff.json"} +SEALED_SUFFIX = {"review": ".review-sealed.json", "ask": ".ask-sealed.json"} def counted(value: object) -> bool: @@ -33,8 +36,8 @@ def diff_entry(entry: object) -> dict: raise ToolError("mode must be a git file mode, or old => new for a changed one") if not counted(entry["additions"]) or not counted(entry["deletions"]) or not isinstance(patch, str) or (entry["status"] == "B" and patch): raise ToolError("additions and deletions must be counts, patch text, empty for a binary file") - if entry["note"] not in NOTES or (entry["note"] and patch): - raise ToolError(f"note must be empty or {limits.WITHHELD!r}, the latter with an empty patch") + if entry["note"] not in NOTES or (entry["note"] == limits.WITHHELD and patch): + raise ToolError(f"note must be empty, {limits.MASKED!r} or {limits.WITHHELD!r}, the latter with an empty patch") return {key: entry[key] for key in DIFF_KEYS} @@ -52,10 +55,44 @@ def valid_diff(value: object) -> list[dict]: return entries +def valid_sealed(value: object, diff: list[dict] | None) -> dict[str, str]: + """Returns: + The clear patches a request seals apart from its diff, checked: an + object from the path of a masked or withheld file of diff to its + patch text, at most SEALED_LIMIT bytes of JSON-encoded text in all; + none without a diff. + """ + hidden = {entry["path"] for entry in diff or [] if entry["note"] in HIDDEN} + if not isinstance(value, dict) or not all(isinstance(patch, str) for patch in value.values()) or not set(value) <= hidden: + raise ToolError("sealed must map the path of a masked or withheld file of the diff to its patch text") + if sum(len(json.dumps(patch, ensure_ascii=False).encode("utf-8")) for patch in value.values()) > SEALED_LIMIT: + raise ToolError(f"the sealed patches must stay within {SEALED_LIMIT} bytes") + return dict(value) + + def diff_bytes(diff: list[dict]) -> bytes: return json.dumps({"diff": diff}, ensure_ascii=False).encode("utf-8") +def sealed_bytes(sealed: dict[str, str]) -> bytes: + return json.dumps({"sealed": sealed}, ensure_ascii=False).encode("utf-8") + + +def sealed_file(folder: Path, kind_name: str, item_id: str) -> Path: + return folder / f"{item_id}{SEALED_SUFFIX[kind_name]}" + + +def store_sealed(folder: Path, kind_name: str, item_id: str, sealed: dict[str, str]) -> None: + write_atomic(sealed_file(folder, kind_name, item_id), sealed_bytes(sealed)) + + +def stored_sealed(folder: Path, kind_name: str, item_id: str) -> dict[str, str]: + data = read_json(sealed_file(folder, kind_name, item_id)) + if not isinstance(data, dict) or not isinstance(data.get("sealed"), dict): + raise Missing(item_id) + return data["sealed"] + + def store_diff(folder: Path, kind_name: str, item_id: str, diff: list[dict]) -> None: write_atomic(folder / f"{item_id}{DIFF_SUFFIX[kind_name]}", diff_bytes(diff)) diff --git a/orchestrator/kvb_orchestrator/handler.py b/orchestrator/kvb_orchestrator/handler.py index 5f6e2bc..77cd8b2 100644 --- a/orchestrator/kvb_orchestrator/handler.py +++ b/orchestrator/kvb_orchestrator/handler.py @@ -67,6 +67,7 @@ SETTINGS_SIGN_ROUTE = "/api/settings/sign" CHOICES_ROUTE = "/api/choices" CHOICES_SIGN_ROUTE = "/api/choices/sign" DIFF_ROUTE = re.compile(r"/api/diffs/(review|ask)/([0-9a-f]{16})") +REVEAL_ROUTE = re.compile(r"/api/diffs/(review|ask)/([0-9a-f]{16})/reveal(/sign)?") FIREWALL_ROUTE = "/api/firewall" FIREWALL_POSTS = {"/api/firewall/preview": "settings_preview", "/api/firewall/settings": "change_settings", "/api/firewall/groups": "add_group", "/api/firewall/sets": "add_set"} FIREWALL_STATE = re.compile(r"/api/firewall/settings/([0-9a-f]{16})") @@ -175,6 +176,10 @@ class Handler(Exchange): return lambda body: self.answer(lambda: hub.sign_board(body), HTTPStatus.OK) if board: return lambda body: self.answer(lambda: hub.board(board.group(1), body), HTTPStatus.OK) + reveal = REVEAL_ROUTE.fullmatch(path) + if reveal: + method = hub.sign_reveal if reveal.group(3) else hub.reveal + return lambda body: self.answer(lambda: method(reveal.group(1), reveal.group(2), body), HTTPStatus.OK) return self.inbox_action(path) def inbox_action(self, path: str) -> Callable[[bytes], None] | None: @@ -252,7 +257,7 @@ class Handler(Exchange): return None def posted(self, path: str) -> bool: - return any(pattern.fullmatch(path) for pattern in (INBOX_ROUTE, UNDO_ROUTE, RULE_ROUTE, REQUEST_ROUTE, RUNNER_ROUTE, FIREWALL_DROP)) + return any(pattern.fullmatch(path) for pattern in (INBOX_ROUTE, UNDO_ROUTE, RULE_ROUTE, REQUEST_ROUTE, RUNNER_ROUTE, FIREWALL_DROP, REVEAL_ROUTE)) def runner(self, path: str, body: bytes) -> None: hub = self.server.hub diff --git a/orchestrator/kvb_orchestrator/hub.py b/orchestrator/kvb_orchestrator/hub.py index cf5a476..093f309 100644 --- a/orchestrator/kvb_orchestrator/hub.py +++ b/orchestrator/kvb_orchestrator/hub.py @@ -17,6 +17,7 @@ from .choosing import Choosing from .cleanup import Cleaning, Spool from .crons import History, queue_live from .denying import Denying +from .diffs import SEALED_SUFFIX from .engines import Engines, Speech from .events import Log, Watch from .firewall import Firewall @@ -25,7 +26,7 @@ from .icons import sheet from .inbox import Inbox from .jobs import Jobs from .language import Language, Model -from .ledger import Ledger, Marks, Reviews +from .ledger import Ledger, Marks, Reviews, Sealed from .matching import Matching from .mcp import Mcp from .messaging import Messaging @@ -45,6 +46,7 @@ from .receipts import ( from .redirects import Redirects from .requesting import Requesting from .resources import RESOURCES, SAMPLES +from .revealing import Revealing from .reviewing import Reviewing from .rules import Offers, Rules from .settings import Settings @@ -84,7 +86,7 @@ def environment(templates: Path) -> jinja2.Environment: @dataclass(frozen=True) -class Hub(Mcp, Reviewing, Asking, Messaging, Choosing, Approving, Denying, Requesting, Cleaning, Offices, Tasking, Gating, Firewall, Inbox, Matching, Rules, Jobs, Sources, Timeline, View, Language): +class Hub(Mcp, Reviewing, Revealing, Asking, Messaging, Choosing, Approving, Denying, Requesting, Cleaning, Offices, Tasking, Gating, Firewall, Inbox, Matching, Rules, Jobs, Sources, Timeline, View, Language): council: Path pages: Path public_port: int @@ -202,7 +204,7 @@ def open_hub(council: Path, pages: Path, public_port: int, state: Path, host_sta pages=pages, public_port=public_port, reviews=reviews, - asks=Ledger(state / "asks.json"), + asks=Sealed(state / "asks.json", reviews.pages, SEALED_SUFFIX["ask"]), decisions=Ledger(state / "decisions.json"), messages=messages, rule_requests=Ledger(state / "rule-requests.json"), diff --git a/orchestrator/kvb_orchestrator/ledger.py b/orchestrator/kvb_orchestrator/ledger.py index 3d85724..57029be 100644 --- a/orchestrator/kvb_orchestrator/ledger.py +++ b/orchestrator/kvb_orchestrator/ledger.py @@ -6,6 +6,7 @@ import threading from collections.abc import Callable from pathlib import Path +from .diffs import SEALED_SUFFIX from .records import read_json, stamp, write_atomic @@ -101,11 +102,28 @@ class Ledger: return dict(self.items[item_id]) -class Reviews(Ledger): +class Sealed(Ledger): + def __init__(self, file: Path, pages: Path, suffix: str) -> None: + """Args: + pages: the folder holding each item's sealed clear patches. + suffix: the name suffix of an item's sealed file there; it is + deleted once the item leaves pending. + """ + super().__init__(file) + self.pages = pages + self.sealed_suffix = suffix + + def transition(self, item_id: str, before: str, changes: dict) -> tuple[dict | None, bool]: + record, changed = super().transition(item_id, before, changes) + if changed and record is not None and record.get("status") != "pending": + (self.pages / f"{item_id}{self.sealed_suffix}").unlink(missing_ok=True) + return record, changed + + +class Reviews(Sealed): def __init__(self, state: Path) -> None: - super().__init__(state / "reviews.json") - self.pages = state / "pages" - self.pages.mkdir(exist_ok=True) + (state / "pages").mkdir(exist_ok=True) + super().__init__(state / "reviews.json", state / "pages", SEALED_SUFFIX["review"]) def stored(self, review_id: str, suffix: str) -> Path: return self.pages / f"{review_id}{suffix}" diff --git a/orchestrator/kvb_orchestrator/revealing.py b/orchestrator/kvb_orchestrator/revealing.py new file mode 100644 index 0000000..1ebd7bd --- /dev/null +++ b/orchestrator/kvb_orchestrator/revealing.py @@ -0,0 +1,82 @@ +from __future__ import annotations + +import time + +from .asking import ask_state +from .diffs import SEALED_SUFFIX, sealed_file, stored_sealed +from .errors import Conflict, Missing, ToolError +from .fields import decode, expect +from .records import shown + +TITLE_LIMIT = 200 +PATHS_SHOWN = 2000 + + +def reveal_item(kind_name: str, item_id: str, paths: list[str]) -> dict: + """Returns: + The item a reveal signs: the sorted paths of one review or ask; its + hash equals no item an executor approves. + """ + return {"kind": "reveal", "target": kind_name, "id": item_id, "paths": paths} + + +class Revealing: + def sealed_status(self, kind_name: str, item_id: str, now: float) -> tuple[dict | None, str | None]: + record = self.reviews.get(item_id) if kind_name == "review" else self.asks.get(item_id) + if record is None: + return None, None + return record, record["status"] if kind_name == "review" else ask_state(record, now)["status"] + + def open_record(self, kind_name: str, item_id: str) -> dict: + """Returns: + The pending review or ask whose sealed patches a reveal reads. + + Raises: + Missing for an unknown item; Conflict once it is decided or + expired, after its sealed file is deleted. + """ + record, status = self.sealed_status(kind_name, item_id, time.time()) + if record is None: + raise Missing(item_id) + if status != "pending": + sealed_file(self.reviews.pages, kind_name, item_id).unlink(missing_ok=True) + raise Conflict({"kind": kind_name, "id": item_id, "status": status}) + return record + + def requested(self, kind_name: str, item_id: str, paths: object) -> tuple[dict, list[str], dict[str, str]]: + record = self.open_record(kind_name, item_id) + if not isinstance(paths, list) or not paths or not all(isinstance(path, str) for path in paths) or len(set(paths)) != len(paths): + raise ToolError("paths must name one or more distinct files") + sealed = stored_sealed(self.reviews.pages, kind_name, item_id) + unsealed = [path for path in paths if path not in sealed] + if unsealed: + raise ToolError(f"nothing sealed to reveal for {shown(unsealed[0])[:TITLE_LIMIT]}") + return record, sorted(paths), sealed + + def sign_reveal(self, kind_name: str, item_id: str, body: bytes) -> dict: + payload = decode(body) + expect(payload, ("paths",)) + _, paths, _ = self.requested(kind_name, item_id, payload["paths"]) + return self.challenge_for([reveal_item(kind_name, item_id, paths)]) + + def reveal(self, kind_name: str, item_id: str, body: bytes) -> dict: + """Returns: + The clear patches of exactly the requested paths, once a pinned + passkey signed their reveal_item unexpired and for the first + time; the approval stream records kind, id and paths, never a + patch. + """ + payload = decode(body) + expect(payload, ("paths", "proof")) + record, paths, sealed = self.requested(kind_name, item_id, payload["paths"]) + self.operator_proven(payload["proof"], reveal_item(kind_name, item_id, paths)) + title = record["title"] if kind_name == "review" else f"{record['tool']}: {record['command']}" + self.log.add({"kind": "approval", "title": f"Reveal {kind_name}: {shown(title)[:TITLE_LIMIT]}", "text": shown(", ".join(paths))[:PATHS_SHOWN], "ref": {"kind": kind_name, "id": item_id, "verb": "reveal", "paths": paths}}) + return {"kind": kind_name, "id": item_id, "patches": {path: sealed[path] for path in paths}} + + def sweep_sealed(self) -> None: + now = time.time() + for kind_name, suffix in SEALED_SUFFIX.items(): + for file in self.reviews.pages.glob(f"*{suffix}"): + if self.sealed_status(kind_name, file.name.removesuffix(suffix), now)[1] != "pending": + file.unlink(missing_ok=True) diff --git a/orchestrator/kvb_orchestrator/reviewing.py b/orchestrator/kvb_orchestrator/reviewing.py index fa5fa99..7399d00 100644 --- a/orchestrator/kvb_orchestrator/reviewing.py +++ b/orchestrator/kvb_orchestrator/reviewing.py @@ -6,7 +6,15 @@ import stat import time from pathlib import Path -from .diffs import DIFF_SUFFIX, diff_bytes, stored_diff, valid_diff +from .diffs import ( + DIFF_SUFFIX, + SEALED_SUFFIX, + diff_bytes, + sealed_bytes, + stored_diff, + valid_diff, + valid_sealed, +) from .errors import Conflict, Missing, ToolError from .fields import WAIT_CAP, bounded, decode, expect, field, seconds from .review_page import review_files @@ -17,7 +25,7 @@ NOTES_LIMIT = limits.NOTES_LIMIT SCORE_LIMIT = 500 GATES = ("pass", "fail") REQUEST = ("repo", "worktree", "branch", "tree", "title", "page", "notes", "gate", "score") -OPTIONAL = ("agent", "diff") +OPTIONAL = ("agent", "diff", "sealed") LISTED = ("id", "created", "worktree", "branch", "tree", "title") STATE = ("id", "status", "tree", "decided") @@ -67,9 +75,12 @@ class Reviewing: score = " ".join(bounded(arguments, "score", SCORE_LIMIT).split()) agent = field(arguments, "agent").strip() if "agent" in arguments else "" diff = valid_diff(arguments["diff"]) if "diff" in arguments else None + sealed = valid_sealed(arguments["sealed"], diff) if "sealed" in arguments else {} page = self.read_page(field(arguments, "page")) extra = {"notes": notes, "gate": arguments["gate"], "score": score, "agent": agent, "diff_files": None if diff is None else len(diff)} attached = {} if diff is None else {DIFF_SUFFIX["review"]: diff_bytes(diff)} + if sealed: + attached[SEALED_SUFFIX["review"]] = sealed_bytes(sealed) review = self.reviews.submit({**fields, "title": fields["title"].strip(), **extra}, page, review_files(page), attached) self.automate("review", review) return {"id": review["id"], "status": review["status"], "url": self.url()} diff --git a/orchestrator/kvb_orchestrator/view.py b/orchestrator/kvb_orchestrator/view.py index 0927803..df0646e 100644 --- a/orchestrator/kvb_orchestrator/view.py +++ b/orchestrator/kvb_orchestrator/view.py @@ -77,6 +77,7 @@ class View: if sampled not in (None, "1"): raise ToolError("track must be 1 or left out") self.tree.beat() + self.sweep_sealed() return self.state(begin, end, now, TRACK_STEPS if sampled else 0) def inlined(self, news: list[dict], window: tuple[float, float], now: float) -> dict[float, dict]: diff --git a/orchestrator/templates/page-diff.js b/orchestrator/templates/page-diff.js index 48a5904..12a91fd 100644 --- a/orchestrator/templates/page-diff.js +++ b/orchestrator/templates/page-diff.js @@ -1,5 +1,44 @@ const DIFF_TONES = {A: "ok", M: "info", D: "bad", R: "violet", B: "neutral"}; - const diffView = {cache: new Map(), version: 0, float: null, picked: null, timer: 0}; + const HIDDEN_NOTES = new Set(["masked", "content withheld"]); + const diffView = {cache: new Map(), revealed: new Map(), version: 0, float: null, picked: null, timer: 0}; + const revealedOf = (item) => diffView.revealed.get(keyOf(item)) || new Map(); + const hiddenFile = (item, file) => HIDDEN_NOTES.has(file.note) && !revealedOf(item).has(file.path); + + function forgetRevealed() { + const waiting = new Set(state.inbox.map(keyOf)); + for (const key of [...diffView.revealed.keys()]) { + if (!waiting.has(key)) { + diffView.revealed.delete(key); + } + } + } + + async function revealFiles(item, paths) { + const route = `/api/diffs/${item.kind}/${encodeURIComponent(item.id)}/reveal`; + const label = paths.length === 1 ? `Reveal: ${short(paths[0])}` : `Reveal ${paths.length} files`; + const signed = await signedProof(`${route}/sign`, {paths}, label, null); + if (!signed || !signed.proof) { + return; + } + const result = await sendJson(route, {paths, proof: signed.proof}); + if (!result.ok || !result.data) { + toast(`${label}: not possible`, fail(result), false); + return; + } + diffView.revealed.set(keyOf(item), new Map([...revealedOf(item), ...Object.entries(result.data.patches)])); + diffView.version += 1; + drawInbox(); + drawStream(); + } + + function revealButton(item, paths, words) { + const hint = `${words}: your passkey signs the reveal; the clear text stays in this page only and the reveal is logged`; + return h("button", {type: "button", class: "btn small ghost reveal", ...named(words, hint), on: {click: (event) => { + event.preventDefault(); + event.stopPropagation(); + revealFiles(item, paths); + }}}, icon("bi-eye"), words); + } function diffOf(item) { if (!item || typeof item.diff !== "number" || !["review", "ask"].includes(item.kind)) { @@ -30,17 +69,23 @@ h("td", {class: "num"}, row.old), h("td", {class: "num"}, row.new), h("td", {class: "sign"}, SIGNS[row.kind] || " "), h("td", {class: "code"}, revealed(row.text))); } - function diffBlock(file) { + function diffBlock(item, raw) { + const clear = revealedOf(item).get(raw.path); + const hidden = hiddenFile(item, raw); + const file = clear === undefined ? raw : {...raw, patch: clear, note: ""}; const tone = DIFF_TONES[file.status] || "neutral"; const head = [h("span", {class: "letter", "data-tone": tone}, file.status), h("code", {class: "path"}, revealed(file.path)), file.mode && file.mode !== "100644" ? h("code", {class: "mode", title: `mode: ${file.mode}`}, file.mode) : null, h("span", {class: "spacer"}), file.note ? h("span", {class: "note", title: file.note}, icon("bi-eye-slash"), file.note) : null, + hidden ? revealButton(item, [file.path], "Reveal") : null, + clear === undefined ? null : h("span", {class: "note revealed", title: "revealed by your passkey, kept in this page only"}, icon("bi-eye"), "revealed"), file.status === "B" ? icon("bi-file-earmark-binary") : h("span", {class: "stat"}, h("span", {class: "add"}, `+${file.additions}`), h("span", {class: "del"}, `-${file.deletions}`))]; + const shown = clear === undefined ? (hidden ? raw.note : null) : "revealed"; if (file.status === "B" || !file.patch) { - return h("section", {class: "diff-file", "data-path": file.path, "data-status": file.status, "data-patch": "none"}, h("div", {class: "diff-head"}, head)); + return h("section", {class: "diff-file", "data-path": file.path, "data-status": file.status, "data-patch": "none", "data-hidden": shown}, h("div", {class: "diff-head"}, head)); } const table = h("table", {class: "diff"}, h("tbody", {}, Diffs.rows(file.patch).map(patchRow))); - return h("section", {class: "diff-file", "data-path": file.path, "data-status": file.status}, + return h("section", {class: "diff-file", "data-path": file.path, "data-status": file.status, "data-hidden": shown}, h("details", {open: !Diffs.collapsed(file)}, h("summary", {class: "diff-head"}, head), h("div", {class: "diff-scroll"}, table))); } @@ -52,7 +97,10 @@ if (found === "loading") { return h("p", {class: "muted"}, "…"); } - return h("div", {class: "diff-files", "aria-label": `Diff: ${found.length}`}, found.map(diffBlock)); + const hidden = found.filter((file) => hiddenFile(item, file)).map((file) => file.path); + return h("div", {class: "diff-files", "aria-label": `Diff: ${found.length}`}, + hidden.length > 1 ? h("div", {class: "reveal-all"}, revealButton(item, hidden, `Reveal all ${hidden.length}`)) : null, + found.map((file) => diffBlock(item, file))); } function feedbackList(ref) { diff --git a/orchestrator/templates/page-load.js b/orchestrator/templates/page-load.js index 38c8b85..ef580fc 100644 --- a/orchestrator/templates/page-load.js +++ b/orchestrator/templates/page-load.js @@ -21,6 +21,7 @@ const previous = state.now; const resumed = ui.offline; state = next; + forgetRevealed(); ui.offline = false; if (JSON.stringify(state.settings) !== settingsSig) { adoptSettings(state.settings); diff --git a/orchestrator/templates/style-diff.css b/orchestrator/templates/style-diff.css index 28fa926..3bae88d 100644 --- a/orchestrator/templates/style-diff.css +++ b/orchestrator/templates/style-diff.css @@ -17,6 +17,9 @@ .diff-file .diff-head .mode { color: var(--warn); font-size: var(--fs-xs); } .diff-file .diff-head .note .bi { margin-right: 4px; vertical-align: -2px; } .diff-file .diff-head .note { display: inline-flex; align-items: center; color: var(--warn); font-size: var(--fs-xs); } +.diff-file .diff-head .note.revealed { color: var(--ok); } +.diff-file .diff-head .reveal { flex: none; } +.diff-files .reveal-all { display: flex; justify-content: flex-end; } .feedback-list { display: grid; gap: 4px; margin: var(--sp-1) 0 0; padding: 0; list-style: none; } .feedback-list li { display: flex; align-items: baseline; gap: 6px; padding: 4px 8px; border-radius: 8px; background: var(--info-soft); font-size: var(--fs-sm); } .feedback-list .text { flex: 1; min-width: 0; overflow-wrap: anywhere; white-space: pre-wrap; } diff --git a/tests/e2e/test_reveal_withheld_e2e.py b/tests/e2e/test_reveal_withheld_e2e.py new file mode 100644 index 0000000..993694c --- /dev/null +++ b/tests/e2e/test_reveal_withheld_e2e.py @@ -0,0 +1,149 @@ +from __future__ import annotations + +import json +import os +import shutil +import sys +import unittest +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path[:0] = [str(HERE.parent), str(HERE)] + +import hub_e2e_kit as base +from hub_e2e_kit import WIDTHS +from hub_support import PAGE + +OUT = Path(os.environ.get("E2E_OUT", "/tmp/reveal-shots")) +MASK = "••••" +APP_CLEAR = '@@ -1 +1 @@\n-old\n+api_key = "plain-example-words"' +CONF_CLEAR = '@@ -0,0 +1 @@\n+token = "plain-other-words"' +DIFF = [ + {"path": "app.py", "status": "M", "mode": "100644", "additions": 1, "deletions": 1, "patch": f"@@ -1 +1 @@\n-old\n+{MASK}", "note": "masked"}, + {"path": "conf.py", "status": "A", "mode": "100644", "additions": 1, "deletions": 0, "patch": "", "note": "content withheld"}, + {"path": "open.txt", "status": "A", "mode": "100644", "additions": 1, "deletions": 0, "patch": "@@ -0,0 +1 @@\n+seen", "note": ""}, +] +SEALED = {"app.py": APP_CLEAR, "conf.py": CONF_CLEAR} +WORDS = ("plain-example", "plain-other") +COMMON = """ + const words = @WORDS@; + const row = (ref) => document.querySelector(`#main .inbox-list > li[data-ref="${ref}"]`); + const files = (ref) => all(".diff-file", row(ref)); + const look = (ref) => ({ + hidden: files(ref).map((file) => file.dataset.hidden || null), + buttons: all("button.reveal", row(ref)).map(name), + masked: text(row(ref)).includes("@MASK@"), + clear: words.filter((word) => text(row(ref)).includes(word)), + }); + const wide = () => document.documentElement.scrollWidth > innerWidth + 1; +""" +SHOW = """(async () => {@HELPERS@@COMMON@ + if (!(await until(() => row("@ONE@") && row("@ALL@") && files("@ONE@").length === 3 && files("@ALL@").length === 3, 8000))) { + return {error: text(document.getElementById("main"))}; + } + return {one: look("@ONE@"), all: look("@ALL@"), leaked: words.filter((word) => document.body.textContent.includes(word)), wide: wide()}; +})()""" +REVEAL = """(async () => {@HELPERS@@COMMON@ + navigator.credentials.get = @SIGNS@ ? navigator.credentials.get.bind(navigator.credentials) : () => Promise.reject(new DOMException("The operator canceled.", "NotAllowedError")); + const posted = []; + const send = window.fetch.bind(window); + window.fetch = (path, init) => { + if (String(path).includes("/reveal")) { + posted.push([String(path).replace(/^.*[/]api[/]diffs[/]/, ""), JSON.parse(init.body).paths]); + } + return send(path, init); + }; + if (!(await until(() => row("@REF@") && files("@REF@").length === 3, 8000))) { + return {error: text(document.getElementById("main"))}; + } + all("button.reveal", row("@REF@")).find((button) => name(button) === "@BUTTON@").click(); + if (@SIGNS@) { + await until(() => files("@REF@").filter((file) => file.dataset.hidden === "revealed").length === @COUNT@, 15000); + } else { + await sleep(1500); + } + const view = await (await send("/api/view", {cache: "no-store"})).text(); + let stored = ""; + try { + stored = JSON.stringify({...localStorage}); + } catch { + stored = ""; + } + return {after: look("@REF@"), posted, kept: words.filter((word) => view.includes(word) || stored.includes(word)), wide: wide()}; +})()""" + + +def probe(template: str, **values: str) -> str: + found = template.replace("@HELPERS@", base.HELPERS).replace("@COMMON@", COMMON).replace("@WORDS@", json.dumps(WORDS)).replace("@MASK@", MASK) + for name, value in values.items(): + found = found.replace(f"@{name}@", value) + return found + + +@unittest.skipUnless(shutil.which(os.environ.get("CHROMIUM", "chromium")), "no Chromium") +class TestRevealWithheld(base.OrchestratorE2ECase): + def setUp(self): + super().setUp() + OUT.mkdir(parents=True, exist_ok=True) + page = self.page("sealed.html", PAGE) + self.sets = {width: {"ONE": self.review(f"feat: one {width}", page), "ALL": self.review(f"feat: all {width}", page)} for width, _ in WIDTHS} + + def review(self, title: str, page: str) -> str: + result = self.tool("review_request", {**self.arguments(title, "", page), "agent": "Skills", "diff": DIFF, "sealed": SEALED}) + self.assertFalse(result["isError"], result) + return f"review:{result['structuredContent']['id']}" + + def steps(self, browser: str, signs: bool) -> list[dict]: + local = f"http://localhost:{self.port}/" + flag = "true" if signs else "false" + steps = [] + for width, height in WIDTHS: + ids = self.sets[width] + shot = str(OUT / f"{browser}-{width}") + steps += [ + {"width": width, "height": height, "url": f"{local}?{width}#stream/inbox", "wait": 1200, "probe": probe(SHOW, **ids), "shot": f"{shot}-masked.png"}, + {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(REVEAL, REF=ids["ONE"], BUTTON="Reveal", COUNT="1", SIGNS=flag), "shot": f"{shot}-revealed-file.png"}, + {"width": width, "height": height, "url": None, "wait": 0, "probe": probe(REVEAL, REF=ids["ALL"], BUTTON="Reveal all 2", COUNT="2", SIGNS=flag), "shot": f"{shot}-revealed-all.png"}, + ] + return steps + + def check(self, data: dict, browser: str, signs: bool) -> None: + self.assertEqual([error for error in data["errors"] if "(style-src-elem)" not in error and "/favicon.ico" not in error], []) + hidden = {"hidden": ["masked", "content withheld", None], "buttons": ["Reveal", "Reveal"], "masked": True, "clear": []} + results = iter(data["results"]) + for width, _ in WIDTHS: + ids = self.sets[width] + show, one, every = (next(results) for _ in range(3)) + with self.subTest(browser=browser, width=width): + self.assertNotIn("error", show, show) + self.assertEqual(show["one"], hidden, show) + self.assertEqual(show["all"], {**hidden, "buttons": ["Reveal all 2", "Reveal", "Reveal"]}, show) + self.assertEqual(show["leaked"], [], show) + one_id, all_id = (ids[name].split(":", 1)[1] for name in ("ONE", "ALL")) + self.assertEqual(one["posted"], [[f"review/{one_id}/reveal/sign", ["app.py"]], *([[f"review/{one_id}/reveal", ["app.py"]]] if signs else [])], one) + self.assertEqual(every["posted"], [[f"review/{all_id}/reveal/sign", ["app.py", "conf.py"]], *([[f"review/{all_id}/reveal", ["app.py", "conf.py"]]] if signs else [])], every) + if signs: + self.assertEqual(one["after"], {"hidden": ["revealed", "content withheld", None], "buttons": ["Reveal"], "masked": False, "clear": ["plain-example"]}, one) + self.assertEqual(every["after"], {"hidden": ["revealed", "revealed", None], "buttons": [], "masked": False, "clear": list(WORDS)}, every) + else: + self.assertEqual((one["after"], every["after"]), (hidden, {**hidden, "buttons": ["Reveal all 2", "Reveal", "Reveal"]})) + self.assertEqual((one["kept"], every["kept"]), ([], [])) + self.assertFalse(any(found["wide"] for found in (show, one, every)), width) + + def test_chromium_masked_files_reveal_per_file_and_per_item_with_the_passkey_at_three_widths(self): + data = self.drive(self.steps("chromium", True), self.authenticator()) + (OUT / "chromium-reveal.json").write_text(json.dumps(data, indent=1), encoding="utf-8") + self.check(data, "chromium", True) + rows = [row for row in self.events("kinds=approval")["events"] if row["title"].startswith("Reveal ")] + self.assertEqual(len(rows), 2 * len(WIDTHS)) + self.assertFalse(any(word in json.dumps(rows) for word in WORDS)) + + @unittest.skipUnless(shutil.which(os.environ.get("FIREFOX", "firefox")), "no Firefox") + def test_firefox_masked_files_stay_masked_without_a_signature_at_three_widths(self): + data = self.drive(self.steps("firefox", False), None, "bidi.mjs", os.environ.get("FIREFOX", "firefox")) + (OUT / "firefox-reveal.json").write_text(json.dumps(data, indent=1), encoding="utf-8") + self.check(data, "firefox", False) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/hub_case.py b/tests/hub_case.py index 410b9f1..c4a1efb 100644 --- a/tests/hub_case.py +++ b/tests/hub_case.py @@ -143,6 +143,14 @@ class HubCase(unittest.TestCase): self.assertEqual(status, 200, body) return signer.proof(json.loads(body)["payload"], self.origin) + def reveal_proof(self, kind: str, item_id: str, paths: list[str], signer: Key) -> dict: + status, _, body = self.post_json(f"/api/diffs/{kind}/{item_id}/reveal/sign", {"paths": paths}, self.guard(self.host)) + self.assertEqual(status, 200, body) + return signer.proof(json.loads(body)["payload"], self.origin) + + def reveal(self, kind: str, item_id: str, paths: list[str], proof: object) -> tuple[int, dict, bytes]: + return self.post_json(f"/api/diffs/{kind}/{item_id}/reveal", {"paths": paths, "proof": proof}, self.guard(self.host)) + def say(self, payload: dict, signer: Key) -> tuple[int, dict, bytes]: proof = self.message_proof({"upload": "", "voice": "", **payload}, signer) return self.post_json("/api/messages", {"upload": "", "voice": "", **payload, "proof": proof}, self.guard(self.host)) diff --git a/tests/test_hook_exec_hardening.py b/tests/test_hook_exec_hardening.py index 94982dd..9db907e 100644 --- a/tests/test_hook_exec_hardening.py +++ b/tests/test_hook_exec_hardening.py @@ -301,7 +301,7 @@ class TestBindingMatchesWriteTree(Repos): mutate(repo) after = self.ids(repo) self.assertEqual(before[1] != after[1], before[0] != after[0]) - self.assertEqual(shown(staged_diff.bound_diff(repo, after[1], None)), shown(staged_diff.tree_diff(repo, after[0], None))) + self.assertEqual(shown(staged_diff.bound_diff(repo, after[1], None)[0]), shown(staged_diff.tree_diff(repo, after[0], None)[0])) def test_the_preview_lists_exactly_what_git_commit_records(self): for name, setup in {"intent-to-add new file": intend_new, **UNREAD_CASES}.items(): @@ -309,7 +309,7 @@ class TestBindingMatchesWriteTree(Repos): repo = self.fresh(name.replace(" ", "-")) stage_a(repo) setup(repo) - preview = shown(staged_diff.bound_diff(repo, staged_diff.binding(repo), None)) + preview = shown(staged_diff.bound_diff(repo, staged_diff.binding(repo), None)[0]) git(repo, "commit", "-q", "-m", "x") self.assertEqual(preview, self.recorded(repo)) @@ -442,7 +442,7 @@ class TestHookGitEnvironment(Isolated): ask = {"tool": "Bash", "command": "git commit -m x", "cwd": str(repo), "session": "s", "agent": ""} with mock.patch.dict(os.environ, {name: str(path) for name, path in decoys.items()}), mock.patch.object(subprocess, "run", spy): pinned = staged_diff.binding(repo) - preview = shown(staged_diff.bound_diff(repo, pinned, None)) + preview = shown(staged_diff.bound_diff(repo, pinned, None)[0]) chosen = permission_hook.bound(ALLOW, ask, permission_hook.commit_diff(ask, None)[0]) self.assertEqual((pinned, preview, chosen), (expected, [("added.txt", "A"), ("base.txt", "M")], ALLOW)) self.assertTrue(seen) diff --git a/tests/test_hub_inbox_gone.py b/tests/test_hub_inbox_gone.py index cdda766..7840a13 100644 --- a/tests/test_hub_inbox_gone.py +++ b/tests/test_hub_inbox_gone.py @@ -43,7 +43,7 @@ const context = { state: {inbox: [], radio: [], settings: {}, now}, ui: {screen: "inbox", busy: false, selected: new Set(), cursor: 0, snoozed: false, asOf: null, playing: false, offline: false}, known: {batches: new Set()}, motion: {current: new Map([["x", {}]]), asOf: null}, settingsSig: "{}", - windowQuery: () => "", adoptSettings: () => {}, tuneMusic: () => {}, drawSky: () => {}, announce: () => {}, drawTime: () => {}, drawAgent: () => {}, relayout: () => {}, pulses: () => {}, tick: () => {}, + windowQuery: () => "", adoptSettings: () => {}, tuneMusic: () => {}, drawSky: () => {}, announce: () => {}, drawTime: () => {}, drawAgent: () => {}, relayout: () => {}, pulses: () => {}, tick: () => {}, forgetRevealed: () => {}, GONE, items, now, }; const source = names.map((name) => fs.readFileSync(`${dir}/${name}`, "utf8")).join("\n"); diff --git a/tests/test_hub_operator_tasks.py b/tests/test_hub_operator_tasks.py index 5c2ca84..3c7392b 100644 --- a/tests/test_hub_operator_tasks.py +++ b/tests/test_hub_operator_tasks.py @@ -28,6 +28,10 @@ PAGE_MODULES = ["page-constants.js", "page-reveal.js", "page-dom.js", "page-diff SECRET = 'api_key = "plain-example-words"' ARMOURED = "before\n-----BEGIN EXAMPLE-----\nplain words only\n-----END EXAMPLE-----\nafter\n" WITHHELD = "content withheld" +MASKED = "masked" +MASK = "••••" +APP_CLEAR = f"@@ -1,2 +1,4 @@\n keep = 1\n-old = 2\n+new = 3\n+{SECRET}\n+print(1)" +APP_SHOWN = f"@@ -1,2 +1,4 @@\n keep = 1\n-old = 2\n+new = 3\n+{MASK}\n+print(1)" ALLOW = {"behavior": "allow"} RULES = """ rules: @@ -83,7 +87,7 @@ class El extends Node { contains() { return false; } querySelector() { return null; } querySelectorAll() { return []; } - click() { for (const run of this.listeners.click || []) { run({target: this, preventDefault() {}}); } } + click() { for (const run of this.listeners.click || []) { run({target: this, preventDefault() {}, stopPropagation() {}}); } } } const elements = new Map(); const document = { @@ -100,7 +104,7 @@ document.getElementById("emoji-bases").textContent = "[]"; const all =(node) => (typeof node === "string" ? [] : [node, ...node.children.flatMap(all)]); const classes = (node) => String(node.className || node.attrs.class || "").split(" ").filter(Boolean); const labels = (node) => all(node).filter((el) => el.tag === "button").map((el) => el.getAttribute("aria-label") || el.textContent); -const calls = {proofs: [], posts: [], sheets: [], radio: []}; +const calls = {proofs: [], posts: [], sheets: [], radio: [], signs: []}; const now = 1_800_000_000; const base = {agent: "Skills", to: "Skills", detail: "", green: true, gate: "", score: "", warnings: [], options: [], recommended: "", command: "", directory: "", sha256: "", runnable: false, deniable: false, job: null, rule: null, dry_run: null, diff: null, pinned: false, until: 0, held: false, ts: now - 60}; const operator = {...base, kind: "operator", id: "operator/ship", title: "make ship", command: "make ship", directory: "/repo", detail: "release the build", runnable: true, deniable: true, green: false, sha256: "abc"}; @@ -116,7 +120,11 @@ const context = { KINDS: [["operator", "Operator"], ["review", "Review"]], speech: {current: null}, look: {hidden: new Set()}, settings: {channels: {title: true}}, known: {batches: new Set()}, relative: new Intl.RelativeTimeFormat("en"), absolute: new Intl.DateTimeFormat("en"), calm: {matches: true}, drawn: {screen: "", sig: ""}, proofFor: async (entries, label) => { calls.proofs.push({entries, label}); return {proof: {payload: "p", assertion: "a"}}; }, - sendJson: async (path, body) => { calls.posts.push({path, body}); return {ok: true, data: {id: "b1", due: now + 3}}; }, + sendJson: async (path, body) => { + calls.posts.push({path, body}); + return path.endsWith("/reveal") ? {ok: true, data: {kind: "review", id: "3123456789abcdef", patches: {"conf.py": "@@ -1 +1 @@\n-a\n+clear-token", "late.py": "@@ -0,0 +1 @@\n+clear-late"}}} : {ok: true, data: {id: "b1", due: now + 3}}; + }, + signedProof: async (path, fields, label) => { calls.signs.push([path, fields.paths, label]); return {proof: {payload: "p", assertion: "a"}}; }, radioSend: async (message) => { calls.radio.push(message); return true; }, load: async () => {}, toast: () => {}, fail: () => [], undoToast: () => {}, jobOpen: () => false, dismissJob: () => {}, consoleOf: () => null, settleConsoles: () => {}, repaintConsoles: () => {}, @@ -196,7 +204,33 @@ setTimeout(() => { results.proofs = calls.proofs.map((call) => call.entries.map((entry) => [entry.kind, entry.verb, entry.label])); results.posts = calls.posts.map((call) => [call.path, call.body.items.length, call.body.proof !== null]); results.radio = calls.radio; - console.log(JSON.stringify(results)); + drawInbox = () => {}; + drawStream = () => {}; + const secret = {...review, id: "3123456789abcdef", diff: 2}; + diffView.cache.set("review:3123456789abcdef", [ + {path: "conf.py", status: "M", mode: "100644", additions: 1, deletions: 1, patch: "@@ -1 +1 @@\\n-a\\n+••••", note: "masked"}, + {path: "late.py", status: "A", mode: "100644", additions: 1, deletions: 0, patch: "", note: "content withheld"}, + ]); + state.inbox = [...state.inbox, secret]; + const hiddenOf = (card) => ({ + hidden: all(card).filter((el) => classes(el).includes("diff-file")).map((el) => el.dataset.hidden || null), + buttons: labels(card).filter((label) => label.startsWith("Reveal")), + code: all(card).filter((el) => el.tag === "td" && classes(el).includes("code")).map((el) => el.textContent), + }); + const secretCard = inboxItem(secret, 3); + results.reveal = {before: hiddenOf(secretCard)}; + all(secretCard).find((el) => el.tag === "button" && el.getAttribute("aria-label") === "Reveal all 2").click(); + setTimeout(() => { + results.reveal.after = hiddenOf(inboxItem(secret, 3)); + results.reveal.signs = calls.signs; + results.reveal.posts = calls.posts.filter((call) => call.path.endsWith("/reveal")).map((call) => [call.path, call.body.paths, call.body.proof !== null]); + forgetRevealed(); + results.reveal.kept = diffView.revealed.size; + state.inbox = state.inbox.filter((item) => item !== secret); + forgetRevealed(); + results.reveal.forgotten = [diffView.revealed.size, hiddenOf(inboxItem(secret, 3)).hidden]; + console.log(JSON.stringify(results)); + }, 20); }, 20); `; vm.runInNewContext(`${source}\n${probe}`, {...context, setTimeout, Element: El, HTMLElement: El, now, operator, review, ask, decision, calls, all, classes, labels}); @@ -319,6 +353,14 @@ class TestOperatorPage(unittest.TestCase): self.assertEqual((files[0]["note"], files[0]["mode"], files[0]["stat"]), ([], [], ["+2", "-1"])) self.assertEqual((files[2]["mode"], files[3]["note"], files[3]["stat"]), (["100644 => 100755"], [WITHHELD], ["+9", "-0"])) + def test_a_hidden_file_offers_its_reveal_and_the_signed_reveal_shows_the_clear_patch_until_the_item_leaves(self): + reveal = self.data["reveal"] + self.assertEqual(reveal["before"], {"hidden": [MASKED, WITHHELD], "buttons": ["Reveal all 2", "Reveal", "Reveal"], "code": ["@@ -1 +1 @@", "a", MASK]}) + self.assertEqual(reveal["signs"], [["/api/diffs/review/3123456789abcdef/reveal/sign", ["conf.py", "late.py"], "Reveal 2 files"]]) + self.assertEqual(reveal["posts"], [["/api/diffs/review/3123456789abcdef/reveal", ["conf.py", "late.py"], True]]) + self.assertEqual(reveal["after"], {"hidden": ["revealed", "revealed"], "buttons": [], "code": ["@@ -1 +1 @@", "a", "clear-token", "@@ -0,0 +1 @@", "clear-late"]}) + self.assertEqual((reveal["kept"], reveal["forgotten"]), (1, [0, [MASKED, WITHHELD]])) + def test_a_commit_ask_shows_its_diff_like_a_review_and_its_command_and_files_take_feedback(self): ask = self.data["ask"] self.assertEqual((ask["ref"], ask["to"], ask["files"]), ("ask:1123456789abcdef", "Skills", [["src/app.py", 3]])) @@ -355,18 +397,29 @@ class TestStagedDiff(unittest.TestCase): patch.start() self.addCleanup(patch.stop) - def test_the_staged_diff_lists_each_file_once_and_a_file_matching_a_secret_rule_ships_only_its_counts_and_the_note(self): - found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo,self.rules)} + def test_the_staged_diff_lists_each_file_once_masks_only_the_secret_rule_hit_and_seals_the_clear_patch_apart(self): + diff, sealed = staged_diff.review_diff(self.repo, self.rules) + found = {entry["path"]: entry for entry in diff} self.assertEqual(sorted(found), ["added.txt", "app.py", "gone.txt", "logo.png"]) self.assertEqual({path: (entry["status"], entry["mode"], entry["additions"], entry["deletions"], entry["note"]) for path, entry in found.items()}, { - "added.txt": ("A", "100644", 1, 0, ""), "app.py": ("M", "100644", 3, 1, WITHHELD), "gone.txt": ("D", "100644", 0, 1, ""), "logo.png": ("B", "100644", 0, 0, ""), + "added.txt": ("A", "100644", 1, 0, ""), "app.py": ("M", "100644", 3, 1, MASKED), "gone.txt": ("D", "100644", 0, 1, ""), "logo.png": ("B", "100644", 0, 0, ""), }) - self.assertEqual([found[path]["patch"] for path in ("added.txt", "app.py", "gone.txt", "logo.png")], ["@@ -0,0 +1 @@\n+fresh", "", "@@ -1 +0,0 @@\n-bye", ""]) - self.assertNotIn("plain-example", json.dumps(list(found.values()))) - self.assertFalse(hasattr(staged_diff, "masked")) - self.assertEqual(diffs.valid_diff(list(found.values())), [found[path] for path in sorted(found)]) + self.assertEqual([found[path]["patch"] for path in ("added.txt", "app.py", "gone.txt", "logo.png")], ["@@ -0,0 +1 @@\n+fresh", APP_SHOWN, "@@ -1 +0,0 @@\n-bye", ""]) + self.assertNotIn("plain-example", json.dumps(diff)) + self.assertEqual(sealed, {"app.py": APP_CLEAR}) + self.assertEqual(diffs.valid_diff(diff), [found[path] for path in sorted(found)]) + self.assertEqual(diffs.valid_sealed(sealed, diff), sealed) - def test_an_armour_line_in_the_old_or_the_new_blob_withholds_the_whole_file(self): + def test_only_the_span_a_secret_rule_matches_is_masked_across_lines_and_the_rest_of_the_file_shows(self): + (self.repo / "app.py").write_text(f'lead = 1; {SECRET}; tail = 2\napi_key =\n "plain-other-words"\nprint(2)\n', encoding="utf-8") + git(self.repo, "add", "app.py") + diff, sealed = staged_diff.review_diff(self.repo, self.rules) + app = next(entry for entry in diff if entry["path"] == "app.py") + self.assertEqual((app["note"], app["patch"].split("\n")), (MASKED, ["@@ -1,2 +1,4 @@", "-keep = 1", "-old = 2", f"+lead = 1; {MASK}; tail = 2", f"+{MASK}", f"+{MASK}", "+print(2)"])) + self.assertNotIn("plain-", json.dumps(diff)) + self.assertEqual(sealed["app.py"].split("\n")[3:6], [f"+lead = 1; {SECRET}; tail = 2", "+api_key =", '+ "plain-other-words"']) + + def test_an_armour_block_in_the_old_or_the_new_blob_is_masked_from_its_begin_through_its_end_line(self): (self.repo / "key.txt").write_text(ARMOURED, encoding="utf-8") (self.repo / "plain.txt").write_text("plain\n", encoding="utf-8") git(self.repo, "add", "key.txt", "plain.txt") @@ -375,27 +428,38 @@ class TestStagedDiff(unittest.TestCase): (self.repo / "plain.txt").write_text("plain\n-----BEGIN lowercase is no armour-----\n", encoding="utf-8") (self.repo / "fresh.txt").write_text(ARMOURED, encoding="utf-8") git(self.repo, "add", "key.txt", "plain.txt", "fresh.txt") - found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo,self.rules)} - self.assertEqual([(found[path]["patch"], found[path]["note"], found[path]["deletions"]) for path in ("key.txt", "fresh.txt")], [("", WITHHELD, 3), ("", WITHHELD, 0)]) + diff, sealed = staged_diff.review_diff(self.repo, self.rules) + found = {entry["path"]: entry for entry in diff} + self.assertEqual([(found[path]["patch"], found[path]["note"], found[path]["deletions"]) for path in ("key.txt", "fresh.txt")], [ + (f"@@ -1,5 +1,2 @@\n before\n-{MASK}\n-{MASK}\n-{MASK}\n after", MASKED, 3), (f"@@ -0,0 +1,5 @@\n+before\n+{MASK}\n+{MASK}\n+{MASK}\n+after", MASKED, 0), + ]) self.assertEqual((found["plain.txt"]["note"], found["plain.txt"]["additions"]), ("", 1)) - self.assertNotIn("plain words only", json.dumps(list(found.values()))) + self.assertNotIn("plain words only", json.dumps(diff)) + self.assertEqual(sorted(sealed), ["fresh.txt", "key.txt"]) + self.assertIn("-plain words only", sealed["key.txt"].split("\n")) - def test_past_the_cap_the_rest_is_listed_by_stat_only(self): + def test_past_the_cap_the_rest_is_listed_by_stat_only_and_a_masked_file_stays_sealed(self): with mock.patch.object(staged_diff, "DIFF_LIMIT", 40): - found = staged_diff.review_diff(self.repo,self.rules) - self.assertEqual([(entry["path"], bool(entry["patch"]), entry["note"]) for entry in found], [("added.txt", True, ""), ("app.py", False, WITHHELD), ("gone.txt", False, ""), ("logo.png", False, "")]) + found, sealed = staged_diff.review_diff(self.repo, self.rules) + self.assertEqual([(entry["path"], bool(entry["patch"]), entry["note"]) for entry in found], [("added.txt", True, ""), ("app.py", False, MASKED), ("gone.txt", False, ""), ("logo.png", False, "")]) self.assertEqual([(entry["additions"], entry["deletions"]) for entry in found], [(1, 0), (3, 1), (0, 1), (0, 0)]) + self.assertEqual(sealed, {"app.py": APP_CLEAR}) - def test_without_secret_rules_or_with_a_broken_rule_file_every_text_file_is_withheld(self): + def test_without_secret_rules_or_with_a_broken_rule_file_every_text_file_is_withheld_and_sealed(self): broken = self.root / "broken-rules" broken.mkdir() (broken / "bad.yaml").write_text("cwe: CWE-798\nrules: [unclosed\n", encoding="utf-8") for rules in (None, self.root / "missing", broken): with self.subTest(rules=str(rules)): - found = staged_diff.review_diff(self.repo,rules) + found, sealed = staged_diff.review_diff(self.repo, rules) self.assertEqual([(entry["patch"], entry["note"]) for entry in found], [("", WITHHELD)] * 3 + [("", "")]) + self.assertEqual(sealed, {"added.txt": "@@ -0,0 +1 @@\n+fresh", "app.py": APP_CLEAR, "gone.txt": "@@ -1 +0,0 @@\n-bye"}) self.assertIsNone(staged_diff.secret_rules(broken)) + def test_the_sealed_patches_stop_at_their_own_limit(self): + with mock.patch.object(staged_diff, "SEALED_LIMIT", 30): + self.assertEqual(staged_diff.review_diff(self.repo, None)[1], {"added.txt": "@@ -0,0 +1 @@\n+fresh"}) + def test_the_diff_fails_past_the_file_cap_or_the_path_limit_instead_of_the_hub_refusing_it(self): for name, value in (("FILE_CAP", 3), ("PATH_LIMIT", 8)): with self.subTest(limit=name), mock.patch.object(staged_diff, name, value), self.assertRaises(staged_diff.DiffError): @@ -430,13 +494,13 @@ class TestStagedDiff(unittest.TestCase): (self.repo / "open.txt").write_text("seen = 1\n", encoding="utf-8") git(self.repo, "add", "vault", "open.txt") write_settings(self.repo / ".claude" / "settings.local.json", deny(["~/repo/vault"])) - found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)} + found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)[0]} self.assertEqual([found[path]["patch"] for path in ("vault/key", "open.txt")], ["", "@@ -0,0 +1 @@\n+seen = 1"]) self.assertNotIn("stolen", json.dumps(list(found.values()))) def test_the_staged_diff_without_any_settings_file_diffs_normal_files(self): self.assertEqual([file for file, _ in sandbox_paths.settings_files(self.repo) if file.exists()], []) - found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)} + found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)[0]} self.assertEqual((found["added.txt"]["patch"], found["app.py"]["additions"]), ("@@ -0,0 +1 @@\n+fresh", 3)) def test_a_planted_empty_deny_read_list_still_leaves_the_floor_unread(self): @@ -446,7 +510,7 @@ class TestStagedDiff(unittest.TestCase): git(self.repo, "add", ".ssh", "notes.txt") write_settings(self.repo / ".claude" / "settings.json", deny([])) with mock.patch.dict(os.environ, {"HOME": str(self.repo)}): - found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)} + found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)[0]} self.assertEqual([found[path]["patch"] for path in (".ssh/id_ed25519", "notes.txt")], ["", "@@ -0,0 +1 @@\n+seen = 1"]) self.assertNotIn("stolen", json.dumps(list(found.values()))) @@ -463,7 +527,7 @@ class TestStagedDiff(unittest.TestCase): (self.repo / "vault" / "key").write_text("stolen = 1\n", encoding="utf-8") git(self.repo, "add", "vault") write_settings(self.repo / ".claude" / "settings.json", deny(["vault"])) - found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo,self.rules)} + found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo,self.rules)[0]} self.assertEqual((found["vault/key"]["status"], found["vault/key"]["patch"], found["added.txt"]["patch"]), ("A", "", "@@ -0,0 +1 @@\n+fresh")) def test_the_session_project_settings_and_read_rules_add_denies(self): @@ -474,7 +538,7 @@ class TestStagedDiff(unittest.TestCase): write_settings(self.root / "project" / ".claude" / "settings.local.json", deny(["~/repo/vault"])) write_settings(self.repo / ".claude" / "settings.json", {"permissions": {"deny": ["Read(/notes.txt)", "Bash(rm:*)"]}}) write_settings(self.root / ".claude" / "settings.json", {"permissions": {"deny": ["Read(*.pem)"]}}) - found = {entry["path"]: entry["patch"] for entry in staged_diff.review_diff(self.repo, self.rules)} + found = {entry["path"]: entry["patch"] for entry in staged_diff.review_diff(self.repo, self.rules)[0]} self.assertEqual([found[path] for path in ("vault/key", "notes.txt", "deep.pem", "open.txt")], ["", "", "", "@@ -0,0 +1 @@\n+seen = 1"]) def test_each_settings_scope_resolves_relative_entries_from_its_own_folder_and_read_rules_like_claude_code(self): @@ -498,14 +562,14 @@ class TestStagedDiff(unittest.TestCase): self.assertFalse(sandbox_paths.denied(str(self.repo / "env.txt"), roots)) (self.repo / ".env").write_text("hidden = 1\n", encoding="utf-8") git(self.repo, "add", ".env") - found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)} + found = {entry["path"]: entry for entry in staged_diff.review_diff(self.repo, self.rules)[0]} self.assertEqual((found[".env"]["patch"], found["added.txt"]["patch"]), ("", "@@ -0,0 +1 @@\n+fresh")) def test_an_allow_turns_into_a_deny_when_the_staged_tree_changed_since_the_preview_even_in_a_file_it_withholds(self): ask = {**ASK, "tool": "Bash", "command": "git commit -m x", "cwd": str(self.repo)} changed = {"behavior": "deny", "message": "staged tree changed since the preview"} edits = { - "withheld content": lambda: (self.repo / "app.py").write_text(f"{SECRET}\nslipped in\n", encoding="utf-8"), + "masked content": lambda: (self.repo / "app.py").write_text(f"{SECRET}\nslipped in\n", encoding="utf-8"), "mode only": lambda: (self.repo / "added.txt").chmod(0o755), "binary": lambda: (self.repo / "logo.png").write_bytes(b"\x89PNG\x00\x03"), } @@ -514,7 +578,8 @@ class TestStagedDiff(unittest.TestCase): git(self.repo, "add", "-A") pinned, shown = permission_hook.commit_diff(ask, self.rules) index = subprocess.run(["git", "ls-files", "-s", "-z"], cwd=self.repo, capture_output=True, check=True).stdout - self.assertEqual((pinned, shown["diff"]), (hashlib.sha256(index).hexdigest(), staged_diff.review_diff(self.repo,self.rules))) + diff, sealed = staged_diff.review_diff(self.repo, self.rules) + self.assertEqual((pinned, shown), (hashlib.sha256(index).hexdigest(), {"diff": diff, "sealed": sealed})) self.assertEqual(permission_hook.bound(ALLOW, ask, pinned), ALLOW) edit() self.assertEqual(permission_hook.bound(ALLOW, ask, pinned), ALLOW) @@ -553,12 +618,12 @@ class TestStagedDiff(unittest.TestCase): empty = self.root / "empty-rules" empty.mkdir() (empty / "none.yaml").write_text("rules: []\n", encoding="utf-8") - self.assertEqual({(entry["patch"], entry["note"]) for entry in staged_diff.review_diff(self.repo,empty)}, {("", WITHHELD), ("", "")}) + self.assertEqual({(entry["patch"], entry["note"]) for entry in staged_diff.review_diff(self.repo,empty)[0]}, {("", WITHHELD), ("", "")}) def test_a_file_name_that_is_no_utf8_ships_as_backslash_escapes(self): (self.repo / os.fsdecode(b"caf\xe9.txt")).write_text("x\n", encoding="utf-8") git(self.repo, "add", "-A") - found = staged_diff.review_diff(self.repo,self.rules) + found = staged_diff.review_diff(self.repo,self.rules)[0] self.assertIn("caf\\xe9.txt", [entry["path"] for entry in found]) json.dumps(found, ensure_ascii=False).encode("utf-8") @@ -566,7 +631,7 @@ class TestStagedDiff(unittest.TestCase): tree = git(self.repo, "rev-parse", "HEAD^{tree}").strip() (self.repo / "added.txt").write_text("one\x0ctwo\n", encoding="utf-8") git(self.repo, "add", "added.txt") - self.assertEqual(next(entry for entry in staged_diff.review_diff(self.repo,self.rules) if entry["path"] == "added.txt")["additions"], 1) + self.assertEqual(next(entry for entry in staged_diff.review_diff(self.repo,self.rules)[0] if entry["path"] == "added.txt")["additions"], 1) (self.repo / ".git" / "objects" / tree[:2] / tree[2:]).unlink() with self.assertRaises(staged_diff.DiffError): staged_diff.review_diff(self.repo,self.rules) @@ -577,7 +642,7 @@ class TestStagedDiff(unittest.TestCase): git(fresh, "init", "-q", "-b", "main") (fresh / "one.txt").write_text("one\n", encoding="utf-8") git(fresh, "add", "one.txt") - self.assertEqual([(entry["path"], entry["status"]) for entry in staged_diff.review_diff(fresh, self.rules)], [("one.txt", "A")]) + self.assertEqual([(entry["path"], entry["status"]) for entry in staged_diff.review_diff(fresh, self.rules)[0]], [("one.txt", "A")]) commit = git(self.repo, "rev-parse", "HEAD").strip() (self.repo / ".git" / "objects" / commit[:2] / commit[2:]).unlink() with self.assertRaises(staged_diff.DiffError): @@ -680,8 +745,14 @@ class TestHubOperatorTasks(HubCase): status, _, body = self.get(f"/api/diffs/review/{answer['id']}") self.assertEqual(status, 200, body) shipped = json.loads(body)["diff"] - self.assertEqual([(entry["path"], entry["status"], entry["note"]) for entry in shipped], [("added.txt", "A", ""), ("app.py", "M", WITHHELD), ("gone.txt", "D", ""), ("logo.png", "B", "")]) + self.assertEqual([(entry["path"], entry["status"], entry["note"]) for entry in shipped], [("added.txt", "A", ""), ("app.py", "M", MASKED), ("gone.txt", "D", ""), ("logo.png", "B", "")]) + self.assertEqual(shipped[1]["patch"], APP_SHOWN) self.assertNotIn(b"plain-example", body) + self.assertNotIn("plain-example", json.dumps(self.snapshot())) + sealed = self.state / "pages" / f"{answer['id']}.review-sealed.json" + self.assertEqual(sealed.stat().st_mode & 0o777, 0o600) + status, _, body = self.reveal("review", answer["id"], ["app.py"], self.reveal_proof("review", answer["id"], ["app.py"], self.pin())) + self.assertEqual((status, json.loads(body)["patches"]), (200, {"app.py": APP_CLEAR}), body) self.assertEqual(self.tool("review_status", {"id": answer["id"]})["structuredContent"]["tree"], git(repo, "write-tree").strip()) for path in ("/api/diffs/review/0123456789abcdef", f"/api/diffs/ask/{answer['id']}", f"/api/diffs/decision/{answer['id']}"): with self.subTest(path=path): @@ -694,8 +765,13 @@ class TestHubOperatorTasks(HubCase): self.assertFalse(result["isError"], result) self.assertEqual(len(json.loads(self.get(f"/api/diffs/review/{result['structuredContent']['id']}")[2])["diff"]), 5) good = {"path": "a", "status": "M", "mode": "100644 => 100755", "additions": 1, "deletions": 0, "patch": "@@ -1 +1 @@\n+x", "note": ""} - self.assertFalse(self.tool("review_request", {**arguments, "diff": [good, {**good, "path": "b", "patch": "", "note": WITHHELD}]})["isError"]) - for broken in ([{**good, "status": "X"}], [{**good, "status": "B"}], [{**good, "mode": "755"}], [{**good, "note": "masked"}], [{**good, "note": WITHHELD}], [{**good, "masked": []}], + hidden = [good, {**good, "path": "b", "patch": "", "note": WITHHELD}, {**good, "path": "c", "note": MASKED}] + self.assertFalse(self.tool("review_request", {**arguments, "diff": hidden, "sealed": {"b": "@@ -1 +1 @@\n+y", "c": "@@ -1 +1 @@\n+z"}})["isError"]) + for sealed in ({"a": "x"}, {"b": 1}, ["b"], {"b": "x" * (hub_limits.SEALED_LIMIT + 1)}): + with self.subTest(sealed=str(sealed)[:60]): + self.assertTrue(self.tool("review_request", {**arguments, "diff": hidden, "sealed": sealed})["isError"]) + self.assertTrue(self.tool("review_request", {**{key: value for key, value in arguments.items() if key != "diff"}, "sealed": {"b": "x"}})["isError"]) + for broken in ([{**good, "status": "X"}], [{**good, "status": "B"}], [{**good, "mode": "755"}], [{**good, "note": "hidden"}], [{**good, "note": WITHHELD}], [{**good, "masked": []}], [{**good, "extra": 1}], [{**good, "additions": -1}], {"a": 1}, [{**good, "path": "p" * (hub_limits.PATH_LIMIT + 1)}], [good] * (hub_limits.FILE_CAP + 1), [{**good, "patch": "x" * (diffs.DIFF_LIMIT + 1)}]): with self.subTest(broken=str(broken)[:60]): @@ -726,6 +802,10 @@ class TestHubOperatorTasks(HubCase): self.assertEqual((item["kind"], item["diff"]), ("ask", 4)) status, _, body = self.get(f"/api/diffs/ask/{ask_id}") self.assertEqual((status, [entry["status"] for entry in json.loads(body)["diff"]]), (200, ["A", "M", "D", "B"])) + self.assertEqual((attached["sealed"], json.loads(body)["diff"][1]["patch"]), ({"app.py": APP_CLEAR}, APP_SHOWN)) + self.assertEqual((self.state / "pages" / f"{ask_id}.ask-sealed.json").stat().st_mode & 0o777, 0o600) + status, _, body = self.reveal("ask", ask_id, ["app.py"], self.reveal_proof("ask", ask_id, ["app.py"], self.pin())) + self.assertEqual((status, json.loads(body)["patches"]), (200, {"app.py": APP_CLEAR}), body) plain = self.create_ask(ASK) self.assertIsNone(next(item for item in self.snapshot()["inbox"] if item["id"] == plain)["diff"]) self.assertEqual(self.post_json("/api/asks", {**ASK, "diff": [{"path": "a"}]}, self.local())[0], 400) @@ -759,7 +839,8 @@ class TestSharedLimits(unittest.TestCase): self.assertEqual(Path(signed.limits.__file__).resolve(), (HUB / "client" / "hub_limits.py").resolve()) self.assertEqual((staged_diff.DIFF_LIMIT, diffs.DIFF_LIMIT, reviewing.NOTES_LIMIT), (hub_limits.DIFF_LIMIT, hub_limits.DIFF_LIMIT, hub_limits.NOTES_LIMIT)) self.assertEqual((diffs.DIFF_BODY_LIMIT, diffs.FILE_CAP, diffs.PATH_LIMIT, fields.PATH_LIMIT, permission_hook.PATH_LIMIT), (hub_limits.DIFF_BODY_LIMIT, hub_limits.FILE_CAP, *[hub_limits.PATH_LIMIT] * 3)) - self.assertEqual((staged_diff.WITHHELD, diffs.NOTES), (WITHHELD, ("", WITHHELD))) + self.assertEqual((staged_diff.WITHHELD, staged_diff.MASKED, staged_diff.MASK, diffs.NOTES), (WITHHELD, MASKED, MASK, ("", WITHHELD, MASKED))) + self.assertEqual((staged_diff.SEALED_LIMIT, diffs.SEALED_LIMIT), (hub_limits.SEALED_LIMIT, hub_limits.SEALED_LIMIT)) self.assertTrue(fields.PATH.fullmatch("/" + "p" * (hub_limits.PATH_LIMIT - 1))) self.assertFalse(fields.PATH.fullmatch("/" + "p" * hub_limits.PATH_LIMIT)) diff --git a/tests/test_hub_radioset.py b/tests/test_hub_radioset.py index e37049e..1bdf935 100644 --- a/tests/test_hub_radioset.py +++ b/tests/test_hub_radioset.py @@ -44,6 +44,7 @@ function page() { state: {now: 0, radio: []}, ui: {offline: false}, settingsSig: "", motion: {current: new Map([[1, 1]]), asOf: null}, window: {getSelection: () => "", setTimeout: (run, ms) => timers.push({run, ms}), clearTimeout: stub}, document: {hidden: false}, adoptSettings: stub, tuneMusic: stub, drawSky: stub, render: stub, drawTime: stub, drawAgent: stub, drawStream: stub, relayout: stub, pulses: stub, tick: stub, + forgetRevealed: stub, }; context.announce = () => announced.push(context.state.now); vm.runInNewContext(code, context); diff --git a/tests/test_hub_sealed_reveal.py b/tests/test_hub_sealed_reveal.py new file mode 100644 index 0000000..bc4f5e5 --- /dev/null +++ b/tests/test_hub_sealed_reveal.py @@ -0,0 +1,137 @@ +from __future__ import annotations + +import json +import time +import unittest +from pathlib import Path +from unittest import mock + +from hub_case import HubCase +from hub_support import ASK, PAGE, key +from kvb_orchestrator import approving + +MASKED = "masked" +WITHHELD = "content withheld" +APP_CLEAR = '@@ -1 +1 @@\n-old\n+api_key = "plain-example-words"' +CONF_CLEAR = '@@ -0,0 +1 @@\n+token = "plain-other-words"' +DIFF = [ + {"path": "app.py", "status": "M", "mode": "100644", "additions": 1, "deletions": 1, "patch": "@@ -1 +1 @@\n-old\n+••••", "note": MASKED}, + {"path": "conf.py", "status": "A", "mode": "100644", "additions": 1, "deletions": 0, "patch": "", "note": WITHHELD}, + {"path": "open.txt", "status": "A", "mode": "100644", "additions": 1, "deletions": 0, "patch": "@@ -0,0 +1 @@\n+seen", "note": ""}, +] +SEALED = {"app.py": APP_CLEAR, "conf.py": CONF_CLEAR} + + +class TestSealedReveal(HubCase): + def review(self) -> str: + result = self.tool("review_request", {**self.arguments("feat: sealed", "", self.page("sealed.html", PAGE)), "agent": "Skills", "diff": DIFF, "sealed": SEALED}) + self.assertFalse(result["isError"], result) + return result["structuredContent"]["id"] + + def ask(self, timeout: float) -> str: + return self.create_ask({**ASK, "command": "git commit -m x", "timeout": timeout, "diff": DIFF, "sealed": SEALED}) + + def sealed(self, kind: str, item_id: str) -> Path: + return self.state / "pages" / f"{item_id}.{kind}-sealed.json" + + def assert_no_clear_text(self, body: bytes) -> None: + for word in (b"plain-example", b"plain-other"): + self.assertNotIn(word, body) + + def test_the_sealed_patches_sit_apart_with_mode_0600_and_never_reach_the_diff_the_state_or_the_stream(self): + items = {"review": self.review(), "ask": self.ask(60)} + for kind, item_id in items.items(): + with self.subTest(kind=kind): + self.assertEqual(self.sealed(kind, item_id).stat().st_mode & 0o777, 0o600) + status, _, body = self.get(f"/api/diffs/{kind}/{item_id}") + self.assertEqual((status, json.loads(body)["diff"]), (200, DIFF)) + self.assert_no_clear_text(body) + for route in ("page", "files"): + self.assert_no_clear_text(self.get(f"/reviews/{item_id}/{route}")[2]) + self.assert_no_clear_text(json.dumps(self.snapshot()).encode("utf-8")) + self.assert_no_clear_text(json.dumps(self.events("kinds=approval,review,ask")).encode("utf-8")) + self.assert_no_clear_text(self.index().encode("utf-8")) + + def test_a_sealed_path_outside_the_hidden_files_or_without_a_diff_is_refused(self): + arguments = {**self.arguments("feat: sealed", "", self.page("bad.html", PAGE)), "agent": "Skills"} + for extra in ({"diff": DIFF, "sealed": {"open.txt": "x"}}, {"diff": DIFF, "sealed": {"app.py": 1}}, {"sealed": SEALED}): + with self.subTest(extra=sorted(extra)): + self.assertTrue(self.tool("review_request", {**arguments, **extra})["isError"]) + self.assertEqual(self.post_json("/api/asks", {**ASK, "diff": DIFF, "sealed": {"open.txt": "x"}}, self.local())[0], 400) + self.assertEqual(sorted(path.name for path in (self.state / "pages").glob("*-sealed.json")), []) + + def test_a_signed_reveal_answers_exactly_the_requested_paths_per_file_and_per_item_and_logs_no_content(self): + signer = self.pin() + for kind, item_id in (("review", self.review()), ("ask", self.ask(60))): + with self.subTest(kind=kind): + status, _, body = self.reveal(kind, item_id, ["app.py"], self.reveal_proof(kind, item_id, ["app.py"], signer)) + self.assertEqual((status, json.loads(body)), (200, {"kind": kind, "id": item_id, "patches": {"app.py": APP_CLEAR}}), body) + status, _, body = self.reveal(kind, item_id, ["conf.py", "app.py"], self.reveal_proof(kind, item_id, ["app.py", "conf.py"], signer)) + self.assertEqual((status, json.loads(body)["patches"]), (200, SEALED), body) + rows = [row for row in self.events("kinds=approval")["events"] if row["title"].startswith("Reveal ")] + self.assertEqual([row["text"] for row in rows], ["app.py", "app.py, conf.py"] * 2) + self.assertEqual([row["ref"]["verb"] for row in rows], ["reveal"] * 4) + self.assert_no_clear_text(json.dumps(rows).encode("utf-8")) + + def test_a_reveal_without_a_pinned_unexpired_unused_signature_over_exactly_its_paths_answers_4xx_without_content(self): + signer = self.pin() + review_id = self.review() + one = self.reveal_proof("review", review_id, ["app.py"], signer) + stranger = self.reveal_proof("review", review_id, ["app.py"], key(-7, "stranger")) + cases = { + "no proof": (["app.py"], None, 400), + "other paths": (["app.py", "conf.py"], one, 400), + "unpinned key": (["app.py"], stranger, 400), + "unsealed path": (["open.txt"], one, 400), + "repeated path": (["app.py", "app.py"], one, 400), + } + for case, (paths, proof, expected) in cases.items(): + with self.subTest(case=case): + status, _, body = self.reveal("review", review_id, paths, proof) + self.assertEqual(status, expected, body) + self.assert_no_clear_text(body) + self.assertEqual(self.reveal("review", review_id, ["app.py"], one)[0], 200) + status, _, body = self.reveal("review", review_id, ["app.py"], one) + self.assertEqual(status, 400, body) + self.assert_no_clear_text(body) + self.assertEqual(self.post_json(f"/api/diffs/review/{review_id}/reveal/sign", {"paths": ["open.txt"]}, self.guard(self.host))[0], 400) + self.assertEqual(self.reveal("review", "0123456789abcdef", ["app.py"], one)[0], 404) + expired = self.reveal_proof("review", review_id, ["app.py"], signer) + later = time.time() + approving.signing.LIFETIME + 60 + with mock.patch.object(approving, "time", mock.Mock(time=lambda: later)): + self.assertEqual(self.reveal("review", review_id, ["app.py"], expired)[0], 400) + self.assertEqual(self.reveal("review", review_id, ["app.py"], expired)[0], 200) + plain = self.request("feat: nothing sealed", "") + self.assertEqual(self.post_json(f"/api/diffs/review/{plain}/reveal/sign", {"paths": ["app.py"]}, self.guard(self.host))[0], 404) + + def test_the_sealed_file_is_deleted_after_the_grant_the_denial_or_the_expiry_and_a_reveal_then_conflicts(self): + signer = self.pin() + granted, rejected = self.review(), self.review() + proven = self.proven([self.entry("review", granted, "", "")], signer) + self.assertEqual(self.call("POST", f"/reviews/{granted}/approve", proven, self.guard(self.host))[0], 200) + self.assertEqual(self.call("POST", f"/reviews/{rejected}/reject", b"", self.guard(self.host))[0], 200) + denied = self.ask(60) + self.assertEqual(self.call("POST", f"/api/asks/{denied}/deny", b"", self.guard(self.host))[0], 200) + waited, swept = self.ask(0), self.ask(0) + self.assertEqual(self.ask_status(waited, "0"), "expired") + self.assertFalse(self.sealed("ask", waited).exists()) + self.assertTrue(self.sealed("ask", swept).exists()) + self.snapshot() + for kind, item_id in (("review", granted), ("review", rejected), ("ask", denied), ("ask", waited), ("ask", swept)): + with self.subTest(kind=kind, item=item_id): + self.assertFalse(self.sealed(kind, item_id).exists()) + status, _, body = self.post_json(f"/api/diffs/{kind}/{item_id}/reveal/sign", {"paths": ["app.py"]}, self.guard(self.host)) + self.assertEqual(status, 409, body) + self.assert_no_clear_text(body) + + def test_a_reveal_of_a_decided_item_drops_a_sealed_file_left_behind(self): + review_id = self.review() + left = self.sealed("review", review_id).read_bytes() + self.assertEqual(self.call("POST", f"/reviews/{review_id}/reject", b"", self.guard(self.host))[0], 200) + self.sealed("review", review_id).write_bytes(left) + self.assertEqual(self.post_json(f"/api/diffs/review/{review_id}/reveal/sign", {"paths": ["app.py"]}, self.guard(self.host))[0], 409) + self.assertFalse(self.sealed("review", review_id).exists()) + + +if __name__ == "__main__": + unittest.main()