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.
This commit is contained in:
Ben Younes authored and GitHub committed 2026-05-23 09:04:59 -05:00
1 parent 27d72f119c
commit 71d00613db
2 files changed
+131 -2

No files matched your search

@@ -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());
@@ -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>&lt;region&gt;_&lt;random&gt;</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"));
}
}