mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(s3): answer 501 for PutObject conditions S3 does not implement (#5128)
* fix(s3): answer 501 for PutObject conditions S3 does not implement S3 implements two conditional PutObject forms: If-None-Match: * to create only, and If-Match with an ETag to replace only if unchanged. It answers the other two spellings, If-Match: * and If-None-Match with a literal ETag, with 501 NotImplemented. Floci gave both a meaning (If-Match: * as "must exist", If-None-Match: <etag> as a literal comparison), so code relying on them passed locally and failed against S3. Both now answer 501 before the body is decoded or validated. A quoted "*" is still the If-None-Match wildcard. The two tests that asserted the literal-ETag behaviour now assert 501, and the quoting test keeps its unquoted-value coverage on the If-Match path. * test(s3): cover a malformed aws-chunked body in the 501 ordering test The ordering test exercised checksum validation only, so it would still pass with the guard moved after aws-chunked decoding. A truncated chunked body, 400 IncompleteBody without the header, now answers 501 with it. * fix(s3): judge each member of a conditional header list The precondition check splits If-Match and If-None-Match on commas and treats a "*" anywhere in the list as a match, so If-Match: "<etag>", * passed the 501 guard and then overwrote the object whatever its ETag. The guard now reads the list the same way. If-Match: "*" was already the wildcard to the precondition check and stays 501, now with a test. * fix(s3): keep empty members when judging a conditional header list String.split drops trailing empty strings, so If-None-Match: "," produced no members, passed the 501 guard, and overwrote the object. A limit of -1 keeps them, and an empty member is not the wildcard. * refactor(s3): judge unimplemented PutObject conditions with the service's ETag normalizer The 501 guard ahead of PutObject and checkWritePreconditions after it must read If-Match and If-None-Match the same way, or a spelling one accepts and the other rejects slips through. The guard now calls S3Service's normalizeEntityTag, made package-private static, instead of a copy of it. * docs(s3): keep only the list rule in the 501 guard's comments The normalizer comment claimed the guard reads If-Match and If-None-Match exactly as the precondition check does, which the split limit contradicts for a header of only commas. The guard's block comment restated its name and the tests' ordering; the rule a reader cannot see from the code, that a * in an If-Match list cannot stand in for the ETag beside it, stays.
This commit is contained in:
3 files changed
+81
-29
No files matched your search
@@ -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<String, String> 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<String> 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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
+57
-28
@@ -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"))
|
||||
|
||||
Reference in new issue
Block a user