fix(iam): return protocol-correct access-denied statuses (#4673)

* fix(iam): return protocol-correct access-denied statuses

* chore(test): alphabetize IAM enforcement imports
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-09-29 10:52:39 -04:00
1 parent 8219929344
commit 970bdf2e54
13 files changed
+172 -33

No files matched your search

@@ -26,6 +26,8 @@ import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.UriInfo;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.ArgumentCaptor;
import java.nio.charset.StandardCharsets;
@@ -196,7 +198,38 @@ class IamEnforcementFilterTest {
verify(actionRegistry).resolve(eq("dynamodb"), eq(containerRequest));
ArgumentCaptor<Response> denied = ArgumentCaptor.captor();
verify(containerRequest).abortWith(denied.capture());
assertEquals(403, denied.getValue().getStatus());
assertEquals(400, denied.getValue().getStatus());
}
@ParameterizedTest
@CsvSource({
"AWS_JSON_1_0, application/json, 400",
"AWS_JSON_1_1, application/json, 400",
"REST, application/x-amz-json-1.1, 403"
})
void denialStatusUsesTheClaimedProtocolRatherThanContentType(WireProtocol protocol, String contentType,
int expectedStatus) {
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
String auth = "AWS4-HMAC-SHA256 Credential=AKIAUSER/20260629/us-east-1/dynamodb/aws4_request, "
+ "SignedHeaders=host, Signature=abc";
requestContext.setAccountId("000000000000");
when(accountResolver.extractAccessKeyId(auth)).thenReturn("AKIAUSER");
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
when(containerRequest.getMediaType()).thenReturn(MediaType.valueOf(contentType));
stubClaim(containerRequest, protocol, dynamoDbDescriptor());
when(iamService.resolveCallerContext("AKIAUSER")).thenReturn(CallerContext.of(List.of()));
when(actionRegistry.resolve("dynamodb", containerRequest)).thenReturn("dynamodb:PutItem");
when(evaluator.evaluateResolvedResourcePolicy(any(), any(), any(), eq("dynamodb:PutItem"), any(), any()))
.thenReturn(IamPolicyEvaluator.Decision.DENY);
newFilter().filter(containerRequest);
ArgumentCaptor<Response> denied = ArgumentCaptor.captor();
verify(containerRequest).abortWith(denied.capture());
assertEquals(expectedStatus, denied.getValue().getStatus());
assertEquals(MediaType.APPLICATION_JSON_TYPE, denied.getValue().getMediaType());
assertTrue(entityString(denied.getValue()).contains("\"__type\":\"AccessDeniedException\""));
assertTrue(entityString(denied.getValue()).contains("User is not authorized to perform: dynamodb:PutItem"));
}
@Test
@@ -1398,9 +1431,10 @@ class IamEnforcementFilterTest {
void jsonProtocolGetsJsonErrorResponse() {
// DynamoDB / Cognito / Kinesis / ... — JSON 1.0/1.1, JSON error response.
Response r = IamEnforcementFilter.accessDeniedResponse(
"dynamodb:PutItem", "dynamodb", MediaType.valueOf("application/x-amz-json-1.0"));
"dynamodb:PutItem", "dynamodb", MediaType.valueOf("application/x-amz-json-1.0"),
null, WireProtocol.AWS_JSON_1_0);
assertEquals(403, r.getStatus());
assertEquals(400, r.getStatus());
assertEquals(MediaType.APPLICATION_JSON_TYPE, r.getMediaType());
String body = entityString(r);
assertTrue(body.contains("\"__type\":\"AccessDeniedException\""), body);
@@ -72,7 +72,7 @@ class DynamoDbFgacEnforcementIntegrationTest {
.when()
.post("/")
.then()
.statusCode(403)
.statusCode(400)
.body(containsString("AccessDeniedException"));
}
@@ -101,7 +101,7 @@ class DynamoDbFgacEnforcementIntegrationTest {
.when()
.post("/")
.then()
.statusCode(403)
.statusCode(400)
.body(containsString("AccessDeniedException"));
}
@@ -151,7 +151,7 @@ class DynamoDbFgacEnforcementIntegrationTest {
.when()
.post("/")
.then()
.statusCode(403)
.statusCode(400)
.body(containsString("AccessDeniedException"));
}
@@ -92,7 +92,7 @@ class LambdaScopedTablePermissionIntegrationTest {
{"TableName":"not-granted-%s","Key":{"id":{"S":"record"}}}
""".formatted(suffix))
.when().post("/")
.then().statusCode(403).body(containsString("AccessDenied"));
.then().statusCode(400).body(containsString("AccessDenied"));
} finally {
executionRoleCredentials.unregister(ACCOUNT_ID, credentials.accessKeyId());
iamService.deleteRolePolicy(roleName, "TableAccess");
@@ -76,7 +76,7 @@ class ScpEnforcementLeaveOrganizationIntegrationTest {
// --- the member is now bounded by the OU's deny SCP ------------------------------------
org(memberId, "LeaveOrganization", "{}")
.then()
.statusCode(403)
.statusCode(400)
.body(containsString("AccessDeniedException"));
// An action the SCP does not deny must still succeed (FullAWSAccess baseline holds).
@@ -144,7 +144,7 @@ class OrganizationsCrossAccountIntegrationTest {
.when()
.post("/")
.then()
.statusCode(403)
.statusCode(400)
.body("__type", equalTo("AccessDeniedException"));
}
@@ -214,7 +214,7 @@ class OrganizationsCrossAccountIntegrationTest {
.when()
.post("/")
.then()
.statusCode(403)
.statusCode(400)
.body("__type", equalTo("AccessDeniedException"));
organizations(INVITED_ACCOUNT, "CreateAccount",
@@ -222,7 +222,7 @@ class OrganizationsCrossAccountIntegrationTest {
.when()
.post("/")
.then()
.statusCode(403)
.statusCode(400)
.body("__type", equalTo("AccessDeniedException"));
}
@@ -279,7 +279,7 @@ class OrganizationsCrossAccountIntegrationTest {
.when()
.post("/")
.then()
.statusCode(403)
.statusCode(400)
.body("__type", equalTo("AccessDeniedException"));
organizations(MANAGEMENT_ACCOUNT, "CancelHandshake", "{\"HandshakeId\":\"" + canceledHandshakeId + "\"}")
@@ -337,7 +337,7 @@ class OrganizationsCrossAccountIntegrationTest {
.when()
.post("/")
.then()
.statusCode(403)
.statusCode(400)
.body("__type", equalTo("AccessDeniedException"));
}
@@ -157,7 +157,7 @@ class OrganizationsInvalidEffectivePolicyIntegrationTest {
.when()
.post("/")
.then()
.statusCode(403)
.statusCode(400)
.body("__type", equalTo("AccessDeniedException"));
}
@@ -185,7 +185,7 @@ class OrganizationsInvalidEffectivePolicyIntegrationTest {
.when()
.post("/")
.then()
.statusCode(403)
.statusCode(400)
.body("__type", equalTo("AccessDeniedException"));
}
}
@@ -208,7 +208,7 @@ class OrganizationsServiceTest {
AwsException error = assertThrows(AwsException.class, () -> service.createOrganizationalUnit(
OUTSIDER_ACCOUNT, organization.getRoot().getId(), "Unit", null));
assertEquals("AccessDeniedException", error.getErrorCode());
assertEquals(403, error.getHttpStatus());
assertEquals(400, error.getHttpStatus());
}
@Test