Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): reject unknown bucket location constraints (#4259)
Aniket Shukla179835f835da
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 d7f6469099..64df200689 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@@ -6,6 +6,7 @@ import io.github.hectorvent.floci.core.common.AccountResolver;import io.github.hectorvent.floci.core.common.AwsArnUtils;import io.github.hectorvent.floci.core.common.AwsException;import io.github.hectorvent.floci.core.common.AwsNamespaces;+import io.github.hectorvent.floci.core.common.AwsRegions;import io.github.hectorvent.floci.core.common.IamEnforcementFilter;import io.github.hectorvent.floci.core.common.XmlBuilder;import io.github.hectorvent.floci.core.common.XmlParser;@@ -60,7 +61,6 @@ import java.util.Locale;import java.util.Map;import java.util.Optional;import java.util.Set;-import java.util.regex.Pattern;import javax.xml.stream.XMLStreamConstants;import javax.xml.stream.XMLStreamException;import javax.xml.stream.XMLStreamReader;@@ -85,7 +85,6 @@ public class S3Controller {.ofPattern("EEE, dd MMM yyyy HH:mm:ss z", Locale.US).withZone(ZoneId.of("GMT"));private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();- private static final Pattern LOCATION_CONSTRAINT_PATTERN = Pattern.compile("[a-z]{2}(-[a-z]+)+-\\d+");private final S3Service s3Service;private final S3SelectService s3SelectService;@@ -4152,6 +4151,6 @@ public class S3Controller {}private static boolean isValidLocationConstraint(String value) {- return "EU".equalsIgnoreCase(value) || LOCATION_CONSTRAINT_PATTERN.matcher(value).matches();+ return "EU".equalsIgnoreCase(value) || AwsRegions.isRegionId(value);}}diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.javaindex 4fe927559c..4e52fb4251 100644--- a/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3IntegrationTest.java@@ -488,6 +488,64 @@ class S3IntegrationTest {.body(containsString("InvalidLocationConstraint"));}+ @Test+ void createBucketRejectsUnknownLocationConstraintWithoutCreatingBucket() {+ for (String invalidRegion : List.of("polygondwanaland-west-1", "us-fake-9")) {+ String bucket = "unknown-region-" + invalidRegion;+ String createBucketConfiguration = """+ <CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <LocationConstraint>%s</LocationConstraint>+ </CreateBucketConfiguration>+ """.formatted(invalidRegion);++ given()+ .contentType("application/xml")+ .body(createBucketConfiguration)+ .when()+ .put("/" + bucket)+ .then()+ .statusCode(400)+ .body(containsString("InvalidLocationConstraint"));++ given()+ .when()+ .head("/" + bucket)+ .then()+ .statusCode(404);+ }+ }++ @Test+ void createBucketAcceptsKnownRegionOutsideAdvertisedRegionList() {+ String bucket = "ap-south-two-bucket";+ String createBucketConfiguration = """+ <CreateBucketConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">+ <LocationConstraint>ap-south-2</LocationConstraint>+ </CreateBucketConfiguration>+ """;++ given()+ .contentType("application/xml")+ .body(createBucketConfiguration)+ .when()+ .put("/" + bucket)+ .then()+ .statusCode(200);++ given()+ .when()+ .head("/" + bucket)+ .then()+ .statusCode(200)+ .header("x-amz-bucket-region", equalTo("ap-south-2"));++ given()+ .when()+ .delete("/" + bucket)+ .then()+ .statusCode(204);+ }+@Testvoid createBucketRejectsOverlyLongBucketName() {String longBucketName = "30388849b0eaef3dfba3aa83849d28987be6fb7920bdbf3233bdc8e966f73870.json";