fix(lambdaweb): give the code bucket check the version as s3:VersionId (#6483)

This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-10-25 07:39:20 -04:00
1 parent 3dc5d356d0
commit 8f3a6eafeb
6 files changed
+61 -10

No files matched your search

@@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
@@ -121,6 +122,23 @@ class ServiceCallAuthorizerTest {
authorizer.authorize("s3", "sns:Publish", TOPIC, "arn:aws:s3:::bucket", OWNER));
}
@Test
void theKeysTheTargetAddsReachThePolicy() {
String object = "arn:aws:s3:::code/app.zip";
String policy = """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow",
"Principal":{"Service":"lambda.amazonaws.com"},"Action":"s3:GetObjectVersion","Resource":"%s",
"Condition":{"StringEquals":{"s3:VersionId":"v2"}}}]}""".formatted(object);
providers.add((scope, arn) -> object.equals(arn)
? List.of(new ResourcePolicyProvider.ResourcePolicy(policy, OWNER)) : List.of());
assertEquals(Decision.ALLOW, authorizer.authorize("lambda", "s3:GetObjectVersion", object, null, OWNER,
Map.of(IamConditionContextResolver.VERSION_ID, List.of("v2"))));
assertEquals(Decision.DENY, authorizer.authorize("lambda", "s3:GetObjectVersion", object, null, OWNER,
Map.of(IamConditionContextResolver.VERSION_ID, List.of("v1"))));
assertEquals(Decision.DENY, authorizer.authorize("lambda", "s3:GetObjectVersion", object, null, OWNER));
}
@Test
void aDenyInTheTopicPolicyWins() {
topicPolicy("""
@@ -92,9 +92,7 @@ class LambdaWebCodeBucketIntegrationTest {
void lambdaNeedsGetObjectVersionUnderTheConditionsItSends() {
s3Service.putBucketVersioning(bucket, "Enabled");
put(bucket);
String refusal = "Lambda service principal lambda.amazonaws.com does not have s3:GetObject and"
+ " s3:GetObjectVersion permission on s3://" + bucket + "/app.zip. Update the bucket policy to grant"
+ " the Lambda service principal access and retry.";
String refusal = lambdaRefusal();
refused(null, refusal);
policy(allow("\"s3:GetObject\"", null));
@@ -115,8 +113,29 @@ class LambdaWebCodeBucketIntegrationTest {
refused(null, refusal);
}
private void put(String in) {
s3Service.putObject(in, "app.zip", LambdaWebZips.indexJs(""), "application/zip", Map.of());
@Test
void aGrantForOneVersionAdmitsOnlyThatVersion() {
s3Service.putBucketVersioning(bucket, "Enabled");
String older = put(bucket);
String latest = put(bucket);
policy(allow("\"s3:GetObjectVersion\"", "{\"StringEquals\":{\"s3:VersionId\":\"" + latest + "\"}}"));
accepted(null);
accepted(latest);
refused(older, lambdaRefusal());
policy(allow("\"s3:GetObjectVersion\"", "{\"StringEquals\":{\"s3:VersionId\":\"" + older + "\"}}"));
refused(null, lambdaRefusal());
accepted(older);
}
private String lambdaRefusal() {
return "Lambda service principal lambda.amazonaws.com does not have s3:GetObject and s3:GetObjectVersion"
+ " permission on s3://" + bucket + "/app.zip. Update the bucket policy to grant the Lambda service"
+ " principal access and retry.";
}
private String put(String in) {
return s3Service.putObject(in, "app.zip", LambdaWebZips.indexJs(""), "application/zip", Map.of())
.getVersionId();
}
private void policy(String statements) {
@@ -947,7 +947,8 @@ class LambdaWebServiceTest {
S3Service s3 = mock(S3Service.class);
LambdaWebService service = service(new MemoryStorageFactory(), "http://localhost:4566", s3);
when(serviceCalls.authorize(eq("lambda"), eq("s3:GetObjectVersion"), eq("arn:aws:s3:::code/app.zip"),
startsWith(ARN + "/revision/rev-"), eq(ACCOUNT_ID))).thenReturn(Decision.DENY);
startsWith(ARN + "/revision/rev-"), eq(ACCOUNT_ID), eq(Map.of("s3:versionid", List.of("v1")))))
.thenReturn(Decision.DENY);
when(s3.openObject("code", "app.zip", null)).thenReturn(object(new byte[0]));
AwsException create = error(() -> service.createFunction(REGION, createRequest("app")));