Skip to content

fix(functions): keep the raw request body and leave req.body undefined, as deployed functions do - #328

Merged
chhavi-mandowara-cstk merged 2 commits into
v2-devfrom
CL-8970
Dec 4, 2026
Merged

chhavi-mandowara-cstk merged 2 commits into
v2-devfrom
CL-8970

Conversation

@chhavi-mandowara-cstk

@chhavi-mandowara-cstk chhavi-mandowara-cstk commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What was wrong

Deployed AWS functions (lambda-node-bridge/cf-sv-with-helpers.ts) run the function on a real Node http.Server request that still holds the body. req.body is a separate lazy getter parsed from the same bytes:

Body req.body on AWS
no content-type undefined
application/json JSON.parse (400 if it fails)
application/octet-stream a Buffer
application/x-www-form-urlencoded querystring.parse
text/plain the string
any other type undefined

launch:functions:serve used Express's json, urlencoded, text and raw parsers. They read the request stream before the function ran, which caused three problems:

  1. The raw body was lost. A function reading the stream (for await, 'data'/'end', pipe) got nothing, which breaks webhook signature checks.
  2. A function that waits for 'end' on a text/plain body hung. The parser had already consumed the stream and 'end' had fired. Deployed, that is a 504.
  3. req.body was {} when there is no body or the type is unknown.

This is CL-8970 gap 8.

The change (src/functions/cloud-functions.ts)

The four Express parsers are replaced by one reader, readBodyLikeDeployedFunctions:

  • A request with no body is not touched (no transfer-encoding, and no or zero content-length). Its req.body is undefined.
  • Otherwise the body is read once, up to the 6 MB cap. The bytes are then put back into the request stream with request.unshift() before 'end' is emitted, so the function can read the raw stream and req.body.
  • req.body is set by the bridge's table above. Types are matched with Express's request.is(...). An empty body is undefined for every type, so an empty JSON body is not a 400.
  • An empty body that is already complete is not read to the end. The reader waits one setImmediate for the body to arrive. A request that is then complete with nothing buffered goes to the function unread, so 'end' is still pending for a function that waits for it.
  • A body the function leaves unread is drained once its response finishes, as Node's own _dump would (only when nothing is reading it). Reading the body marks the request as consumed, which stops Node from doing that itself, so 'end' and 'close' never fired.
  • next() runs on the next tick. A handler that pipes the request into its response and returns still starts flowing before serve ends the response (gap 4), as when the handler ran straight from the request event.
  • Kept from gap 1: a malformed JSON body is a bare 400, and a body over the cap a bare 413, both logged as Request rejected before the function ran: …. After a 413 the rest of the upload is drained, so a keep-alive connection answers its next request. A client that drops the connection mid-body is logged as a 400 request.aborted, as body-parser logged it, and the function does not run.
  • AGENTS.md. The approved V1 differences describe the raw stream and the undefined body, and what Azure and GCP (V1) do.
  • .talismanrc. Refreshed checksums for cloud-functions.test.ts and AGENTS.md (test code and prose, no credentials).

Tests

src/functions/cloud-functions.test.ts, new or extended:

  • hands the handler an undefined body for … with no body: a GET, an empty JSON POST and an empty chunked JSON POST.
  • hands the handler an undefined body for a content type deployed functions do not parse (application/xml).
  • keeps a {application/json,text/plain} body in the request stream as well as in request.body.
  • lets a handler that waits for the end event read a text/plain body instead of hanging.
  • answers the next request on a keep-alive connection when the handler leaves the body unread.
  • answers the next request on a keep-alive connection after refusing a body over the payload limit.
  • keeps serving after a client drops its connection in the middle of sending a body.
  • hands the handler an undefined body for an empty chunked JSON body whose end arrives after its headers.
  • lets a handler that waits for the end event answer an empty chunked body of a type serve does not parse.
  • ends and closes a request whose large body the handler leaves unread once its response is sent on a keep-alive connection, as Node does.
  • The existing form test's urlencoded case now sends tags=a&tags=b (it already did after gap 6). The gap 1 400/413 tests are unchanged and pass.

Proof:

  • Against the code before this change, 7 of the new tests fail. The text/plain one times out: that is the hang.
  • Each line the new tests don't fail on the old code for is proven by a mutant:
    • removing unshift fails 4 tests;
    • calling next() directly fails the echo test;
    • dropping the empty-body check fails the chunked-JSON case;
    • dropping resume() after a 413 fails the keep-alive-after-413 test;
    • dropping the 'error' listener fails the disconnect test;
    • dropping the drain on response finish fails the unread-body test;
    • dropping the empty-body guard in parseBody fails the late-terminator chunked JSON test.
  • Coverage: no new uncovered line.

Full suite in the pre-push hook: 189 suites, 3844 tests passed. Every hook ran (Snyk included), with no --no-verify. The content-type guard (test/integration/transport-content-type.test.ts) passes: types are matched with request.is, so the file has no application/json literal.

Review fixes (55c8c92)

This commit fixes Dhruv's three Low findings:

  • A client hang-up was logged as a 500. It is now a 400 request.aborted.
  • An empty chunked body hung a function waiting for 'end'. It now goes to the function unread.
  • An unread body kept the request open. It is now drained when the response finishes, which also covers the 400 bad-JSON path.

Each fix has a test that fails without it.

E2E (http-sanity cloud-functions checks, AWS expectations)

Against launch:functions:serve, with the 30 checks from e2e PR contentstack/contentfly-e2e-tests#502:

  • Before this change (gap 7, b44f470): 25/30:
    • a GET with no body hands req.body as undefined gets {};
    • the raw request stream still holds the body gets 0 bytes;
    • the new a handler that waits for the end event can read a text/plain body times out;
    • the new a body of a type deployed functions do not parse hands req.body as undefined gets {}.
  • With this change: 29/30. All four pass, and no check got worse. The one still failing is gap 10 (require, __dirname).

The gap 8 e2e PR, contentstack/contentfly-e2e-tests#502, adds the two new checks and a raw-body-events.js site function.

Not changed

  • A malformed JSON body is still refused before the function runs (gap 1). AWS answers it only when the function reads req.body.
  • The function now starts once the whole body has arrived, as on Lambda, so a local upload can't be streamed while it is still coming in.
  • Bodies are decoded as UTF-8 and Content-Encoding: gzip is not inflated, as on AWS. Express honoured charset and inflated gzip.
  • An urlencoded body with more than 1000 fields keeps the first 1000, as AWS's querystring.parse does. body-parser answered 413.

Jira: https://contentstack.atlassian.net/browse/CL-8970


Generated by Claude Code

…d, as deployed functions do

Deployed AWS functions (lambda-node-bridge) hand the function a real
request stream that still holds the body, and a req.body parsed from the
same bytes: undefined with no content type, JSON, a Buffer for
octet-stream, querystring for urlencoded, the string for text/plain, and
undefined for any other type. Serve used Express's body parsers, which
read the stream, so a function reading it (a webhook signature check)
got nothing, one waiting for 'end' on a text/plain body hung, and
req.body was {} with no body or an unknown type.

Serve now reads the body once, up to the 6mb cap, and puts the bytes back
into the request stream with unshift before 'end' is emitted, then sets
req.body by the bridge's rules. A request with no body is not touched.
next() runs on the next tick, so a handler that pipes the request and
returns still starts flowing before serve ends the response, as before.

Kept from gap 1: a malformed JSON body is a bare 400, and a body over
the cap a bare 413; the rest of an oversized body is drained so a
keep-alive connection answers its next request. A client that drops the
connection mid-body is answered as a request error without the function
running.

AGENTS.md describes the change, and the .talismanrc checksums of the two
touched files are refreshed.
@chhavi-mandowara-cstk
chhavi-mandowara-cstk requested a review from a team as a code owner December 4, 2026 11:49
@snyk-io

snyk-io Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues
✅ Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

Harshi-Shah-CS
Harshi-Shah-CS previously approved these changes Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

🔒 Security Scan Results

ℹ️ Note: Only vulnerabilities with available fixes (upgrades or patches) are counted toward thresholds.

Check Type Count (with fixes) Without fixes Threshold Result
🔴 Critical Severity 0 0 10 ✅ Passed
🟠 High Severity 0 2 25 ✅ Passed
🟡 Medium Severity 0 10 500 ✅ Passed
🔵 Low Severity 0 0 1000 ✅ Passed

⏱️ SLA Breach Summary

✅ No SLA breaches detected. All vulnerabilities are within acceptable time thresholds.

Severity Breaches (with fixes) Breaches (no fixes) SLA Threshold (with/no fixes) Status
🔴 Critical 0 0 15 / 30 days ✅ Passed
🟠 High 0 0 30 / 120 days ✅ Passed
🟡 Medium 0 0 90 / 365 days ✅ Passed
🔵 Low 0 0 180 / 365 days ✅ Passed

ℹ️ Vulnerabilities Without Available Fixes (Informational Only)

The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:

  • Critical without fixes: 0
  • High without fixes: 2
  • Medium without fixes: 10
  • Low without fixes: 0

✅ BUILD PASSED - All security checks passed

SakshiKoli-CS
SakshiKoli-CS previously approved these changes Dec 4, 2026
dhruv-parekh-cs
dhruv-parekh-cs previously approved these changes Dec 4, 2026

@dhruv-parekh-cs dhruv-parekh-cs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the whole PR · pass 1/3: 0 high, 0 medium, 3 low (inline).

Checked the body handling against lambda-node-bridge/cf-sv-with-helpers.ts: plain JSON.parse, no charset/BOM or Content-Encoding handling and undefined for other types all match the bridge, so those differences from body-parser are not raised.

Cross-repo (CL-8970, with contentstack/contentfly-e2e-tests#502): no issues. The e2e expectations (text/plain readable from 'end', application/xml → req.body undefined) match what this PR serves; #502's serve run (29/30) needs this PR merged first.

— 🤖 launch-pr-review-agent (Claude)


Generated by Claude Code

Comment thread src/functions/cloud-functions.ts Resolved
Comment thread src/functions/cloud-functions.ts Resolved
Comment thread src/functions/cloud-functions.ts Resolved
…a hang-up as 400

Three review findings on the body handling.

A client that hangs up mid-body is now rejected as a 400
request.aborted, as body-parser logged it, instead of a 500 server
error.

An empty chunked body no longer hangs a function that waits for the
request's 'end' event. Serve waits one turn for the body to arrive and,
when the request is already complete with nothing buffered, hands it to
the function without reading it to the end, so 'end' still fires for the
function.

A body the function leaves unread is drained once its response finishes,
as Node's own dump would, so the request still ends and closes. Reading
the body marks the request as consumed, which stopped Node from doing it.
This also covers the 400 bad-JSON path.

AGENTS.md describes the change, and the .talismanrc checksums of the two
touched files are refreshed.
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

🔒 Security Scan Results

ℹ️ Note: Only vulnerabilities with available fixes (upgrades or patches) are counted toward thresholds.

Check Type Count (with fixes) Without fixes Threshold Result
🔴 Critical Severity 0 0 10 ✅ Passed
🟠 High Severity 0 2 25 ✅ Passed
🟡 Medium Severity 0 10 500 ✅ Passed
🔵 Low Severity 0 0 1000 ✅ Passed

⏱️ SLA Breach Summary

✅ No SLA breaches detected. All vulnerabilities are within acceptable time thresholds.

Severity Breaches (with fixes) Breaches (no fixes) SLA Threshold (with/no fixes) Status
🔴 Critical 0 0 15 / 30 days ✅ Passed
🟠 High 0 0 30 / 120 days ✅ Passed
🟡 Medium 0 0 90 / 365 days ✅ Passed
🔵 Low 0 0 180 / 365 days ✅ Passed

ℹ️ Vulnerabilities Without Available Fixes (Informational Only)

The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:

  • Critical without fixes: 0
  • High without fixes: 2
  • Medium without fixes: 10
  • Low without fixes: 0

✅ BUILD PASSED - All security checks passed

1 similar comment
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

🔒 Security Scan Results

ℹ️ Note: Only vulnerabilities with available fixes (upgrades or patches) are counted toward thresholds.

Check Type Count (with fixes) Without fixes Threshold Result
🔴 Critical Severity 0 0 10 ✅ Passed
🟠 High Severity 0 2 25 ✅ Passed
🟡 Medium Severity 0 10 500 ✅ Passed
🔵 Low Severity 0 0 1000 ✅ Passed

⏱️ SLA Breach Summary

✅ No SLA breaches detected. All vulnerabilities are within acceptable time thresholds.

Severity Breaches (with fixes) Breaches (no fixes) SLA Threshold (with/no fixes) Status
🔴 Critical 0 0 15 / 30 days ✅ Passed
🟠 High 0 0 30 / 120 days ✅ Passed
🟡 Medium 0 0 90 / 365 days ✅ Passed
🔵 Low 0 0 180 / 365 days ✅ Passed

ℹ️ Vulnerabilities Without Available Fixes (Informational Only)

The following vulnerabilities were detected but do not have fixes available (no upgrade or patch). These are excluded from failure thresholds:

  • Critical without fixes: 0
  • High without fixes: 2
  • Medium without fixes: 10
  • Low without fixes: 0

✅ BUILD PASSED - All security checks passed

@chhavi-mandowara-cstk
chhavi-mandowara-cstk merged commit 5eb03c8 into v2-dev Dec 4, 2026
12 checks passed
@chhavi-mandowara-cstk
chhavi-mandowara-cstk deleted the CL-8970 branch December 4, 2026 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants