From b2de0d4215bb9c4b89a88982e25f40f200c4ef43 Mon Sep 17 00:00:00 2001 From: paula-alves Date: Fri, 20 Nov 2026 04:18:20 +0000 Subject: [PATCH] fix(sports): swallow a network-level headshot fetch failure instead of leaking a bare TypeError (BIT-31961) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit saveHeadshot() awaited this.deps.fetch() with no try/catch. When the headshot URL was unreachable (DNS, connection reset, timeout), the bare undici TypeError: fetch failed propagated to the caller's .catch, which logged it via log.warn({err}). Pino's logHooks fingerprints any warn carrying an Error, and a bare undici fetch failure has no stack frame in our code, so it collapsed into the same ERRSIG (e4af6ce83729) as every other unwrapped fetch site in the api component — reopening BIT-31961 a third time even though the stripe and invoicexpress call sites (PR #256) were already wrapped and deployed. A headshot that can't be downloaded is not worth paging: catch the network failure inside saveHeadshot, log the reason as a plain string (no err object, so logHooks does not fingerprint it), and return false. The next sync retries it. This is the last bare fetch call site reachable from the api component. Paperclip-Issue: BIT-31961 Paperclip-ERRSIG: e4af6ce83729 --- apps/api/src/sports/service.test.ts | 30 ++++++++++++++++++++++++++--- apps/api/src/sports/service.ts | 13 ++++++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/apps/api/src/sports/service.test.ts b/apps/api/src/sports/service.test.ts index d04e273..6da8246 100644 --- a/apps/api/src/sports/service.test.ts +++ b/apps/api/src/sports/service.test.ts @@ -2,14 +2,14 @@ import { describe, expect, it } from "vitest"; import { SportsService } from "./service.js"; type Level = "info" | "warn"; -type LogCall = { level: Level; msg: string }; +type LogCall = { level: Level; msg: string; obj: unknown }; const makeLog = (): { info: (obj: unknown, msg: string) => void; warn: (obj: unknown, msg: string) => void; error: () => void; debug: () => void; calls: LogCall[] } => { const calls: LogCall[] = []; return { calls, - info: (_obj, msg) => calls.push({ level: "info", msg }), - warn: (_obj, msg) => calls.push({ level: "warn", msg }), + info: (obj, msg) => calls.push({ level: "info", msg, obj }), + warn: (obj, msg) => calls.push({ level: "warn", msg, obj }), error: () => {}, debug: () => {}, }; @@ -294,3 +294,27 @@ describe("linkStoredSquads downgrades a 57P01 admin-terminated connection (BIT-3 expect(log.calls.some((c) => c.level === "info" && /database connection terminated/.test(c.msg))).toBe(false); }); }); + +describe("saveHeadshot swallows a network-level fetch failure (BIT-31961)", () => { + it("returns false and logs a warn without a bare Error when the headshot URL is unreachable, so the bare undici TypeError does not reach the hub fingerprint", async () => { + const log = makeLog(); + const db = { + person: { update: async () => { throw new Error("must not be reached on a network failure"); } }, + }; + const storage = { referenceKey: () => "key", write: async () => { throw new Error("must not be reached on a network failure"); } }; + const deps = { db, log, fetch: (async () => { throw new TypeError("fetch failed"); }) as never, storage: storage as never, signer: null as never }; + const service = new SportsService(deps as unknown as ConstructorParameters[0]); + const saveHeadshot = (service as unknown as { saveHeadshot: (orgId: string, personId: string, src: string) => Promise }).saveHeadshot.bind(service); + + await expect(saveHeadshot("org-1", "person-1", "https://www.ligaportugal.pt/headshot.jpg")).resolves.toBe(false); + + const warn = log.calls.find((c) => c.level === "warn" && /headshot download unavailable/.test(c.msg)); + expect(warn).toBeTruthy(); + // The warn must not carry the bare undici TypeError as `err`: pino's logHooks fingerprints any warn that has an + // Error `err`, and a bare `TypeError: fetch failed` has no frame in our code, so it would collapse into the + // same ERRSIG as every other unwrapped fetch site (BIT-31961). The reason is logged as a plain string instead. + expect(warn!.obj && typeof warn!.obj === "object" && "err" in (warn!.obj as Record)).toBe(false); + expect((warn!.obj as { reason?: string }).reason).toBe("fetch failed"); + expect(log.calls.some((c) => c.level === "info" && /must not be reached/.test(c.msg))).toBe(false); + }); +}); diff --git a/apps/api/src/sports/service.ts b/apps/api/src/sports/service.ts index 10c4acf..cce3527 100644 --- a/apps/api/src/sports/service.ts +++ b/apps/api/src/sports/service.ts @@ -2035,7 +2035,18 @@ export class SportsService { private async saveHeadshot(orgId: string, personId: string, src: string): Promise { const u = new URL(src); if (u.protocol !== "https:" || !HEADSHOT_HOSTS.includes(u.hostname)) return false; - const res = await this.deps.fetch(u.toString(), { signal: AbortSignal.timeout(20_000) }); + let res: { ok: boolean; status: number; json(): Promise; text(): Promise; arrayBuffer(): Promise }; + try { + res = await this.deps.fetch(u.toString(), { signal: AbortSignal.timeout(20_000) }); + } catch (err) { + // A network-level failure (DNS, connection reset, timeout) has no Response and surfaces as a bare undici + // `TypeError: fetch failed` whose stack has no frame in our code, so it collapses into a single hub + // fingerprint regardless of the call site (BIT-31961). A headshot that can't be downloaded is not worth + // paging: log the reason without the bare Error object (so pino's logHooks does not fingerprint it) and + // skip this URL — the next sync retries it. + this.deps.log.warn({ personId, host: u.hostname, reason: err instanceof Error ? err.message : "fetch failed" }, "sports: headshot download unavailable"); + return false; + } if (!res.ok) return false; const data = Buffer.from(await res.arrayBuffer()); if (data.length > HEADSHOT_MAX_BYTES) return false; -- 2.47.3