diff --git a/.talismanrc b/.talismanrc index b55b39e..d525e3b 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: 86abf6589263684bab1a2e418764916059b59e95ac5e465c3820ecf8ac82be86 + checksum: 17042b1f06e9955cf1159c8ac96f6ed300083c77a8d5fe48db64d3880d3e7fa2 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: 626305f8439f9b04652c287465e08ef681e9ae1eb3547313d5a8bbb5ff70a342 + checksum: a099f552d4c8ac79678ff2b2fa83e82e29d083ea0d267e601e269cbd5507acc6 - filename: src/functions/function.errors.ts checksum: cad477cf24c97bf3395427235ca1de0f305369d05d61c21a83435654df2269e3 - filename: src/transport/proxy.test.ts diff --git a/AGENTS.md b/AGENTS.md index 9be8fe8..2f1e195 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -537,7 +537,9 @@ The approved differences from V1 are these, and only these: Buffer, all up to Lambda's 6mb cap. `request.cookies` is the parsed `Cookie` header, or `{}`. `request.query` also holds the path parameters, which win over a query value of the same name (`/items/abc?id=zzz` → `{ id: 'abc' }`), as on AWS (CL-8970); Azure and GCP (V1) keep them only - in `request.params`. + 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' } }`). 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 d980f51..4843d32 100644 --- a/src/functions/cloud-functions.test.ts +++ b/src/functions/cloud-functions.test.ts @@ -334,6 +334,16 @@ describe('CloudFunctions serve routing', () => { expect(JSON.parse(response.body)).toEqual({ id: 'abc' }); }); + it('keeps the query string flat, as deployed functions do', async () => { + writeFunctionFile('query.js', 'export default function query(request, response) { response.json(request.query); }'); + const port = await freePort(); + + await new CloudFunctions(workspace).serve(port); + const response = await sendRequest(port, '/query?a[b]=1&list=1&list=2'); + + expect(JSON.parse(response.body)).toEqual({ 'a[b]': '1', list: ['1', '2'] }); + }); + it('answers a post request on an exact route', async () => { writeFunctionFile('echo.js', 'export default function echo(request, response) { response.json({ method: request.method }); }'); const port = await freePort(); @@ -366,7 +376,7 @@ describe('CloudFunctions serve routing', () => { it.each([ ['application/json', '{"name":"launch","tags":["a","b"]}'], - ['application/x-www-form-urlencoded', 'name=launch&tags[]=a&tags[]=b'], + ['application/x-www-form-urlencoded', 'name=launch&tags=a&tags=b'], ])('hands a %s body to the handler as a parsed object', async (contentType, body) => { writeFunctionFile('body.js', 'export default function body(request, response) { response.json({ body: request.body }); }'); const port = await freePort(); @@ -377,6 +387,20 @@ describe('CloudFunctions serve routing', () => { expect(JSON.parse(response.body)).toEqual({ body: { name: 'launch', tags: ['a', 'b'] } }); }); + it('keeps an urlencoded form flat, as deployed functions do', async () => { + writeFunctionFile('body.js', 'export default function body(request, response) { response.json({ body: request.body }); }'); + const port = await freePort(); + + await new CloudFunctions(workspace).serve(port); + const response = await sendRequest(port, '/body', { + method: 'POST', + headers: { 'content-type': 'application/x-www-form-urlencoded' }, + body: 'a[b]=1&c=2&list=1&list=2', + }); + + expect(JSON.parse(response.body)).toEqual({ body: { 'a[b]': '1', c: '2', list: ['1', '2'] } }); + }); + it.each(['text/plain', 'application/octet-stream'])( 'hands a %s body over the 100kb parser default to the handler, as Lambda allows', async (contentType) => { diff --git a/src/functions/cloud-functions.ts b/src/functions/cloud-functions.ts index f2d28ad..b942c78 100755 --- a/src/functions/cloud-functions.ts +++ b/src/functions/cloud-functions.ts @@ -401,7 +401,7 @@ function applyRequestMiddleware(app: Express): void { app.use(redirectLikeDeployedFunctions); app.use(sendLikeDeployedFunctions); app.use(express.json({ limit: DEPLOYED_PAYLOAD_LIMIT })); - app.use(express.urlencoded({ extended: true, limit: DEPLOYED_PAYLOAD_LIMIT })); + app.use(express.urlencoded({ extended: false, limit: DEPLOYED_PAYLOAD_LIMIT })); app.use(express.text({ limit: DEPLOYED_PAYLOAD_LIMIT })); app.use(express.raw({ limit: DEPLOYED_PAYLOAD_LIMIT })); } @@ -456,6 +456,7 @@ export class CloudFunctions { const app = express(); app.disable('x-powered-by'); + app.set('query parser', 'simple'); applyRequestMiddleware(app); this.applyAppRouter(exactRouteResources, app); this.applyAppRouter(dynamicRouteResources, app);