From e28c58cc8a7462ceb24d63aac80267e7cd34ec6a Mon Sep 17 00:00:00 2001 From: Sam Fakhreddine Date: Sun, 11 Oct 2026 13:10:53 -0600 Subject: [PATCH] fix(lambda): keep every AddPermission condition and store account principals as root ARNs (#5123) AddPermission kept only one condition and stored an account principal as the bare ID. It now writes every condition member in one statement, laid out the way AWS writes it: SourceArn under ArnLike, SourceAccount, FunctionUrlAuthType, PrincipalOrgID and EventSourceToken under StringEquals, and InvokedViaFunctionUrl under Bool as the string "true". A 12-digit account principal is stored as that account's root ARN in the function's partition, and an ARN is recognised before the dot test, so an IAM role, user or session name with a dot is no longer taken for a service. The condition members are checked against the model's enum and patterns before the function is looked up, and AWS::Lambda::Permission passes all of them through from a template. Fixes #4627 --- .../LambdaAddressingCfnProvisioner.java | 18 +- .../floci/services/lambda/LambdaService.java | 97 +++++++++-- .../LambdaAddressingCfnProvisionerTest.java | 29 ++++ .../services/lambda/LambdaServiceTest.java | 160 ++++++++++++++++++ 4 files changed, 286 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaAddressingCfnProvisioner.java b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaAddressingCfnProvisioner.java index 8189251751..61b54176d9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaAddressingCfnProvisioner.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaAddressingCfnProvisioner.java @@ -42,6 +42,10 @@ public class LambdaAddressingCfnProvisioner implements CfnResourceProvisioner { private static final String PERMISSION = "AWS::Lambda::Permission"; private static final String URL = "AWS::Lambda::Url"; + // The optional AWS::Lambda::Permission string properties, which share their names with the + // AddPermission members that become the statement's conditions. + private static final List PERMISSION_CONDITION_PROPERTIES = List.of( + "SourceArn", "SourceAccount", "PrincipalOrgID", "FunctionUrlAuthType", "EventSourceToken"); private final LambdaService lambdaService; @@ -113,10 +117,16 @@ public class LambdaAddressingCfnProvisioner implements CfnResourceProvisioner { request.put("StatementId", statementId); request.put("Action", ctx.resolveOptional(props, "Action")); request.put("Principal", ctx.resolveOptional(props, "Principal")); - String sourceArn = ctx.resolveOptional(props, "SourceArn"); - if (sourceArn != null && !sourceArn.isBlank()) request.put("SourceArn", sourceArn); - String sourceAccount = ctx.resolveOptional(props, "SourceAccount"); - if (sourceAccount != null && !sourceAccount.isBlank()) request.put("SourceAccount", sourceAccount); + for (String property : PERMISSION_CONDITION_PROPERTIES) { + String value = ctx.resolveOptional(props, property); + if (value != null && !value.isBlank()) { + request.put(property, value); + } + } + String invokedViaFunctionUrl = ctx.resolveOptional(props, "InvokedViaFunctionUrl"); + if (invokedViaFunctionUrl != null && !invokedViaFunctionUrl.isBlank()) { + request.put("InvokedViaFunctionUrl", Boolean.parseBoolean(invokedViaFunctionUrl)); + } try { lambdaService.addPermission(ctx.region(), functionName, null, request); } catch (RuntimeException failure) { diff --git a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java index 9b426f7849..9f7f45123e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambda/LambdaService.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.AwsRegions; import io.github.hectorvent.floci.core.common.CustomResourceLiveness; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.RequestScopes; @@ -54,6 +55,7 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Base64; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -128,6 +130,11 @@ public class LambdaService implements ResourceProvider { private static final Pattern LIST_ESM_FUNCTION_NAME_PATTERN = Pattern.compile( "^(arn:(aws[a-zA-Z-]*)?:lambda:)?((eusc-)?[a-z]{2}((-gov)|(-iso([a-z]?)))?-[a-z]+-\\d{1}:)?(\\d{12}:)?" + "(function:)?([a-zA-Z0-9-_\\.]+)(:(\\$LATEST(\\.PUBLISHED)?|[a-zA-Z0-9-_]+))?$"); + // AddPermission members: botocore's PrincipalOrgID, SourceOwner and EventSourceToken shapes. + private static final Pattern PRINCIPAL_ORG_ID_PATTERN = Pattern.compile("o-[a-z0-9]{10,32}"); + private static final Pattern ACCOUNT_ID_PATTERN = Pattern.compile("\\d{12}"); + private static final Pattern EVENT_SOURCE_TOKEN_PATTERN = Pattern.compile("[a-zA-Z0-9._\\-]+"); + private static final int MAX_EVENT_SOURCE_TOKEN_LENGTH = 256; /** botocore lambda/2015-03-31 Runtime shape enum, kept in sync with service-2.json. */ static final List RUNTIME_VALUES = List.of( @@ -3592,6 +3599,7 @@ public class LambdaService implements ResourceProvider { } public Map addPermission(String region, String functionName, String qualifier, Map request) { + validatePermissionConditions(request); LambdaArnUtils.ResolvedFunctionRef ref = resolveWithRegion(region, functionName, qualifier); LambdaFunction observed = getFunction(region, ref.name()); synchronized (lockForPolicyMutation(observed.getFunctionArn())) { @@ -3612,25 +3620,16 @@ public class LambdaService implements ResourceProvider { String principal = (String) request.get("Principal"); String action = (String) request.get("Action"); - String sourceArn = (String) request.get("SourceArn"); - String sourceAccount = (String) request.get("SourceAccount"); - Map statement = new java.util.LinkedHashMap<>(); + Map statement = new LinkedHashMap<>(); statement.put("Sid", statementId); statement.put("Effect", "Allow"); - if (principal != null && principal.contains(".")) { - statement.put("Principal", Map.of("Service", principal)); - } else if (principal != null && principal.startsWith("arn:")) { - statement.put("Principal", Map.of("AWS", principal)); - } else { - statement.put("Principal", principal); - } + statement.put("Principal", permissionPrincipal(principal, region)); statement.put("Action", action); statement.put("Resource", resourceArn); - if (sourceArn != null) { - statement.put("Condition", Map.of("ArnLike", Map.of("AWS:SourceArn", sourceArn))); - } else if (sourceAccount != null) { - statement.put("Condition", Map.of("StringEquals", Map.of("AWS:SourceAccount", sourceAccount))); + Map condition = permissionCondition(request); + if (!condition.isEmpty()) { + statement.put("Condition", condition); } fn.getPolicies().add(statement); @@ -3640,6 +3639,76 @@ public class LambdaService implements ResourceProvider { } } + /** The model constraints on the AddPermission members that become conditions. */ + private static void validatePermissionConditions(Map request) { + validatePattern(request.get("SourceAccount"), "sourceAccount", ACCOUNT_ID_PATTERN); + validatePattern(request.get("PrincipalOrgID"), "principalOrgID", PRINCIPAL_ORG_ID_PATTERN); + validateEnum(request.get("FunctionUrlAuthType"), "functionUrlAuthType", FUNCTION_URL_AUTH_TYPE_VALUES); + validateMaxLength(request.get("EventSourceToken"), "eventSourceToken", MAX_EVENT_SOURCE_TOKEN_LENGTH); + validatePattern(request.get("EventSourceToken"), "eventSourceToken", EVENT_SOURCE_TOKEN_PATTERN); + } + + /** + * The statement's Principal as AWS writes it: an ARN, or an account ID as that account's root + * ARN, is an {@code AWS} principal, a domain-style name is a {@code Service}, and anything else + * (such as {@code *}) is kept as given. ARNs are checked first because an IAM role, user or + * session name may contain a dot. + */ + private static Object permissionPrincipal(String principal, String region) { + if (principal == null) { + return null; + } + if (principal.startsWith("arn:")) { + return Map.of("AWS", principal); + } + if (ACCOUNT_ID_PATTERN.matcher(principal).matches()) { + String partition = AwsRegions.partitionFor(region); + return Map.of("AWS", AwsArnUtils.Arn.global(partition, "iam", principal, "root").toString()); + } + if (principal.contains(".")) { + return Map.of("Service", principal); + } + return principal; + } + + /** + * The statement's Condition, in the layout AWS writes: {@code SourceArn} under {@code ArnLike}, + * the other string members under {@code StringEquals}, and {@code InvokedViaFunctionUrl} under + * {@code Bool} as the string {@code "true"}. Empty when the request carries none of them. + */ + private static Map permissionCondition(Map request) { + Map stringEquals = new LinkedHashMap<>(); + String sourceAccount = (String) request.get("SourceAccount"); + if (sourceAccount != null) { + stringEquals.put("AWS:SourceAccount", sourceAccount); + } + String functionUrlAuthType = (String) request.get("FunctionUrlAuthType"); + if (functionUrlAuthType != null) { + stringEquals.put("lambda:FunctionUrlAuthType", functionUrlAuthType); + } + String principalOrgId = (String) request.get("PrincipalOrgID"); + if (principalOrgId != null) { + stringEquals.put("aws:PrincipalOrgID", principalOrgId); + } + String eventSourceToken = (String) request.get("EventSourceToken"); + if (eventSourceToken != null) { + stringEquals.put("lambda:EventSourceToken", eventSourceToken); + } + + Map condition = new LinkedHashMap<>(); + if (!stringEquals.isEmpty()) { + condition.put("StringEquals", stringEquals); + } + String sourceArn = (String) request.get("SourceArn"); + if (sourceArn != null) { + condition.put("ArnLike", Map.of("AWS:SourceArn", sourceArn)); + } + if (Boolean.TRUE.equals(request.get("InvokedViaFunctionUrl"))) { + condition.put("Bool", Map.of("lambda:InvokedViaFunctionUrl", "true")); + } + return condition; + } + public Map getPolicy(String region, String functionName, String qualifier) { LambdaArnUtils.ResolvedFunctionRef ref = resolveWithRegion(region, functionName, qualifier); LambdaFunction observed = getFunction(region, ref.name()); diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaAddressingCfnProvisionerTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaAddressingCfnProvisionerTest.java index f1d20a75d1..872de806b7 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaAddressingCfnProvisionerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cloudformation/provisioners/LambdaAddressingCfnProvisionerTest.java @@ -98,6 +98,35 @@ class LambdaAddressingCfnProvisionerTest { assertEquals("app-fn|InvokePermission", r.getPhysicalId()); } + @Test + void everyConditionPropertyReachesAddPermission() { + // Catches: a template's PrincipalOrgID, FunctionUrlAuthType, EventSourceToken or + // InvokedViaFunctionUrl dropped, which widens the statement to every caller its principal names + ObjectNode props = props("app-fn") + .put("Principal", "*") + .put("SourceArn", "arn:aws:s3:::test-bucket") + .put("SourceAccount", "111111111111") + .put("PrincipalOrgID", "o-1234567890") + .put("FunctionUrlAuthType", "NONE") + .put("EventSourceToken", "amzn1.ask.skill.12345678-1234-1234-1234-123456789012") + .put("InvokedViaFunctionUrl", true); + + provisioner.provision(resource(), props, ctx()); + + ArgumentCaptor> request = ArgumentCaptor.captor(); + verify(lambda).addPermission(eq(REGION), eq("app-fn"), isNull(), request.capture()); + assertEquals(Map.of( + "StatementId", "InvokePermission", + "Action", "lambda:InvokeFunction", + "Principal", "*", + "SourceArn", "arn:aws:s3:::test-bucket", + "SourceAccount", "111111111111", + "PrincipalOrgID", "o-1234567890", + "FunctionUrlAuthType", "NONE", + "EventSourceToken", "amzn1.ask.skill.12345678-1234-1234-1234-123456789012", + "InvokedViaFunctionUrl", true), request.getValue()); + } + private ProvisionContext ctx() { CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class); when(engine.resolve(any())).thenAnswer(inv -> { diff --git a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java index 0e68570888..09ea6867e5 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambda/LambdaServiceTest.java @@ -2549,4 +2549,164 @@ class LambdaServiceTest { assertEquals(100, esm.getBatchSize()); } + + @Test + void addPermission_keepsSourceAccountAlongsideSourceArn() { + // Catches: SourceAccount is silently dropped from the statement when SourceArn is also given + service.createFunction(REGION, baseRequest("perm-conditions-fn")); + + Map statement = service.addPermission(REGION, "perm-conditions-fn", null, Map.of( + "StatementId", "s3-invoke", + "Action", "lambda:InvokeFunction", + "Principal", "s3.amazonaws.com", + "SourceArn", "arn:aws:s3:::test-bucket", + "SourceAccount", "111111111111")); + + assertEquals(Map.of( + "ArnLike", Map.of("AWS:SourceArn", "arn:aws:s3:::test-bucket"), + "StringEquals", Map.of("AWS:SourceAccount", "111111111111")), statement.get("Condition")); + } + + @Test + void addPermission_storesAnAccountIdPrincipalAsItsRootArn() { + // Catches: a bare 12-digit account ID principal is stored raw instead of as an AWS root ARN + service.createFunction(REGION, baseRequest("perm-principal-fn")); + + Map statement = service.addPermission(REGION, "perm-principal-fn", null, Map.of( + "StatementId", "cross-account", + "Action", "lambda:InvokeFunction", + "Principal", "111122223333")); + + assertEquals(Map.of("AWS", "arn:aws:iam::111122223333:root"), statement.get("Principal")); + } + + @Test + void addPermission_keepsEveryStringConditionInOneStatement() { + // Catches: PrincipalOrgID or FunctionUrlAuthType dropped, or split from SourceArn/SourceAccount + service.createFunction(REGION, baseRequest("perm-all-fields-fn")); + + Map statement = service.addPermission(REGION, "perm-all-fields-fn", null, Map.of( + "StatementId", "urlPermission", + "Action", "lambda:InvokeFunctionUrl", + "Principal", "*", + "SourceArn", "arn:aws:s3:::test-bucket", + "SourceAccount", "111111111111", + "PrincipalOrgID", "o-1234567890", + "FunctionUrlAuthType", "NONE")); + + assertEquals("*", statement.get("Principal")); + assertEquals(Map.of( + "StringEquals", Map.of( + "AWS:SourceAccount", "111111111111", + "lambda:FunctionUrlAuthType", "NONE", + "aws:PrincipalOrgID", "o-1234567890"), + "ArnLike", Map.of("AWS:SourceArn", "arn:aws:s3:::test-bucket")), statement.get("Condition")); + } + + @Test + void addPermission_storesAnEventSourceTokenAsAStringEqualsCondition() { + // Catches: EventSourceToken dropped, which leaves an Alexa skill grant open to every caller + service.createFunction(REGION, baseRequest("perm-alexa-fn")); + + Map statement = service.addPermission(REGION, "perm-alexa-fn", null, Map.of( + "StatementId", "alexaSkill", + "Action", "lambda:InvokeFunction", + "Principal", "*", + "EventSourceToken", "amzn1.ask.skill.12345678-1234-1234-1234-123456789012")); + + assertEquals(Map.of("StringEquals", Map.of( + "lambda:EventSourceToken", "amzn1.ask.skill.12345678-1234-1234-1234-123456789012")), + statement.get("Condition")); + } + + @Test + void addPermission_invokedViaFunctionUrl_isABoolConditionWithAStringValue() { + // Catches: InvokedViaFunctionUrl dropped, which turns the public function URL grant into an + // unconditioned "*" grant on InvokeFunction; and a JSON boolean, which clients parsing the + // condition values as strings cannot read + service.createFunction(REGION, baseRequest("perm-url-invoke-fn")); + + Map statement = service.addPermission(REGION, "perm-url-invoke-fn", null, Map.of( + "StatementId", "FunctionURLInvokeAllowPublicAccess", + "Action", "lambda:InvokeFunction", + "Principal", "*", + "InvokedViaFunctionUrl", true)); + + assertEquals(Map.of("Bool", Map.of("lambda:InvokedViaFunctionUrl", "true")), statement.get("Condition")); + } + + @Test + void addPermission_roleArnWithADotInItsName_staysAnAwsPrincipal() { + // Catches: the dot check running before the ARN check, which stores an IAM ARN as a Service + service.createFunction(REGION, baseRequest("perm-dotted-role-fn")); + + Map statement = service.addPermission(REGION, "perm-dotted-role-fn", null, Map.of( + "StatementId", "dotted-role", + "Action", "lambda:InvokeFunction", + "Principal", "arn:aws:iam::111122223333:role/my.role")); + + assertEquals(Map.of("AWS", "arn:aws:iam::111122223333:role/my.role"), statement.get("Principal")); + } + + @Test + void addPermission_accountIdInAChinaRegion_isTheRootArnInTheAwsCnPartition() { + // Catches: an account principal minted in the commercial partition for a function outside it + Map request = baseRequest("perm-cn-principal-fn"); + request.put("Role", "arn:aws-cn:iam::000000000000:role/test-role"); + service.createFunction("cn-north-1", request); + + Map statement = service.addPermission("cn-north-1", "perm-cn-principal-fn", null, Map.of( + "StatementId", "cross-account", + "Action", "lambda:InvokeFunction", + "Principal", "111122223333")); + + assertEquals(Map.of("AWS", "arn:aws-cn:iam::111122223333:root"), statement.get("Principal")); + } + + @Test + void addPermission_functionUrlAuthTypeOutsideTheEnum_isRejectedAndLeavesNoStatement() { + // Catches: an unknown FunctionUrlAuthType stored as a condition no function URL can ever match + service.createFunction(REGION, baseRequest("perm-auth-type-fn")); + + AwsException error = assertThrows(AwsException.class, () -> + service.addPermission(REGION, "perm-auth-type-fn", null, Map.of( + "StatementId", "url", + "Action", "lambda:InvokeFunctionUrl", + "Principal", "*", + "FunctionUrlAuthType", "PUBLIC"))); + + assertEquals("ValidationException", error.getErrorCode()); + assertEquals("1 validation error detected: Value 'PUBLIC' at 'functionUrlAuthType' failed to satisfy " + + "constraint: Member must satisfy enum value set: [NONE, AWS_IAM]", error.getMessage()); + AwsException missing = assertThrows(AwsException.class, + () -> service.getPolicy(REGION, "perm-auth-type-fn", null)); + assertEquals("ResourceNotFoundException", missing.getErrorCode()); + } + + @Test + void addPermission_conditionValuesOutsideTheModelPatterns_areRejected() { + // Catches: a SourceAccount, PrincipalOrgID or EventSourceToken the model's pattern refuses being stored + service.createFunction(REGION, baseRequest("perm-patterns-fn")); + + AwsException sourceAccount = assertThrows(AwsException.class, () -> + service.addPermission(REGION, "perm-patterns-fn", null, Map.of( + "StatementId", "s3", "Action", "lambda:InvokeFunction", "Principal", "s3.amazonaws.com", + "SourceAccount", "arn:aws:iam::111111111111:root"))); + AwsException principalOrgId = assertThrows(AwsException.class, () -> + service.addPermission(REGION, "perm-patterns-fn", null, Map.of( + "StatementId", "org", "Action", "lambda:InvokeFunction", "Principal", "*", + "PrincipalOrgID", "my-org"))); + AwsException eventSourceToken = assertThrows(AwsException.class, () -> + service.addPermission(REGION, "perm-patterns-fn", null, Map.of( + "StatementId", "alexa", "Action", "lambda:InvokeFunction", "Principal", "*", + "EventSourceToken", "not a token"))); + + assertEquals("1 validation error detected: Value 'arn:aws:iam::111111111111:root' at 'sourceAccount' " + + "failed to satisfy constraint: Member must satisfy regular expression pattern: \\d{12}", + sourceAccount.getMessage()); + assertEquals("1 validation error detected: Value 'my-org' at 'principalOrgID' failed to satisfy " + + "constraint: Member must satisfy regular expression pattern: o-[a-z0-9]{10,32}", + principalOrgId.getMessage()); + assertEquals("ValidationException", eventSourceToken.getErrorCode()); + } }