From 11554b759a440007727b6d0f8e0625332b4024a0 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 23 Nov 2026 14:09:11 -0700 Subject: [PATCH] Item 3: an agent cannot approve its own proposals: the before-tool hook refuses sisi approve in any wrapping; a decision needs the typed word read from the controlling terminal; refused attempts are recorded with the session. done / next: items 4 and 9 (backlog and pending-approval statuses, main-repo origin) Co-Authored-By: Claude Sonnet 5.5 --- SECURITY.md | 48 ++++++--- sisi/cycle.py | 111 ++++++++++++++++++-- sisi/watch.py | 36 +++++++ tests/support.py | 45 +++++++++ tests/test_cycle_hardening.py | 185 +++++++++++++++++++++++++++++++++- tests/test_learning_loop.py | 6 +- tests/test_personal.py | 2 +- 7 files changed, 405 insertions(+), 28 deletions(-) diff --git a/SECURITY.md b/SECURITY.md index f630332..36a36cd 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -28,18 +28,41 @@ limited, and the rest waits for you: own trusted project, a risk pattern that lowers a profile, any replaced or disabled rule, and rule text or a library entry from a learning confined to one project. Protected rules (`canon/protected.json`) cannot be changed by a cycle or by `sisi overlay` at all, and if that file cannot be read, no rule can be changed. -- Replaying a command check runs it, so a replay happens only in a repository on the trust list, only if the - command exists at both commits, and only if the earlier commit is an ancestor of the later one. +- A cycle file is untrusted input, and `sisi cycle check` never executes anything from it. `sisi cycle apply` + replays a command check only in a repository on the trust list, only between two commits taken from the learning's + own record (its unit's start and closing commits, or the ones stored with the finding; never from the file), only + if the command names a path that differs between the two trees, and only inside an exported copy of each + commit's tree: no `.git` directory (the command cannot read history), an environment of `PATH`, `HOME` (an empty + directory) and `LANG` only (none of the caller's credentials or tokens), a time limit, and the copy deleted + afterwards. A learning with no recorded commits cannot be validated: its command check waits for you as + `unvalidated`. What this does not do: the replayed command still runs as you, with your file permissions, and + sisi does not sandbox it; which is why a command that was not seen to fail and then pass waits for you. `sisi approve` lists every waiting change in full (the exact command, the pattern and profile, the rule text as a -diff) and decides it with `--yes` or `--no`. What this does guarantee: a decision from a process with no terminal -(an agent, a script, a pipe) is refused, unless your config has `learning.agent_relayed_approval` set (off by -default), and then the record says `relayed by an agent`; every decision is written to `approvals.jsonl` with the -time, the session if known, and whether it came from a terminal. What it does not guarantee: sisi cannot tell a -person at a terminal from a program that allocates one, and anyone who can write your personal data folder can -edit the config, the pending list or the ledger directly. The protection is that an agent is told to show you the -change and to give you the command to type yourself, and that the cycle cannot apply anything consequential on its -own; it is not a defence against code that already runs with your permissions. +diff). Deciding one takes two layers: + +- The before-tool hook refuses any shell command an agent runs whose text invokes `sisi approve` with an argument, + however it is wrapped (`script`, `sh -c`, `env`, `expect`, a path to the launcher, `python -m sisi`, quotes and + backslashes inside the words), for main threads and workers alike, and tells the agent that approval is yours + alone and to show you the command to run yourself. The plain listing (`sisi approve` with no arguments) is + allowed. The hook fails open on an error, so it is a guard, not a wall: a command built from pieces, from a + variable or from an encoded string is not recognised by its text. +- `sisi approve ID --yes|--no` itself prints the whole action and a short random word on the controlling terminal + device (`/dev/tty`, never standard input) and decides only when that word is typed back there. A process with no + controlling terminal is refused; so is one whose terminal returns the wrong word or nothing. This is the typed + word, and it is what `script -qec "sisi approve ID --yes" /dev/null cpus: case.skipTest("machine overloaded (load %.0f on %d processors): speed not measured" % (load, cpus)) + + +class FakeTerminal: + """What `sisi approve` reads its decision from. Writes are kept; the line it answers with is the + word the prompt asked for (or `typed`, to type something else, or None to type nothing).""" + + def __init__(self, typed="the word"): + self.out, self.typed = [], typed + + def write(self, text): + self.out.append(text) + + def flush(self): + pass + + def readline(self): + if self.typed is None: + return "" + if self.typed != "the word": + return self.typed + "\n" + import re + m = re.search(r"Type the word (\w+)", "".join(self.out)) + return (m.group(1) if m else "") + "\n" + + def close(self): + pass + + @property + def text(self): + return "".join(self.out) + + +def terminal(typed="the word"): + """Patch: the process has a controlling terminal, and the owner types `typed` at it.""" + from unittest import mock + t = FakeTerminal(typed) + p = mock.patch("sisi.cycle._open_terminal", return_value=t) + p.fake = t + return p diff --git a/tests/test_cycle_hardening.py b/tests/test_cycle_hardening.py index d246aae..0ea3d0b 100644 --- a/tests/test_cycle_hardening.py +++ b/tests/test_cycle_hardening.py @@ -3,7 +3,7 @@ Each test is an abuse case: the cycle file's author (an agent, untrusted) tries to get something to run, validate or take effect that nobody approved. """ -import json, os, subprocess, sys +import json, os, subprocess, sys, unittest from support import SisiCase from test_learning_loop import Base, PROMOTE, FINDING, CAUSE, _contract @@ -130,3 +130,186 @@ class Replay(Base): r = self.apply([self.cmd_action("grep -q fixed f.txt")]) self.assertEqual(r.returncode, 0, r.stderr) self.assertEqual(self.rows()[0]["validation"]["result"], "validated") + + +def _shell_payload(cmd, sid="sess-hook"): + return {"hook_event_name": "PreToolUse", "tool_name": "Bash", "tool_input": {"command": cmd}, + "session_id": sid, "cwd": os.getcwd()} + + +class Approval(Base): + """R-11.5-11, R-14.3-2, R-16-11, R-16-8 (item 3): an agent cannot approve its own proposals.""" + + def park(self): + _contract(self) + self.git_init() + self.retro(*PROMOTE[1:]) + self.apply([{"action": "promote", "learning": "L-0001", "mechanism_kind": "command-check", + "cmd": "make deploy --force-everything"}]) + self.assertEqual(len(self.pending()), 1) + + def hook(self, cmd, sid="sess-hook"): + """Run the before-tool hook on a shell command. Returns the exit code the hook ends with.""" + import contextlib, io + from sisi import watch + try: + with contextlib.redirect_stderr(io.StringIO()): + watch.before(_shell_payload(cmd, sid)) + except SystemExit as e: + return e.code + return 0 + + def test_the_before_tool_hook_refuses_every_wrapping_of_approve(self): + """R-14.3-2 (item 3a): script, sh -c, env, expect, a path to the launcher and python -m sisi.""" + self.park() + wrapped = [ + 'script -qec "sisi approve P-0001 --yes" /dev/null