Repository navigation
feat(s3): honor conditional request headers on reads and writes (#397) - #408
Merged
Merged
Conversation
All four RFC 9110 preconditions were ignored, which is the worst possible outcome for the pattern they exist to serve: a consumer using `If-None-Match: *` to claim a lock, elect a leader, or refuse to clobber a checkpoint got a 200 every time, so its lost-race branch was unreachable and its tests passed while proving the opposite of the intended invariant. Writes. PutObject, CopyObject and CompleteMultipartUpload now evaluate If-None-Match and If-Match against the current version of the destination key. `If-None-Match: *` is 412 when the key exists; If-Match is 412 on an ETag mismatch and 404 NoSuchKey when the key is absent — not a 412, since there is no ETag to compare. A delete marker counts as absent for both, per the conditional-writes reference. A non-`*` If-None-Match value is rejected rather than ignored, so a header sent to prevent an overwrite can never silently permit one. A rejected write is a no-op: the stored object is byte-identical afterwards, and a rejected Complete leaves its upload open to be aborted. Exactly one winner. N concurrent `If-None-Match: *` PUTs to one key produce exactly one 200 and N-1 412s, as do N concurrent If-Match PUTs asserting the same ETag. StateManager has no compare-and-swap and MemoryStateManager is last-write-wins, so this needed a per-key mutex held across check→Put; without it 23 of 32 concurrent writers won. The guarantee is process-local — airtight for substrate's single-process topology, but it would not hold across two emulator processes sharing one state backend. That limit is documented in the type, in docs/services.md, and in the CHANGELOG rather than papered over. Reads. GetObject and HeadObject evaluate all four preconditions before the Range step, so a failed precondition is reported rather than a partial response served from an entity the caller did not ask for. Both combination rules from the GetObject reference are implemented: If-Match true with If-Unmodified-Since false is 200, and If-None-Match false with If-Modified-Since true is 304. Copies. CopyObject also honors the four x-amz-copy-source-if-* headers, which gate reading the source rather than overwriting the destination; both sets may appear on one request and both are checked before anything is written. A failed copy-source condition is always 412, including where the equivalent GET would be 304 — there is no cached entity for a server-side copy to revalidate against. The 409 ConditionalRequestConflict and concurrent-delete 404 real S3 can return are deliberately not emulated: both are races against wall-clock timing rather than states a deterministic emulator can reach. Every semantic here was verified against the AWS conditional-writes, GetObject, PutObject, CopyObject and CompleteMultipartUpload references. Closes #397
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This was referenced Jul 31, 2026
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.
Closes #397.
S3's four RFC 9110 precondition headers were read by nothing. That is the worst possible failure mode for the pattern they exist to serve: a consumer using
If-None-Match: *to claim a lock, elect a leader, or refuse to clobber a checkpoint received a200every time. Its lost-race branch was unreachable, and its test suite passed while demonstrating the exact opposite of the invariant it was written to protect.Writes
PutObject,CopyObjectandCompleteMultipartUpload— the three operations AWS lists as supporting conditional writes — now evaluate against the current version of the destination key:If-None-Match: *200If-None-Match: *412 PreconditionFailedIf-None-Match: *200If-None-Match: <not *>412If-Match: "<etag>"200If-Match: "<etag>"412If-Match: "<etag>"404 NoSuchKeyTwo judgment calls worth flagging:
If-Matchon an absent key is404, not412. Straight from the conditional-writes page: "If there's no current object version with the same name, or if the current object version is a delete marker, the operation fails with a404 Not Founderror." There is no ETag to compare, so the caller's real problem is the missing key.*If-None-Matchon a write is rejected, not ignored. S3 documents the header as expecting*. Treating an unsupported value as "no condition" would let a header sent specifically to prevent an overwrite silently permit one — the failure mode this whole issue is about.A rejected write is a no-op.
TestS3_ConditionalWrite_412LeavesObjectByteIdenticalasserts body, ETag,Content-Length,Content-Type,Last-Modifiedand user metadata are all unchanged afterwards, because a412that still overwrote the object would satisfy a status-only assertion while destroying exactly the data the header protects. A rejectedCompleteMultipartUploadadditionally leaves its upload open, so a caller that lost the race can still abort it.Exactly one winner
N concurrent
If-None-Match: *PUTs to one key produce exactly one200and N-1412s. N concurrentIf-MatchPUTs asserting the same ETag do too — that is the compare-and-swap primitive optimistic locking needs.StateManagerexposes no compare-and-swap (Get/Put/Delete/List only) andMemoryStateManageris last-write-wins, so a check followed by aPutleaves a window in which every writer observes the key as absent. This needed a striped per-key mutex held across check→Put. I verified the test isn't vacuous by stubbing the lock out: 23 of 32 writers won.The guarantee is deliberately bounded and process-local. It is airtight for substrate's single-process topology, which is how tests use it, but it would not hold across two emulator processes sharing one state backend. That limit is stated in the type's doc comment, in
docs/services.md, and in the CHANGELOG — the reporter asked to be told if substrate could not guarantee this rather than have it papered over, and this is the honest answer: yes within a process, no across processes.Reads
GetObjectandHeadObjectevaluate all four preconditions before the range step. That ordering is why #397 was sequenced after #396: RFC 9110 requires a failed precondition to be reported rather than a partial response served, and a client that got a206here would splice bytes from the wrong generation of the object into its cache.Both combination rules from the
GetObjectreference are implemented, without special-casing, by evaluatingIf-Modified-Sinceonly whenIf-None-Matchwas absent andIf-Unmodified-Sinceonly whenIf-Matchwas absent:If-Matchtrue andIf-Unmodified-Sincefalse →200. The caller named the exact entity it wanted and got it.If-None-Matchfalse andIf-Modified-Sincetrue →304. The caller already holds this entity, dates notwithstanding.Copies
CopyObjectcarries two independent sets. The unprefixed headers gate overwriting the destination; the fourx-amz-copy-source-if-*headers gate reading the source. Both may appear on one request, and both are checked before anything is written.Every failed copy-source condition is a
412, including the case where the equivalent GET would be a304—CopyObjectdocumentsPreconditionFailedas its only conditional outcome, since there is no cached entity for a server-side copy to revalidate against.Deliberately not emulated
Real S3 can return
409 ConditionalRequestConflictwhen a concurrent operation interferes, and404when a concurrent delete lands mid-write. Both are races against wall-clock timing rather than states a deterministic emulator can reach, so substrate resolves every conditional write to one of the documented rows. A consumer that must exercise its 409 handler needs a fault-injection tier, not this one. Noted in the docs so nobody mistakes silence for coverage.DeleteObjectIf-Matchis out of scope per the reporter.Verification
emulator/s3_conditional_test.go, all green under-race. 100% statement coverage ofemulator/s3_conditional.go.If-None-MatchandIf-Matchforms, the byte-identical-after-412 assertion, delete-marker-as-absent, precondition-precedes-range (including when both would error), ETag quoting/case/weak-validator/list forms, all three RFC 9110 date formats, unparseable and empty header values, case-insensitive header lookup, bothCopyObjectfamilies together, andCompleteMultipartUploadincluding malformed-XML-beats-precondition.make test(race),golangci-lint run ./...(0 issues),test/e2e(race),make docs-reference-check— all clean.Every semantic was verified against the AWS conditional-writes, conditional-reads,
GetObject,PutObject,CopyObjectandCompleteMultipartUploadreferences, not from built-in knowledge.One caveat: the
PreconditionFailedmessage string ("At least one of the pre-conditions you specified did not hold") is the well-known S3 text, but the AWS error-responses page failed to fetch on every attempt across two sessions, so it is not doc-verified. TheCodestring — which is what an SDK actually matches on — is confirmed by the operation references.