fix: make S3 conditional puts atomic (#1329)

This commit is contained in:
David Phillips authored and GitHub committed 2026-06-13 23:25:18 -05:00
1 parent 763cdb5314
commit f70709a70f
5 files changed
+185 -38

No files matched your search

@@ -490,11 +490,6 @@ public class S3Controller {
return handleCopyObject(copySource, bucket, key, contentType, httpHeaders);
}
Response preconditionResponse = checkWritePreconditions(bucket, key, ifMatch, ifNoneMatch);
if (preconditionResponse != null) {
return preconditionResponse;
}
Map<String, String> inlineTags = parseInlineTaggingHeader(tagging);
String lockMode = httpHeaders.getHeaderString("x-amz-object-lock-mode");
@@ -529,6 +524,8 @@ public class S3Controller {
.withAcl(cannedAcl)
.withChecksumAlgorithm(checksumAlgorithm)
.withClientChecksum(extractChecksumFromHeaders(httpHeaders))
.withIfMatch(ifMatch)
.withIfNoneMatch(ifNoneMatch)
.withTagging(inlineTags));
var resp = Response.ok().header("ETag", obj.getETag());
if (obj.getVersionId() != null) {
@@ -1973,14 +1970,25 @@ public class S3Controller {
}
private Response xmlErrorResponse(AwsException e) {
String condition = e instanceof S3PreconditionFailedException preconditionFailedException
? preconditionFailedException.condition()
: null;
return xmlErrorResponse(e, condition);
}
private Response xmlErrorResponse(AwsException e, String condition) {
if (e.getMessage() == null) {
return Response.status(e.getHttpStatus()).build();
}
String xml = new XmlBuilder()
XmlBuilder xmlBuilder = new XmlBuilder()
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
.start("Error")
.elem("Code", e.getErrorCode())
.elem("Message", e.getMessage())
.elem("Message", e.getMessage());
if (condition != null) {
xmlBuilder.elem("Condition", condition);
}
String xml = xmlBuilder
.elem("RequestId", java.util.UUID.randomUUID().toString())
.end("Error")
.build();
@@ -2033,10 +2041,10 @@ public class S3Controller {
}
if (ifMatch != null && !eTagMatches(ifMatch, existing.getETag())) {
return preconditionFailedResponse();
return preconditionFailedResponse("If-Match");
}
if (ifNoneMatch != null && eTagMatches(ifNoneMatch, existing.getETag())) {
return preconditionFailedResponse();
return preconditionFailedResponse("If-None-Match");
}
return null;
}
@@ -2059,8 +2067,12 @@ public class S3Controller {
}
private Response preconditionFailedResponse() {
return preconditionFailedResponse(null);
}
private Response preconditionFailedResponse(String condition) {
return xmlErrorResponse(new AwsException("PreconditionFailed",
"At least one of the pre-conditions you specified did not hold.", 412));
S3PreconditionFailedException.MESSAGE, 412), condition);
}
private boolean eTagMatches(String headerValue, String eTag) {
@@ -0,0 +1,18 @@
package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.AwsException;
final class S3PreconditionFailedException extends AwsException {
static final String MESSAGE = "At least one of the pre-conditions you specified did not hold";
private final String condition;
S3PreconditionFailedException(String condition) {
super("PreconditionFailed", MESSAGE, 412);
this.condition = condition;
}
String condition() {
return condition;
}
}
@@ -283,10 +283,19 @@ public class S3Service {
Bucket bucket = bucketStore.get(bucketName)
.orElseThrow(() -> new AwsException("NoSuchBucket",
"The specified bucket does not exist.", 404));
synchronized (bucket) {
return storeObjectInternal(bucket, bucketName, key, data, contentType, metadata, checksum, parts, options);
}
}
private S3Object storeObjectInternal(Bucket bucket, String bucketName, String key, byte[] data,
String contentType, Map<String, String> metadata,
S3Checksum checksum, List<Part> parts, PutObjectOptions options) {
PutObjectOptions effectiveOptions = options != null ? options : new PutObjectOptions();
String normalizedServerSideEncryption = normalizeServerSideEncryption(effectiveOptions.getServerSideEncryption());
SseCustomerKey sseCustomerKey = validateSseCustomerKey(effectiveOptions.getSseCustomerAlgorithm(), effectiveOptions.getSseCustomerKey(), effectiveOptions.getSseCustomerKeyMd5());
rejectConflictingServerSideEncryption(normalizedServerSideEncryption, sseCustomerKey);
checkWritePreconditions(bucketName, key, effectiveOptions.getIfMatch(), effectiveOptions.getIfNoneMatch());
S3Object object = new S3Object(bucketName, key, data, contentType);
if (metadata != null) {
@@ -365,6 +374,49 @@ public class S3Service {
return object;
}
private void checkWritePreconditions(String bucketName, String key, String ifMatch, String ifNoneMatch) {
if (ifMatch == null && ifNoneMatch == null) {
return;
}
S3Object existing;
try {
existing = headObject(bucketName, key);
}
catch (AwsException e) {
if ("NoSuchKey".equals(e.getErrorCode()) && ifMatch == null) {
return;
}
throw e;
}
if (ifMatch != null && !eTagMatches(ifMatch, existing.getETag())) {
throw new S3PreconditionFailedException("If-Match");
}
if (ifNoneMatch != null && eTagMatches(ifNoneMatch, existing.getETag())) {
throw new S3PreconditionFailedException("If-None-Match");
}
}
private boolean eTagMatches(String headerValue, String eTag) {
String normalizedETag = normalizeEntityTag(eTag);
for (String candidate : headerValue.split(",")) {
String normalizedCandidate = normalizeEntityTag(candidate);
if ("*".equals(normalizedCandidate) || normalizedCandidate.equals(normalizedETag)) {
return true;
}
}
return false;
}
private String normalizeEntityTag(String value) {
String normalized = value == null ? "" : value.trim();
if (normalized.length() >= 2 && normalized.startsWith("\"") && normalized.endsWith("\"")) {
normalized = normalized.substring(1, normalized.length() - 1);
}
return normalized;
}
private void applyObjectLock(S3Object object, Bucket bucket,
String objectLockMode, Instant retainUntilDate, String legalHoldStatus) {
if (objectLockMode != null) {
@@ -17,6 +17,8 @@ public class PutObjectOptions {
private String sseCustomerKeyMd5;
private String acl;
private String checksumAlgorithm;
private String ifMatch;
private String ifNoneMatch;
private Map<String, String> tagging;
public String getStorageClass() { return storageClass; }
@@ -58,6 +60,12 @@ public class PutObjectOptions {
public String getChecksumAlgorithm() { return checksumAlgorithm; }
public PutObjectOptions withChecksumAlgorithm(String checksumAlgorithm) { this.checksumAlgorithm = checksumAlgorithm; return this; }
public String getIfMatch() { return ifMatch; }
public PutObjectOptions withIfMatch(String ifMatch) { this.ifMatch = ifMatch; return this; }
public String getIfNoneMatch() { return ifNoneMatch; }
public PutObjectOptions withIfNoneMatch(String ifNoneMatch) { this.ifNoneMatch = ifNoneMatch; return this; }
public Map<String, String> getTagging() { return tagging; }
public PutObjectOptions withTagging(Map<String, String> tagging) { this.tagging = tagging; return this; }