Skip to content

fix: guard postgres memory upserts by owner - #16

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

fix: guard postgres memory upserts by owner#16
NeerajCodz wants to merge 1 commit into
mainfrom
codex/fix-postgres-upsert-cross-tenant-vulnerability

Conversation

@NeerajCodz

Copy link
Copy Markdown
Member

Motivation

  • The PostgreSQL adapter used ON CONFLICT (id) DO UPDATE that unconditionally assigned tenant_id/user_id and other fields from the incoming record, enabling a caller who can supply/collide id to reassign or overwrite another tenant's memory.
  • The schema defines id as a global primary key, so the conflict resolution must not allow ownership reassignment during a write.

Description

  • Removed updates to tenant_id and user_id from the ON CONFLICT update list in src/adapters/postgres.rs so ownership cannot be overwritten on conflict.
  • Added a WHERE nextral_memories.tenant_id = EXCLUDED.tenant_id AND nextral_memories.user_id = EXCLUDED.user_id clause to the ON CONFLICT ... DO UPDATE to ensure conflicted updates only apply when the existing row already belongs to the same tenant and user.
  • Capture the number of affected rows from the execute call and return a CoreError::Conflict with a clear message when the INSERT ... ON CONFLICT matched an existing id belonging to a different tenant/user so callers get a deterministic error instead of silent overwrite.

Testing

  • Ran cargo test -q and all tests passed (11 passed, 0 failed).
  • Ran cargo fmt --check which failed due to unrelated/pre-existing formatting differences outside this patch.

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