Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(s3): compare conditional GET dates at second precision (#5598)

TiMca1d74c49e1e
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 2c5ac6f451..5cb1068a12 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
@@ -68,6 +68,7 @@ import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
+import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
@@ -3568,7 +3569,8 @@ public class S3Controller {
private Response checkPreconditions(S3Object obj, String ifMatch, String ifNoneMatch,
String ifModifiedSince, String ifUnmodifiedSince) {
String eTag = obj.getETag();
- Instant lastModified = obj.getLastModified();
+ // Dates compare at second precision, the resolution of an HTTP date and of Last-Modified.
+ Instant lastModified = obj.getLastModified().truncatedTo(ChronoUnit.SECONDS);
if (ifMatch != null && !eTagMatches(ifMatch, eTag)) {
return preconditionFailedResponse();
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalGetIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalGetIntegrationTest.java
new file mode 100644
index 0000000000..a324b0ee3c
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalGetIntegrationTest.java
@@ -0,0 +1,79 @@
+package io.github.hectorvent.floci.services.s3;
+
+import io.quarkus.test.junit.QuarkusTest;
+import org.junit.jupiter.api.Test;
+
+import java.util.UUID;
+
+import static io.restassured.RestAssured.given;
+import static org.hamcrest.Matchers.equalTo;
+
+/**
+ * A conditional GET or HEAD carrying an object's own Last-Modified, as a client revalidating a cached
+ * copy sends it. Last-Modified has second precision, so the object has not been modified since itself.
+ */
+@QuarkusTest
+class S3ConditionalGetIntegrationTest {
+
+ @Test
+ void getObject_ifModifiedSinceOwnLastModified_304() {
+ String bucket = createBucket("get-if-modified-own");
+ String eTag = putObject(bucket, "obj.txt", "body");
+ String lastModified = lastModified(bucket, "obj.txt");
+
+ given().header("If-Modified-Since", lastModified)
+ .when().get("/" + bucket + "/obj.txt")
+ .then().statusCode(304)
+ .header("ETag", equalTo(eTag))
+ .header("Last-Modified", equalTo(lastModified));
+ }
+
+ @Test
+ void getObject_ifUnmodifiedSinceOwnLastModified_200() {
+ String bucket = createBucket("get-if-unmodified-own");
+ putObject(bucket, "obj.txt", "body");
+ String lastModified = lastModified(bucket, "obj.txt");
+
+ given().header("If-Unmodified-Since", lastModified)
+ .when().get("/" + bucket + "/obj.txt")
+ .then().statusCode(200).body(equalTo("body"));
+ }
+
+ @Test
+ void headObject_ifModifiedSinceOwnLastModified_304() {
+ String bucket = createBucket("head-if-modified-own");
+ putObject(bucket, "obj.txt", "body");
+ String lastModified = lastModified(bucket, "obj.txt");
+
+ given().header("If-Modified-Since", lastModified)
+ .when().head("/" + bucket + "/obj.txt")
+ .then().statusCode(304);
+ }
+
+ @Test
+ void headObject_ifUnmodifiedSinceOwnLastModified_200() {
+ String bucket = createBucket("head-if-unmodified-own");
+ putObject(bucket, "obj.txt", "body");
+ String lastModified = lastModified(bucket, "obj.txt");
+
+ given().header("If-Unmodified-Since", lastModified)
+ .when().head("/" + bucket + "/obj.txt")
+ .then().statusCode(200);
+ }
+
+ private static String createBucket(String label) {
+ String bucket = label + "-" + UUID.randomUUID().toString().substring(0, 8);
+ given().when().put("/" + bucket).then().statusCode(200);
+ return bucket;
+ }
+
+ private static String putObject(String bucket, String key, String body) {
+ return given().body(body).when().put("/" + bucket + "/" + key)
+ .then().statusCode(200).extract().header("ETag");
+ }
+
+ private static String lastModified(String bucket, String key) {
+ return given().when().head("/" + bucket + "/" + key)
+ .then().statusCode(200).extract().header("Last-Modified");
+ }
+}
diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalGetPrecisionIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalGetPrecisionIntegrationTest.java
new file mode 100644
index 0000000000..cfb30fa076
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ConditionalGetPrecisionIntegrationTest.java
@@ -0,0 +1,100 @@
+package io.github.hectorvent.floci.services.s3;
+
+import io.github.hectorvent.floci.services.s3.model.S3Object;
+import io.quarkus.test.junit.QuarkusMock;
+import io.quarkus.test.junit.QuarkusTest;
+import io.restassured.specification.RequestSpecification;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.mockito.Mockito;
+
+import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.util.Locale;
+
+import static io.restassured.RestAssured.given;
+
+/**
+ * Conditional GET and HEAD compare Last-Modified at second precision. Objects are stamped to the
+ * millisecond but Last-Modified goes out to the second, so a client revalidating with the value it
+ * was given sends a date up to 999 ms before the stored one. The stored object is stubbed with a
+ * fixed timestamp, so the sub-second case is exercised on every run rather than whenever a write
+ * happens to land off a whole second.
+ *
+ * <p>The stub is a default answer rather than {@code when(...)} stubbing because background writers
+ * share the mocked S3Service (see {@link S3InternalErrorIntegrationTest}).
+ */
+@QuarkusTest
+class S3ConditionalGetPrecisionIntegrationTest {
+ private static final String BUCKET = "conditional-get-precision";
+ private static final byte[] BODY = "body".getBytes(StandardCharsets.UTF_8);
+ // IMF-fixdate, two-digit day, as Last-Modified is sent.
+ private static final DateTimeFormatter HTTP_DATE = DateTimeFormatter
+ .ofPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US)
+ .withZone(ZoneOffset.UTC);
+ private static final Instant LAST_MODIFIED_SECOND = Instant.parse("2026-01-01T00:00:05Z");
+
+ @BeforeEach
+ void stubObjects() {
+ S3Service stub = Mockito.mock(S3Service.class, invocation -> {
+ String method = invocation.getMethod().getName();
+ if ("headObject".equals(method)) {
+ return storedObject(invocation.getArgument(1));
+ }
+ if ("openObject".equals(method)) {
+ return new S3Service.ObjectRead(storedObject(invocation.getArgument(1)),
+ new ByteArrayInputStream(BODY));
+ }
+ return Mockito.RETURNS_DEFAULTS.answer(invocation);
+ });
+ QuarkusMock.installMockForType(stub, S3Service.class);
+ }
+
+ /** {@code fraction.txt} is stamped 456 ms past the second, {@code whole.txt} on it. */
+ private static S3Object storedObject(String key) {
+ S3Object object = new S3Object(BUCKET, key, BODY, "text/plain");
+ long millis = "fraction.txt".equals(key) ? 456 : 0;
+ object.setLastModified(LAST_MODIFIED_SECOND.plusMillis(millis));
+ return object;
+ }
+
+ @ParameterizedTest(name = "{0} {1} {2} at Last-Modified {3}s -> {4}")
+ @CsvSource({
+ "GET, fraction.txt, If-Modified-Since, -1, 200",
+ "GET, fraction.txt, If-Modified-Since, 0, 304",
+ "GET, fraction.txt, If-Modified-Since, 1, 304",
+ "GET, fraction.txt, If-Unmodified-Since, -1, 412",
+ "GET, fraction.txt, If-Unmodified-Since, 0, 200",
+ "GET, fraction.txt, If-Unmodified-Since, 1, 200",
+ "HEAD, fraction.txt, If-Modified-Since, -1, 200",
+ "HEAD, fraction.txt, If-Modified-Since, 0, 304",
+ "HEAD, fraction.txt, If-Modified-Since, 1, 304",
+ "HEAD, fraction.txt, If-Unmodified-Since, -1, 412",
+ "HEAD, fraction.txt, If-Unmodified-Since, 0, 200",
+ "HEAD, fraction.txt, If-Unmodified-Since, 1, 200",
+ "GET, whole.txt, If-Modified-Since, -1, 200",
+ "GET, whole.txt, If-Modified-Since, 0, 304",
+ "GET, whole.txt, If-Modified-Since, 1, 304",
+ "GET, whole.txt, If-Unmodified-Since, -1, 412",
+ "GET, whole.txt, If-Unmodified-Since, 0, 200",
+ "GET, whole.txt, If-Unmodified-Since, 1, 200",
+ "HEAD, whole.txt, If-Modified-Since, -1, 200",
+ "HEAD, whole.txt, If-Modified-Since, 0, 304",
+ "HEAD, whole.txt, If-Modified-Since, 1, 304",
+ "HEAD, whole.txt, If-Unmodified-Since, -1, 412",
+ "HEAD, whole.txt, If-Unmodified-Since, 0, 200",
+ "HEAD, whole.txt, If-Unmodified-Since, 1, 200",
+ })
+ void dateConditionComparesToTheSecond(String method, String key, String header,
+ long offsetSeconds, int status) {
+ String date = HTTP_DATE.format(LAST_MODIFIED_SECOND.plusSeconds(offsetSeconds));
+ RequestSpecification request = given().header(header, date);
+ String path = "/" + BUCKET + "/" + key;
+ ("HEAD".equals(method) ? request.when().head(path) : request.when().get(path))
+ .then().statusCode(status);
+ }
+}