fix: scope event source mappings, DynamoDB jobs, scaling activities and user pools to the request region (#5056)

* fix: scope Lambda event source mappings, DynamoDB export and import jobs, Auto Scaling activities and Cognito user pools to the request region

These resources are stored for every region in an account, and their list
and lookup paths returned all of them, so a request signed for one region
saw another region's resources:

- Lambda: GetEventSourceMapping and ListEventSourceMappings (and update and
  delete, which go through the lookup) keep to the mapping's region; a
  mapping persisted before its region was recorded uses its function ARN's.
- DynamoDB: ListExports, ListImports, DescribeExport and DescribeImport
  keep to the region in the job's ARN.
- Auto Scaling: scaling activities record their group's region and
  DescribeScalingActivities filters on it; activities persisted before it
  was recorded stay visible.
- Cognito: ListUserPools keeps to the region in the pool ARN, else the pool
  id's prefix.

Each case is covered by a test that creates the resource in eu-west-1 and
reads it from eu-west-1 and us-west-2; all four fail without this change.

* fix(dynamodb): look up a replayed ImportTable client token in the request region only

DescribeImport and ListImports now see only the request region's jobs, but
ImportTable still matched a ClientToken across every region, so replaying a
token from a second region returned the first region's import, which that
region's DescribeImport then reported as not found. The token lookup now
takes the same region filter, and RegionScopedListsIntegrationTest covers the
replay.

* fix(cognito): refuse a user pool from another region on every signed operation

ListUserPools only showed the request region's pools, but DescribeUserPool,
DeleteUserPool, AdminGetUser and the rest looked a pool up by id alone, so a
caller in another region could still read or delete it. AWS serves a pool
only in the region its id names. Every operation that names a UserPoolId is
signed for the caller's region, except the unsigned UpdateAuthEventFeedback,
so the handler now refuses such a pool as not found before dispatching; the
unsigned sign-up and sign-in flows, the OIDC endpoints and the API Gateway
authorizer are unchanged. RegionScopedListsIntegrationTest covers describe,
AdminGetUser and delete from another region.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-05 00:30:39 -04:00
1 parent d97b21ae62
commit d9b2707ad4
8 files changed
+356 -1

No files matched your search

@@ -1021,6 +1021,7 @@ public class AutoScalingService {
public List<ScalingActivity> describeScalingActivities(String region, String asgName) {
return activities.values().stream()
.filter(a -> a.getRegion() == null || a.getRegion().equals(region))
.filter(a -> asgName == null || asgName.equals(a.getAutoScalingGroupName()))
.sorted(Comparator.comparing(ScalingActivity::getStartTime).reversed())
.collect(Collectors.toList());
@@ -1031,6 +1032,7 @@ public class AutoScalingService {
ScalingActivity activity = new ScalingActivity();
activity.setActivityId(UUID.randomUUID().toString());
activity.setAutoScalingGroupName(asgName);
activity.setRegion(region);
activity.setDescription(description);
activity.setCause(cause);
activity.setStartTime(Instant.now());
@@ -18,6 +18,8 @@ public class ScalingActivity {
private String statusCode; // InProgress | Successful | Failed | Cancelled
private String statusMessage;
private int progress; // 0-100
// The Auto Scaling group's region; null for activities persisted before it was recorded.
private String region;
public ScalingActivity() {}
@@ -45,6 +47,9 @@ public class ScalingActivity {
public String getStatusMessage() { return statusMessage; }
public void setStatusMessage(String v) { this.statusMessage = v; }
public String getRegion() { return region; }
public void setRegion(String v) { this.region = v; }
public int getProgress() { return progress; }
public void setProgress(int v) { this.progress = v; }
}
@@ -41,6 +41,11 @@ public class CognitoJsonHandler {
}
public Response handle(String action, JsonNode request, String region) {
// Every operation naming a UserPoolId is signed for the caller's region, except the unsigned
// UpdateAuthEventFeedback, whose region Floci cannot know.
if (region != null && request.hasNonNull("UserPoolId") && !"UpdateAuthEventFeedback".equals(action)) {
service.requireUserPoolInRegion(request.path("UserPoolId").asText(), region);
}
return switch (action) {
case "CreateUserPool" -> handleCreateUserPool(request, region);
case "DescribeUserPool" -> handleDescribeUserPool(request);
@@ -743,7 +743,36 @@ public class CognitoService implements ResourceProvider {
}
public List<UserPool> listUserPools() {
return poolStore.scan(k -> true);
String region = regionResolver.getRegion();
return poolStore.scan(k -> true).stream()
.filter(pool -> {
String poolRegion = poolRegion(pool);
return poolRegion == null || poolRegion.equals(region);
})
.toList();
}
/**
* Refuses a pool from another region as not found, as AWS does: a pool lives in the region its
* id names. A pool whose region cannot be told stays reachable, as {@link #listUserPools} lists it.
*/
public void requireUserPoolInRegion(String userPoolId, String region) {
poolStore.get(userPoolId).ifPresent(pool -> {
String poolRegion = poolRegion(pool);
if (poolRegion != null && !poolRegion.equals(region)) {
throw userPoolNotFound(userPoolId);
}
});
}
/** 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())) {
return AwsArnUtils.parse(pool.getArn()).region();
}
String id = pool.getId();
int underscore = id == null ? -1 : id.indexOf('_');
return underscore > 0 ? id.substring(0, underscore) : null;
}
@Override
@@ -4725,10 +4725,19 @@ public class DynamoDbService {
"Export not found: " + exportArn, 400);
}
return exportStore.get(exportArn)
.filter(d -> inRequestRegion(d.getExportArn()))
.orElseThrow(() -> new AwsException("ExportNotFoundException",
"Export not found: " + exportArn, 400));
}
/** Export and import jobs are stored by ARN for every region; a request sees only its own region's. */
private boolean inRequestRegion(String jobArn) {
if (jobArn == null || !AwsArnUtils.isArn(jobArn)) {
return true;
}
return AwsArnUtils.parse(jobArn).region().equals(regionResolver.getRegion());
}
public record ListExportsResult(List<ExportSummary> exportSummaries, String nextToken) {}
public ListExportsResult listExports(String tableArn, Integer maxResults, String nextToken) {
@@ -4736,6 +4745,7 @@ public class DynamoDbService {
return new ListExportsResult(List.of(), null);
}
var all = exportStore.scan(k -> true).stream()
.filter(d -> inRequestRegion(d.getExportArn()))
.filter(d -> tableArn == null || tableArn.equals(d.getTableArn()))
.toList();
requirePageSize(maxResults, "maxResults");
@@ -4842,6 +4852,7 @@ public class DynamoDbService {
return null;
}
var existing = importStore.scan(k -> true).stream()
.filter(d -> inRequestRegion(d.getImportArn()))
.filter(d -> clientToken.equals(d.getClientToken()))
.findFirst()
.orElse(null);
@@ -5042,6 +5053,7 @@ public class DynamoDbService {
public ImportTableDescription describeImport(String importArn) {
return Optional.ofNullable(importStore)
.flatMap(store -> store.get(importArn))
.filter(d -> inRequestRegion(d.getImportArn()))
.orElseThrow(() -> new AwsException("ImportNotFoundException",
"The specified import was not found.", 400));
}
@@ -5053,6 +5065,7 @@ public class DynamoDbService {
return new ListImportsResult(List.of(), null);
}
var all = importStore.scan(k -> true).stream()
.filter(d -> inRequestRegion(d.getImportArn()))
.filter(d -> tableArn == null || tableArn.equals(d.getTableArn()))
.toList();
requirePageSize(pageSize, "pageSize");
@@ -2010,10 +2010,23 @@ public class LambdaService implements ResourceProvider {
public EventSourceMapping getEventSourceMapping(String uuid) {
return esmStore.get(uuid)
.filter(this::inRequestRegion)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"EventSourceMapping not found: " + uuid, 404));
}
/**
* Mappings are stored by UUID for every region, so a request sees only its own region's. A mapping
* persisted before its region was recorded takes the region of its function ARN.
*/
private boolean inRequestRegion(EventSourceMapping esm) {
String region = esm.getRegion();
if (region == null && esm.getFunctionArn() != null && AwsArnUtils.isArn(esm.getFunctionArn())) {
region = AwsArnUtils.parse(esm.getFunctionArn()).region();
}
return region == null || region.equals(regionResolver.getRegion());
}
public List<EventSourceMapping> listEventSourceMappings(String functionArn) {
return listEventSourceMappings(functionArn, null);
}
@@ -2030,6 +2043,7 @@ public class LambdaService implements ResourceProvider {
} else {
mappings = esmStore.list();
}
mappings = mappings.stream().filter(this::inRequestRegion).toList();
if (eventSourceArn != null && !eventSourceArn.isBlank()) {
mappings = mappings.stream()
.filter(esm -> eventSourceArn.equals(esm.getEventSourceArn()))