Fix #24: Enforce ETag IfMatchEtag preconditions in Replace/Upsert/Delete/Patch#29
Draft
McNultyyy wants to merge 1 commit into
Draft
Fix #24: Enforce ETag IfMatchEtag preconditions in Replace/Upsert/Delete/Patch#29McNultyyy wants to merge 1 commit into
McNultyyy wants to merge 1 commit into
Conversation
44332dc to
c4d66f5
Compare
lemonlion
approved these changes
Apr 21, 2026
c4d66f5 to
65f3b41
Compare
Move ETagTests.cs and ETagRedTeamEdgeCaseTests.cs from the Unit test project to Integration, converting from InMemoryContainer (internal API) to InMemoryCosmos.Create() which goes through the full SDK pipeline. Key changes: - Replace InMemoryContainer fields with InMemoryCosmosResult + IDisposable - Use _cosmos.Container for SDK operations, _cosmos.ExportState()/ ImportState()/RestoreToPointInTime() for state management - Inline container creation uses 'using var cosmos = InMemoryCosmos.Create()' - Adapt 5 edge-case tests for SDK pipeline behavior: - Empty/whitespace/unquoted ETags are dropped by HTTP header parser - Patch handler does not propagate IfNoneMatch headers Bump version to 4.0.6. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
65f3b41 to
6b52671
Compare
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.
Summary
Fixes #24 — Hardens
CheckIfMatchandCheckIfMatchStreaminInMemoryContainer.csto properly enforce ETag preconditions for Replace, Upsert, Delete, and Patch operations.Problem
Both
CheckIfMatch(typed APIs) andCheckIfMatchStream(stream APIs) silently ignored theIfMatchEtagrequest option — they never threw 412 Precondition Failed even when the provided ETag didn't match the stored one. This allowed callers to overwrite items without optimistic concurrency being enforced.Fix
InMemoryContainer.csCheckIfMatch: Now checks_etagsfor the key and throwsInMemoryCosmosException(412 PreconditionFailed)when the provided ETag doesn't match the stored one.CheckIfMatchStream: Same logic — returnsfalsewhen the stored ETag doesn't match.IfMatchEtagonly applies to update (PUT/DELETE) operations, not inserts (POST). When Upsert creates a new item,CheckIfMatchcorrectly passes through — the missing-key case is handled by the Upsert create path itself.*: Always passes for all operations (no change to existing behaviour).Tests (Integration project)
Tests.Integration/ETagTests.cs— Replace, Upsert, Delete, Patch with stale/current/wildcard ETags across typed, stream, and batch APIs.Tests.Integration/ETagRedTeamEdgeCaseTests.cs— adversarial scenarios:Version Bump
Patch version bumped to 4.0.6 (from 4.0.5).
Test Results
Rebased on latest
mainIncorporates all v4.0 changes (internal classes,
FakeCosmosHandlerpipeline, test project restructuring, issue #18CosmosExceptionfix).