Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(sts): validate DurationSeconds (#4418)

Niels Kootstra444e9e8d6a8f
diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java
index b98f680022..41fab2e7cc 100644
--- a/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java
@@ -100,6 +100,10 @@ public class StsQueryHandler {
if (validation != null) {
return validation;
}
+ Response durationValidation = validateDurationSeconds(params, 900, 43200);
+ if (durationValidation != null) {
+ return durationValidation;
+ }
String roleArn = getParam(params, "RoleArn");
String sessionName = getParam(params, "RoleSessionName");
int durationSeconds = getIntParam(params, "DurationSeconds", 3600);
@@ -182,6 +186,10 @@ public class StsQueryHandler {
}
private Response handleGetSessionToken(MultivaluedMap<String, String> params) {
+ Response durationValidation = validateDurationSeconds(params, 900, 129600);
+ if (durationValidation != null) {
+ return durationValidation;
+ }
int durationSeconds = getIntParam(params, "DurationSeconds", 43200);
String accessKeyId = "ASIA" + randomId(16);
String secretKey = randomSecret(40);
@@ -200,6 +208,10 @@ public class StsQueryHandler {
if (validation != null) {
return validation;
}
+ Response durationValidation = validateDurationSeconds(params, 900, 43200);
+ if (durationValidation != null) {
+ return durationValidation;
+ }
String roleArn = getParam(params, "RoleArn");
String sessionName = getParam(params, "RoleSessionName");
String providerId = getParam(params, "ProviderId");
@@ -341,6 +353,10 @@ public class StsQueryHandler {
if (validation != null) {
return validation;
}
+ Response durationValidation = validateDurationSeconds(params, 900, 43200);
+ if (durationValidation != null) {
+ return durationValidation;
+ }
String roleArn = getParam(params, "RoleArn");
String principalArn = getParam(params, "PrincipalArn");
int durationSeconds = getIntParam(params, "DurationSeconds", 3600);
@@ -403,6 +419,10 @@ public class StsQueryHandler {
if (validation != null) {
return validation;
}
+ Response durationValidation = validateDurationSeconds(params, 900, 129600);
+ if (durationValidation != null) {
+ return durationValidation;
+ }
String name = getParam(params, "Name");
int durationSeconds = getIntParam(params, "DurationSeconds", 43200);
@@ -454,6 +474,39 @@ public class StsQueryHandler {
return null;
}
+ /**
+ * Validates the optional {@code DurationSeconds} parameter against {@code minSeconds}/{@code maxSeconds}.
+ * Returns {@code null} when the parameter is absent or valid; otherwise a {@code ValidationError} (out of
+ * range) or {@code InvalidParameterValue} (not an integer) response, matching AWS's own wire behavior.
+ */
+ private Response validateDurationSeconds(MultivaluedMap<String, String> params, int minSeconds, int maxSeconds) {
+ String value = params.getFirst("DurationSeconds");
+ if (value == null) {
+ return null;
+ }
+ int durationSeconds;
+ try {
+ durationSeconds = Integer.parseInt(value);
+ } catch (NumberFormatException e) {
+ return AwsQueryResponse.error("InvalidParameterValue",
+ "Value " + value + " for parameter DurationSeconds is invalid. Reason: Must be an integer.",
+ AwsNamespaces.STS, 400);
+ }
+ if (durationSeconds < minSeconds) {
+ return AwsQueryResponse.error("ValidationError",
+ "1 validation error detected: Value '" + durationSeconds + "' at 'durationSeconds' failed to "
+ + "satisfy constraint: Member must have value greater than or equal to " + minSeconds,
+ AwsNamespaces.STS, 400);
+ }
+ if (durationSeconds > maxSeconds) {
+ return AwsQueryResponse.error("ValidationError",
+ "1 validation error detected: Value '" + durationSeconds + "' at 'durationSeconds' failed to "
+ + "satisfy constraint: Member must have value less than or equal to " + maxSeconds,
+ AwsNamespaces.STS, 400);
+ }
+ return null;
+ }
+
private String credentialsXml(String accessKeyId, String secretKey, String sessionToken, Instant expiration) {
return new XmlBuilder()
.start("Credentials")
diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/StsQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/StsQueryHandlerTest.java
index 486aacd09a..234e8a7ba4 100644
--- a/src/test/java/io/github/hectorvent/floci/services/iam/StsQueryHandlerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/iam/StsQueryHandlerTest.java
@@ -9,6 +9,8 @@ import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -17,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
class StsQueryHandlerTest {
@@ -27,11 +30,18 @@ class StsQueryHandlerTest {
Pattern.compile("<SessionToken>([^<]+)</SessionToken>");
private static StsQueryHandler newHandler() {
+ EmulatorConfig config = mock(EmulatorConfig.class);
+ EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
+ EmulatorConfig.IamServiceConfig iam = mock(EmulatorConfig.IamServiceConfig.class);
+ when(config.services()).thenReturn(services);
+ when(services.iam()).thenReturn(iam);
+ when(iam.enforcementEnabled()).thenReturn(false);
+
return new StsQueryHandler(
mock(IamService.class),
mock(AccountResolver.class),
new RegionResolver(REGION, "000000000000"),
- mock(EmulatorConfig.class),
+ config,
mock(AssumeRolePolicyEvaluator.class),
mock(WebIdentityTrustPolicyEvaluator.class),
mock(WebIdentityTokenVerifier.class),
@@ -63,6 +73,158 @@ class StsQueryHandlerTest {
assertNotEquals(extract(SESSION_TOKEN, firstBody), extract(SESSION_TOKEN, secondBody));
}
+ @ParameterizedTest
+ @ValueSource(strings = {"900", "43200"})
+ void assumeRoleAcceptsDurationSecondsAtTheLimits(String durationSeconds) {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("RoleArn", "arn:aws:iam::000000000000:role/TestRole");
+ params.putSingle("RoleSessionName", "test-session");
+ params.putSingle("DurationSeconds", durationSeconds);
+
+ Response response = newHandler().handle("AssumeRole", params);
+
+ assertEquals(200, response.getStatus(), (String) response.getEntity());
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"900", "129600"})
+ void getSessionTokenAcceptsDurationSecondsAtTheLimits(String durationSeconds) {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("DurationSeconds", durationSeconds);
+
+ Response response = newHandler().handle("GetSessionToken", params);
+
+ assertEquals(200, response.getStatus(), (String) response.getEntity());
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"900", "129600"})
+ void getFederationTokenAcceptsDurationSecondsAtTheLimits(String durationSeconds) {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("Name", "test-user");
+ params.putSingle("DurationSeconds", durationSeconds);
+
+ Response response = newHandler().handle("GetFederationToken", params);
+
+ assertEquals(200, response.getStatus(), (String) response.getEntity());
+ }
+
+ @Test
+ void assumeRoleRejectsDurationSecondsBelowMinimum() {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("RoleArn", "arn:aws:iam::000000000000:role/TestRole");
+ params.putSingle("RoleSessionName", "test-session");
+ params.putSingle("DurationSeconds", "899");
+
+ Response response = newHandler().handle("AssumeRole", params);
+
+ assertEquals(400, response.getStatus());
+ String body = (String) response.getEntity();
+ assertTrue(body.contains("ValidationError"), body);
+ assertTrue(body.contains("greater than or equal to 900"), body);
+ }
+
+ @Test
+ void assumeRoleRejectsDurationSecondsAboveMaximum() {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("RoleArn", "arn:aws:iam::000000000000:role/TestRole");
+ params.putSingle("RoleSessionName", "test-session");
+ params.putSingle("DurationSeconds", "43201");
+
+ Response response = newHandler().handle("AssumeRole", params);
+
+ assertEquals(400, response.getStatus());
+ String body = (String) response.getEntity();
+ assertTrue(body.contains("ValidationError"), body);
+ assertTrue(body.contains("less than or equal to 43200"), body);
+ }
+
+ @Test
+ void assumeRoleRejectsNonNumericDurationSeconds() {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("RoleArn", "arn:aws:iam::000000000000:role/TestRole");
+ params.putSingle("RoleSessionName", "test-session");
+ params.putSingle("DurationSeconds", "notanumber");
+
+ Response response = newHandler().handle("AssumeRole", params);
+
+ assertEquals(400, response.getStatus());
+ String body = (String) response.getEntity();
+ assertTrue(body.contains("InvalidParameterValue"), body);
+ }
+
+ @Test
+ void getSessionTokenRejectsDurationSecondsBelowMinimum() {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("DurationSeconds", "899");
+
+ Response response = newHandler().handle("GetSessionToken", params);
+
+ assertEquals(400, response.getStatus());
+ String body = (String) response.getEntity();
+ assertTrue(body.contains("ValidationError"), body);
+ assertTrue(body.contains("greater than or equal to 900"), body);
+ }
+
+ @Test
+ void getSessionTokenRejectsDurationSecondsAboveMaximum() {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("DurationSeconds", "129601");
+
+ Response response = newHandler().handle("GetSessionToken", params);
+
+ assertEquals(400, response.getStatus());
+ String body = (String) response.getEntity();
+ assertTrue(body.contains("ValidationError"), body);
+ assertTrue(body.contains("less than or equal to 129600"), body);
+ }
+
+ @Test
+ void getFederationTokenRejectsDurationSecondsAboveMaximum() {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("Name", "test-user");
+ params.putSingle("DurationSeconds", "129601");
+
+ Response response = newHandler().handle("GetFederationToken", params);
+
+ assertEquals(400, response.getStatus());
+ String body = (String) response.getEntity();
+ assertTrue(body.contains("ValidationError"), body);
+ assertTrue(body.contains("less than or equal to 129600"), body);
+ }
+
+ @Test
+ void assumeRoleWithWebIdentityRejectsDurationSecondsBelowMinimum() {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("RoleArn", "arn:aws:iam::000000000000:role/TestRole");
+ params.putSingle("RoleSessionName", "test-session");
+ params.putSingle("WebIdentityToken", "token");
+ params.putSingle("DurationSeconds", "899");
+
+ Response response = newHandler().handle("AssumeRoleWithWebIdentity", params);
+
+ assertEquals(400, response.getStatus());
+ String body = (String) response.getEntity();
+ assertTrue(body.contains("ValidationError"), body);
+ assertTrue(body.contains("greater than or equal to 900"), body);
+ }
+
+ @Test
+ void assumeRoleWithSAMLRejectsDurationSecondsBelowMinimum() {
+ MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
+ params.putSingle("RoleArn", "arn:aws:iam::000000000000:role/TestRole");
+ params.putSingle("PrincipalArn", "arn:aws:iam::000000000000:saml-provider/test");
+ params.putSingle("SAMLAssertion", "not-checked-before-duration-validation");
+ params.putSingle("DurationSeconds", "899");
+
+ Response response = newHandler().handle("AssumeRoleWithSAML", params);
+
+ assertEquals(400, response.getStatus());
+ String body = (String) response.getEntity();
+ assertTrue(body.contains("ValidationError"), body);
+ assertTrue(body.contains("greater than or equal to 900"), body);
+ }
+
private static String extract(Pattern pattern, String body) {
Matcher matcher = pattern.matcher(body);
assertTrue(matcher.find(), "expected " + pattern + " in " + body);