fix(s3): reject unknown bucket location constraints (#4259)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-24 07:08:50 -04:00
1 parent 0a601d54eb
commit 179835f835
2 files changed
+60 -3

No files matched your search

@@ -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);
}
}
@@ -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);
}
@Test
void createBucketRejectsOverlyLongBucketName() {
String longBucketName = "30388849b0eaef3dfba3aa83849d28987be6fb7920bdbf3233bdc8e966f73870.json";