diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java index 0057083e03..933e532800 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Controller.java @@ -76,6 +76,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.function.Function; +import java.util.function.Predicate; import java.util.stream.Stream; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; @@ -870,6 +871,8 @@ public class S3Controller { return handleCopyObject(copySource, bucket, key, contentType, httpHeaders, uriInfo, authorization); } + rejectUnimplementedPutConditions(ifMatch, ifNoneMatch); + Map inlineTags = parseInlineTaggingHeader(resolveInlineTaggingSource(tagging, uriInfo)); String lockMode = httpHeaders.getHeaderString("x-amz-object-lock-mode"); @@ -3407,6 +3410,26 @@ public class S3Controller { return null; } + // Members are judged one by one, so a "*" inside an If-Match list cannot stand in for the ETag + // it is listed beside. + private static void rejectUnimplementedPutConditions(String ifMatch, String ifNoneMatch) { + if ((ifMatch != null && anyEntityTag(ifMatch, "*"::equals)) + || (ifNoneMatch != null && anyEntityTag(ifNoneMatch, tag -> !"*".equals(tag)))) { + throw new AwsException("NotImplemented", + "A header you provided implies functionality that is not implemented.", 501); + } + } + + private static boolean anyEntityTag(String headerValue, Predicate test) { + // A limit of -1 keeps empty members, so a header of only commas is still judged. + for (String candidate : headerValue.split(",", -1)) { + if (test.test(S3Service.normalizeEntityTag(candidate))) { + return true; + } + } + return false; + } + private boolean hasPreconditions(String ifMatch, String ifNoneMatch, String ifModifiedSince, String ifUnmodifiedSince) { return ifMatch != null || ifNoneMatch != null || ifModifiedSince != null || ifUnmodifiedSince != null; diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java index d4e596c4fd..6a1cf1a0b0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3Service.java @@ -1005,7 +1005,7 @@ public class S3Service implements Resettable, ResourceProvider { } } - private String normalizeEntityTag(String value) { + static 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); diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalWriteIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalWriteIntegrationTest.java index f2e775f841..51077d5c8d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalWriteIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalWriteIntegrationTest.java @@ -99,36 +99,66 @@ class S3ConditionalWriteIntegrationTest { } @Test - void putObject_ifNoneMatchEtag_succeedsWhenEtagDiffers() { - String bucket = createBucket("put-if-none-different"); - putObject(bucket, "object.txt", "first"); + void putObject_ifNoneMatchEtag_501AndDoesNotWrite() { + String bucket = createBucket("put-if-none-etag"); + String eTag = putObject(bucket, "object.txt", "first"); - given() - .header("If-None-Match", "\"not-the-current-etag\"") - .body("second") - .when() - .put("/" + bucket + "/object.txt") - .then() - .statusCode(200); + // S3 implements If-None-Match only as "*". A literal ETag, matching or not, is 501. + for (String ifNoneMatch : List.of(eTag, "\"not-the-current-etag\"", "*, " + eTag, ",")) { + assertNotImplemented(given() + .header("If-None-Match", ifNoneMatch) + .body("second") + .when() + .put("/" + bucket + "/object.txt") + .then()); + } - assertObjectBody(bucket, "object.txt", "second"); + assertObjectBody(bucket, "object.txt", "first"); } @Test - void putObject_ifNoneMatchEtag_412WhenEtagMatches() { - String bucket = createBucket("put-if-none-match"); - String eTag = putObject(bucket, "object.txt", "first"); + void putObject_ifMatchStar_501AndDoesNotWrite() { + String bucket = createBucket("put-if-match-star"); + putObject(bucket, "object.txt", "first"); - ValidatableResponse response = given() - .header("If-None-Match", eTag) + for (String key : List.of("object.txt", "missing.txt")) { + for (String ifMatch : List.of("*", "\"*\"", STALE_ETAG + ", *")) { + assertNotImplemented(given() + .header("If-Match", ifMatch) + .body("second") + .when() + .put("/" + bucket + "/" + key) + .then()); + } + } + + assertObjectBody(bucket, "object.txt", "first"); + given().when().get("/" + bucket + "/missing.txt").then().statusCode(404); + } + + @Test + void putObject_unimplementedCondition_isAnsweredBeforeTheBodyIsDecodedOrValidated() { + String bucket = createBucket("put-if-match-star-checksum"); + + assertNotImplemented(given() + .header("If-Match", "*") + .header("x-amz-checksum-crc32", "INVALID==") .body("second") .when() .put("/" + bucket + "/object.txt") - .then(); + .then()); - assertPreconditionFailed(response, "If-None-Match"); + // A truncated aws-chunked body is 400 IncompleteBody without the header. + assertNotImplemented(given() + .header("If-Match", "*") + .header("x-amz-content-sha256", "STREAMING-UNSIGNED-PAYLOAD-TRAILER") + .header("Content-Encoding", "aws-chunked") + .body("5\r\nhello\r\n") + .when() + .put("/" + bucket + "/object.txt") + .then()); - assertObjectBody(bucket, "object.txt", "first"); + given().when().get("/" + bucket + "/object.txt").then().statusCode(404); } @Test @@ -177,21 +207,15 @@ class S3ConditionalWriteIntegrationTest { .then() .statusCode(200); - String currentETag = given() - .when() - .head("/" + bucket + "/object.txt") - .then() - .statusCode(200) - .extract().header("ETag"); - + // The object changed above, so the original ETag is stale, quoted or not. ValidatableResponse response = given() - .header("If-None-Match", stripQuotes(currentETag)) + .header("If-Match", stripQuotes(eTag)) .body("third") .when() .put("/" + bucket + "/object.txt") .then(); - assertPreconditionFailed(response, "If-None-Match"); + assertPreconditionFailed(response, "If-Match"); response = given() .header("If-None-Match", "\"*\"") @@ -510,6 +534,11 @@ class S3ConditionalWriteIntegrationTest { .body(equalTo(body)); } + private static void assertNotImplemented(ValidatableResponse response) { + response.statusCode(501) + .body("Error.Code", equalTo("NotImplemented")); + } + private static void assertPreconditionFailed(ValidatableResponse response, String condition) { response.statusCode(412) .body("Error.Code", equalTo("PreconditionFailed"))