fix(apigateway): answer 401 when a Cognito authorizer refuses the scopes (#6636)

This commit is contained in:
Mario Toffia authored and GitHub committed 2026-10-26 09:36:16 -04:00
1 parent ce0933be9b
commit e9c37666c7
4 files changed
+16 -10

No files matched your search

+1
View File
@@ -350,6 +350,7 @@ This is what lets a browser read a `401`/`403`/`400` as such instead of as a COR
| `AWS_IAM` access key the emulator never issued | `ACCESS_DENIED` |
| Lambda authorizer returns `Deny` | `ACCESS_DENIED` |
| Lambda authorizer fails or throws | `AUTHORIZER_FAILURE` |
| Cognito authorizer gets no token, an invalid one, one from another pool, or one claiming none of the method's authorization scopes (`401 Unauthorized`) | `UNAUTHORIZED` |
| Method with `apiKeyRequired` and no usable `x-api-key` (`403 Forbidden`) | `INVALID_API_KEY` |
| Request validator rejects a parameter / the body | `BAD_REQUEST_PARAMETERS` / `BAD_REQUEST_BODY` |
| `passthroughBehavior` rejects the request `Content-Type` (`415`) | `UNSUPPORTED_MEDIA_TYPE` |
@@ -1022,10 +1022,6 @@ public class ApiGatewayExecuteController {
return new AuthorizerResult(gatewayResponse(scope, GatewayResponseType.UNAUTHORIZED, 401,
"Unauthorized"), null, null);
}
if (result.failure() == CognitoUserPoolAuthorizer.Failure.ACCESS_DENIED) {
return new AuthorizerResult(gatewayResponse(scope, GatewayResponseType.ACCESS_DENIED, 403,
"User is not authorized to access this resource"), null, null);
}
return new AuthorizerResult(null, result.principalId(), result.context());
}
if ("CUSTOM".equals(authorizationType)) {
@@ -21,8 +21,7 @@ import java.util.stream.Collectors;
public class CognitoUserPoolAuthorizer {
public enum Failure {
UNAUTHORIZED,
ACCESS_DENIED
UNAUTHORIZED
}
public record Result(Failure failure, String principalId, Map<String, Object> context) {}
@@ -62,10 +61,11 @@ public class CognitoUserPoolAuthorizer {
if (!providerArns.contains(verifiedPoolArn)) {
return new Result(Failure.UNAUTHORIZED, null, null);
}
// AWS answers 401, not 403, when the token claims none of the method's scopes, ID tokens included.
List<String> requiredScopes = method.getAuthorizationScopes();
if (requiredScopes != null && !requiredScopes.isEmpty()) {
if (!"access".equals(verified.tokenUse())) {
return new Result(Failure.ACCESS_DENIED, null, null);
return new Result(Failure.UNAUTHORIZED, null, null);
}
Set<String> granted = new HashSet<>();
Object scopeClaim = verified.claims().get("scope");
@@ -73,7 +73,7 @@ public class CognitoUserPoolAuthorizer {
granted.addAll(Arrays.asList(value.trim().split("\\s+")));
}
if (requiredScopes.stream().noneMatch(granted::contains)) {
return new Result(Failure.ACCESS_DENIED, null, null);
return new Result(Failure.UNAUTHORIZED, null, null);
}
}
String principalId = String.valueOf(verified.claims().getOrDefault("sub", "unknown"));
@@ -17,6 +17,7 @@ import java.util.Map;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;
@QuarkusTest
@@ -72,9 +73,17 @@ class ApiGatewayCognitoAuthorizerIntegrationTest {
given().header("Authorization", "Bearer " + accessToken)
.when().get(base + "secured").then().statusCode(401);
given().header("X-Id-Token", "Bearer " + idToken)
.when().get(base + "secured").then().statusCode(403);
.when().get(base + "secured").then().statusCode(401)
.body("message", equalTo("Unauthorized"));
given().header("X-Id-Token", "Bearer " + accessToken)
.when().get(base + "missing-scope").then().statusCode(403);
.when().get(base + "missing-scope").then().statusCode(401)
.body("message", equalTo("Unauthorized"));
given().contentType(ContentType.JSON).body("""
{"responseTemplates":{"application/json":"{\\"title\\": \\"Unauthorized\\", \\"status\\": 401}"}}
""").when().put("/restapis/" + apiId + "/gatewayresponses/UNAUTHORIZED").then().statusCode(201);
given().header("X-Id-Token", "Bearer " + accessToken)
.when().get(base + "missing-scope").then().statusCode(401)
.body("title", equalTo("Unauthorized"));
given().header("X-Id-Token", "not-a-jwt")
.when().get(base + "secured").then().statusCode(401);
given().header("X-Id-Token", "Bearer " + accessToken)