Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(control-api): close body-read and non-JSON leak gaps in the registration client

Jose Ramirez43820b62b66a
diff --git a/control-api/src/memberRegistrationServiceClient.ts b/control-api/src/memberRegistrationServiceClient.ts
index fc2795096..2ef5d1215 100644
--- a/control-api/src/memberRegistrationServiceClient.ts
+++ b/control-api/src/memberRegistrationServiceClient.ts
@@ -100,14 +100,37 @@ export async function memberRegistrationServiceRequest<T>(
// by routes/external/invitations.ts:212, routes/admin/teams.ts:42 and
// routes/external/teams.ts:36 (spec §4.1).
if (response.status >= 500) {
+ // NOTE: this message must NOT contain the exact string "Member registration
+ // service" (capital M) — routes/admin/teams.ts:97,157 and
+ // routes/external/teams.ts:55 call sendInvitationServiceError(res, error)
+ // before next(error), which matches via message.includes('Member
+ // registration service'). If this 5xx message matched that string, a typed
+ // MemberRegistrationUnavailableError would be silently swallowed into a
+ // 502 invitation_service_unavailable instead of the intended 503.
throw unavailable(
{ ...context, upstreamStatus: response.status },
`member-registration service ${method} ${path} failed (${response.status})`
)
}
- const raw = await response.text()
+ // A stalled body (headers sent, body never completes) or a mid-body reset
+ // throws here (raw AbortError / TypeError: terminated) even though the
+ // fetch() above already resolved. Without this try/catch that throw escapes
+ // untyped and collapses to a generic 500 — the exact failure this file
+ // exists to prevent.
+ let raw: string
+ try {
+ raw = await response.text()
+ } catch (cause) {
+ throw unavailable(
+ { ...context, upstreamStatus: response.status },
+ 'member-registration service response could not be read',
+ cause
+ )
+ }
+
let parsed: unknown = null
+ let nonJsonBody = false
if (raw) {
try {
parsed = JSON.parse(raw) as unknown
@@ -119,13 +142,18 @@ export async function memberRegistrationServiceRequest<T>(
cause
)
}
+ nonJsonBody = true
parsed = null
}
}
if (!response.ok) {
- let errorMessage =
- parsed && typeof parsed === 'object' && 'error' in parsed
+ // A non-JSON 4xx body must never be interpolated verbatim: it's echoed to
+ // callers by routes/admin/teams.ts:44, so an intermediary's HTML error
+ // page (possibly containing internal hostnames) must not leak through.
+ let errorMessage = nonJsonBody
+ ? '<non-JSON response>'
+ : parsed && typeof parsed === 'object' && 'error' in parsed
? String((parsed as { error: unknown }).error)
: raw
if (parsed && typeof parsed === 'object' && 'message' in parsed) {
diff --git a/control-api/test/memberRegistrationClient.errors.test.ts b/control-api/test/memberRegistrationClient.errors.test.ts
index 4512667f2..ce6a25e8e 100644
--- a/control-api/test/memberRegistrationClient.errors.test.ts
+++ b/control-api/test/memberRegistrationClient.errors.test.ts
@@ -112,4 +112,30 @@ describe('member-registration client failure classification', () => {
expect(error.message).toContain('Member registration service')
expect(error.message).toContain('(403)')
})
+
+ it('a body read failure (stalled body / mid-body reset) becomes Unavailable, not a raw AbortError', async () => {
+ const response = new Response('{"ok":true}', { status: 200 })
+ vi.spyOn(response, 'text').mockRejectedValue(
+ new DOMException('The user aborted a request.', 'AbortError')
+ )
+ vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response))
+ await expect(send()).rejects.toBeInstanceOf(MemberRegistrationUnavailableError)
+ })
+
+ it('REGRESSION: a non-JSON 404 keeps the legacy plain Error but does not leak the raw body', async () => {
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn().mockResolvedValue(
+ new Response('<html><body>upstream-internal-host.local error</body></html>', {
+ status: 404,
+ headers: { 'content-type': 'text/html' },
+ })
+ )
+ )
+ const error = await caught()
+ expect(error).not.toBeInstanceOf(MemberRegistrationUnavailableError)
+ expect(error.message).toContain('Member registration service')
+ expect(error.message).toContain('(404)')
+ expect(error.message).not.toContain('upstream-internal-host.local')
+ })
})