fix(stepfunctions): scope callbacks and workers to execution region (#7633)

This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-11-05 15:48:46 -04:00
1 parent 0c33083803
commit 36fbc33a2f
6 files changed
+169 -15

No files matched your search

@@ -580,8 +580,8 @@ public class AslExecutor {
}
/**
* Runs {@code body} on this worker thread under a CDI request scope whose account is
* the one encoded in the state machine ARN, so service integrations (Lambda, DynamoDB,
* Runs {@code body} on this worker thread under a CDI request scope whose account and region
* are encoded in the state machine ARN, so service integrations (Lambda, DynamoDB,
* SQS, ECS, …) and the execution-store writes resolve to the execution's account rather
* than the configured default. Without this, an execution started under account B would
* have its integrations run against account A's resources.
@@ -610,14 +610,15 @@ public class AslExecutor {
* run Parallel branches on their own worker threads under the execution's account: the request
* scope (and thus {@link RequestContext}) is thread-bound, so a branch submitted to the executor
* pool would otherwise run with no active scope and resolve its Task integrations against the
* default account instead of the execution's. Each branch thread therefore activates its own
* default account and region instead of the execution's. Each branch thread therefore activates its own
* scope here, mirroring how {@link #executeAsync}/{@link #executeSync} wrap {@code doExecute}.
* Delegates to {@link RequestScopes#callAsChecked}, which restores a previously active scope's
* account so a reused thread does not keep the execution's account.
* account and region so a reused thread does not keep the execution's scope.
*/
private <T> T callUnderExecutionAccount(StateMachine sm, Callable<T> body) throws Exception {
String accountId = AwsArnUtils.accountOrDefault(sm.getStateMachineArn(), null);
return RequestScopes.callAsChecked(accountId == null || accountId.isBlank() ? null : accountId, body);
String region = AwsArnUtils.regionOrDefault(sm.getStateMachineArn(), null);
return RequestScopes.callAsChecked(accountId == null || accountId.isBlank() ? null : accountId, region, body);
}
private void doExecute(StateMachine sm, Execution exec, List<HistoryEvent> history,
@@ -1321,22 +1321,26 @@ public class StepFunctionsService implements Resettable, ResourceProvider, Store
// The token selects one of the table's two resource kinds. Both lookups return that same ARN,
// so the reader deduplicates it without synthesizing a wildcard for the other kind.
return Optional.of(callbackResourceArn(names.size() == 1 ? names.getFirst() : null,
regionResolver.getAccountId()));
regionResolver.getAccountId(), region));
}
/**
* AWS authorizes activity callbacks on the activity and integration callbacks on the state machine.
* Retain that resource after completion so authorized retries still reach TaskTimedOut.
*/
public synchronized String callbackResourceArn(String token, String callerAccountId) {
public String callbackResourceArn(String token, String callerAccountId) {
return callbackResourceArn(token, callerAccountId, regionResolver.getRegion());
}
private synchronized String callbackResourceArn(String token, String callerAccountId, String region) {
PendingTask pending = token == null ? null : pendingTaskTokens.get(token);
if (pending != null) {
requireTokenOwner(pending.accountId(), callerAccountId);
requireTokenScope(pending.accountId(), pending.resourceArn(), callerAccountId, region);
return pending.resourceArn();
}
ClosedTask closed = token == null ? null : closedTaskTokens.get(token);
if (closed != null && System.nanoTime() - closed.closedAtNanos() < CLOSED_TOKEN_RETENTION_NANOS) {
requireTokenOwner(closed.accountId(), callerAccountId);
requireTokenScope(closed.accountId(), closed.resourceArn(), callerAccountId, region);
return closed.resourceArn();
}
throw new AwsException("InvalidToken", "Invalid Token: 'Invalid token'", 400);
@@ -1404,7 +1408,7 @@ public class StepFunctionsService implements Resettable, ResourceProvider, Store
String accountId = regionResolver.getAccountId();
PendingTask pending = token == null ? null : pendingTaskTokens.get(token);
if (pending != null) {
requireTokenOwner(pending.accountId(), accountId);
requireTokenScope(pending.accountId(), pending.resourceArn(), accountId, regionResolver.getRegion());
if (pending.result() != null) {
throw new AwsException("TaskTimedOut", "The associated task has already been closed.", 400);
}
@@ -1416,16 +1420,19 @@ public class StepFunctionsService implements Resettable, ResourceProvider, Store
closed = null;
}
if (closed != null) {
requireTokenOwner(closed.accountId(), accountId);
requireTokenScope(closed.accountId(), closed.resourceArn(), accountId, regionResolver.getRegion());
throw new AwsException("TaskTimedOut", "The associated task has already been closed.", 400);
}
throw new AwsException("InvalidToken", "Invalid Token: 'Invalid token'", 400);
}
private void requireTokenOwner(String owner, String caller) {
private void requireTokenScope(String owner, String resourceArn, String caller, String region) {
if (!owner.equals(caller)) {
throw new AwsException("AccessDeniedException", "Not authorized to access this resource.", 400);
}
if (!AwsArnUtils.isArnFor(resourceArn, "states") || !AwsArnUtils.parse(resourceArn).region().equals(region)) {
throw new AwsException("InvalidToken", "Invalid Token: 'Invalid token'", 400);
}
}
CompletableFuture<JsonNode> claimPendingTask(String token, TaskResult result) {