Skip to content

fix(postgres): prevent cross-tenant memory overwrite in upsert#4

Open
NeerajCodz wants to merge 1 commit into
mainfrom
codex/fix-postgres-upsert-vulnerability
Open

fix(postgres): prevent cross-tenant memory overwrite in upsert#4
NeerajCodz wants to merge 1 commit into
mainfrom
codex/fix-postgres-upsert-vulnerability

Conversation

@NeerajCodz

Copy link
Copy Markdown
Contributor

Motivation

  • The upsert_memory path previously used ON CONFLICT (id) DO UPDATE and unconditionally reassigned tenant_id/user_id and other fields, allowing a caller-supplied or guessed global id to overwrite another tenant's memory record.
  • The schema uses a global id primary key, so ownership must be enforced at write time to prevent cross-tenant corruption.

Description

  • Updated src/adapters/postgres.rs upsert_memory to stop updating tenant_id/user_id on conflict and to add a WHERE nextral_memories.tenant_id = EXCLUDED.tenant_id AND nextral_memories.user_id = EXCLUDED.user_id guard to the ON CONFLICT ... DO UPDATE clause.
  • Capture the rows_affected returned from execute and return a CoreError::Conflict when no rows were updated, which indicates an id collision with a different tenant/user scope.
  • Preserves existing behavior for same-tenant updates and otherwise fails the write rather than silently moving ownership.

Testing

  • Ran the unit test suite with cargo test -p nextral --lib and all tests passed (11 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