Skip to content

fix(security): an unresolved session has no write target (#152) - #157

Merged
abogartz merged 1 commit into
mainfrom
fix/152-unresolved-writer-scope
Oct 28, 2026
Merged

abogartz merged 1 commit into
mainfrom
fix/152-unresolved-writer-scope

Conversation

@abogartz

Copy link
Copy Markdown
Owner

Closes #152

What was wrong

The read side and the write side disagreed about what an unknown identity means.

Reads already fail closed (#127/#133): readerForSession gives an unresolved session global-only, never the module scope of whoever set up last. Writes went through repoScopeFor(), whose last resort is the module scope — so a memory_remember from a session the host could not resolve was:

  • stamped with repo B's key when a repo-B plugin instance had loaded last → B recalls A's private content through ordinary scoped recall; or
  • stamped nothing when no module scope existed → an unstamped row, which every reader calls global, so every project that ever opens the store reads it.

No force, no admin seat, no interleaving required. memory_update, memory_import and auto-capture all inherited the same fallback.

Reproduced before fixing

Both reported cases plus the two paths the ticket did not name, on main @ 91f7a56 (v0.14.13), macOS / Bun 1.4.3 (the report was Windows / Bun 1.3.10):

(fail) an unresolved named session never writes into the LAST setup's repository
(fail) an unresolved named session cannot publish a global memory
(fail) an unresolved named session cannot re-stamp a row into another repository
(fail) memory_import refuses an unresolved named session before writing anything
(fail) auto-capture stores nothing for a session whose repository is unresolved
      → Received length: 1   (the mined row was in the store, unstamped = global)

After: 10/10 green in test/security-unresolved-write-scope.test.ts, and the 4 controls in that file (bound session, workspace-bound instance, explicit global:true, sessionless writer) were green before the change too — the fix did not buy them.

The rule

A named session with no resolved repository has no write target. Reads and writes now answer the same question the same way: an identity that cannot read a repository cannot write one either.

  • repokey.ts — writeTarget(sid): named session → its own binding (firm or provisional, no module fallback) or ok:false; nobody named (viewer process, restore, scripts, tests) → the module scope, including null. That last branch is the deliberate admin path and is unchanged.
  • memory.ts — the stamp resolution at the single write choke point throws UnresolvedWriterError. Covering the choke point is what protects callers that reach the store without a tool, present or future.
  • index.ts — memory_remember, memory_update, memory_import and memory_init answer with the same message before the gate runs (one shared unresolvedWriteMessage, so the two cannot drift) and record evidence write_refused. memory_import also binds identity first, like the other three, so a resolvable serve-host session is never falsely refused.
  • Auto-capture checks before the model call — no tokens spent on a capture that cannot be stored — and records auto_capture_refused.
  • portable.ts — importMemories resolves the writer before reading a single line, so a refusal is zero rows, not half a file.

Preserved deliberately: explicit global: true (an owner was stated), the sessionless admin path, and the provisional binding #127 introduced (an unknown session reaching a workspace-bound instance still writes there — the ordinary single-project shape stays frictionless).

Contract change (the one behavior tests used to pin that flips)

test/repo-scope-session.test.ts "server-start window" asserted that an early write from a location-less setup produces an unstamped global row. That assertion is the second half of the vulnerability, so the test now asserts the new contract: the write is refused, the day file is untouched, and once session.execution.started binds the session the same write lands stamped.

Consequence for operators: a plugin instance loaded with no location.directory can no longer store project memories implicitly. It says so on every attempt (Write refused: … pass global:true …). Configuring the plugin per project, or asking for global: true, are the two ways out.

Test harness honesty

Eleven tests wrote through named-but-unbound sessions and leaned on the module fallback. Each now declares the workspace it simulates, which is what a real host does by handing the tool a sessionID: test/scenarios.test.ts and test/gated-injection.test.ts (ctx(sid)), test/helpers/scripted.ts (session factory). No assertion was weakened to get there.

Evidence

  • bun scripts/merge-gate.ts → all green: tsc, biome, unit 612 pass / 12 skip / 0 fail, e2e smoke + BDD 11 pass / 0 fail.
  • bunx tsc --noEmit clean; biome lint exit 0 (advisory warnings only, unchanged in kind).
  • New test file adds zero biome diagnostics.

Not in this PR

Reads already failed closed for a session whose repository never resolved
(#127/#133): it sees global rows and nothing else. Writes did not. The stamp
at the store's single choke point fell back to the module scope, so a
memory_remember from an unresolvable session in repo A was stamped into
whichever workspace's setup() ran last, and with no module scope at all it
was stamped nothing — which every reader calls global. Auto-capture and
memory_import inherited the same fallback.

appendMemory now resolves the writer through writeTarget(): a session the
host named but never resolved gets ok:false, and the store refuses the write
instead of guessing an owner. The model-facing tools answer with the same
message before the gate runs and log evidence write_refused; capture skips
before spending a model call (auto_capture_refused); import resolves before
reading a single row. Explicit global:true and the deliberately sessionless
admin path (viewer, restore, scripts) are unchanged.

Contract change, pinned by test/repo-scope-session.test.ts: a write during
the server-start window is refused rather than stored as a global row.
@abogartz
abogartz merged commit 4b024dc into main Oct 28, 2026
4 checks passed
@abogartz
abogartz deleted the fix/152-unresolved-writer-scope branch October 28, 2026 20:23
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.14 🎉

The release is available on:

Your semantic-release bot 📦🚀

@WhollyMindless

Copy link
Copy Markdown
Contributor

Independent re-verification of e1256e0 after its merge into v0.14.14: the #152 findings are fixed; no blocking finding from this pass.

  • Our original two unresolved-writer leak probes and the bound-session control: 3/3 pass on released main ed5ecde (Windows/Bun 1.3.10). Both vulnerable calls now refuse, and the other repository recalls no private marker.
  • security-unresolved-write-scope.test.ts + repo-scope-session.test.ts: 18/18 pass, including import/capture refusal, late session binding, explicit-global and sessionless controls.
  • Reviewed the production diff and the changed fixtures: named fixtures now explicitly bind their workspace; the server-start test intentionally reverses the former unsafe global-write expectation.
  • Also trial-merged fix(security): finish entry-point breadth — session-scoped export + ownership-checked undo (#143 #153) #156 onto this main: the three new PR regression suites pass 18/18 together on Windows/Bun 1.4.4, with clean type-checking.

The separate export artifact collision found while reviewing #156 is posted there with a reproducer and a tested patch; it does not invalidate this writer-scope fix.

abogartz added a commit to WhollyMindless/opencode-self-improvement that referenced this pull request Oct 28, 2026
Brings in abogartz#152's write-identity rule (writeTarget / UnresolvedWriterError at
the appendMemory choke point) plus the release commit. No textual conflicts.
Verified semantically after the merge, not just textually: the full gate is
green (tsc, biome, unit 608 pass / 12 skip / 0 fail, e2e smoke+BDD 11 pass),
and this PR's own abogartz#143/abogartz#153 probes stay green — restoreSnapshot publishes
through writeAtomic, so abogartz#152's appendMemory refusal is not on the undo path.
abogartz pushed a commit that referenced this pull request Oct 29, 2026
memory_update's replacement candidate is built FROM the row it replaces: tags and issue carry over (an update of an imported row no longer launders away its provenance marker or issue link), and kw keeps target.kw when omitted while an explicit kw still replaces. A prose edit is not a metadata reset.

runWriteGate no longer throws AFTER the append when the write_promoted audit line fails - the row is durable at that point, and a throw made memory_update's catch declare 'Nothing was written' while the replacement sat on disk un-superseded (two active rows; retry said 'Duplicate ignored' about a write that happened). The failure rides the promoted outcome as GateOutcome.evidenceError; pre-append faults keep throwing fail-closed. The update entry completes the supersession on that outcome (markSuperseded is first-call-wins, safe to complete), reports the merge_review PARTIAL shape from 156e887, catches a markSuperseded throw the same way, and gains a duplicate-aware resolution: naming a superseded row whose active successor already carries exactly this content reports the duplicate verdict instead of a dead end. Pre-write failures - lock timeout, the 152 choke-point refusal, pre-append evidence faults - stay fail-closed with the truthful Nothing-was-written message; the catch's stale 'only the lock timeout throws' premise is corrected.

Rebased onto ed5ecde (0.14.14): #157's ownerless-writer refusal is a pre-write entry check and is untouched - this fix keeps it fail-closed in the catch's unmoved branch.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P1: unresolved named sessions publish private memories into the last repo or globally

2 participants