fix(iam): check the rest of the Lambda function API's routes (#6226)

Under IAM enforcement a REST request whose route no rule maps is let through unchecked. 51
operations of Lambda's function API had no rule, 25 of them served by Floci, so any signed caller
could read a function's configuration, environment variables included, publish versions, set its
concurrency and event invoke config, and create, read or delete layer versions and code signing
configs, with no Lambda permission at all. ListFunctionsByCodeSigningConfig, whose route ends in
/functions, was checked as lambda:ListFunctions.

Every operation of the function API now has a rule and is checked as the action of its own name,
as the Service Authorization Reference gives it, including the routes Floci does not serve yet:
provisioned concurrency, capacity providers, layer version permissions, the recursion, scaling and
runtime management configs, PutFunctionCodeSigningConfig, InvokeAsync, ListFunctionUrlConfigs and
the resource policy routes. IamRestRouteCoverageTest checks every operation of the lambda model,
as it does Scheduler, Pipes, MicroVMs and network connectors.

Each is checked against the resource the reference gives it:
- a function's settings against the function, as the other function routes are;
- an event source mapping against event-source-mapping:<uuid>, which DeleteEventSourceMapping now
  gets too in place of *;
- a code signing config against code-signing-config:<id>, the id that ends the ARN the route takes,
  in the request's account and Region, as Lambda looks it up;
- a layer version, its policy and its permissions against layer:<name>:<version>, the name as
  given, even a name given as an ARN or holding an encoded /, since Lambda looks the layer up by it
  as it is, and the version as the number Lambda parses, so 01 is checked as 1;
  PublishLayerVersion against layer:<name>, and GetLayerVersionByArn against the version its Arn
  names, its number read the same way;
- account settings, creating a config, and listing configs, layers or a layer's versions against
  *, as they name no resource.

AWS also checks lambda:AddPermission, lambda:GetPolicy or lambda:RemovePermission for the resource
policy operations, and iam:PassRole for CreateCapacityProvider. Floci serves neither, so they are
checked as their own action alone.

docs/services/iam.md says which Lambda routes are mapped and what each is checked against.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-22 07:24:10 -05:00
1 parent 8427c0b71d
commit 6763b405dd
7 files changed
+353 -13

No files matched your search

@@ -263,9 +263,9 @@ class IamActionRegistryTest {
mockCtx("GET", "/2018-10-31/layers", byArn, MediaType.APPLICATION_JSON_TYPE, "{}")));
MultivaluedHashMap<String, String> other = new MultivaluedHashMap<>();
other.putSingle("find", "Layer");
assertNull(registry.resolveRoute("lambda",
assertEquals("lambda:ListLayers", registry.resolveRoute("lambda",
mockCtx("GET", "/2018-10-31/layers", other, MediaType.APPLICATION_JSON_TYPE, "{}")));
assertNull(lambdaAction("GET", "/2018-10-31/layers"));
assertEquals("lambda:ListLayers", lambdaAction("GET", "/2018-10-31/layers"));
}
@Test
@@ -52,7 +52,8 @@ class IamRestRouteCoverageTest {
"DeleteBucketMetadataConfiguration", "s3:DeleteBucket");
/** Models every operation of which is checked: the services whose routes the registry maps in full. */
private static final Set<String> FULLY_ROUTED_MODELS = Set.of("scheduler", "pipes", "lambda-microvms", "lambda-core");
private static final Set<String> FULLY_ROUTED_MODELS =
Set.of("scheduler", "pipes", "lambda", "lambda-microvms", "lambda-core");
private final IamActionRegistry registry = new IamActionRegistry();
@@ -158,6 +158,94 @@ class IamRestRouteEnforcementIntegrationTest {
}
}
@Test
void lambdaFunctionSettingsRoutesAreCheckedAgainstTheFunction() {
// Each is checked as its own action against the function it names: a grant on another function
// does not cover it, and one on the function does. The request then reaches Lambda, which has no
// such function.
String user = user("route-lambda-settings");
String name = "route-settings-" + suffix();
String function = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":function:" + name;
String[][] routes = {
{"GET", "/2015-03-31/functions/" + name + "/configuration", "lambda:GetFunctionConfiguration"},
{"GET", "/2015-03-31/functions/" + name + "/versions", "lambda:ListVersionsByFunction"},
{"POST", "/2015-03-31/functions/" + name + "/versions", "lambda:PublishVersion"},
{"PUT", "/2017-10-31/functions/" + name + "/concurrency", "lambda:PutFunctionConcurrency"},
{"GET", "/2019-09-30/functions/" + name + "/concurrency", "lambda:GetFunctionConcurrency"},
{"PUT", "/2019-09-25/functions/" + name + "/event-invoke-config", "lambda:PutFunctionEventInvokeConfig"},
{"GET", "/2020-06-30/functions/" + name + "/code-signing-config", "lambda:GetFunctionCodeSigningConfig"},
};
for (String[] route : routes) {
policy(user, allow(route[2], function + "-other"));
refused(send(user, route[0], route[1]), route[2]);
policy(user, allow(route[2], function));
send(user, route[0], route[1]).statusCode(not(403));
}
}
@Test
void lambdaMappingConfigAndLayerRoutesAreCheckedAgainstWhatTheyName() {
// A grant on the same resource in another account does not cover the caller's own.
String user = user("route-lambda-resources");
String uuid = "14e0db71-5d35-4eb5-b481-8945cf9d10c2";
String layerName = "route-layer-" + suffix();
String mapping = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":event-source-mapping:" + uuid;
String config = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":code-signing-config:csc-0123456789abcdef0";
String layer = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":layer:" + layerName;
String[][] routes = {
{"GET", "/2015-03-31/event-source-mappings/" + uuid, "lambda:GetEventSourceMapping", mapping},
{"PUT", "/2015-03-31/event-source-mappings/" + uuid, "lambda:UpdateEventSourceMapping", mapping},
{"GET", "/2020-04-22/code-signing-configs/" + config, "lambda:GetCodeSigningConfig", config},
{"GET", "/2020-04-22/code-signing-configs/" + config + "/functions",
"lambda:ListFunctionsByCodeSigningConfig", config},
{"GET", "/2018-10-31/layers/" + layerName + "/versions/1", "lambda:GetLayerVersion", layer + ":1"},
{"DELETE", "/2018-10-31/layers/" + layerName + "/versions/1", "lambda:DeleteLayerVersion", layer + ":1"},
{"POST", "/2018-10-31/layers/" + layerName + "/versions", "lambda:PublishLayerVersion", layer},
};
for (String[] route : routes) {
policy(user, allow(route[2], route[3].replace(ACCOUNT, "111122223333")));
refused(send(user, route[0], route[1]), route[2]);
policy(user, allow(route[2], route[3]));
send(user, route[0], route[1]).statusCode(not(403));
}
// A config's function list ends in /functions, as ListFunctions does, and is not checked as it.
policy(user, allow("lambda:ListFunctions", "*"));
refused(send(user, "GET", "/2020-04-22/code-signing-configs/" + config + "/functions"),
"lambda:ListFunctionsByCodeSigningConfig");
}
@Test
void aLayerVersionIsCheckedAsLambdaReadsItsNameAndNumber() {
// Lambda parses version 01 as version 1, and the router keeps an encoded / inside the layer's name, so
// a Deny on that layer version applies however the request spells them.
String user = user("route-layer-spelling");
String name = "route-spelt-" + suffix();
String layer = "arn:aws:lambda:us-east-1:" + ACCOUNT + ":layer:" + name;
policy(user, allow("lambda:GetLayerVersion", "*") + "," + deny("lambda:GetLayerVersion", layer + ":1")
+ "," + deny("lambda:GetLayerVersion", layer + "/x:1"));
refused(as(user, "lambda").get("/2018-10-31/layers/" + name + "/versions/01").then(),
"lambda:GetLayerVersion");
refused(as(user, "lambda").queryParam("find", "LayerVersion").queryParam("Arn", layer + ":01")
.get("/2018-10-31/layers").then(), "lambda:GetLayerVersion");
refused(as(user, "lambda").urlEncodingEnabled(false).get("/2018-10-31/layers/" + name + "%2Fx/versions/1")
.then(), "lambda:GetLayerVersion");
// Allowed, each reaches Lambda, which looks up that version of that layer.
policy(user, allow("lambda:GetLayerVersion", "*"));
as(user, "lambda").get("/2018-10-31/layers/" + name + "/versions/01").then().statusCode(404)
.body("message", containsString("Layer version 1 for layer " + name + " not found"));
as(user, "lambda").urlEncodingEnabled(false).get("/2018-10-31/layers/" + name + "%2Fx/versions/1").then()
.statusCode(404).body("message", containsString("for layer " + name + "/x not found"));
}
@Test
void lambdaAccountSettingsAreChecked() {
String user = user("route-lambda-account");
policy(user, allow("lambda:ListFunctions", "*"));
refused(as(user, "lambda").get("/2016-08-19/account-settings").then(), "lambda:GetAccountSettings");
policy(user, allow("lambda:GetAccountSettings", "*"));
as(user, "lambda").get("/2016-08-19/account-settings").then().statusCode(200);
}
@Test
void aMicrovmIsCheckedAgainstTheImageItRuns() {
// RunMicrovm and the actions on a running MicroVM act on its image, so a Deny on the image applies to
@@ -238,6 +326,15 @@ class IamRestRouteEnforcementIntegrationTest {
return given().header("Authorization", auth(keys.get(user), service)).contentType("application/json");
}
/** A Lambda request as {@code user}, with an empty JSON body on a write. */
private ValidatableResponse send(String user, String method, String path) {
RequestSpecification request = as(user, "lambda");
if ("POST".equals(method) || "PUT".equals(method)) {
request.body("{}");
}
return request.request(method, path).then();
}
private static String suffix() {
return Long.toString(System.nanoTime(), 36);
}
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
@@ -1256,6 +1257,88 @@ class ResourceArnBuilderTest {
}
}
@Test
void lambdaMappingConfigAndLayerRoutesResolveTheResourceTheyName() {
when(ctx.getMethod()).thenReturn("GET");
request("/2015-03-31/event-source-mappings/14e0db71-5d35-4eb5-b481-8945cf9d10c2");
assertEquals("arn:aws:lambda:us-east-1:000000000000:event-source-mapping:14e0db71-5d35-4eb5-b481-8945cf9d10c2",
builder.build("lambda", ctx, "us-east-1", ACCOUNT));
// A config is looked up by the id that ends its ARN, in the request's account and Region.
String config = "arn:aws:lambda:us-east-1:000000000000:code-signing-config:csc-0123456789abcdef0";
for (String path : new String[] {"/2020-04-22/code-signing-configs/" + config,
"/2020-04-22/code-signing-configs/" + config + "/functions",
"/2020-04-22/code-signing-configs/arn:aws:lambda:eu-west-1:111122223333:code-signing-config:"
+ "csc-0123456789abcdef0"}) {
request(path);
assertEquals(config, builder.build("lambda", ctx, "us-east-1", ACCOUNT), path);
}
// A layer version, its policy and its permissions.
String version = "arn:aws:lambda:us-east-1:000000000000:layer:my-layer:3";
for (String path : new String[] {"/2018-10-31/layers/my-layer/versions/3",
"/2018-10-31/layers/my-layer/versions/3/policy",
"/2018-10-31/layers/my-layer/versions/3/policy/statement-1"}) {
request(path);
assertEquals(version, builder.build("lambda", ctx, "us-east-1", ACCOUNT), path);
}
// Lambda looks a layer named by an ARN up by that name as given, not as the layer the ARN names, so
// the layer of that name is the one checked.
String named = "arn:aws:lambda:us-east-1:000000000000:layer:my-layer";
request("/2018-10-31/layers/" + named + "/versions/3");
assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:" + named + ":3",
builder.build("lambda", ctx, "us-east-1", ACCOUNT));
// GetLayerVersionByArn reads the version its Arn names.
request("/2018-10-31/layers");
queryParams.putSingle("find", "LayerVersion");
queryParams.putSingle("Arn", version);
assertEquals(version, builder.build("lambda", ctx, "us-east-1", ACCOUNT));
queryParams.clear();
// Listing layers, a layer's versions or configs, and creating a config, name no resource.
for (String path : new String[] {"/2018-10-31/layers", "/2018-10-31/layers/my-layer/versions",
"/2020-04-22/code-signing-configs"}) {
request(path);
assertEquals("*", builder.build("lambda", ctx, "us-east-1", ACCOUNT), path);
}
when(ctx.getMethod()).thenReturn("POST");
request("/2020-04-22/code-signing-configs");
assertEquals("*", builder.build("lambda", ctx, "us-east-1", ACCOUNT));
// PublishLayerVersion makes a version of the layer, so it is checked against the layer.
request("/2018-10-31/layers/my-layer/versions");
assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:my-layer",
builder.build("lambda", ctx, "us-east-1", ACCOUNT));
// A function's own settings, such as its concurrency or versions, are checked against the function.
request("/2015-03-31/functions/my-function/versions");
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function",
builder.build("lambda", ctx, "us-east-1", ACCOUNT));
when(ctx.getMethod()).thenReturn("PUT");
request("/2017-10-31/functions/my-function/concurrency");
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function",
builder.build("lambda", ctx, "us-east-1", ACCOUNT));
}
@Test
void aLayerVersionIsReadAsLambdaReadsItsNameAndNumber() {
// Lambda parses the version number, so 01 is version 1, and the router keeps an encoded / inside the
// layer's name, so the layer checked is the one Lambda looks up.
when(ctx.getMethod()).thenReturn("GET");
request("/2018-10-31/layers/my-layer/versions/01");
assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:my-layer:1",
builder.build("lambda", ctx, "us-east-1", ACCOUNT));
request("/2018-10-31/layers/my%2Flayer/versions/3/policy");
assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:my/layer:3",
builder.build("lambda", ctx, "us-east-1", ACCOUNT));
when(ctx.getMethod()).thenReturn("POST");
request("/2018-10-31/layers/my%2Flayer/versions");
assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:my/layer",
builder.build("lambda", ctx, "us-east-1", ACCOUNT));
// GetLayerVersionByArn parses the number the same way.
when(ctx.getMethod()).thenReturn("GET");
request("/2018-10-31/layers");
queryParams.putSingle("find", "LayerVersion");
queryParams.putSingle("Arn", "arn:aws:lambda:us-east-1:000000000000:layer:my-layer:01");
assertEquals("arn:aws:lambda:us-east-1:000000000000:layer:my-layer:1",
builder.build("lambda", ctx, "us-east-1", ACCOUNT));
}
@Test
void anActionOnARunningMicrovmIsCheckedAgainstTheImageItRuns() {
String image = "arn:aws:lambda:us-east-1:000000000000:microvm-image:img";
@@ -1503,4 +1586,11 @@ class ResourceArnBuilderTest {
String arn = builder.build("lambda", ctx, "us-east-1", "000000000000");
assertEquals("arn:aws:lambda:us-east-1:000000000000:function:my-function:1/durable-execution/run-1/0b1c", arn);
}
/** Stubs the request path as sent and, decoded, as {@link UriInfo#getPath()} gives it. */
private void request(String sent) {
when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost:4566" + sent));
when(uriInfo.getBaseUri()).thenReturn(URI.create("http://localhost:4566/"));
when(uriInfo.getPath()).thenReturn(URI.create(sent).getPath());
}
}