fix: guard postgres memory upserts by owner - #16
Open
NeerajCodz wants to merge 1 commit into
Open
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
ON CONFLICT (id) DO UPDATEthat unconditionally assignedtenant_id/user_idand other fields from the incoming record, enabling a caller who can supply/collideidto reassign or overwrite another tenant's memory.idas a global primary key, so the conflict resolution must not allow ownership reassignment during a write.Description
tenant_idanduser_idfrom theON CONFLICTupdate list insrc/adapters/postgres.rsso ownership cannot be overwritten on conflict.WHERE nextral_memories.tenant_id = EXCLUDED.tenant_id AND nextral_memories.user_id = EXCLUDED.user_idclause to theON CONFLICT ... DO UPDATEto ensure conflicted updates only apply when the existing row already belongs to the same tenant and user.executecall and return aCoreError::Conflictwith a clear message when theINSERT ... ON CONFLICTmatched an existing id belonging to a different tenant/user so callers get a deterministic error instead of silent overwrite.Testing
cargo test -qand all tests passed (11 passed, 0 failed).cargo fmt --checkwhich failed due to unrelated/pre-existing formatting differences outside this patch.Codex Task