Skip to content

fix(core): hermetic runs enforce the data handover and clean up their directories (#976, #977) - #987

Merged
jimhoyd merged 19 commits into
mainfrom
claude/hermetic-hardening-976
Sep 30, 2026
Merged

jimhoyd merged 19 commits into
mainfrom
claude/hermetic-hardening-976

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Fixes #976
Fixes #977

#976: a hermetic run can no longer write the site's live data

Core can't see where trusted extension code writes, so it now refuses the two things that could not have agreed to the hermetic obligation.

  • Contract bump. Keeping every file under context.data when hermetic is true is now extension contract 2 (extensionContract = 2). A contract-1 extension (anything built before feat(core,auth,store,audit)!: hermetic test runs with declared seeds; expectJson; resolved captures in failures (#930) #947) is refused wherever contracts are checked: add, list --strict, static validate, defineExtension, and composeHost before its host() runs. Every first-party descriptor and definition moves to 2, along with the fixtures, recipes, the Auth.js proof and the [Request]: Support independent provider packages and standard inert artifacts #844 contract test. The "future contract" fixtures move to 3.
  • Confirmation. composeHost records the data directory it gave every host() on the host it returns, under Symbol.for('urlcode.host.hermeticConfirmation').
    • A hermetic load (test, audit, benchmark, MCP run_tests, or a --local-review validate/routes) refuses a host that has registrations unless that record names its own directory. The refusal code is hermetic-host-unconfirmed.
    • On refusal it closes the host and removes the directory before any request runs.
    • This covers the skew case that the bump alone misses: a host built by an older copy of core's composeHost, or registrations written by hand.
    • It uses a Symbol.for key so a second core copy agrees on it, Object.keys (the host settings check) never lists it, and a host.mjs that spreads the composed host keeps it.
    • It detects version skew, not a hostile host file: host.mjs is trusted operator code.

Why this option. I chose bump plus confirmation over having extensions declare where they write:

  • A declared path list would still rest on the extension's word, just like the contract.
  • It is also brittle, because SQLite creates -wal, -shm and journal siblings.

The bump alone doesn't catch an older composeHost, and the confirmation alone doesn't catch a contract-1 extension composed by this core. Together they close both routes in #976, and they stay generic in core: no package import. Comment drift fixed: HostContext.data/hermetic and LoadOptions.hermetic now include the local review.

Behaviour change. A hand-written host (export default {extensions:[registration]}) still serves, inspects and runs a pinned validate, but a hermetic run refuses it. The fix is to wrap each registration in a defineExtension definition and compose them. proofs/ecosystem/host.mjs and three tests that ran test/run_test with hand-written hosts were converted.

#977: temp directories are cleaned up and swept

  • Run directories (the hermetic data directory, and the per-run URLCODE_DATA_DIR from startRestartable and startServer({isolateData})) are now named urlcode-<hermetic|data>-<pid>-XXXXXX under the OS temp dir. They are created and tracked by packages/core/src/temp-dirs.ts.

  • A process exit hook removes any that are still tracked. process.exit reaches that hook from:

    • the unhandled-rejection path, which now closes the host (bounded) first;
    • the new SIGINT/SIGTERM handler for non-serving commands, which closes the host for at most 3 s and exits 130/143.
  • dev, serve and studio swap that handler for their graceful stop.

  • The MCP runners send SIGTERM and wait 5 s before SIGKILL.

  • Sweep. SIGKILL can't be caught, so the first run directory a process creates first sweeps stale ones. A directory is removed only if all of these hold:

    • its name matches the pattern;
    • lstat shows a real directory, not a link;
    • it is owned by this uid with mode 0700;
    • it has been unmodified for at least an hour;
    • its pid is neither this process nor still running.

    Removal never follows a symlink. The one-hour age guard covers a pid from another pid namespace sharing /tmp.

Docs

RIM-EXT-HERMETIC-001, docs/EXTENSIONS.md (hermetic runs, the extension contract, hand-written hosts), docs/READINESS.md (test data and seeds), SECURITY.md, llms.txt / llms-full.txt, proofs/ecosystem/README.md.

Evidence

  • test/hermetic-hardening.test.ts (new):
    • contract-1 refused before host(), with data/ never created;
    • an unconfirmed host refused, closed and its directory removed;
    • a spread composed host still confirmed;
    • sweep rules (dead/alive pid, young, wrong mode, foreign name, own pid, a symlinked candidate, a link inside a swept dir not followed);
    • a real urlcode test --local-review child with a seeded host killed by SIGINT (130), SIGTERM (143) and ended by an unhandled rejection (1), leaving no urlcode-* directory in its TMPDIR.
  • test/operator-host.test.ts: contract 2 and a contract-1 refusal.
  • npm run verify and npm run test:package pass locally (Node 26, macOS). Core packs 1056122 bytes, inside the fix(store): deterministic operator-audit lease; package budgets as measurement plus margin (#971) #975 budget; no budget edit.

Limitations

  • Not tested: the MCP runner's SIGTERM-then-SIGKILL path (it needs the 120 s timeout).
  • A process killed outright leaves its directories until a later run is at least an hour past it.
  • On Windows the uid/mode checks are skipped, and removing a directory with open SQLite handles can fail until the sweep.
  • This is not an independent security review.

🤖 Generated with Claude Code

… directories (#976, #977)

#976: the hermetic obligation (keep every file under context.data) is now
extension contract 2. A contract-1 extension is refused before its host()
runs. composeHost records on the host it returns, under a Symbol.for key,
the data directory it gave every host(); a hermetic load refuses a host with
registrations that lacks that record for its own directory
(hermetic-host-unconfirmed), so an older core copy's composeHost or
hand-made registrations can no longer run a hermetic check against the
site's live data/. Every first-party descriptor and definition, fixture,
recipe, proof and the #844 contract test move to contract 2; the ecosystem
proof host is composed with composeHost.

#977: run directories are named urlcode-<hermetic|data>-<pid>-XXXXXX and
tracked; an exit hook removes what is left, non-serving commands close the
host and exit 130/143 on SIGINT/SIGTERM, the unhandled-rejection path goes
through the same bounded close, and the MCP runners send SIGTERM with a 5 s
grace before SIGKILL. The first run directory of a process sweeps stale ones:
lstat-checked real 0700 directories owned by this user, an hour old, whose
pid is gone; removal never follows a link.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd enabled auto-merge (squash) September 29, 2026 22:32
Comment thread test/hermetic-hardening.test.ts Fixed
jimhoyd and others added 5 commits September 29, 2026 17:41
)

The sweep test expects the 0755 directory to be swept on win32, where there
is no uid or mode to check; the signal subtests skip on win32, where kill()
ends the process without running handlers; the child's temp directory is
redirected with TEMP/TMP as well as TMPDIR and keeps the system environment
Windows needs. Docs state the Windows limitation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
jimhoyd and others added 6 commits September 29, 2026 19:20
…ectory's newest entry (#976, #977)

#976 review: the hermetic confirmation was a key on the host object, so a
host.mjs that spread or reused a composeHost result could carry a hand-made
registration past it (reproduced writing the site's data/). A hermetic load
now puts a fresh WeakSet in a Symbol.for slot beside the data directory,
composeHost adds each registration it composed, and the load refuses unless
every exported registration is in the set. Docs state it is a correctness
check (host code is trusted), not a security boundary.

#977 review: the one-hour guard read the directory's own mtime, which a
database write inside does not change. The sweep now requires the directory
and everything in it (bounded lstat walk: 1,024 entries, 8 levels, never
following links; kept beyond the bound) to be an hour old. npm test and
test:shard preload test/scratch-tmpdir.ts, which points TMPDIR/TEMP/TMP of
the run and every process it spawns at a scratch directory, so tests never
sweep the developer's real temporary directory.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd

jimhoyd commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator Author

Added 1d410c3 (after merging main at 0017bcc). It fixes the two review findings on #976 and #977.

#976: the confirmation is now checked per registration, not per host object.

  • During a hermetic load, loadOperatorHost puts a fresh WeakSet in a Symbol.for('urlcode.host.hermeticConfirmed') slot beside the data directory.
  • composeHost adds every registration it composed to that set.
  • The load refuses (hermetic-host-unconfirmed) unless every exported registration is in the set. That covers a spread of an empty compose with a hand-written extensions list, a hand-written registration pushed onto composeHost(...).extensions, and composed registrations reused beside a hand-written one. New subtests cover all three, and each asserts that the site's data/ is never created.
  • A host that spreads a composed host and exports only composed registrations still passes.
  • hermeticConfirmationKey is removed.
  • SECURITY.md, EXTENSIONS.md, READINESS.md, llms.txt and the RIM-EXT-HERMETIC-001 card now say exactly what is checked. They also say this is a correctness check, not a security boundary, because host code is trusted.

#977: the sweep's age now comes from the newest entry in a directory.

  • A directory is swept only if it and everything in it is at least an hour old. The age check is a bounded lstat walk: up to 1,024 entries and 8 levels, never following links. A directory beyond that bound is kept.
  • The test covers the reviewer's case: a directory 3 hours old, with a just-rewritten db.sqlite and a dead pid, is kept. A fresh db.sqlite-wal nested two levels down keeps its directory too, and so does one with more entries than the walk checks. The first directory is swept once its database is also an hour old.
  • The remaining limit is documented: a run in another pid namespace that writes nothing for an hour can still be swept.
  • Test isolation: npm test and npm run test:shard preload test/scratch-tmpdir.ts. It points TMPDIR, TEMP and TMP of the run, and of every process it spawns, at a scratch directory that is removed at exit. The three hermetic suites also import it directly.
  • One limitation remains: the workspace package suites (packages/*) and the *.integration.ts runs do not preload it yet.

Local results (macOS, Node 26): npm run verify and npm run test:package are green (1316/1316 core tests). Windows has not been run locally; this relies on CI.

jimhoyd and others added 5 commits September 29, 2026 20:29
…ce, not a serialized value

CodeQL js/bad-code-sanitization flagged JSON.stringify interpolated into generated JavaScript in
test/hermetic-hardening.test.ts; the schema is constant, so it is now written as literal source.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd merged commit f635f84 into main Sep 30, 2026
29 checks passed
@jimhoyd
jimhoyd deleted the claude/hermetic-hardening-976 branch September 30, 2026 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants