mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(s3): honour CopyObject destination and copy-source preconditions (#4984)
* fix(s3): honour CopyObject destination and copy-source preconditions CopyObject ignored every precondition. If-None-Match: * over an existing destination, and If-Match with a stale destination ETag, both answered 200 and overwrote it; If-Match against a missing destination created it; and the four x-amz-copy-source-if-* headers were never read, on CopyObject or UploadPartCopy. Destination If-Match / If-None-Match now travel into the write the copy already performs, so they are checked by the same code and under the same bucket monitor as a conditional PutObject: 412 on a failed condition, 404 NoSuchKey for If-Match on a missing key. Other If-None-Match values keep the literal-ETag meaning they have on PutObject. The copy-source conditions are checked against the resolved source object with the pairing rules S3 documents for CopyObject: a matching if-match makes if-unmodified-since irrelevant, and a matching if-none-match fails whatever if-modified-since says. Every failure is 412, never 304. Dates compare at second precision, the resolution of an HTTP date and of Last-Modified. * test(s3): pin CopyObject's condition names and pairing controls Destination failures now assert the <Condition> they report, and each pairing test first shows its date condition's result alone, so the pair's outcome can only come from the precedence rule. * test(s3): assert a rejected UploadPartCopy leaves the existing part intact
This commit is contained in:
5 files changed
+347
-3
No files matched your search
@@ -28,6 +28,7 @@ import io.github.hectorvent.floci.services.s3.model.FilterRule;
|
||||
import io.github.hectorvent.floci.services.s3.model.NotificationConfiguration;
|
||||
import io.github.hectorvent.floci.services.s3.model.ObjectAttributeName;
|
||||
import io.github.hectorvent.floci.services.s3.model.CopyObjectOptions;
|
||||
import io.github.hectorvent.floci.services.s3.model.CopySourceConditions;
|
||||
import io.github.hectorvent.floci.services.s3.model.QueueNotification;
|
||||
import io.github.hectorvent.floci.services.s3.model.ObjectLockRetention;
|
||||
import io.github.hectorvent.floci.services.s3.model.Part;
|
||||
@@ -2793,7 +2794,10 @@ public class S3Controller {
|
||||
.withGrantWrite(httpHeaders.getHeaderString("x-amz-grant-write"))
|
||||
.withGrantFullControl(httpHeaders.getHeaderString("x-amz-grant-full-control"))
|
||||
.withGrantReadAcp(httpHeaders.getHeaderString("x-amz-grant-read-acp"))
|
||||
.withGrantWriteAcp(httpHeaders.getHeaderString("x-amz-grant-write-acp")));
|
||||
.withGrantWriteAcp(httpHeaders.getHeaderString("x-amz-grant-write-acp"))
|
||||
.withIfMatch(httpHeaders.getHeaderString("If-Match"))
|
||||
.withIfNoneMatch(httpHeaders.getHeaderString("If-None-Match"))
|
||||
.withCopySourceConditions(copySourceConditions(httpHeaders)));
|
||||
XmlBuilder xmlBuilder = new XmlBuilder()
|
||||
.raw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
|
||||
.start("CopyObjectResult", AwsNamespaces.S3)
|
||||
@@ -2830,7 +2834,8 @@ public class S3Controller {
|
||||
String eTag = s3Service.uploadPartCopy(destBucket, destKey, uploadId, partNumber,
|
||||
sourceBucket, sourceObject.objectKey(), sourceObject.versionId(), copySourceRange,
|
||||
copySourceSseCustomerHeaders(httpHeaders),
|
||||
sseCustomerHeaders(httpHeaders));
|
||||
sseCustomerHeaders(httpHeaders),
|
||||
copySourceConditions(httpHeaders));
|
||||
// The destination multipart upload's own SSE settings (captured at
|
||||
// CreateMultipartUpload), not anything from this request's headers.
|
||||
// UploadPartCopy doesn't take server-side-encryption headers itself,
|
||||
@@ -2856,6 +2861,16 @@ public class S3Controller {
|
||||
return response.build();
|
||||
}
|
||||
|
||||
private CopySourceConditions copySourceConditions(HttpHeaders httpHeaders) {
|
||||
String ifModifiedSince = httpHeaders.getHeaderString("x-amz-copy-source-if-modified-since");
|
||||
String ifUnmodifiedSince = httpHeaders.getHeaderString("x-amz-copy-source-if-unmodified-since");
|
||||
return new CopySourceConditions(
|
||||
httpHeaders.getHeaderString("x-amz-copy-source-if-match"),
|
||||
httpHeaders.getHeaderString("x-amz-copy-source-if-none-match"),
|
||||
ifModifiedSince != null ? parseHttpDate(ifModifiedSince) : null,
|
||||
ifUnmodifiedSince != null ? parseHttpDate(ifUnmodifiedSince) : null);
|
||||
}
|
||||
|
||||
private S3Service.SseCustomerHeaders copySourceSseCustomerHeaders(HttpHeaders httpHeaders) {
|
||||
return new S3Service.SseCustomerHeaders(
|
||||
httpHeaders.getHeaderString("x-amz-copy-source-server-side-encryption-customer-algorithm"),
|
||||
|
||||
@@ -54,6 +54,7 @@ import java.nio.file.StandardOpenOption;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -728,6 +729,36 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the {@code x-amz-copy-source-if-*} preconditions to the source of a copy. The pairing
|
||||
* rules are the ones S3 documents for CopyObject: a matching {@code if-match} makes
|
||||
* {@code if-unmodified-since} irrelevant, and a matching {@code if-none-match} fails whatever
|
||||
* {@code if-modified-since} says. Unlike a conditional GET, every failure is a 412, never a 304.
|
||||
* Dates compare at second precision, the resolution of an HTTP date and of Last-Modified. S3
|
||||
* documents no {@code <Condition>} value for these, so the error carries none, as a failed
|
||||
* conditional GET does here.
|
||||
*/
|
||||
private void checkCopySourcePreconditions(S3Object source, CopySourceConditions conditions) {
|
||||
if (conditions == null) {
|
||||
return;
|
||||
}
|
||||
Instant lastModified = source.getLastModified().truncatedTo(ChronoUnit.SECONDS);
|
||||
if (conditions.ifMatch() != null && !eTagMatches(conditions.ifMatch(), source.getETag())) {
|
||||
throw new S3PreconditionFailedException(null);
|
||||
}
|
||||
if (conditions.ifUnmodifiedSince() != null && conditions.ifMatch() == null
|
||||
&& lastModified.isAfter(conditions.ifUnmodifiedSince())) {
|
||||
throw new S3PreconditionFailedException(null);
|
||||
}
|
||||
if (conditions.ifNoneMatch() != null && eTagMatches(conditions.ifNoneMatch(), source.getETag())) {
|
||||
throw new S3PreconditionFailedException(null);
|
||||
}
|
||||
if (conditions.ifModifiedSince() != null && conditions.ifNoneMatch() == null
|
||||
&& !lastModified.isAfter(conditions.ifModifiedSince())) {
|
||||
throw new S3PreconditionFailedException(null);
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizeEntityTag(String value) {
|
||||
String normalized = value == null ? "" : value.trim();
|
||||
if (normalized.length() >= 2 && normalized.startsWith("\"") && normalized.endsWith("\"")) {
|
||||
@@ -1948,6 +1979,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
{
|
||||
CopyObjectOptions effectiveOptions = options != null ? options : new CopyObjectOptions();
|
||||
S3Object source = getObject(sourceBucket, sourceKey, versionId);
|
||||
checkCopySourcePreconditions(source, effectiveOptions.getCopySourceConditions());
|
||||
validateSseCustomerAccess(source,
|
||||
effectiveOptions.getCopySourceSseCustomerAlgorithm(),
|
||||
effectiveOptions.getCopySourceSseCustomerKey(),
|
||||
@@ -1960,6 +1992,7 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
String destBucket, String destKey, CopyObjectOptions options) {
|
||||
CopyObjectOptions effectiveOptions = options != null ? options : new CopyObjectOptions();
|
||||
S3Object source = getObject(sourceBucket, sourceKey);
|
||||
checkCopySourcePreconditions(source, effectiveOptions.getCopySourceConditions());
|
||||
validateSseCustomerAccess(source,
|
||||
effectiveOptions.getCopySourceSseCustomerAlgorithm(),
|
||||
effectiveOptions.getCopySourceSseCustomerKey(),
|
||||
@@ -3420,7 +3453,19 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
String copySourceRange,
|
||||
SseCustomerHeaders copySourceSseCustomerHeaders,
|
||||
SseCustomerHeaders sseCustomerHeaders) {
|
||||
return uploadPartCopy(destBucket, destKey, uploadId, partNumber, sourceBucket, sourceKey,
|
||||
sourceVersionId, copySourceRange, copySourceSseCustomerHeaders, sseCustomerHeaders,
|
||||
CopySourceConditions.NONE);
|
||||
}
|
||||
|
||||
public String uploadPartCopy(String destBucket, String destKey, String uploadId, int partNumber,
|
||||
String sourceBucket, String sourceKey, String sourceVersionId,
|
||||
String copySourceRange,
|
||||
SseCustomerHeaders copySourceSseCustomerHeaders,
|
||||
SseCustomerHeaders sseCustomerHeaders,
|
||||
CopySourceConditions copySourceConditions) {
|
||||
S3Object source = getObject(sourceBucket, sourceKey, sourceVersionId);
|
||||
checkCopySourcePreconditions(source, copySourceConditions);
|
||||
validateSseCustomerAccess(source,
|
||||
copySourceSseCustomerHeaders.algorithm(),
|
||||
copySourceSseCustomerHeaders.key(),
|
||||
@@ -5674,7 +5719,9 @@ public class S3Service implements Resettable, ResourceProvider {
|
||||
.withGrantReadAcp(effectiveOptions.getGrantReadAcp())
|
||||
.withGrantWriteAcp(effectiveOptions.getGrantWriteAcp())
|
||||
.withChecksumAlgorithm(copyChecksumAlgorithm != null ? copyChecksumAlgorithm.name() : null)
|
||||
.withTagging(effectiveTags));
|
||||
.withTagging(effectiveTags)
|
||||
.withIfMatch(effectiveOptions.getIfMatch())
|
||||
.withIfNoneMatch(effectiveOptions.getIfNoneMatch()));
|
||||
}
|
||||
|
||||
private record AnnotationSnapshot(ObjectAnnotation metadata, byte[] payload) {}
|
||||
|
||||
@@ -29,6 +29,10 @@ public class CopyObjectOptions {
|
||||
private String checksumAlgorithm;
|
||||
// Whether annotations travel with the copy: COPY (the default) or EXCLUDE.
|
||||
private String annotationDirective;
|
||||
// Destination preconditions (If-Match / If-None-Match), checked atomically with the write.
|
||||
private String ifMatch;
|
||||
private String ifNoneMatch;
|
||||
private CopySourceConditions copySourceConditions = CopySourceConditions.NONE;
|
||||
|
||||
public String getMetadataDirective() { return metadataDirective; }
|
||||
public CopyObjectOptions withMetadataDirective(String metadataDirective) { this.metadataDirective = metadataDirective; return this; }
|
||||
@@ -104,4 +108,16 @@ public class CopyObjectOptions {
|
||||
|
||||
public String getAnnotationDirective() { return annotationDirective; }
|
||||
public CopyObjectOptions withAnnotationDirective(String annotationDirective) { this.annotationDirective = annotationDirective; return this; }
|
||||
|
||||
public String getIfMatch() { return ifMatch; }
|
||||
public CopyObjectOptions withIfMatch(String ifMatch) { this.ifMatch = ifMatch; return this; }
|
||||
|
||||
public String getIfNoneMatch() { return ifNoneMatch; }
|
||||
public CopyObjectOptions withIfNoneMatch(String ifNoneMatch) { this.ifNoneMatch = ifNoneMatch; return this; }
|
||||
|
||||
public CopySourceConditions getCopySourceConditions() { return copySourceConditions; }
|
||||
public CopyObjectOptions withCopySourceConditions(CopySourceConditions copySourceConditions) {
|
||||
this.copySourceConditions = copySourceConditions != null ? copySourceConditions : CopySourceConditions.NONE;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package io.github.hectorvent.floci.services.s3.model;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* The {@code x-amz-copy-source-if-*} preconditions of a CopyObject or UploadPartCopy request,
|
||||
* evaluated against the source object. Each field is null when its header was absent (or, for the
|
||||
* dates, unparseable, which S3 treats the same way).
|
||||
*/
|
||||
public record CopySourceConditions(String ifMatch, String ifNoneMatch,
|
||||
Instant ifModifiedSince, Instant ifUnmodifiedSince) {
|
||||
|
||||
public static final CopySourceConditions NONE = new CopySourceConditions(null, null, null, null);
|
||||
}
|
||||
Reference in new issue
Block a user