diff --git a/README.md b/README.md index 8dbd875..a2e87e9 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Open `http://127.0.0.1:8766/`. Save the credentials printed by `init` somewhere 3. Connect as **scheduler** and choose **Propose next compiled step**. `record_review` is proposed with its argument bound to the verified inventory observation. Execute that proposal. The review becomes a case event. 4. Propose the next compiled step. `issue_order` receives the observed part number, quote, price, quote ID, and quote version. The decision requests independent manager approval. 5. Connect as **manager**, select the proposal from case history, and approve it. Reconnect as **scheduler**, select the same proposal, and execute. SyberWork submits a real POST to the reference source's `/orders` with an `Idempotency-Key` and `If-Match` header. The order is stored in the *other* database. You can fetch it from `http://127.0.0.1:8999/orders/{id}` using the ID in the effect response. -6. Reconnect as **manager** and sign off. The case becomes complete only after the order effect and the later signoff. **Verify history** checks the per-case hash chain. +6. Reconnect as **manager** and sign off. The case becomes complete only after the order effect and the later signoff. **Verify history** checks the per-case hash chain, and the witness's signed heads when a witness log is configured. The **Contract & connector studio** accepts immutable contract and policy JSON documents. The compiler produces a dependency-ordered known path when one is not authored, plus inspectable action gates, acceptance queries, and an input form schema. Publish a higher version, then use **Amendment replay** to compare decisions against the recorded history. Replay makes no model calls, destination writes, or live source reads. @@ -93,7 +93,7 @@ This output only creates a proposal. Admission rechecks the bound observation, g - `POST /api/contracts`, `/api/policies`, `/api/actions/{name}`, `/api/sources/{name}`: administrator publication. - `POST /api/cases`: create a case pinned to a contract version. - `POST /api/cases/{id}/refresh`: fetch and record a versioned fact from a configured source. -- `POST /api/cases/{id}/facts`: record a manually asserted fact, tagged as unverified. +- `POST /api/cases/{id}/facts`: record a manually asserted fact, tagged as unverified. It is refused (`attestation_denied`) for a key the contract reads (a required fact, an argument binding, a fact acceptance clause, or a resolution's evidence), unless a required fact names this same source and does not ask for verification. A key the contract never reads is recorded and decides nothing. - `POST /api/cases/{id}/resolution-request`, `/resolution-resolve`, `/resolution-escalate`, `/cancel`: open a source-backed decision task, verify its authoritative resolution, escalate it after its deadline, or close the case explicitly. - `POST /api/cases/{id}/proposals`, `/compiled`, `/suggest`: human, compiled, or model proposal. - `POST /api/cases/{id}/candidates`, `/searches` (search or operator credential), `/evaluations` (evaluator credential); `GET /api/cases/{id}/candidates`: provisional candidates for a contract with an `evolution` section. See [docs/EVOLUTION.md](docs/EVOLUTION.md). @@ -128,7 +128,7 @@ A versioned contract can declare a resolution with an owner role, escalation rol ## Deployment boundary -The included service is local and single-installation. Its role-bearing tokens are provisioned locally. It does not provide SSO, multi-tenant isolation, a distributed transaction with an external API, a customer-specific semantic layer, or an automatic translation from arbitrary prose into a correct contract. The interface requires human review of published contracts. Preserve the `.syberwork` database and credentials together when moving the installation. A hash chain detects accidental editing against its local history; it does not prevent a database owner from replacing and rehashing the whole chain. +The included service is local and single-installation. Its role-bearing tokens are provisioned locally. It does not provide SSO, multi-tenant isolation, a distributed transaction with an external API, a customer-specific semantic layer, or an automatic translation from arbitrary prose into a correct contract. The interface requires human review of published contracts. Preserve the `.syberwork` database and credentials together when moving the installation. A hash chain detects accidental editing against its local history; it does not prevent a database owner from replacing and rehashing the whole chain. When serve reads the witness's log (`SYBERWORK_WITNESS_LOG`, `SYBERWORK_WITNESS_PUBLIC`), Verify history and `syberwork verify` also check the heads the witness signed, which are outside the database, so a history rewritten after a head was signed fails. Events after the newest signed head, and an installation with no witness log, keep the chain's limit, and the verify response says which applies. A cell is the deployment that comes after that appliance: one organization, one database, one runtime. `Work` talks to SQLite or PostgreSQL through the same stores. `syberwork worker` finishes effects that were claimed and then interrupted. `syberwork backup` and `restore` move the cell. The loopback default remains. Set `SYBERWORK_BIND` only when the process should listen on another address. Details and the limits are in [Industrial Cell v0.1](docs/INDUSTRIAL_CELL.md). diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index e4efe80..53dbf73 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -34,6 +34,6 @@ For each past proposal, replay evaluates a selected new contract and policy agai ## Integration -HTTP sources and effects are bound by administrator-installed definitions and do not accept an LLM-provided destination URL. Authorization headers use environment variable names in definitions; secrets never enter the contract. Cross-origin redirects are blocked. Source URLs and the planner URL go through `trusted_origin`. HTTPS must name a public host; loopback HTTP stays `127.0.0.1` with a port. `guard_request` checks the addresses HTTPS resolves to before the call and refuses a name it cannot resolve; a host with percent-escapes or other characters `urllib.request` would rewrite is refused before it is classified. The credentialed openers ignore `http_proxy`/`https_proxy` (`targets.credentialed_opener`), so a bearer token is never handed to an environment proxy, and their HTTPS connections are pinned: each resolves and checks its host again and dials only the addresses it accepted, keeping the name for SNI and Host. IANA special-purpose ranges are refused explicitly rather than by `is_global` alone. Header values (`If-Match` from `version_arg`) are refused if they hold a line break, control character or edge whitespace, before the effect is claimed. A manual attestation (`POST /api/cases/{id}/facts`) may not set a key the case's contract reads from another source or only verified, and an `observer`-only credential reads no case, contract, certificate, metric or builder view. The console document refuses framing (`frame-ancestors 'none'`). The signing key for a chain head is not stored in the case database. `python -m syberlabs.witness` holds that seed in a separate process, checks the chain, and appends to the transparency log. `WitnessClient` keeps the public key and opens the log read-only. The log refuses to open a file that already has a `cases` table. A process running as the same OS user can still open the file writable, and an operator who holds the seed can rebuild the log. It is not a public transparency service. Model suggestions are proposals accepted under separate model-bearing credentials, never direct network effects. The included `reference_system.py` is one fully running integration. `translate_stabilize` is a tested translator for one SyberRuntime outcome, not a live adapter. Barn, OmniOS, and customer tools still need adapters matching their authority and event semantics. The other rows in `spec/MAPPINGS.md` stay proposed. +HTTP sources and effects are bound by administrator-installed definitions and do not accept an LLM-provided destination URL. Authorization headers use environment variable names in definitions; secrets never enter the contract. Cross-origin redirects are blocked. Source URLs and the planner URL go through `trusted_origin`. HTTPS must name a public host; loopback HTTP stays `127.0.0.1` with a port. `guard_request` checks the addresses HTTPS resolves to before the call and refuses a name it cannot resolve; a host with percent-escapes or other characters `urllib.request` would rewrite is refused before it is classified. The credentialed openers ignore `http_proxy`/`https_proxy` (`targets.credentialed_opener`), so a bearer token is never handed to an environment proxy, and their HTTPS connections are pinned: each resolves and checks its host again and dials only the addresses it accepted, keeping the name for SNI and Host. IANA special-purpose ranges are refused explicitly rather than by `is_global` alone. Header values (`If-Match` from `version_arg`) are refused if they hold a line break, control character or edge whitespace, before the effect is claimed. A manual attestation (`POST /api/cases/{id}/facts`) may not set a key the case's contract reads from another source or only verified, and an `observer`-only credential reads no case, contract, certificate, metric or builder view. The console document refuses framing (`frame-ancestors 'none'`). The signing key for a chain head is not stored in the case database. `python -m syberlabs.witness` holds that seed in a separate process, checks the chain, and appends to the transparency log. `WitnessClient` keeps the public key and opens the log read-only. The log refuses to open a file that already has a `cases` table. A process running as the same OS user can still open the file writable, and an operator who holds the seed can rebuild the log. It is not a public transparency service. Verify history (`Work.verify_history`: `GET /api/cases/{id}/verify`, `syberwork verify`) reads that log when one is configured: `valid` is false when a signed head conflicts with the case (`head_differs`, `beyond_history`, `chain_broken`), so a history the database owner rewrote and rehashed after signing fails. The response carries `chain`, `witness` (`witnessed_through`, `count`, `conflicts`, or null without a log) and a note saying what the check could not see; events after the newest signed head are covered by the chain alone. Model suggestions are proposals accepted under separate model-bearing credentials, never direct network effects. The included `reference_system.py` is one fully running integration. `translate_stabilize` is a tested translator for one SyberRuntime outcome, not a live adapter. Barn, OmniOS, and customer tools still need adapters matching their authority and event semantics. The other rows in `spec/MAPPINGS.md` stay proposed. The [`economic_http` effect](ECONOMIC_ACTIONS.md) adds exact atomic units and a cross-case budget reservation to this same admission boundary. Its trusted settlement service is a separate authority; SyberWork records that service's matching receipt and status assertion, not independent blockchain finality. diff --git a/docs/console/NOTES.md b/docs/console/NOTES.md index 539a9c0..8154ea5 100644 --- a/docs/console/NOTES.md +++ b/docs/console/NOTES.md @@ -102,7 +102,7 @@ tab or choose **Forget all** to drop them. | 7 | manager | **Approve** (press twice) | The armed button names the action, the role, and the exact arguments the approval binds to. | | 8 | scheduler | **Execute** (press twice) | A real POST to the reference system with `Idempotency-Key` and `If-Match`; *Applied · destination acknowledged HTTP 201*. Only the original proposer can execute; anyone else gets *only the original proposer (scheduler) may execute* with a switch link. | | 9 | manager | **Sign off as manager** | The sign-off is offered only after `issue_order` has applied, because the server accepts an earlier signature but it never satisfies the clause. The case becomes *Complete*. | -| 10 | any | History (`5`), then `v` | The server's `/verify` result is recorded with the head it covered (`intact through #13 …`). It is marked stale when newer events arrive. | +| 10 | any | History (`5`), then `v` | The server's `/verify` result is recorded with the head it covered (`intact through #13 …`). It is marked stale when newer events arrive. With a witness log configured, `/verify` is also invalid when a signed head conflicts with the history (`Work.verify_history`). | | 11 | manager | History → Witness → **Ask witness to sign** (press twice) | Before: *not witnessed*. Another credential sees the button disabled with *manager role required to ask the witness* and **⇄ switch to manager** (the role is `SYBERWORK_WITNESS_ROLE`). The armed note says the witness appends a leaf that cannot be removed and nothing is written to the case. After: *Witness signed #1–#13*, the panel reads *witnessed through #13* with the leaf, its signed head and *in the log*, and the button is disabled: *a signed head in the log already covers #1–#13*. An event added later is listed as *not witnessed*, and asking again signs the longer history. Without a configured log, **Witness** reads *not configured* and names the variables. | If the destination is down at step 8, the effect becomes *Outcome unknown · check destination*, the diff --git a/syberlabs/publish.py b/syberlabs/publish.py index bd0a1bb..00405d3 100644 --- a/syberlabs/publish.py +++ b/syberlabs/publish.py @@ -37,6 +37,7 @@ from syberlabs import critic, outcome from syberlabs.checks import _stop from syberlabs.errors import Rejected +from syberlabs.gitspace import Repo from syberlabs.session import NoWrite from syberlabs.targets import credentialed_opener, guard_request, trusted_origin @@ -189,6 +190,31 @@ def validate(doc: dict) -> None: raise _bad(f"unknown field {sorted(unknown)[0]} for {effect}") +def _bounded(argv: list[str], env: dict, timeout: float) -> tuple[int | None, str]: + """Run ``argv`` to completion or to ``timeout``; ``(None, "")`` when the deadline ended it. + + The command runs in its own session (POSIX) and a timeout ends that session, or the process tree on + Windows, through ``checks._stop``: ``subprocess.run(timeout=...)`` ends ``git`` and leaves a transport + it started, such as ``ssh``, running past the publish. Output is read from a file, never a pipe, so a + child that keeps the pipe open cannot hold the call past its deadline. + """ + with tempfile.TemporaryFile() as out: + proc = subprocess.Popen(argv, env=env, stdin=subprocess.DEVNULL, stdout=out, stderr=subprocess.DEVNULL, + start_new_session=hasattr(os, "setsid")) + deadline = time.monotonic() + timeout + while proc.poll() is None: + if time.monotonic() > deadline or os.fstat(out.fileno()).st_size > MAX_OUTPUT: + _stop(proc) + return None, "" + time.sleep(0.02) + out.seek(0) + return proc.returncode, out.read(MAX_OUTPUT).decode(errors="replace") + + +def _git_env() -> dict: + return {**os.environ, "GIT_TERMINAL_PROMPT": "0", "LC_ALL": "C"} + + class _Publisher: """Shared plumbing: the thread a proposal belongs to, and bounded subprocesses.""" @@ -211,8 +237,12 @@ def _lease(self, case_id: str) -> str: return "" if "{thread}" in self.doc.get("branch", "syberlabs/{thread}") else self._thread(case_id).base def _remote_head(self, ref: str) -> str | None: - raw = self.kit.repo.git("ls-remote", self.doc["remote"], ref, timeout=self.timeout) - for line in raw.decode().splitlines(): + # The same argv and scrubbed environment as ``Repo.git``, under a deadline that ends the transport too. + code, text = _bounded(["git", "-C", str(self.kit.repo.root), "-c", "core.fsmonitor=false", + "ls-remote", self.doc["remote"], ref], Repo._env(), self.timeout) + if code != 0: + raise RuntimeError("ls-remote did not finish") + for line in text.splitlines(): commit, _, name = line.partition("\t") if name == ref: return commit @@ -222,9 +252,11 @@ def apply(self, case_id: str, args: dict, key: str) -> dict: ref = "refs/heads/" + self._branch(case_id) argv = ["git", "-C", str(self.kit.repo.root), "push", "--porcelain", f"--force-with-lease={ref}:{self._lease(case_id)}", self.doc["remote"], f"{args['commit']}:{ref}"] - done = subprocess.run(argv, capture_output=True, text=True, timeout=self.timeout, - env={**os.environ, "GIT_TERMINAL_PROMPT": "0", "LC_ALL": "C"}) - flags = [line.split("\t") for line in done.stdout.splitlines() if "\t" in line] + code, text = _bounded(argv, _git_env(), self.timeout) + if code is None: + # The deadline ended the push and everything it started; the remote may or may not have the ref. + raise RuntimeError("push outcome unknown") + flags = [line.split("\t") for line in text.splitlines() if "\t" in line] mine = next((f for f in flags if len(f) >= 2 and f[1].endswith(":" + ref)), None) if mine and mine[0] in ("*", " ", "="): return {"destination": f"git:{self.doc['remote']}:{ref}", "external_id": args["commit"]} diff --git a/syberwork/cli.py b/syberwork/cli.py index fc612c8..05bee26 100644 --- a/syberwork/cli.py +++ b/syberwork/cli.py @@ -225,7 +225,7 @@ def main(): elif args.command == "inspect": print(json.dumps(db.inspect(args.case_id), indent=2)) elif args.command == "verify": - print(json.dumps({"valid": db.verify_chain(args.case_id)})) + print(json.dumps(db.verify_history(args.case_id, _witness_log(parser)))) if __name__ == "__main__": diff --git a/syberwork/core.py b/syberwork/core.py index ef1e3d3..39f83c4 100644 --- a/syberwork/core.py +++ b/syberwork/core.py @@ -1237,6 +1237,34 @@ def verify_chain(self, case_id: str) -> bool: with self.tx() as db: return self.chain_ok(db, case_id) + def verify_history(self, case_id: str, log=None) -> dict: + """``GET /api/cases/{id}/verify`` and ``syberwork verify``: the hash chain, and the witness's log when one is read. + + The chain alone (``chain``) catches an edit that was not rehashed. Whoever owns the database can rewrite an + event and rehash every event after it, and the chain still verifies. A head the witness signed is outside the + database, under a key the database owner does not hold, so with ``log`` (``TransparencyLog.open_readonly``) + a rewritten, truncated or rehashed history shows as a conflict (``head_differs``, ``beyond_history``, + ``chain_broken``) and ``valid`` is false. Without a log, or for events after ``witnessed_through``, ``valid`` + is the chain's answer and the note says what it cannot see. Events are read once, in one transaction. + """ + with self.tx() as db: + chain = self.chain_ok(db, case_id) + events = self._events(db, case_id) + if log is None: + return {"valid": chain, "chain": chain, "witness": None, + "note": "Hash chain only: it does not detect a history rewritten and rehashed by the database owner. " + "Read a witness log (SYBERWORK_WITNESS_LOG, SYBERWORK_WITNESS_PUBLIC) to check against signed heads"} + status = self._witness_judgement(case_id, events, log) + conflicts = [entry for entry in status["entries"] if entry["verdict"] in WITNESS_CONFLICTS] + through = status["witnessed_through"] + note = ("A head the witness signed conflicts with this history: it was changed after signing" if conflicts else + f"Chain checked, and events #1 to #{through} match a head the witness signed" if through else + "Chain checked; the witness has signed no head of this case yet, so a rehash would not show") + return {"valid": chain and not conflicts, "chain": chain, + "witness": {"witnessed_through": through, "count": status["count"], + "conflicts": [{"index": e["index"], "count": e["count"], "verdict": e["verdict"]} for e in conflicts]}, + "note": note} + def side_channel(self, case_id: str) -> list[dict]: """JCS digest and deciding rule id for each new event. Neither is in the event hash. diff --git a/syberwork/server.py b/syberwork/server.py index fe91548..843393d 100644 --- a/syberwork/server.py +++ b/syberwork/server.py @@ -285,7 +285,8 @@ def do_GET(self): elif len(parts) == 4 and parts[:2] == ["api", "cases"] and parts[3] == "candidates": self._json(work.candidates(parts[2])) elif len(parts) == 4 and parts[:2] == ["api", "cases"] and parts[3] == "verify": - self._json({"valid": work.verify_chain(parts[2])}) + # The chain, and the witness's signed heads when serve reads its log (Work.verify_history). + self._json(work.verify_history(parts[2], witness_log)) elif len(parts) == 4 and parts[:2] == ["api", "cases"] and parts[3] == "side": # Work.side_channel: the JCS digest and deciding rule id stored beside each event. Neither is # in the event hash; events written before that table existed have no row. diff --git a/tests/test_console.py b/tests/test_console.py index 32c0e9f..62d3440 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -30,6 +30,7 @@ from syberlabs.admission import approval_roles from syberlabs.build import Kit from syberlabs.canonical import canonical +from syberlabs.events import GENESIS, event_digest from syberlabs.certificate import certify, write_certificate from syberlabs.dsse import CHAIN_HEAD_TYPE, CHECKPOINT_TYPE, sign_chain_head, sign_dsse from syberlabs import ed25519 @@ -615,19 +616,20 @@ def test_the_route_reports_a_stored_digest_as_stored_and_verify_judges_it(self): db.execute("UPDATE event_side SET jcs=? WHERE case_id=? AND seq=1", ("0" * 64, case)) rows = self.cell.json(f"/api/cases/{case}/side")[1] self.assertEqual(rows[0]["jcs"], "0" * 64) - self.assertEqual(self.cell.json(f"/api/cases/{case}/verify"), (200, {"valid": False})) + status, body = self.cell.json(f"/api/cases/{case}/verify") + self.assertEqual((status, body["valid"], body["chain"]), (200, False, False)) with connect_db(self.cell.database) as db: db.execute("DELETE FROM event_side WHERE case_id=? AND seq=1", (case,)) self.assertEqual([row["seq"] for row in self.cell.json(f"/api/cases/{case}/side")[1]][:1], [2]) - self.assertEqual(self.cell.json(f"/api/cases/{case}/verify")[1], {"valid": True}, - "verify cannot notice a missing row, which is why the console says so instead of calling it legacy") + self.assertTrue(self.cell.json(f"/api/cases/{case}/verify")[1]["valid"], + "verify cannot notice a missing row, which is why the console says so instead of calling it legacy") # Nor does verify check a row with no digest, or a rule id: the console presents neither as checked. decision = next(e["seq"] for e in self.cell.work.inspect(case)["events"] if e["kind"] == "decision") with connect_db(self.cell.database) as db: db.execute("UPDATE event_side SET jcs=NULL, rule='invented.rule' WHERE case_id=? AND seq=?", (case, decision)) row = next(r for r in self.cell.json(f"/api/cases/{case}/side")[1] if r["seq"] == decision) self.assertEqual((row["jcs"], row["rule"]), (None, "invented.rule")) - self.assertEqual(self.cell.json(f"/api/cases/{case}/verify")[1], {"valid": True}) + self.assertTrue(self.cell.json(f"/api/cases/{case}/verify")[1]["valid"]) app = read_text(STATIC / "app.js") for wording in ("it cannot notice a missing row", "Verify history does not check a row without a digest", "the rule ids are in no hash and nothing checks them", "only Verify history tells them apart"): @@ -718,10 +720,33 @@ def test_a_history_changed_after_signing_is_not_witnessed(self): self.assertEqual(self.cell.json(f"/api/cases/{case}/witness")[1]["witnessed_through"], len(events)) with closing(sqlite3.connect(self.cell.database)) as db, db: db.execute("UPDATE events SET body=? WHERE case_id=? AND seq=1", (canonical({"tampered": True}), case)) - self.assertEqual(self.cell.json(f"/api/cases/{case}/verify")[1], {"valid": False}) + verify = self.cell.json(f"/api/cases/{case}/verify")[1] + self.assertEqual((verify["valid"], verify["chain"], [c["verdict"] for c in verify["witness"]["conflicts"]]), + (False, False, ["chain_broken"])) body = self.cell.json(f"/api/cases/{case}/witness")[1] self.assertEqual(([e["verdict"] for e in body["entries"]], body["witnessed_through"]), (["chain_broken"], None)) + def test_a_rehashed_history_passes_the_chain_but_not_verify(self): + """Security review follow-up: the database owner rewrites event #1 and rehashes every event after it. The + chain verifies, which is the limit of a hash chain; Verify history reads the witness's log and refuses it.""" + case, events = self.case_events() + self.attest(events) + self.assertEqual(self.cell.json(f"/api/cases/{case}/verify")[1]["witness"]["witnessed_through"], len(events)) + with closing(sqlite3.connect(self.cell.database)) as db, db: + db.execute("UPDATE events SET body=? WHERE case_id=? AND seq=1", (canonical({"tampered": True}), case)) + db.execute("DELETE FROM event_side WHERE case_id=?", (case,)) + previous = GENESIS + with closing(sqlite3.connect(self.cell.database)) as db, db: + for event in self.cell.work.inspect(case)["events"]: + event = dict(event, previous=previous) + previous = event_digest(event) + db.execute("UPDATE events SET previous=?, hash=? WHERE case_id=? AND seq=?", (event["previous"], previous, case, event["seq"])) + self.assertTrue(self.cell.work.verify_chain(case), "a rehashed chain verifies on its own") + status, body = self.cell.json(f"/api/cases/{case}/verify") + self.assertEqual((status, body["valid"], body["chain"]), (200, False, True)) + self.assertEqual([c["verdict"] for c in body["witness"]["conflicts"]], ["head_differs"]) + self.assertIsNone(body["witness"]["witnessed_through"]) + def test_an_unreadable_leaf_is_skipped_not_a_crash(self): case, events = self.case_events() self.attest(events) diff --git a/tests/test_security_review.py b/tests/test_security_review.py index ed94033..e71f912 100644 --- a/tests/test_security_review.py +++ b/tests/test_security_review.py @@ -10,6 +10,7 @@ import json import os import socket +import subprocess import sys import tempfile import threading @@ -17,7 +18,7 @@ import unittest import urllib.error import urllib.request -from contextlib import contextmanager +from contextlib import closing, contextmanager from pathlib import Path from unittest.mock import patch @@ -357,6 +358,109 @@ class Kit: self.assertEqual(beat.stat().st_size if beat.exists() else 0, size, "the grandchild kept running") +class GitPushTimeoutEndsTheTree(unittest.TestCase): + """Follow-up: ``GitPush`` ran ``git push`` under ``subprocess.run(timeout=...)``, which ends ``git`` and + leaves the transport it started (``ssh``) running. Its timeout now ends the whole session, as a publish + command's and a check's do, for the push and for the ``ls-remote`` status read.""" + + @unittest.skipUnless(hasattr(os, "killpg"), "the process-group path is POSIX; Windows uses taskkill /T") + def test_a_timed_out_push_or_status_leaves_no_transport_running(self): + from syberlabs.gitspace import Repo + from syberlabs.publish import GitPush + + for step in ("apply", "status"): + with self.subTest(step=step), tempfile.TemporaryDirectory() as folder: + root, beat = Path(folder) / "repo", Path(folder) / "beat" + child = ("import time,pathlib\n" + f"p=pathlib.Path({str(beat)!r})\n" + "while True:\n p.open('a').write('.')\n time.sleep(0.05)\n") + transport = Path(folder) / "transport.py" + transport.write_text("import subprocess,sys,time\n" + f"subprocess.Popen([sys.executable,'-c',{child!r}])\ntime.sleep(60)\n") + git = lambda *a: subprocess.run(["git", "-C", str(root), *a], check=True, capture_output=True) + root.mkdir() + git("init", "-q") + git("-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "--allow-empty", "-m", "c") + git("remote", "add", "origin", "ssh://example.invalid/repo.git") + # A stand-in for ssh that starts a child of its own and never answers. + git("config", "core.sshCommand", f"{Path(sys.executable).as_posix()} {transport.as_posix()}") + commit = git("rev-parse", "HEAD").stdout.decode().strip() + + class Kit: + repo = Repo(root) + + push = GitPush(Kit(), {"remote": "origin", "timeout_seconds": 1}) + started = time.monotonic() + # Either way the outcome is unknown; what this test checks is what is left running. + with self.assertRaises((RuntimeError, subprocess.TimeoutExpired)): + getattr(push, step)("abcdef0123", {"commit": commit}, "k") + self.assertLess(time.monotonic() - started, 30) + time.sleep(0.3) + size = beat.stat().st_size if beat.exists() else 0 + time.sleep(0.6) + self.assertEqual(beat.stat().st_size if beat.exists() else 0, size, "the transport's child kept running") + + +class HistoryVerifiesAgainstTheWitness(unittest.TestCase): + """Follow-up: the hash chain does not stop a database owner who rewrites and rehashes history. Verify history + (``Work.verify_history``, ``GET /verify``, ``syberwork verify``) now also reads the witness's log, whose signed + heads are outside the database, so a rehash after a head was signed fails verification.""" + + def _rehash(self, database, work, case, seq): + import sqlite3 + from syberlabs.canonical import canonical + from syberlabs.events import GENESIS, event_digest + with closing(sqlite3.connect(database)) as db, db: + db.execute("UPDATE events SET body=? WHERE case_id=? AND seq=?", (canonical({"rewritten": True}), case, seq)) + db.execute("DELETE FROM event_side WHERE case_id=?", (case,)) + previous = GENESIS + with closing(sqlite3.connect(database)) as db, db: + for event in work.inspect(case)["events"]: + event = dict(event, previous=previous) + previous = event_digest(event) + db.execute("UPDATE events SET previous=?, hash=? WHERE case_id=? AND seq=?", (event["previous"], previous, case, event["seq"])) + + def test_a_rehash_after_signing_fails_and_the_limit_is_stated_without_a_log(self): + from syberlabs.ed25519 import generate + from syberlabs.tlog import TransparencyLog + from syberlabs.witness import Witness + from tests.test_external_witness import _install_note + + seed, public = generate() + # Registered first, so it runs last: Windows deletes an SQLite file only after every handle is closed. + scratch = tempfile.TemporaryDirectory() + self.addCleanup(scratch.cleanup) + folder = scratch.name + database = Path(folder) / "case.sqlite" + work = Work(database) + self.addCleanup(work.close) + _install_note(work) + case = work.create_case("note", 1, {"name": "a"}, "operator") + work.propose(case, "note", {}, "operator", ["operator"]) + witness = Witness(str(Path(folder) / "witness.sqlite"), seed) + self.addCleanup(witness.close) + log = TransparencyLog.open_readonly(Path(folder) / "witness.sqlite", public) + self.addCleanup(log.close) + + unsigned = work.verify_history(case, log) + self.assertEqual((unsigned["valid"], unsigned["witness"]["witnessed_through"]), (True, None)) + self.assertIn("no head", unsigned["note"]) + count = len(work.inspect(case)["events"]) + witness.attest(work.inspect(case)["events"]) + signed = work.verify_history(case, log) + self.assertEqual((signed["valid"], signed["witness"]["witnessed_through"], signed["witness"]["conflicts"]), + (True, count, [])) + + self._rehash(database, work, case, 1) + self.assertTrue(work.verify_chain(case), "the chain alone cannot see a rehash") + alone = work.verify_history(case) + self.assertEqual((alone["valid"], alone["chain"], alone["witness"]), (True, True, None)) + self.assertIn("rehashed", alone["note"]) + checked = work.verify_history(case, log) + self.assertEqual((checked["valid"], checked["chain"]), (False, True)) + self.assertEqual([c["verdict"] for c in checked["witness"]["conflicts"]], ["head_differs"]) + + class BindAndUsers(unittest.TestCase): """Follow-ups: an empty SYBERWORK_BIND passed the loopback guard, and users.json was read once."""