From b44f4703e75bfcf5760dc067bbf2133c3518dcef Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 4 Dec 2026 10:19:29 +0000 Subject: [PATCH] fix(functions): route exactly and answer an empty 404, as deployed functions do Deployed, nginx sends a request to a cloud function only when its path matches the function's route exactly: an exact-path entry keyed by the URI, or for a [param] route a Lua pattern that must match the whole path. Serve used Express's defaults, so /Hello ran hello.js, /hello/ did too, and a path no function claims got Express's "Cannot GET" HTML page. Serve now turns on Express's case sensitive routing and strict routing, and answers a request no route matched with an empty 404, as the lambda-node-bridge does. Error responses (a malformed body) still go to answerRequestError, since answerNotFound is not an error handler. AGENTS.md records the change, and the .talismanrc checksums of the two touched files are refreshed. --- .talismanrc | 4 ++-- AGENTS.md | 6 +++++- src/functions/cloud-functions.test.ts | 28 +++++++++++++++++++++++++-- src/functions/cloud-functions.ts | 7 +++++++ 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/.talismanrc b/.talismanrc index d525e3b..68a2edd 100644 --- a/.talismanrc +++ b/.talismanrc @@ -239,7 +239,7 @@ fileignoreconfig: - "a project name passed on argv to the project resolver" - "expect\\(Object.keys\\(DEPENDENCIES\\)\\).toEqual\\(\\['project', 'env'\\]\\)" - filename: AGENTS.md - checksum: 17042b1f06e9955cf1159c8ac96f6ed300083c77a8d5fe48db64d3880d3e7fa2 + checksum: 990c5f77386440b38417ad3277d8cf6afdff910a12d21884e8773b449f8f0ab7 allowed_patterns: - "Do not add an exemption to a guard to make a new file pass" - "passes its own error wording to `client.request\\(req, MESSAGES\\)`" @@ -286,7 +286,7 @@ fileignoreconfig: - filename: test/integration/projects-list-command.test.ts checksum: 6709766943430b80aad2d7200102d936a92321e86c41f66d6a60a5baf82a81bf - filename: src/functions/cloud-functions.test.ts - checksum: a099f552d4c8ac79678ff2b2fa83e82e29d083ea0d267e601e269cbd5507acc6 + checksum: b4ea493e2e58fae9bdd9a7d890eb6bfb0a66eaa20c3afbd2dea989e82cdb56a5 - filename: src/functions/function.errors.ts checksum: cad477cf24c97bf3395427235ca1de0f305369d05d61c21a83435654df2269e3 - filename: src/transport/proxy.test.ts diff --git a/AGENTS.md b/AGENTS.md index 2f1e195..11af16a 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -539,7 +539,11 @@ The approved differences from V1 are these, and only these: (`/items/abc?id=zzz` → `{ id: 'abc' }`), as on AWS (CL-8970); Azure and GCP (V1) keep them only in `request.params`. The query string and an urlencoded body are parsed flat with Node's `querystring`, as on AWS (CL-8970): `a[b]=1` stays the key `'a[b]'` and a repeated key becomes an - array; Azure and GCP (V1) nest them (`{ a: { b: '1' } }`). + array; Azure and GCP (V1) nest them (`{ a: { b: '1' } }`). Routes match exactly, as nginx + routes a deployed function (CL-8970): case-sensitive (`/Hello` does not run `hello.js`) and with + no trailing slash (`/hello/` does not either), and a path no function claims gets an empty 404 + instead of Express's HTML page. Deployed, such a request never reaches the function and nginx + answers it with its own 404 page. The project `.env` is loaded before the functions are bundled. Azure and GCP still run the vendored V1 server, which leaves text and octet-stream bodies unparsed; locally a handler that reads the raw request stream for those two types gets an empty stream. diff --git a/src/functions/cloud-functions.test.ts b/src/functions/cloud-functions.test.ts index 4843d32..15fd712 100644 --- a/src/functions/cloud-functions.test.ts +++ b/src/functions/cloud-functions.test.ts @@ -603,13 +603,37 @@ describe('CloudFunctions serve routing', () => { ]); }); - it('answers 404 for a path no function claims', async () => { + it('answers an empty 404 for a path no function claims, as deployed functions do', async () => { writeFunctionFile('hello.js', 'export default function hello(request, response) { response.send("hi"); }'); const port = await freePort(); await new CloudFunctions(workspace).serve(port); + const response = await sendRequest(port, '/absent'); - expect((await sendRequest(port, '/absent')).status).toBe(404); + expect(response.status).toBe(404); + expect(response.body).toBe(''); + }); + + it('matches a route only in its exact case, as deployed functions do', async () => { + writeFunctionFile('hello.js', 'export default function hello(request, response) { response.send("hi"); }'); + const port = await freePort(); + + await new CloudFunctions(workspace).serve(port); + + expect((await sendRequest(port, '/Hello')).status).toBe(404); + expect((await sendRequest(port, '/hello')).status).toBe(200); + }); + + it('does not match a route with a trailing slash, as deployed functions do', async () => { + writeFunctionFile('hello.js', 'export default function hello(request, response) { response.send("hi"); }'); + writeFunctionFile(join('items', '[id].js'), 'export default function item(request, response) { response.send(request.params.id); }'); + const port = await freePort(); + + await new CloudFunctions(workspace).serve(port); + + expect((await sendRequest(port, '/hello/')).status).toBe(404); + expect((await sendRequest(port, '/items/42/')).status).toBe(404); + expect((await sendRequest(port, '/items/42')).status).toBe(200); }); it('defaults cache-control to no-store and keeps a header the handler set', async () => { diff --git a/src/functions/cloud-functions.ts b/src/functions/cloud-functions.ts index b942c78..54b887e 100755 --- a/src/functions/cloud-functions.ts +++ b/src/functions/cloud-functions.ts @@ -380,6 +380,10 @@ function endLikeDeployedFunctions(response: Response, answering: Answering): voi } } +export function answerNotFound(_request: Request, response: Response): void { + response.status(404).end(); +} + export function answerRequestError( error: Error & { status?: number; type?: string }, _request: Request, @@ -457,9 +461,12 @@ export class CloudFunctions { const app = express(); app.disable('x-powered-by'); app.set('query parser', 'simple'); + app.set('case sensitive routing', true); + app.set('strict routing', true); applyRequestMiddleware(app); this.applyAppRouter(exactRouteResources, app); this.applyAppRouter(dynamicRouteResources, app); + app.use(answerNotFound); app.use(answerRequestError); return this.startServer(app, servingPort, [