Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(s3): report EntityTooSmall for a presigned POST below the content-length-range minimum (#5154)

jwoa9558bad471f3fc
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 328b603984..3db36142cd 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
@@ -79,6 +79,7 @@ import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
+import java.util.StringJoiner;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
@@ -3435,10 +3436,10 @@ public class S3Controller {
xmlBuilder.elem("Condition", condition);
}
// S3 InvalidArgument responses carry ArgumentName and ArgumentValue so the SDK can
- // surface which input was rejected, and EntityTooLarge the proposed and allowed sizes.
- // They travel through AwsException.extendedData.
+ // surface which input was rejected, and EntityTooSmall and EntityTooLarge the proposed and
+ // allowed sizes. They travel through AwsException.extendedData.
if (e.getExtendedData() != null) {
- for (String sizeField : List.of("ProposedSize", "MaxSizeAllowed")) {
+ for (String sizeField : List.of("ProposedSize", "MinSizeAllowed", "MaxSizeAllowed")) {
Object size = e.getExtendedData().get(sizeField);
if (size != null) {
xmlBuilder.elem(sizeField, size.toString());
@@ -3855,9 +3856,7 @@ public class S3Controller {
actualValue = fields.get(lookupKey);
}
if (actualValue == null || !actualValue.equals(expectedValue)) {
- throw new AwsException("AccessDenied",
- "Invalid according to Policy: Policy Condition failed: "
- + "[\"eq\", \"$" + fieldName + "\", \"" + expectedValue + "\"]", 403);
+ throw policyConditionFailed("[\"eq\", \"$" + fieldName + "\", \"" + expectedValue + "\"]");
}
}
}
@@ -3869,11 +3868,13 @@ public class S3Controller {
}
String operator = condition.get(0).asText().toLowerCase(Locale.ROOT);
if ("content-length-range".equals(operator)) {
- long min = condition.get(1).asLong();
- long max = condition.get(2).asLong();
- if (contentLength < min || contentLength > max) {
- throw new AwsException("EntityTooLarge",
- "Your proposed upload exceeds the maximum allowed size.", 400);
+ long min = contentLengthBound(condition, 1);
+ long max = contentLengthBound(condition, 2);
+ if (contentLength < min) {
+ throw S3Service.entityTooSmall(contentLength, min);
+ }
+ if (contentLength > max) {
+ throw S3Service.entityTooLarge(contentLength, max);
}
} else if ("eq".equals(operator)) {
String fieldRef = condition.get(1).asText();
@@ -3881,9 +3882,7 @@ public class S3Controller {
String fieldName = fieldRef.startsWith("$") ? fieldRef.substring(1) : fieldRef;
String actualValue = resolveFieldValue(fieldName.toLowerCase(Locale.ROOT), bucket, fields);
if (actualValue == null || !actualValue.equals(expectedValue)) {
- throw new AwsException("AccessDenied",
- "Invalid according to Policy: Policy Condition failed: "
- + "[\"eq\", \"$" + fieldName + "\", \"" + expectedValue + "\"]", 403);
+ throw policyConditionFailed("[\"eq\", \"$" + fieldName + "\", \"" + expectedValue + "\"]");
}
} else if ("starts-with".equals(operator)) {
String fieldRef = condition.get(1).asText();
@@ -3891,11 +3890,43 @@ public class S3Controller {
String fieldName = fieldRef.startsWith("$") ? fieldRef.substring(1) : fieldRef;
String actualValue = resolveFieldValue(fieldName.toLowerCase(Locale.ROOT), bucket, fields);
if (actualValue == null || !actualValue.startsWith(prefix)) {
- throw new AwsException("AccessDenied",
- "Invalid according to Policy: Policy Condition failed: "
- + "[\"starts-with\", \"$" + fieldName + "\", \"" + prefix + "\"]", 403);
+ throw policyConditionFailed("[\"starts-with\", \"$" + fieldName + "\", \"" + prefix + "\"]");
+ }
+ }
+ }
+
+ /**
+ * One bound of a content-length-range condition: a number that fits in a long, any fraction dropped,
+ * or a string holding a whole number that does. Any other bound fails the condition, as S3 answers
+ * {@code ["content-length-range", "test", "10"]}.
+ */
+ private static long contentLengthBound(JsonNode condition, int index) {
+ JsonNode bound = condition.get(index);
+ if (bound.isNumber() && bound.canConvertToLong()) {
+ return bound.longValue();
+ }
+ if (bound.isTextual()) {
+ try {
+ return Long.parseLong(bound.textValue());
+ } catch (NumberFormatException e) {
+ throw policyConditionFailed(policyConditionText(condition));
}
}
+ throw policyConditionFailed(policyConditionText(condition));
+ }
+
+ /** A policy condition as S3 quotes it in an error: its elements as JSON, comma-separated. */
+ private static String policyConditionText(JsonNode condition) {
+ StringJoiner elements = new StringJoiner(", ", "[", "]");
+ for (JsonNode element : condition) {
+ elements.add(element.toString());
+ }
+ return elements.toString();
+ }
+
+ private static AwsException policyConditionFailed(String condition) {
+ return new AwsException("AccessDenied",
+ "Invalid according to Policy: Policy Condition failed: " + condition, 403);
}
private static String resolveFieldValue(String fieldName, String bucket, Map<String, String> fields) {
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 5d823348e3..0e5407121f 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
@@ -2327,6 +2327,18 @@ public class S3Service implements Resettable, ResourceProvider {
Map.of("MaxSizeAllowed", maxSizeAllowed));
}
+ /**
+ * The message of S3's {@code EntityTooSmall} error as S3 sends it, which, like
+ * {@link #ENTITY_TOO_LARGE_MESSAGE}, is not the wording of the error code table's description.
+ */
+ static final String ENTITY_TOO_SMALL_MESSAGE = "Your proposed upload is smaller than the minimum allowed size";
+
+ /** S3's error for an upload under the least it takes, naming the size proposed and the least allowed. */
+ static AwsException entityTooSmall(long proposedSize, long minSizeAllowed) {
+ return new AwsException("EntityTooSmall", ENTITY_TOO_SMALL_MESSAGE, 400,
+ Map.of("ProposedSize", proposedSize, "MinSizeAllowed", minSizeAllowed));
+ }
+
static void requireCopyableSize(S3Object source) {
if (source.getSize() > MAX_COPY_OBJECT_SOURCE_SIZE) {
throw new AwsException("InvalidRequest",
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java
index 975647ac1a..7ea91d23e7 100644
--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3PresignedPostIntegrationTest.java
@@ -1,6 +1,7 @@
package io.github.hectorvent.floci.services.s3;
import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.response.ValidatableResponse;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
@@ -254,26 +255,87 @@ class S3PresignedPostIntegrationTest {
// Create data that exceeds the max content-length-range of 10 bytes
String fileContent = "This content is definitely longer than 10 bytes";
- String policy = buildPolicy(BUCKET, key, "text/plain", 0, 10);
- String policyBase64 = Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8));
+ postTextWithPolicy(key, buildPolicy(BUCKET, key, "text/plain", 0, 10), fileContent)
+ .statusCode(400)
+ .contentType("application/xml")
+ .body(hasXPath("/Error/Code", equalTo("EntityTooLarge")))
+ .body(hasXPath("/Error/Message", equalTo(
+ "Your proposed upload exceeds the maximum allowed size")))
+ .body(hasXPath("/Error/ProposedSize", equalTo(String.valueOf(fileContent.length()))))
+ .body(hasXPath("/Error/MaxSizeAllowed", equalTo("10")));
+ }
+
+ @Test
+ @Order(31)
+ void presignedPostRejectsContentBelowMinimumLength() {
+ String key = "uploads/too-small.txt";
+ // 5 bytes, below the min content-length-range of 10 bytes
+ String fileContent = "12345";
+
+ postTextWithPolicy(key, buildPolicy(BUCKET, key, "text/plain", 10, 20), fileContent)
+ .statusCode(400)
+ .contentType("application/xml")
+ .body(hasXPath("/Error/Code", equalTo("EntityTooSmall")))
+ .body(hasXPath("/Error/Message", equalTo(
+ "Your proposed upload is smaller than the minimum allowed size")))
+ .body(hasXPath("/Error/ProposedSize", equalTo("5")))
+ .body(hasXPath("/Error/MinSizeAllowed", equalTo("10")));
given()
- .multiPart("key", key)
- .multiPart("Content-Type", "text/plain")
- .multiPart("policy", policyBase64)
- .multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256")
- .multiPart("x-amz-credential", "AKIAIOSFODNN7EXAMPLE/20260330/us-east-1/s3/aws4_request")
- .multiPart("x-amz-date", AMZ_DATE_FORMAT.format(Instant.now()))
- .multiPart("x-amz-signature", "dummysignature")
- .multiPart("file", "too-large.txt", fileContent.getBytes(StandardCharsets.UTF_8), "text/plain")
.when()
- .post("/" + BUCKET)
+ .head("/" + BUCKET + "/" + key)
.then()
- .statusCode(400)
+ .statusCode(404);
+ }
+
+ @Test
+ @Order(32)
+ void presignedPostAcceptsContentLengthRangeWrittenAsStrings() {
+ String key = "uploads/string-range.txt";
+
+ postTextWithPolicy(key, buildPolicy(BUCKET, key, "text/plain", "\"5\", \"10\""), "1234567890")
+ .statusCode(204);
+ }
+
+ @Test
+ @Order(33)
+ void presignedPostRejectsContentLengthRangeThatIsNotANumber() {
+ String key = "uploads/not-a-number-range.txt";
+
+ postTextWithPolicy(key, buildPolicy(BUCKET, key, "text/plain", "\"test\", \"10\""), "1234567890")
+ .statusCode(403)
.contentType("application/xml")
- .body(hasXPath("/Error/Code", equalTo("EntityTooLarge")))
+ .body(hasXPath("/Error/Code", equalTo("AccessDenied")))
.body(hasXPath("/Error/Message", equalTo(
- "Your proposed upload exceeds the maximum allowed size.")));
+ "Invalid according to Policy: Policy Condition failed: "
+ + "[\"content-length-range\", \"test\", \"10\"]")));
+
+ given()
+ .when()
+ .head("/" + BUCKET + "/" + key)
+ .then()
+ .statusCode(404);
+ }
+
+ @Test
+ @Order(34)
+ void presignedPostRejectsContentLengthRangeBoundPastALong() {
+ String key = "uploads/past-a-long-range.txt";
+
+ // 2^64 - 1 would wrap to -1 as a long, refusing every upload as too large.
+ postTextWithPolicy(key, buildPolicy(BUCKET, key, "text/plain", "0, 18446744073709551615"), "1234567890")
+ .statusCode(403)
+ .contentType("application/xml")
+ .body(hasXPath("/Error/Code", equalTo("AccessDenied")))
+ .body(hasXPath("/Error/Message", equalTo(
+ "Invalid according to Policy: Policy Condition failed: "
+ + "[\"content-length-range\", 0, 18446744073709551615]")));
+
+ given()
+ .when()
+ .head("/" + BUCKET + "/" + key)
+ .then()
+ .statusCode(404);
}
@Test
@@ -660,6 +722,7 @@ class S3PresignedPostIntegrationTest {
given().delete("/" + BUCKET + "/uploads/no-policy.txt");
given().delete("/" + BUCKET + "/uploads/typed-file.json");
given().delete("/" + BUCKET + "/uploads/within-range.txt");
+ given().delete("/" + BUCKET + "/uploads/string-range.txt");
given().delete("/" + BUCKET + "/uploads/prefix-test.txt");
given().delete("/" + BUCKET + "/uploads/capital-p-ok.txt");
given().delete("/" + BUCKET + "/uploads/with-metadata.txt");
@@ -671,7 +734,27 @@ class S3PresignedPostIntegrationTest {
.statusCode(204);
}
+ private ValidatableResponse postTextWithPolicy(String key, String policy, String fileContent) {
+ String fileName = key.substring(key.lastIndexOf('/') + 1);
+ return given()
+ .multiPart("key", key)
+ .multiPart("Content-Type", "text/plain")
+ .multiPart("policy", Base64.getEncoder().encodeToString(policy.getBytes(StandardCharsets.UTF_8)))
+ .multiPart("x-amz-algorithm", "AWS4-HMAC-SHA256")
+ .multiPart("x-amz-credential", "AKIAIOSFODNN7EXAMPLE/20260330/us-east-1/s3/aws4_request")
+ .multiPart("x-amz-date", AMZ_DATE_FORMAT.format(Instant.now()))
+ .multiPart("x-amz-signature", "dummysignature")
+ .multiPart("file", fileName, fileContent.getBytes(StandardCharsets.UTF_8), "text/plain")
+ .when()
+ .post("/" + BUCKET)
+ .then();
+ }
+
private String buildPolicy(String bucket, String key, String contentType, long minSize, long maxSize) {
+ return buildPolicy(bucket, key, contentType, minSize + ", " + maxSize);
+ }
+
+ private String buildPolicy(String bucket, String key, String contentType, String contentLengthRange) {
String expiration = Instant.now().plusSeconds(3600)
.atZone(ZoneOffset.UTC)
.format(DateTimeFormatter.ISO_INSTANT);
@@ -682,10 +765,10 @@ class S3PresignedPostIntegrationTest {
{"bucket": "%s"},
{"key": "%s"},
{"Content-Type": "%s"},
- ["content-length-range", %d, %d]
+ ["content-length-range", %s]
]
}
- """.formatted(expiration, bucket, key, contentType, minSize, maxSize);
+ """.formatted(expiration, bucket, key, contentType, contentLengthRange);
}
private String buildStartsWithPolicy(String bucket, String keyPrefix, String contentTypePrefix,