Skip to content

Commit e84b0c3

Browse files
committed
refactor(webhooks): drop redundant null-body branch in capped readers
Both capped body readers had an `if (!stream)` fallback to an uncapped `.text()`/empty string. `readStreamToBufferWithLimit` already returns an empty buffer for a null stream, so the branch is redundant and the `.text()` fallback was a theoretical bypass (chunked request, no content-length, null body). Collapse both to a single capped read.
1 parent c40c6d7 commit e84b0c3

2 files changed

Lines changed: 12 additions & 17 deletions

File tree

apps/sim/app/api/webhooks/agentmail/route.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ const AGENTMAIL_BODY_LABEL = 'AgentMail webhook body'
4343
*/
4444
async function readAgentMailBody(req: Request): Promise<string> {
4545
assertContentLengthWithinLimit(req.headers, WEBHOOK_MAX_BODY_BYTES, AGENTMAIL_BODY_LABEL)
46-
const stream = req.body
47-
if (!stream) {
48-
return req.text()
49-
}
50-
const buffer = await readStreamToBufferWithLimit(stream, {
46+
// `readStreamToBufferWithLimit` returns an empty buffer for a null body, so a
47+
// single capped read covers the empty-body case without an uncapped `.text()`
48+
// fallback that could bypass the limit.
49+
const buffer = await readStreamToBufferWithLimit(req.body, {
5150
maxBytes: WEBHOOK_MAX_BODY_BYTES,
5251
label: AGENTMAIL_BODY_LABEL,
5352
})

apps/sim/lib/webhooks/processor.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,14 @@ export async function parseWebhookBody(
8787
try {
8888
assertContentLengthWithinLimit(request.headers, WEBHOOK_MAX_BODY_BYTES, WEBHOOK_BODY_LABEL)
8989

90-
const stream = request.clone().body
91-
if (stream) {
92-
const buffer = await readStreamToBufferWithLimit(stream, {
93-
maxBytes: WEBHOOK_MAX_BODY_BYTES,
94-
label: WEBHOOK_BODY_LABEL,
95-
})
96-
rawBody = new TextDecoder().decode(buffer)
97-
} else {
98-
// A null body stream means the request carries no body, so the parsed
99-
// body is empty — no second clone needed.
100-
rawBody = ''
101-
}
90+
// `readStreamToBufferWithLimit` returns an empty buffer for a null body, so
91+
// this single capped read covers the empty-body case too — no branch or
92+
// redundant clone, and no uncapped `.text()` fallback to bypass.
93+
const buffer = await readStreamToBufferWithLimit(request.clone().body, {
94+
maxBytes: WEBHOOK_MAX_BODY_BYTES,
95+
label: WEBHOOK_BODY_LABEL,
96+
})
97+
rawBody = new TextDecoder().decode(buffer)
10298

10399
if (!rawBody || rawBody.length === 0) {
104100
return { body: {}, rawBody: '' }

0 commit comments

Comments
 (0)