mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(iam): reject an access key that exists nowhere under enforcement (#4182)
With enforcement enabled, a signed request whose access key resolved to no caller context was allowed through, so any access key id authorized any call. Reject it with InvalidAccessKeyId for S3 and UnrecognizedClientException elsewhere, matching what the S3 signature filter and the API Gateway execute path already return. resolveCallerContext returns null for a key that does not exist and for a real credential it cannot map to policies, such as a session carrying no role ARN. Only the first is unauthenticated, so isKnownAccessKey searches sessions and long-term keys across every account and only a key found nowhere is rejected. The test credential, the bare account-id root principal, unresolvable actions and unsigned requests are unchanged.
This commit is contained in:
1 parent
3f5741a965
commit
d30a744b66
5 files changed
+251
-15
No files matched your search
@@ -38,6 +38,7 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
@@ -141,6 +142,108 @@ class IamEnforcementFilterTest {
|
||||
verify(arnBuilder).buildResources("lambda", containerRequest, "us-east-1", "222233334444");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownAccessKeyIsRejectedInsteadOfBypassingEnforcement() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=AKIADOESNOTEXIST0000/20260629/us-east-1/lambda/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=garbage";
|
||||
requestContext.setAccountId("000000000000");
|
||||
when(accountResolver.extractAccessKeyId(auth)).thenReturn("AKIADOESNOTEXIST0000");
|
||||
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
|
||||
when(actionRegistry.resolve("lambda", containerRequest)).thenReturn("lambda:InvokeFunction");
|
||||
when(iamService.resolveCallerContext("AKIADOESNOTEXIST0000")).thenReturn(null);
|
||||
when(iamService.isKnownAccessKey("AKIADOESNOTEXIST0000")).thenReturn(false);
|
||||
|
||||
newFilter().filter(containerRequest);
|
||||
|
||||
ArgumentCaptor<Response> response = ArgumentCaptor.captor();
|
||||
verify(containerRequest).abortWith(response.capture());
|
||||
assertEquals(403, response.getValue().getStatus());
|
||||
assertTrue(response.getValue().getEntity().toString().contains("UnrecognizedClientException"),
|
||||
response.getValue().getEntity().toString());
|
||||
verifyNoInteractions(evaluator);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownAccessKeyOnAnS3RequestIsRejectedWithTheS3ErrorCode() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=AKIADOESNOTEXIST0000/20260629/us-east-1/s3/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=garbage";
|
||||
requestContext.setAccountId("000000000000");
|
||||
when(accountResolver.extractAccessKeyId(auth)).thenReturn("AKIADOESNOTEXIST0000");
|
||||
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
|
||||
when(actionRegistry.resolve("s3", containerRequest)).thenReturn("s3:GetObject");
|
||||
when(iamService.resolveCallerContext("AKIADOESNOTEXIST0000")).thenReturn(null);
|
||||
when(iamService.isKnownAccessKey("AKIADOESNOTEXIST0000")).thenReturn(false);
|
||||
|
||||
newFilter().filter(containerRequest);
|
||||
|
||||
ArgumentCaptor<Response> response = ArgumentCaptor.captor();
|
||||
verify(containerRequest).abortWith(response.capture());
|
||||
assertEquals(403, response.getValue().getStatus());
|
||||
assertTrue(response.getValue().getEntity().toString().contains("InvalidAccessKeyId"),
|
||||
response.getValue().getEntity().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownAccessKeyOnAQueryRequestIsRejectedWithTheQueryErrorCode() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=AKIADOESNOTEXIST0000/20260629/us-east-1/sts/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=garbage";
|
||||
requestContext.setAccountId("000000000000");
|
||||
when(accountResolver.extractAccessKeyId(auth)).thenReturn("AKIADOESNOTEXIST0000");
|
||||
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
|
||||
when(containerRequest.getMediaType()).thenReturn(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
|
||||
when(actionRegistry.resolve("sts", containerRequest)).thenReturn("sts:AssumeRole");
|
||||
when(iamService.resolveCallerContext("AKIADOESNOTEXIST0000")).thenReturn(null);
|
||||
when(iamService.isKnownAccessKey("AKIADOESNOTEXIST0000")).thenReturn(false);
|
||||
|
||||
newFilter().filter(containerRequest);
|
||||
|
||||
ArgumentCaptor<Response> response = ArgumentCaptor.captor();
|
||||
verify(containerRequest).abortWith(response.capture());
|
||||
assertEquals(403, response.getValue().getStatus());
|
||||
// Query services answer an unrecognised credential with InvalidClientTokenId, not the
|
||||
// JSON services' UnrecognizedClientException.
|
||||
assertTrue(response.getValue().getEntity().toString().contains("InvalidClientTokenId"),
|
||||
response.getValue().getEntity().toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aKnownCredentialWithoutAMappableCallerContextStillPassesThrough() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=ASIAIDENTITYSESSION/20260629/us-east-1/lambda/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
requestContext.setAccountId("000000000000");
|
||||
when(accountResolver.extractAccessKeyId(auth)).thenReturn("ASIAIDENTITYSESSION");
|
||||
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
|
||||
when(actionRegistry.resolve("lambda", containerRequest)).thenReturn("lambda:InvokeFunction");
|
||||
when(iamService.resolveCallerContext("ASIAIDENTITYSESSION")).thenReturn(null);
|
||||
when(iamService.isKnownAccessKey("ASIAIDENTITYSESSION")).thenReturn(true);
|
||||
|
||||
newFilter().filter(containerRequest);
|
||||
|
||||
verify(containerRequest, never()).abortWith(any());
|
||||
verifyNoInteractions(evaluator);
|
||||
}
|
||||
|
||||
@Test
|
||||
void bareAccountIdKeyWithNoScpCeilingStillPassesThrough() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
String auth = "AWS4-HMAC-SHA256 Credential=000000000000/20260629/us-east-1/lambda/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
requestContext.setAccountId("000000000000");
|
||||
when(accountResolver.extractAccessKeyId(auth)).thenReturn("000000000000");
|
||||
when(containerRequest.getHeaderString("Authorization")).thenReturn(auth);
|
||||
when(actionRegistry.resolve("lambda", containerRequest)).thenReturn("lambda:InvokeFunction");
|
||||
when(iamService.resolveCallerContext("000000000000")).thenReturn(null);
|
||||
|
||||
newFilter().filter(containerRequest);
|
||||
|
||||
verify(containerRequest, never()).abortWith(any());
|
||||
verify(iamService, never()).isKnownAccessKey(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void filterBuildsResourceArnForDynamoDbTable() {
|
||||
ContainerRequestContext containerRequest = mock(ContainerRequestContext.class);
|
||||
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
/**
|
||||
* With enforcement on, an access key that exists nowhere must be rejected rather than waved
|
||||
* through. Before this was closed, any string in the credential scope authorized the request.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
class UnknownAccessKeyEnforcementIntegrationTest {
|
||||
|
||||
private static final String UNKNOWN_KEY_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKIADOESNOTEXIST0000/20260101/us-east-1/%s/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=not-a-real-signature";
|
||||
|
||||
@Test
|
||||
void jsonServiceRejectsAnUnknownAccessKey() {
|
||||
given()
|
||||
.header("Authorization", UNKNOWN_KEY_AUTH.formatted("lambda"))
|
||||
.contentType("application/json")
|
||||
.when()
|
||||
.get("/2015-03-31/functions")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("UnrecognizedClientException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void s3RejectsAnUnknownAccessKeyWithTheS3ErrorCode() {
|
||||
given()
|
||||
.header("Authorization", UNKNOWN_KEY_AUTH.formatted("s3"))
|
||||
.when()
|
||||
.get("/")
|
||||
.then()
|
||||
.statusCode(403)
|
||||
.body(containsString("InvalidAccessKeyId"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theRootStandInKeyStillWorks() {
|
||||
given()
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=test/20260101/us-east-1/s3/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=whatever")
|
||||
.when()
|
||||
.get("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user