Light, fluffy, and always free - The AWS Local Emulator alternative
fix: return 400 MalformedXML for unparseable PutObjectRetention RetainUntilDate (#1631)
Matt Van Horn526a62317ffd
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.javaindex 5926a83c88..ce7a145556 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@@ -46,6 +46,7 @@ import java.nio.charset.StandardCharsets;import java.time.Instant;import java.time.ZoneId;import java.time.format.DateTimeFormatter;+import java.time.format.DateTimeParseException;import java.util.ArrayList;import java.util.Arrays;import java.util.Iterator;@@ -1753,7 +1754,13 @@ public class S3Controller {String xml = new String(body, StandardCharsets.UTF_8);String mode = XmlParser.extractFirst(xml, "Mode", null);String dateStr = XmlParser.extractFirst(xml, "RetainUntilDate", null);- Instant retainUntil = dateStr != null ? Instant.parse(dateStr) : null;+ Instant retainUntil;+ try {+ retainUntil = dateStr != null ? Instant.parse(dateStr) : null;+ } catch (DateTimeParseException | IllegalArgumentException e) {+ throw new AwsException("MalformedXML",+ "The XML you provided was not well-formed.", 400);+ }boolean bypass = "true".equalsIgnoreCase(httpHeaders.getHeaderString("x-amz-bypass-governance-retention"));s3Service.putObjectRetention(bucket, key, versionId, mode, retainUntil, bypass);diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockIntegrationTest.javaindex 3914981602..c41a1e35db 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockIntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3ObjectLockIntegrationTest.java@@ -122,10 +122,83 @@ class S3ObjectLockIntegrationTest {.body(containsString("<Days>30</Days>"));}+ // --- PutObjectRetention RetainUntilDate parsing ---++ private static final String RETENTION_KEY = "retained-object.txt";++ @Test+ @Order(10)+ void putObjectForRetention() {+ given()+ .body("hello")+ .when()+ .put("/" + LOCK_BUCKET + "/" + RETENTION_KEY)+ .then()+ .statusCode(200);+ }++ @Test+ @Order(11)+ void putObjectRetentionWithValidIsoDateReturns200() {+ String body = """+ <?xml version="1.0" encoding="UTF-8"?>+ <Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <Mode>GOVERNANCE</Mode>+ <RetainUntilDate>2030-01-01T00:00:00Z</RetainUntilDate>+ </Retention>+ """;+ given()+ .body(body)+ .when()+ .put("/" + LOCK_BUCKET + "/" + RETENTION_KEY + "?retention")+ .then()+ .statusCode(200);+ }++ @Test+ @Order(12)+ void putObjectRetentionWithEpochSecondsReturns400MalformedXml() {+ String body = """+ <?xml version="1.0" encoding="UTF-8"?>+ <Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <Mode>GOVERNANCE</Mode>+ <RetainUntilDate>1577836800</RetainUntilDate>+ </Retention>+ """;+ given()+ .body(body)+ .when()+ .put("/" + LOCK_BUCKET + "/" + RETENTION_KEY + "?retention")+ .then()+ .statusCode(400)+ .body(containsString("MalformedXML"))+ .body(not(containsString("Internal Server Error")));+ }++ @Test+ @Order(13)+ void putObjectRetentionWithGarbageDateReturns400MalformedXml() {+ String body = """+ <?xml version="1.0" encoding="UTF-8"?>+ <Retention xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <Mode>GOVERNANCE</Mode>+ <RetainUntilDate>notadate</RetainUntilDate>+ </Retention>+ """;+ given()+ .body(body)+ .when()+ .put("/" + LOCK_BUCKET + "/" + RETENTION_KEY + "?retention")+ .then()+ .statusCode(400)+ .body(containsString("MalformedXML"))+ .body(not(containsString("Internal Server Error")));+ }+// --- non-existent bucket ---@Test- @Order(9)+ @Order(14)void nonExistentBucketReturnsNoSuchBucket() {given().when()