From 6386e8fd073bdbced20c00ca7580809d411d1817 Mon Sep 17 00:00:00 2001 From: Mohith Date: Tue, 3 Nov 2026 20:44:40 +0530 Subject: [PATCH] fix(cognito): reject non-integer MaxResults in auth event and WebAuthn listings (#7418) --- .../services/cognito/CognitoJsonHandler.java | 5 +- ...dminListUserAuthEventsIntegrationTest.java | 26 +++++++ .../CognitoWebAuthnIntegrationTest.java | 71 +++++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java index c710e4ad28..92cb52ed26 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cognito/CognitoJsonHandler.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsErrorResponse; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.Pagination; import io.github.hectorvent.floci.core.common.SmithyValidationErrors; import io.github.hectorvent.floci.services.cognito.CognitoService.UserPoolDomainNames; import io.github.hectorvent.floci.services.cognito.model.CognitoGroup; @@ -1640,7 +1641,7 @@ public class CognitoJsonHandler { private Response handleListWebAuthnCredentials(JsonNode request) { return Response.ok(objectMapper.valueToTree(service.listWebAuthnCredentials( request.path("AccessToken").asText(), - request.hasNonNull("MaxResults") ? request.path("MaxResults").asInt() : null, + Pagination.parseMaxResults(request, "MaxResults", "SerializationException"), request.hasNonNull("NextToken") ? request.path("NextToken").asText() : null))).build(); } @@ -1648,7 +1649,7 @@ public class CognitoJsonHandler { return Response.ok(objectMapper.valueToTree(service.adminListUserAuthEvents( request.path("UserPoolId").asText(), request.path("Username").asText(), - request.hasNonNull("MaxResults") ? request.path("MaxResults").asInt() : null, + Pagination.parseMaxResults(request, "MaxResults", "SerializationException"), request.hasNonNull("NextToken") ? request.path("NextToken").asText() : null))).build(); } diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoAdminListUserAuthEventsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoAdminListUserAuthEventsIntegrationTest.java index b4a192ebd3..af5319928a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoAdminListUserAuthEventsIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoAdminListUserAuthEventsIntegrationTest.java @@ -208,6 +208,32 @@ class CognitoAdminListUserAuthEventsIntegrationTest { .then().statusCode(400).body("__type", equalTo("InvalidParameterException")); } + @Test + void maxResultsThatIsNotAnIntegerIsASerializationException() throws Exception { + String poolId = createEligiblePool("AUDIT"); + String username = createUser(poolId); + for (String bad : new String[] {"1.5", "\"2\"", "4294967297", "true"}) { + listEvents(poolId, username, "\"MaxResults\":" + bad) + .then().statusCode(400) + .body("__type", equalTo("SerializationException")) + .body("message", equalTo("MaxResults must be an integer.")); + } + } + + @Test + void nullMaxResultsBehavesLikeAnOmittedOne() throws Exception { + String poolId = createEligiblePool("AUDIT"); + String clientId = createClient(poolId); + String username = createUser(poolId); + for (int i = 0; i < 3; i++) { + cognitoJson("InitiateAuth", passwordAuth(clientId, username, PASSWORD)); + } + listEvents(poolId, username, "\"MaxResults\":null") + .then().statusCode(200) + .body("AuthEvents.size()", equalTo(3)) + .body("containsKey('NextToken')", equalTo(false)); + } + @Test void newPasswordChallengeStaysInProgressUntilItIsAnswered() throws Exception { String poolId = createEligiblePool("ENFORCED"); diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoWebAuthnIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoWebAuthnIntegrationTest.java index a75f29d6c6..463516c937 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoWebAuthnIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoWebAuthnIntegrationTest.java @@ -243,4 +243,75 @@ class CognitoWebAuthnIntegrationTest { """.formatted(poolId)) .then().statusCode(400).body("__type", equalTo("InvalidParameterException")); } + + @Test + void listWebAuthnCredentialsRejectsAMaxResultsThatIsNotAnInteger() throws Exception { + String accessToken = passkeyUserWithCredentials(1); + for (String bad : new String[] {"1.5", "\"2\"", "4294967297", "true"}) { + cognitoAction("ListWebAuthnCredentials", """ + {"AccessToken":"%s","MaxResults":%s} + """.formatted(accessToken, bad)) + .then().statusCode(400) + .body("__type", equalTo("SerializationException")) + .body("message", equalTo("MaxResults must be an integer.")); + } + } + + @Test + void listWebAuthnCredentialsPagesWithAnIntegerMaxResultsAndTreatsNullAsOmitted() throws Exception { + String accessToken = passkeyUserWithCredentials(3); + JsonNode first = cognitoJson("ListWebAuthnCredentials", """ + {"AccessToken":"%s","MaxResults":2} + """.formatted(accessToken)); + assertEquals(2, first.path("Credentials").size()); + assertFalse(first.path("NextToken").asText().isEmpty()); + JsonNode second = cognitoJson("ListWebAuthnCredentials", """ + {"AccessToken":"%s","MaxResults":2,"NextToken":"%s"} + """.formatted(accessToken, first.path("NextToken").asText())); + assertEquals(1, second.path("Credentials").size()); + assertTrue(second.path("NextToken").isMissingNode()); + + cognitoAction("ListWebAuthnCredentials", """ + {"AccessToken":"%s","MaxResults":null} + """.formatted(accessToken)) + .then().statusCode(200) + .body("Credentials", hasSize(3)) + .body("$", not(hasKey("NextToken"))); + } + + private static String passkeyUserWithCredentials(int count) throws Exception { + String poolId = cognitoJson("CreateUserPool", """ + {"PoolName":"PasskeyPagingPool","UserPoolTier":"ESSENTIALS", + "Policies":{"SignInPolicy":{"AllowedFirstAuthFactors":["PASSWORD","WEB_AUTHN"]}}} + """).path("UserPool").path("Id").asText(); + cognitoJson("SetUserPoolMfaConfig", """ + {"UserPoolId":"%s","MfaConfiguration":"OFF", + "WebAuthnConfiguration":{"RelyingPartyId":"localhost"}} + """.formatted(poolId)); + String clientId = cognitoJson("CreateUserPoolClient", """ + {"UserPoolId":"%s","ClientName":"passkey-paging-client", + "ExplicitAuthFlows":["ALLOW_USER_PASSWORD_AUTH"]} + """.formatted(poolId)).path("UserPoolClient").path("ClientId").asText(); + String username = "passkey-paging-" + UUID.randomUUID(); + cognitoJson("AdminCreateUser", """ + {"UserPoolId":"%s","Username":"%s","MessageAction":"SUPPRESS"} + """.formatted(poolId, username)); + cognitoJson("AdminSetUserPassword", """ + {"UserPoolId":"%s","Username":"%s","Password":"%s","Permanent":true} + """.formatted(poolId, username, PASSWORD)); + String accessToken = cognitoJson("InitiateAuth", """ + {"ClientId":"%s","AuthFlow":"USER_PASSWORD_AUTH", + "AuthParameters":{"USERNAME":"%s","PASSWORD":"%s"}} + """.formatted(clientId, username, PASSWORD)).path("AuthenticationResult").path("AccessToken").asText(); + for (int i = 0; i < count; i++) { + WebAuthnTestAuthenticator authenticator = WebAuthnTestAuthenticator.es256("http://localhost:5173"); + ObjectNode complete = JSON.createObjectNode(); + complete.put("AccessToken", accessToken); + complete.set("Credential", authenticator.register(cognitoJson("StartWebAuthnRegistration", """ + {"AccessToken":"%s"} + """.formatted(accessToken)).path("CredentialCreationOptions"), "none")); + cognitoAction("CompleteWebAuthnRegistration", complete.toString()).then().statusCode(200); + } + return accessToken; + } }