From 71d00613db879666eade4253bfa91338fb079fca Mon Sep 17 00:00:00 2001 From: Ben Younes Date: Sat, 23 May 2026 16:04:59 +0200 Subject: [PATCH] fix(s3): unblock .well-known/* object keys hijacked by Cognito routes (#1003) S3 object keys under .well-known/ (e.g. bucket/.well-known/jwks.json) were being routed to CognitoWellKnownController instead of S3Controller. The controller threw ResourceNotFoundException ("User pool not found"), masking the S3 object even though list-objects-v2 and head-object returned it. Real Cognito UserPool IDs always contain an underscore (region prefix + underscore + random suffix, e.g. us-east-1_xxx); AWS S3 bucket names forbid underscores. Constraining the {poolId} path parameter to "[^/_]+_[^/]+" ensures that any first path segment without an underscore falls through to S3Controller, restoring the documented S3 behaviour without affecting existing Cognito tests. Adds S3WellKnownKeyIntegrationTest covering both jwks.json and openid-configuration object keys; verified failing on main and passing with this fix. --- .../cognito/CognitoWellKnownController.java | 14 ++- .../s3/S3WellKnownKeyIntegrationTest.java | 119 ++++++++++++++++++ 2 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 src/test/java/io/github/hectorvent/floci/services/s3/S3WellKnownKeyIntegrationTest.java 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.java index 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 + * + *

The {@code poolId} path parameter is constrained to {@link #POOL_ID_PATTERN} + * so that S3 object keys like {@code //.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.java new file mode 100644 index 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 .well-known/ must NOT collide with the Cognito + * well-known OIDC endpoints (/{poolId}/.well-known/jwks.json and + * /{poolId}/.well-known/openid-configuration). + * + *

Discriminator: real Cognito UserPool IDs always contain an underscore + * (<region>_<random>, e.g. us-east-1_xxx), + * 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")); + } +}