Skip to content

Commit b5adb51

Browse files
committed
fix(mcp): annotate headers double-cast and cancel redirect body before throw paths
- Add the missing double-cast-allowed annotation on the sanitized-headers cast (strict boundary audit). - Cancel the redirect response body before the hop-cap / blocked-target throws so those paths can't leave a socket checked out on the long-lived Agent.
1 parent b8c5552 commit b5adb51

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

apps/sim/lib/core/security/input-validation.server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,14 @@ export async function followRedirectsGuarded(
506506
const status = response.status
507507
const location = response.headers.get('location')
508508
if (![301, 302, 303, 307, 308].includes(status) || !location) return response
509+
// Cancel the redirect body up front so the throw paths below (hop cap, blocked
510+
// target) can't leave a socket checked out on the long-lived Agent.
511+
await response.body?.cancel().catch(() => {})
509512
if (hop >= MAX_GUARDED_REDIRECTS) {
510513
throw new Error(`Blocked by SSRF policy: more than ${MAX_GUARDED_REDIRECTS} redirects`)
511514
}
512515
const nextUrl = new URL(location, currentUrl)
513516
assertGuardedRedirectTarget(nextUrl)
514-
await response.body?.cancel().catch(() => {})
515517
// Per the fetch spec: 303 (and 301/302 on POST) switch to a bodyless GET, dropping
516518
// the entity headers that described the removed body (a retained Content-Length /
517519
// Content-Type on a bodyless GET is malformed and undici rejects it).
@@ -524,6 +526,7 @@ export async function followRedirectsGuarded(
524526
sanitized.delete('content-type')
525527
sanitized.delete('content-encoding')
526528
sanitized.delete('transfer-encoding')
529+
// double-cast-allowed: Headers is a valid undici HeadersInit at runtime but the DOM/undici types differ
527530
headers = sanitized as unknown as UndiciRequestInit['headers']
528531
}
529532
}

0 commit comments

Comments
 (0)