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
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-11-01 22:03:46 -04:00
1 parent 65e9522433
commit b7697f8148
3 files changed
+32 -2

No files matched your search

+1 -1
View File
@@ -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/<key>` 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.
@@ -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<String> 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<String, String> 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());
}
}
@@ -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<String> none = dateLines(domain, "/date/none");
assertEquals(1, none.size(), none.toString());
assertTrue(none.getFirst().matches(own), none.toString());
List<String> replaced = dateLines(domain, "/date/stale");
assertEquals(1, replaced.size(), replaced.toString());
assertTrue(replaced.getFirst().matches(own) && !replaced.getFirst().endsWith(stale), replaced.toString());
List<String> 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<String> 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)) {