Skip to content

fix: prevent cross-tenant memory id overwrite#6

Open
NeerajCodz wants to merge 1 commit into
mainfrom
codex/fix-tenant-scoped-id-collision
Open

fix: prevent cross-tenant memory id overwrite#6
NeerajCodz wants to merge 1 commit into
mainfrom
codex/fix-tenant-scoped-id-collision

Conversation

@NeerajCodz

Copy link
Copy Markdown
Contributor

Motivation

  • The codebase introduced tenant_id and tenant-scoped reads while writes still upsert by global id, enabling a cross-tenant overwrite when an attacker supplies an existing memory id from another tenant.
  • The change aims to restore tenant isolation by ensuring writes cannot silently replace a memory owned by a different (tenant_id, user_id) scope.

Description

  • Add a pre-flight collision check to the in-memory TestMemoryStore::upsert_memory that returns CoreError::Conflict if an existing record with the same id belongs to a different tenant_id or user_id and only updates when id, tenant_id, and user_id all match (src/testkit/mod.rs).
  • Modify Postgres upsert_memory to restrict the ON CONFLICT (id) DO UPDATE to rows where nextral_memories.tenant_id = EXCLUDED.tenant_id AND nextral_memories.user_id = EXCLUDED.user_id, and return a CoreError::Conflict when no rows are affected (indicating a cross-tenant id collision) (src/adapters/postgres.rs).
  • Add a regression test that ingests a record as tenant_a with a caller-supplied id, then verifies that an ingest from tenant_b using the same id is rejected with a Conflict and that the original tenant_a record remains intact (src/lib.rs tests).

Testing

  • Ran the test suite with cargo test -q and observed all tests pass.
  • Test run result: running 12 tests ... test result: ok. 12 passed; 0 failed.

Codex Task

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant