From b7697f814805e66034d173068d4d19870ebb31fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?TATSUNO=20=E2=80=9CTaz=E2=80=9D=20Yasuhiro?= Date: Mon, 2 Nov 2026 11:03:46 +0900 Subject: [PATCH] feat(lambda-web): write the platform's own Date header on app responses in place of the app's (#7257) * feat(lambda-web): write the platform's own Date header on app responses in place of the app's * test(lambda-web): check the Date header in the headers test on its function --- docs/services/lambda-web-functions.md | 2 +- .../lambdaweb/LambdaWebDataPlane.java | 4 ++- ...mbdaWebDataPlaneDockerIntegrationTest.java | 28 +++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/services/lambda-web-functions.md b/docs/services/lambda-web-functions.md index 356bbc4738..24f1604e3a 100644 --- a/docs/services/lambda-web-functions.md +++ b/docs/services/lambda-web-functions.md @@ -77,7 +77,7 @@ Requests with two revisions in `revisionWeights` are split by weight. Each endpo Each regional domain of a `PerRegion` endpoint is served in its own region, as on AWS. Its environments see that region in `AWS_REGION` and `AWS_DEFAULT_REGION` and get that region's zone from the metadata endpoint, and `maxEnvironments` and `rateLimit` hold in each region on its own. AWS serves the global domain of a `MultiRegion` endpoint from the region nearest the caller. Floci takes `floci.default-region` for where the caller is, so it serves the global domain in that region when the endpoint has it, and in the home region otherwise. The serving region then works as for a regional domain. -Requests are forwarded over HTTP/1.1 with the headers AWS adds and drops: `lambda-web-request-id`, `lambda-web-endpoint-name`, `x-forwarded-proto`, `x-forwarded-port` and `Connection: close` are set, the client address is appended as an `X-Forwarded-For` line, and `Connection`, `Keep-Alive`, `Upgrade`, `Expect`, `Trailer`, the `Proxy-*` headers, `X-Real-IP`, the method override headers and any `lambda-web-*` header the client sent are dropped. A body without a `Content-Length` reaches the app chunked. The path reaches the app without dot segments, as on AWS, and repeated slashes stay. Responses carry `lambda-web-request-id` and `lambda-web-logstream`, lose their connection headers and trailers, and stream as the app writes them. +Requests are forwarded over HTTP/1.1 with the headers AWS adds and drops: `lambda-web-request-id`, `lambda-web-endpoint-name`, `x-forwarded-proto`, `x-forwarded-port` and `Connection: close` are set, the client address is appended as an `X-Forwarded-For` line, and `Connection`, `Keep-Alive`, `Upgrade`, `Expect`, `Trailer`, the `Proxy-*` headers, `X-Real-IP`, the method override headers and any `lambda-web-*` header the client sent are dropped. A body without a `Content-Length` reaches the app chunked. The path reaches the app without dot segments, as on AWS, and repeated slashes stay. Responses carry `lambda-web-request-id` and `lambda-web-logstream`, lose their connection headers and trailers, and stream as the app writes them. The platform writes its own `Date` in place of the app's, as on AWS, and a `date` the app writes in lowercase passes beside it. An `IamAuth` endpoint takes a request signed with SigV4 for the service `lambda` in the region of the domain, in the `Authorization` header or in a presigned query. The request must carry `X-Amz-Content-SHA256`. Lambda takes its value as the payload hash and does not read the body, so `UNSIGNED-PAYLOAD` works. The signing time must be within five minutes. Floci verifies keys whose secret it holds: `test` with the secret `test`, IAM user keys and STS credentials with their session token. Any other key is refused. When `floci.services.iam.enforcement-enabled` is on, the signer also needs `lambda:InvokeWebFunctionEndpoint` on the endpoint ARN. Within one account the identity policy or the function's resource policy can allow it, and across accounts both must. `lambda:Resource/WebFunctionAuthType` and the function's tags as `aws:ResourceTag/` are in the context, and `aws:RequestedRegion` is the region that serves the request, as on AWS. The `test` key is not evaluated, as elsewhere in Floci. The global domain of a `MultiRegion` endpoint takes SigV4 for the region that serves the request, as on AWS. Every endpoint also takes SigV4a (`AWS4-ECDSA-P256-SHA256`), as AWS does, though AWS [documents it](https://docs.aws.amazon.com/lambda/latest/dg/web-functions-endpoint-access-control.html#web-functions-endpoint-access-control-signing) for `MultiRegion` endpoints only. Its region set must cover the region that serves the request. The set is read from `X-Amz-Region-Set` where the signature is: the header for a signed header, even when the header is not signed, and the query for a presigned query. It is split on commas without trimming, a set with an empty entry is refused, and each entry matches ignoring case, with `*` as the only wildcard. So `*`, `us-*` and `us-east-1,us-west-2` all cover `us-west-2`. SigV4a from temporary credentials is refused, as on AWS, while SigV4 takes them. On every endpoint, an `Authorization` that is an AWS signature is removed before the request reaches the app, together with the `X-Amz-Security-Token` header. Other `Authorization` values pass. A presigned query reaches the app unchanged, as on AWS. diff --git a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlane.java b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlane.java index 7a0443333d..0f8d3411e5 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlane.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlane.java @@ -590,7 +590,9 @@ public class LambdaWebDataPlane { headers.set("Connection", "close"); } + /** The platform writes its own {@code Date} in place of the app's, but lets a {@code date} in lowercase pass. */ static void copyResponseHeaders(MultiMap headers, HttpServerResponse out) { + out.headers().add("Date", LambdaWebErrorPages.platformDate(Instant.now())); Set dropped = new HashSet<>(DROPPED_RESPONSE_HEADERS); for (String connection : headers.getAll("Connection")) { for (String token : connection.split(",")) { @@ -598,7 +600,7 @@ public class LambdaWebDataPlane { } } for (Map.Entry header : headers) { - if (!dropped.contains(header.getKey().toLowerCase(Locale.ROOT))) { + if (!"Date".equals(header.getKey()) && !dropped.contains(header.getKey().toLowerCase(Locale.ROOT))) { out.headers().add(header.getKey(), header.getValue()); } } diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java index c50b1afc93..b7eea67d40 100644 --- a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebDataPlaneDockerIntegrationTest.java @@ -79,6 +79,7 @@ class LambdaWebDataPlaneDockerIntegrationTest { const http = require('node:http'); const [host, port] = process.env.AWS_LAMBDA_HTTP_ENDPOINT.split(':'); console.log('starting'); + const STALE_DATE = 'Thu, 01 Jan 2015 00:00:00 GMT'; if (process.env.MODE === 'throw') { throw new Error('init boom'); } if (process.env.MODE === 'exit') { process.exit(1); } if (process.env.MODE === 'missing') { require('no-such-module'); } @@ -117,6 +118,9 @@ class LambdaWebDataPlaneDockerIntegrationTest { }, (e) => { res.statusCode = 500; res.end(String(e)); }); return; } + if (req.url === '/date/none') { res.sendDate = false; res.end('none'); return; } + if (req.url === '/date/stale') { res.setHeader('Date', STALE_DATE); res.end('stale'); return; } + if (req.url === '/date/lower') { res.setHeader('date', STALE_DATE); res.end('lower'); return; } if (req.url.startsWith('/crash')) { setTimeout(() => process.exit(1), 50); return; } if (req.url.startsWith('/exit')) { res.end('bye', () => process.exit(0)); return; } let body = ''; @@ -236,6 +240,23 @@ class LambdaWebDataPlaneDockerIntegrationTest { .body("headers.authorization", nullValue()) .body("headers.x-amz-security-token", nullValue()) .body("headers.x-amz-date", equalTo("20260101T000000Z")); + + // The platform writes its own Date in place of the app's, as on AWS. A lowercase date passes beside it. + String own = "Date: [A-Z][a-z]{2}, \\d{2} [A-Z][a-z]{2} \\d{4} \\d{2}:\\d{2}:\\d{2} GMT"; + String stale = "Thu, 01 Jan 2015 00:00:00 GMT"; + + List none = dateLines(domain, "/date/none"); + assertEquals(1, none.size(), none.toString()); + assertTrue(none.getFirst().matches(own), none.toString()); + + List replaced = dateLines(domain, "/date/stale"); + assertEquals(1, replaced.size(), replaced.toString()); + assertTrue(replaced.getFirst().matches(own) && !replaced.getFirst().endsWith(stale), replaced.toString()); + + List lowercase = dateLines(domain, "/date/lower"); + assertEquals(2, lowercase.size(), lowercase.toString()); + assertTrue(lowercase.getFirst().matches(own), lowercase.toString()); + assertEquals("date: " + stale, lowercase.get(1)); } @Test @@ -901,6 +922,13 @@ class LambdaWebDataPlaneDockerIntegrationTest { } } + /** The date header lines of the answer to {@code path}, in their case and order. */ + private static List dateLines(String domain, String path) throws Exception { + String response = raw("GET " + path + " HTTP/1.1\r\nHost: " + domain + "\r\nConnection: close\r\n\r\n"); + String head = response.substring(0, response.indexOf("\r\n\r\n")); + return List.of(head.split("\r\n")).stream().filter(line -> line.regionMatches(true, 0, "date:", 0, 5)).toList(); + } + private static byte[] zip(String indexJs) throws IOException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (ZipOutputStream zip = new ZipOutputStream(bytes)) {