fix(postgres): prevent cross-tenant memory overwrite in upsert#4
Open
NeerajCodz wants to merge 1 commit into
Open
fix(postgres): prevent cross-tenant memory overwrite in upsert#4NeerajCodz wants to merge 1 commit into
NeerajCodz wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
upsert_memorypath previously usedON CONFLICT (id) DO UPDATEand unconditionally reassignedtenant_id/user_idand other fields, allowing a caller-supplied or guessed globalidto overwrite another tenant's memory record.idprimary key, so ownership must be enforced at write time to prevent cross-tenant corruption.Description
src/adapters/postgres.rsupsert_memoryto stop updatingtenant_id/user_idon conflict and to add aWHERE nextral_memories.tenant_id = EXCLUDED.tenant_id AND nextral_memories.user_id = EXCLUDED.user_idguard to theON CONFLICT ... DO UPDATEclause.rows_affectedreturned fromexecuteand return aCoreError::Conflictwhen no rows were updated, which indicates an id collision with a different tenant/user scope.Testing
cargo test -p nextral --liband all tests passed (11 passed, 0 failed).Codex Task