From f1c7098d9c71ce8d06d09aff888bc5fd606a4a4c 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 20:47:05 +0900 Subject: [PATCH] feat(lambda-web): add a text/plain Content-Type to app responses that send none (#7285) --- docs/services/lambda-web-functions.md | 2 +- .../lambdaweb/LambdaWebDataPlane.java | 14 ++++++++--- ...mbdaWebDataPlaneDockerIntegrationTest.java | 25 ++++++++++++++----- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/docs/services/lambda-web-functions.md b/docs/services/lambda-web-functions.md index 24f1604e3a..f02d0075cf 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. 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. +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. A response without a `Content-Type` gets `text/plain; charset=utf-8`, even on a 204 or a `HEAD`, unless its `Content-Length` is 0. An app's `content-type` goes out as `Content-Type`. 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 0f8d3411e5..6898a0c8dd 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,11 @@ 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. */ + /** + * The platform writes its own {@code Date} in place of the app's, but lets a {@code date} in lowercase pass. It + * writes {@code text/plain} for an app that sends no {@code Content-Type}, even on a 204 or a HEAD, but not with a + * {@code Content-Length} of 0. + */ static void copyResponseHeaders(MultiMap headers, HttpServerResponse out) { out.headers().add("Date", LambdaWebErrorPages.platformDate(Instant.now())); Set dropped = new HashSet<>(DROPPED_RESPONSE_HEADERS); @@ -600,10 +604,14 @@ public class LambdaWebDataPlane { } } for (Map.Entry header : headers) { - if (!"Date".equals(header.getKey()) && !dropped.contains(header.getKey().toLowerCase(Locale.ROOT))) { - out.headers().add(header.getKey(), header.getValue()); + String name = header.getKey(); + if (!"Date".equals(name) && !dropped.contains(name.toLowerCase(Locale.ROOT))) { + out.headers().add("Content-Type".equalsIgnoreCase(name) ? "Content-Type" : name, header.getValue()); } } + if (!headers.contains("Content-Type") && !"0".equals(headers.get("Content-Length"))) { + out.headers().add("Content-Type", "text/plain; charset=utf-8"); + } } static int port(String host, String scheme) { 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 b7eea67d40..ba5ee76896 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 @@ -121,6 +121,9 @@ class LambdaWebDataPlaneDockerIntegrationTest { 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 === '/type/none') { res.end('none'); return; } + if (req.url === '/type/empty') { res.end(); return; } + if (req.url === '/type/lower') { res.setHeader('content-type', 'application/x-custom'); 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 = ''; @@ -245,18 +248,26 @@ class LambdaWebDataPlaneDockerIntegrationTest { 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"); + List none = headerLines(domain, "/date/none", "date"); assertEquals(1, none.size(), none.toString()); assertTrue(none.getFirst().matches(own), none.toString()); - List replaced = dateLines(domain, "/date/stale"); + List replaced = headerLines(domain, "/date/stale", "date"); assertEquals(1, replaced.size(), replaced.toString()); assertTrue(replaced.getFirst().matches(own) && !replaced.getFirst().endsWith(stale), replaced.toString()); - List lowercase = dateLines(domain, "/date/lower"); + List lowercase = headerLines(domain, "/date/lower", "date"); assertEquals(2, lowercase.size(), lowercase.toString()); assertTrue(lowercase.getFirst().matches(own), lowercase.toString()); assertEquals("date: " + stale, lowercase.get(1)); + + // An answer without a Content-Type gets text/plain unless its Content-Length is 0, as on AWS. An app's + // content-type goes out as Content-Type. + String plain = "Content-Type: text/plain; charset=utf-8"; + assertEquals(List.of(plain), headerLines(domain, "/type/none", "content-type")); + assertEquals(List.of(plain), headerLines(domain, "/status/204", "content-type")); + assertEquals(List.of(), headerLines(domain, "/type/empty", "content-type")); + assertEquals(List.of("Content-Type: application/x-custom"), headerLines(domain, "/type/lower", "content-type")); } @Test @@ -922,11 +933,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 { + /** The {@code name} header lines of the answer to {@code path}, in their case and order. */ + private static List headerLines(String domain, String path, String name) 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(); + String prefix = name + ":"; + return List.of(head.split("\r\n")).stream() + .filter(line -> line.regionMatches(true, 0, prefix, 0, prefix.length())).toList(); } private static byte[] zip(String indexJs) throws IOException {