Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Mario Toffiae9c37666c772
diff --git a/docs/services/api-gateway.md b/docs/services/api-gateway.md
index 44bfc94846..27c7902852 100644
--- a/docs/services/api-gateway.md
+++ b/docs/services/api-gateway.md
@@ -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` |
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java
index 92a2127dd4..3d561532ae 100644
--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java
+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayExecuteController.java
@@ -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)) {
diff --git a/src/main/java/io/github/hectorvent/floci/services/apigateway/CognitoUserPoolAuthorizer.java b/src/main/java/io/github/hectorvent/floci/services/apigateway/CognitoUserPoolAuthorizer.java
index cc440d1d5d..bfab609a40 100644
--- a/src/main/java/io/github/hectorvent/floci/services/apigateway/CognitoUserPoolAuthorizer.java
+++ b/src/main/java/io/github/hectorvent/floci/services/apigateway/CognitoUserPoolAuthorizer.java
@@ -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"));
diff --git a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCognitoAuthorizerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCognitoAuthorizerIntegrationTest.java
index 086a26ffe0..ade290f35e 100644
--- a/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCognitoAuthorizerIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/apigateway/ApiGatewayCognitoAuthorizerIntegrationTest.java
@@ -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)