mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(kms): refuse a key or alias ARN that names another account (#7111)
A key or alias ARN naming another account resolved against the caller's own keys, so an alias ARN from account A, used by account B with an alias of the same name, answered with B's key. Floci does not evaluate key policies, so another account's key is never usable: answer NotFoundException with the ARN as given. Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
1 parent
504c9d23bb
commit
b7e96c4be8
2 files changed
+59
-3
No files matched your search
@@ -2258,9 +2258,16 @@ public class KmsService implements ResourceProvider {
|
||||
ensureAwsManagedKeys(region);
|
||||
}
|
||||
if (AwsArnUtils.isArnFor(keyIdOrArn, "kms")) {
|
||||
String arnRegion = AwsArnUtils.parse(keyIdOrArn).region();
|
||||
if (!region.equals(arnRegion)) {
|
||||
throw new AwsException("NotFoundException", "Invalid arn " + arnRegion, 400);
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(keyIdOrArn);
|
||||
if (!region.equals(arn.region())) {
|
||||
throw new AwsException("NotFoundException", "Invalid arn " + arn.region(), 400);
|
||||
}
|
||||
// Floci does not evaluate key policies, so another account's key is never usable here;
|
||||
// without this the store would answer with the caller's own key or alias of that name.
|
||||
if (!arn.accountId().equals(regionResolver.getAccountId())) {
|
||||
throw new AwsException("NotFoundException", arn.resource().startsWith("alias/")
|
||||
? "Alias " + keyIdOrArn + " is not found."
|
||||
: "Key '" + keyIdOrArn + "' does not exist", 400);
|
||||
}
|
||||
}
|
||||
String id = keyIdOrArn;
|
||||
|
||||
@@ -1226,6 +1226,46 @@ class KmsIntegrationTest {
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void anotherAccountsKeyOrAliasArnIsNotFound() {
|
||||
String owner = "AWS4-HMAC-SHA256 Credential=000000000103/20260215/us-east-1/kms/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
String caller = "AWS4-HMAC-SHA256 Credential=000000000104/20260215/us-east-1/kms/aws4_request, "
|
||||
+ "SignedHeaders=host, Signature=abc";
|
||||
String alias = "alias/shared-" + UUID.randomUUID();
|
||||
String ownerKeyArn = kmsAs(owner, "CreateKey", "{}").then().statusCode(200)
|
||||
.extract().path("KeyMetadata.Arn");
|
||||
String callerKeyArn = kmsAs(caller, "CreateKey", "{}").then().statusCode(200)
|
||||
.extract().path("KeyMetadata.Arn");
|
||||
kmsAs(owner, "CreateAlias", "{\"AliasName\":\"%s\",\"TargetKeyId\":\"%s\"}".formatted(alias, ownerKeyArn))
|
||||
.then().statusCode(200);
|
||||
kmsAs(caller, "CreateAlias", "{\"AliasName\":\"%s\",\"TargetKeyId\":\"%s\"}".formatted(alias, callerKeyArn))
|
||||
.then().statusCode(200);
|
||||
String ownerAliasArn = "arn:aws:kms:us-east-1:000000000103:" + alias;
|
||||
String callerAliasArn = "arn:aws:kms:us-east-1:000000000104:" + alias;
|
||||
|
||||
kmsAs(caller, "DescribeKey", "{\"KeyId\":\"%s\"}".formatted(ownerAliasArn)).then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("NotFoundException"))
|
||||
.body("message", equalTo("Alias " + ownerAliasArn + " is not found."));
|
||||
kmsAs(caller, "DescribeKey", "{\"KeyId\":\"%s\"}".formatted(ownerKeyArn)).then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("NotFoundException"))
|
||||
.body("message", equalTo("Key '" + ownerKeyArn + "' does not exist"));
|
||||
kmsAs(caller, "DescribeKey", "{\"KeyId\":\"%s\"}".formatted(callerAliasArn)).then()
|
||||
.statusCode(200)
|
||||
.body("KeyMetadata.Arn", equalTo(callerKeyArn));
|
||||
kmsAs(owner, "DescribeKey", "{\"KeyId\":\"%s\"}".formatted(ownerAliasArn)).then()
|
||||
.statusCode(200)
|
||||
.body("KeyMetadata.Arn", equalTo(ownerKeyArn));
|
||||
|
||||
for (String[] created : new String[][] {{owner, ownerKeyArn}, {caller, callerKeyArn}}) {
|
||||
kmsAs(created[0], "DeleteAlias", "{\"AliasName\":\"%s\"}".formatted(alias)).then().statusCode(200);
|
||||
kmsAs(created[0], "ScheduleKeyDeletion", "{\"KeyId\":\"%s\",\"PendingWindowInDays\":7}"
|
||||
.formatted(created[1])).then().statusCode(200);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"ListKeys", "ListAliases"})
|
||||
void listKeysAndAliasesRejectBadMarker(String action) {
|
||||
@@ -3276,6 +3316,15 @@ class KmsIntegrationTest {
|
||||
.when().post("/");
|
||||
}
|
||||
|
||||
private static Response kmsAs(String authorization, String operation, String body) {
|
||||
return given()
|
||||
.header("Authorization", authorization)
|
||||
.header("X-Amz-Target", "TrentService." + operation)
|
||||
.contentType(KMS_CONTENT_TYPE)
|
||||
.body(body)
|
||||
.when().post("/");
|
||||
}
|
||||
|
||||
private static List<String> listKeyIds() {
|
||||
return callKms("ListKeys", "{}").then().statusCode(200).extract().jsonPath().getList("Keys.KeyId");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user