fix(cognito): keep tag operations and DescribeUserPoolDomain in the pool's region (#5133)

* fix(cognito): keep tag operations and DescribeUserPoolDomain in the pool's region

The region guard for user pools checked only requests naming a UserPoolId.
TagResource, UntagResource and ListTagsForResource name the pool by ARN, and
DescribeUserPoolDomain looks a domain up by its name in a store keyed by name
alone, so another region could still read or change a pool's tags and
describe its domain. The handler now applies the same guard to the pool an
ARN names, and refuses a domain whose pool lives in another region with the
not found an unknown domain gets.

* fix(cognito): answer a missing user pool domain with 400 and match the tagged pool's full ARN

ResourceNotFoundException carries no HTTP status of its own in the cognito-idp
model, so it goes out as the awsJson 400; the domain operations sent 404. The
tag operations now also refuse an ARN that names the pool's id under another
region or account, and the region test covers UntagResource.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-05 23:30:30 -05:00
1 parent 8fee66ae61
commit 899211e498
6 files changed
+126 -11

No files matched your search

@@ -46,6 +46,16 @@ public class CognitoJsonHandler {
if (region != null && request.hasNonNull("UserPoolId") && !"UpdateAuthEventFeedback".equals(action)) {
service.requireUserPoolInRegion(request.path("UserPoolId").asText(), region);
}
// The tag operations name the pool by ARN, and a domain is looked up by its name.
if (region != null) {
switch (action) {
case "TagResource", "UntagResource", "ListTagsForResource" ->
service.requireUserPoolArnInRegion(request.path("ResourceArn").asText(), region);
case "DescribeUserPoolDomain" ->
service.requireUserPoolDomainInRegion(request.path("Domain").asText(), region);
default -> { }
}
}
return switch (action) {
case "CreateUserPool" -> handleCreateUserPool(request, region);
case "DescribeUserPool" -> handleDescribeUserPool(request);
@@ -765,6 +765,27 @@ public class CognitoService implements ResourceProvider {
});
}
/** {@link #requireUserPoolInRegion} for an operation that names the pool by its ARN, as tagging does. */
public void requireUserPoolArnInRegion(String resourceArn, String region) {
requireUserPoolInRegion(extractUserPoolIdFromArn(resourceArn), region);
}
/**
* Refuses a domain whose user pool lives in another region, as the same not found an unknown
* domain gets: domains are stored by name alone, but a domain belongs to its pool's region.
*/
public void requireUserPoolDomainInRegion(String domain, String region) {
if (domain == null || domain.isBlank()) {
return;
}
domainStore.get(domain).map(UserPoolDomain::getUserPoolId).flatMap(poolStore::get).ifPresent(pool -> {
String poolRegion = poolRegion(pool);
if (poolRegion != null && !poolRegion.equals(region)) {
throw new AwsException("ResourceNotFoundException", "Domain does not exist", 400);
}
});
}
/** Pools are stored by id for every region; the region comes from the pool ARN, else the id prefix. */
private static String poolRegion(UserPool pool) {
if (pool.getArn() != null && AwsArnUtils.isArn(pool.getArn())) {
@@ -798,9 +819,14 @@ public class CognitoService implements ResourceProvider {
return Set.of(new SupportedResourceType("cognito-idp:userpool", "cognito-idp", true));
}
/** The ARN must name the pool itself: the same id under another region or account is another pool. */
private UserPool describeUserPoolByArn(String resourceArn) {
String poolId = extractUserPoolIdFromArn(resourceArn);
return describeUserPool(poolId);
UserPool pool = describeUserPool(poolId);
if (pool.getArn() != null && !pool.getArn().equals(resourceArn)) {
throw userPoolNotFound(poolId);
}
return pool;
}
public void tagResource(String resourceArn, Map<String, String> tags) {
@@ -1658,7 +1684,7 @@ public class CognitoService implements ResourceProvider {
throw new AwsException("InvalidParameterException", "Domain is required", 400);
}
return domainStore.get(domain)
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Domain does not exist", 404));
.orElseThrow(() -> new AwsException("ResourceNotFoundException", "Domain does not exist", 400));
}
/**
@@ -1713,7 +1739,7 @@ public class CognitoService implements ResourceProvider {
describeUserPool(userPoolId);
UserPoolDomain userPoolDomain = describeUserPoolDomain(domain);
if (!userPoolDomain.getUserPoolId().equals(userPoolId)) {
throw new AwsException("ResourceNotFoundException", "Domain does not exist", 404);
throw new AwsException("ResourceNotFoundException", "Domain does not exist", 400);
}
String previousCertificateArn = userPoolDomain.getCertificateArn();
String certificateArn = previousCertificateArn;
@@ -1758,7 +1784,7 @@ public class CognitoService implements ResourceProvider {
public void deleteUserPoolDomain(String domain, String userPoolId) {
UserPoolDomain userPoolDomain = describeUserPoolDomain(domain);
if (!userPoolDomain.getUserPoolId().equals(userPoolId)) {
throw new AwsException("ResourceNotFoundException", "Domain does not exist", 404);
throw new AwsException("ResourceNotFoundException", "Domain does not exist", 400);
}
domainStore.delete(domain);
if (userPoolDomain.isCustomDomain()) {
@@ -2,9 +2,11 @@ package io.github.hectorvent.floci.core.common;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.json.JsonPath;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.UUID;
import static io.restassured.RestAssured.given;
@@ -216,4 +218,81 @@ class RegionScopedListsIntegrationTest {
.contentType(COGNITO_JSON).body("{\"UserPoolId\": \"" + poolId + "\"}")
.when().post("/").then().statusCode(200);
}
@Test
void cognitoTagsAndDomainsStayInTheirPoolsRegion() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
JsonPath pool = given().header("Authorization", auth(HOME, "cognito-idp"))
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.CreateUserPool")
.contentType(COGNITO_JSON)
.body("{\"PoolName\": \"region-tags-" + suffix + "\", \"UserPoolTags\": {\"team\": \"home\"}}")
.when().post("/").then().statusCode(200)
.extract().jsonPath();
String poolId = pool.getString("UserPool.Id");
String poolArn = pool.getString("UserPool.Arn");
String domain = "region-domain-" + suffix;
given().header("Authorization", auth(HOME, "cognito-idp"))
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.CreateUserPoolDomain")
.contentType(COGNITO_JSON)
.body("{\"UserPoolId\": \"" + poolId + "\", \"Domain\": \"" + domain + "\"}")
.when().post("/").then().statusCode(200);
// Another region cannot read or change the pool's tags, or describe its domain.
given().header("Authorization", auth(OTHER, "cognito-idp"))
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.TagResource")
.contentType(COGNITO_JSON)
.body("{\"ResourceArn\": \"" + poolArn + "\", \"Tags\": {\"team\": \"other\"}}")
.when().post("/").then().statusCode(400)
.body("__type", endsWith("ResourceNotFoundException"));
given().header("Authorization", auth(OTHER, "cognito-idp"))
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.UntagResource")
.contentType(COGNITO_JSON)
.body("{\"ResourceArn\": \"" + poolArn + "\", \"TagKeys\": [\"team\"]}")
.when().post("/").then().statusCode(400)
.body("__type", endsWith("ResourceNotFoundException"));
given().header("Authorization", auth(OTHER, "cognito-idp"))
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.ListTagsForResource")
.contentType(COGNITO_JSON).body("{\"ResourceArn\": \"" + poolArn + "\"}")
.when().post("/").then().statusCode(400)
.body("__type", endsWith("ResourceNotFoundException"));
given().header("Authorization", auth(OTHER, "cognito-idp"))
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.DescribeUserPoolDomain")
.contentType(COGNITO_JSON).body("{\"Domain\": \"" + domain + "\"}")
.when().post("/").then().statusCode(400)
.body("__type", endsWith("ResourceNotFoundException"));
// An ARN naming the pool's id under another region or account names another pool.
String[] arn = poolArn.split(":", 6);
String otherRegionArn = String.join(":", arn[0], arn[1], arn[2], OTHER, arn[4], arn[5]);
String otherAccountArn = String.join(":", arn[0], arn[1], arn[2], arn[3], "111122223333", arn[5]);
for (String foreignArn : List.of(otherRegionArn, otherAccountArn)) {
given().header("Authorization", auth(HOME, "cognito-idp"))
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.TagResource")
.contentType(COGNITO_JSON)
.body("{\"ResourceArn\": \"" + foreignArn + "\", \"Tags\": {\"team\": \"other\"}}")
.when().post("/").then().statusCode(400)
.body("__type", endsWith("ResourceNotFoundException"));
}
given().header("Authorization", auth(HOME, "cognito-idp"))
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.ListTagsForResource")
.contentType(COGNITO_JSON).body("{\"ResourceArn\": \"" + poolArn + "\"}")
.when().post("/").then().statusCode(200)
.body("Tags.team", equalTo("home"));
given().header("Authorization", auth(HOME, "cognito-idp"))
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.DescribeUserPoolDomain")
.contentType(COGNITO_JSON).body("{\"Domain\": \"" + domain + "\"}")
.when().post("/").then().statusCode(200)
.body("DomainDescription.UserPoolId", equalTo(poolId));
given().header("Authorization", auth(HOME, "cognito-idp"))
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.DeleteUserPoolDomain")
.contentType(COGNITO_JSON)
.body("{\"UserPoolId\": \"" + poolId + "\", \"Domain\": \"" + domain + "\"}")
.when().post("/").then().statusCode(200);
given().header("Authorization", auth(HOME, "cognito-idp"))
.header("X-Amz-Target", "AWSCognitoIdentityProviderService.DeleteUserPool")
.contentType(COGNITO_JSON).body("{\"UserPoolId\": \"" + poolId + "\"}")
.when().post("/").then().statusCode(200);
}
}
@@ -513,7 +513,7 @@ class CognitoCfnIntegrationTest {
private static void assertDomainIsGone(String domain) {
cognitoAction("DescribeUserPoolDomain", "{\"Domain\": \"" + domain + "\"}")
.then()
.statusCode(404)
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundException"));
}
@@ -294,7 +294,7 @@ class CognitoCfnProvisionerTest {
@Test
void updateWhosePriorDomainIsGoneCreatesItAgain() {
when(cognito.describeUserPoolDomain(DOMAIN))
.thenThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 404));
.thenThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 400));
when(cognito.createUserPoolDomain(DOMAIN, POOL_ID, Map.of("CertificateArn", CERTIFICATE_ARN), 2))
.thenReturn(domain(DOMAIN, POOL_ID, CLOUDFRONT));
StackResource r = resource(DOMAIN, Map.of("UserPoolId", POOL_ID, "CloudFrontDistribution", CLOUDFRONT));
@@ -311,7 +311,7 @@ class CognitoCfnProvisionerTest {
when(cognito.describeUserPoolDomain("old.example.com"))
.thenReturn(domain("old.example.com", POOL_ID, "dold.cloudfront.net"));
when(cognito.createUserPoolDomain(any(), any(), any(), any())).thenReturn(domain(DOMAIN, POOL_ID, CLOUDFRONT));
doThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 404))
doThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 400))
.when(cognito).deleteUserPoolDomain("old.example.com", POOL_ID);
StackResource r = resource("old.example.com", Map.of("UserPoolId", POOL_ID));
@@ -329,7 +329,7 @@ class CognitoCfnProvisionerTest {
@Test
void deleteToleratesAnAlreadyDeletedDomain() {
doThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 404))
doThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 400))
.when(cognito).deleteUserPoolDomain(DOMAIN, POOL_ID);
assertDoesNotThrow(() -> provisioner.delete(resource(DOMAIN, Map.of("UserPoolId", POOL_ID)), REGION));
@@ -356,7 +356,7 @@ class CognitoCfnProvisionerTest {
@Test
void deleteWithoutARecordedUserPoolIdOfAMissingDomainIsANoOp() {
when(cognito.describeUserPoolDomain(DOMAIN))
.thenThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 404));
.thenThrow(new AwsException("ResourceNotFoundException", "Domain does not exist", 400));
assertDoesNotThrow(() -> provisioner.delete(resource(DOMAIN, Map.of()), REGION));
verify(cognito, never()).deleteUserPoolDomain(any(), any());
@@ -125,7 +125,7 @@ class CognitoUserPoolDomainIntegrationTest {
}
""".formatted(prefixDomain))
.then()
.statusCode(404)
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundException"));
}
@@ -292,7 +292,7 @@ class CognitoUserPoolDomainIntegrationTest {
}
""".formatted(customDomain))
.then()
.statusCode(404)
.statusCode(400)
.body("__type", equalTo("ResourceNotFoundException"));
}