Skip to content

feat(s3): honor conditional request headers on reads and writes (#397) - #408

Merged
scttfrdmn merged 1 commit into
mainfrom
feat/397-s3-conditional-requests
Jul 31, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
feat/397-s3-conditional-requests

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

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 a 200 every 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, CopyObject and CompleteMultipartUpload — the three operations AWS lists as supporting conditional writes — now evaluate against the current version of the destination key:

Header Destination state Result
If-None-Match: * Key absent 200
If-None-Match: * Key present 412 PreconditionFailed
If-None-Match: * Current version is a delete marker 200
If-None-Match: <not *> Any 412
If-Match: "<etag>" Matches 200
If-Match: "<etag>" Differs 412
If-Match: "<etag>" Absent, or a delete marker 404 NoSuchKey

Two judgment calls worth flagging:

  • If-Match on an absent key is 404, not 412. 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 a 404 Not Found error." There is no ETag to compare, so the caller's real problem is the missing key.
  • A non-* If-None-Match on 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_412LeavesObjectByteIdentical asserts body, ETag, Content-Length, Content-Type, Last-Modified and user metadata are all unchanged afterwards, because a 412 that still overwrote the object would satisfy a status-only assertion while destroying exactly the data the header protects. A rejected CompleteMultipartUpload additionally 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 one 200 and N-1 412s. N concurrent If-Match PUTs asserting the same ETag do too — that is the compare-and-swap primitive optimistic locking needs.

StateManager exposes no compare-and-swap (Get/Put/Delete/List only) and MemoryStateManager is last-write-wins, so a check followed by a Put leaves 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

GetObject and HeadObject evaluate 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 a 206 here would splice bytes from the wrong generation of the object into its cache.

Both combination rules from the GetObject reference are implemented, without special-casing, by evaluating If-Modified-Since only when If-None-Match was absent and If-Unmodified-Since only when If-Match was absent:

  • If-Match true and If-Unmodified-Since false → 200. The caller named the exact entity it wanted and got it.
  • If-None-Match false and If-Modified-Since true → 304. The caller already holds this entity, dates notwithstanding.

Copies

CopyObject carries two independent sets. The unprefixed headers gate overwriting the destination; the four x-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 a 304 — CopyObject documents PreconditionFailed as 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 ConditionalRequestConflict when a concurrent operation interferes, and 404 when 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.

DeleteObject If-Match is out of scope per the reporter.

Verification

  • 71 subtests in the new emulator/s3_conditional_test.go, all green under -race. 100% statement coverage of emulator/s3_conditional.go.
  • Covers: the issue's seven-row write/read table, the exactly-one-winner concurrency test in both If-None-Match and If-Match forms, 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, both CopyObject families together, and CompleteMultipartUpload including malformed-XML-beats-precondition.
  • A regression guard asserts the unconditional path is untouched.
  • 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, CopyObject and CompleteMultipartUpload references, not from built-in knowledge.

One caveat: the PreconditionFailed message 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. The Code string — which is what an SDK actually matches on — is confirmed by the operation references.

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
@scttfrdmn scttfrdmn added this to the v0.79.0 milestone Jul 31, 2026
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.07143% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/s3_plugin.go 75.00% 5 Missing and 5 partials ⚠️

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

S3: conditional writes (If-None-Match / If-Match) always succeed; 412 PreconditionFailed unreachable

1 participant