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 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.java index 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("30")); } + // --- 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 = """ + + + GOVERNANCE + 2030-01-01T00:00:00Z + + """; + given() + .body(body) + .when() + .put("/" + LOCK_BUCKET + "/" + RETENTION_KEY + "?retention") + .then() + .statusCode(200); + } + + @Test + @Order(12) + void putObjectRetentionWithEpochSecondsReturns400MalformedXml() { + String body = """ + + + GOVERNANCE + 1577836800 + + """; + 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 = """ + + + GOVERNANCE + notadate + + """; + 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()