Light, fluffy, and always free - The AWS Local Emulator alternative
fix(s3): unblock .well-known/* object keys hijacked by Cognito routes (#1003)
Ben Younes71d00613db87
diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoWellKnownController.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoWellKnownController.javaindex 9f60676b02..a56352abea 100644--- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoWellKnownController.java+++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoWellKnownController.java@@ -16,11 +16,21 @@ import java.util.Base64;* Exposes Cognito well-known endpoints.* The JWKS endpoint allows downstream services to verify JWTs issued by Floci Cognito pools.* Path mirrors real AWS: /{userPoolId}/.well-known/jwks.json+ *+ * <p>The {@code poolId} path parameter is constrained to {@link #POOL_ID_PATTERN}+ * so that S3 object keys like {@code /<bucket>/.well-known/jwks.json} do not+ * collide with this route. Real Cognito UserPool IDs always contain an+ * underscore (region prefix + {@code _} + random suffix, e.g.+ * {@code us-east-1_AbC123XyZ}), while AWS S3 bucket names forbid underscores+ * — using {@code _} as the discriminator routes S3 traffic correctly without+ * needing a wider request filter.*/@Path("/")@Produces(MediaType.APPLICATION_JSON)public class CognitoWellKnownController {+ private static final String POOL_ID_PATTERN = "[^/_]+_[^/]+";+private final CognitoService cognitoService;@Inject@@ -29,7 +39,7 @@ public class CognitoWellKnownController {}@GET- @Path("/{poolId}/.well-known/jwks.json")+ @Path("/{poolId:" + POOL_ID_PATTERN + "}/.well-known/jwks.json")public Response getJwks(@PathParam("poolId") String poolId) {UserPool pool = cognitoService.describeUserPool(poolId);String kid = cognitoService.getSigningKeyId(pool);@@ -44,7 +54,7 @@ public class CognitoWellKnownController {}@GET- @Path("/{poolId}/.well-known/openid-configuration")+ @Path("/{poolId:" + POOL_ID_PATTERN + "}/.well-known/openid-configuration")public Response getOpenIdConfiguration(@PathParam("poolId") String poolId) {UserPool pool = cognitoService.describeUserPool(poolId);String issuer = cognitoService.getIssuer(pool.getId());diff --git a/src/test/java/io/github/hectorvent/floci/services/s3/S3WellKnownKeyIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/s3/S3WellKnownKeyIntegrationTest.javanew file mode 100644index 0000000000..3bf7f20231--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/s3/S3WellKnownKeyIntegrationTest.java@@ -0,0 +1,119 @@+package io.github.hectorvent.floci.services.s3;++import io.quarkus.test.junit.QuarkusTest;+import org.junit.jupiter.api.MethodOrderer;+import org.junit.jupiter.api.Order;+import org.junit.jupiter.api.Test;+import org.junit.jupiter.api.TestMethodOrder;++import static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;++/**+ * S3 keys under <code>.well-known/</code> must NOT collide with the Cognito+ * well-known OIDC endpoints (<code>/{poolId}/.well-known/jwks.json</code> and+ * <code>/{poolId}/.well-known/openid-configuration</code>).+ *+ * <p>Discriminator: real Cognito UserPool IDs always contain an underscore+ * (<code><region>_<random></code>, e.g. <code>us-east-1_xxx</code>),+ * while AWS S3 bucket names forbid underscores. Any first path segment without+ * an underscore therefore cannot be a Cognito pool id and must be routed to+ * S3.+ */+@QuarkusTest+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class S3WellKnownKeyIntegrationTest {++ private static final String BUCKET = "test-bucket-jwks-967";+ private static final String JWKS_KEY = ".well-known/jwks.json";+ private static final String SIBLING_KEY = ".well-known/test.json";+ private static final String JWKS_BODY = "{\"keys\":[{\"kty\":\"RSA\"}]}";+ private static final String SIBLING_BODY = "{\"hello\":\"world\"}";++ @Test+ @Order(0)+ void createBucket() {+ given()+ .when()+ .put("/" + BUCKET)+ .then()+ .statusCode(200);+ }++ @Test+ @Order(1)+ void putAndGetSiblingWellKnownObject() {+ given()+ .header("Content-Type", "application/json")+ .body(SIBLING_BODY)+ .when()+ .put("/" + BUCKET + "/" + SIBLING_KEY)+ .then()+ .statusCode(200);++ given()+ .when()+ .get("/" + BUCKET + "/" + SIBLING_KEY)+ .then()+ .statusCode(200)+ .body(equalTo(SIBLING_BODY));+ }++ @Test+ @Order(2)+ void putAndGetJwksObject_mustNotBeRoutedToCognito() {+ given()+ .header("Content-Type", "application/json")+ .body(JWKS_BODY)+ .when()+ .put("/" + BUCKET + "/" + JWKS_KEY)+ .then()+ .statusCode(200);++ given()+ .when()+ .get("/" + BUCKET + "/" + JWKS_KEY)+ .then()+ .statusCode(200)+ .body(equalTo(JWKS_BODY));+ }++ @Test+ @Order(3)+ void getJwksObject_mustNotReturnCognitoErrorBody() {+ String body = given()+ .when()+ .get("/" + BUCKET + "/" + JWKS_KEY)+ .then()+ .extract().asString();++ if (body.contains("ResourceNotFoundException") || body.contains("User pool not found")) {+ throw new AssertionError(+ "GET /" + BUCKET + "/" + JWKS_KEY+ + " was routed to Cognito instead of S3. Response: " + body);+ }+ }++ @Test+ @Order(4)+ void getOpenIdConfigurationObject_mustNotBeRoutedToCognito() {+ String openIdKey = ".well-known/openid-configuration";+ String openIdBody = "{\"issuer\":\"custom\"}";++ given()+ .header("Content-Type", "application/json")+ .body(openIdBody)+ .when()+ .put("/" + BUCKET + "/" + openIdKey)+ .then()+ .statusCode(200);++ given()+ .when()+ .get("/" + BUCKET + "/" + openIdKey)+ .then()+ .statusCode(200)+ .body(containsString("custom"));+ }+}