From ee9a681696ceccf0f56f2d64c1551935219a240e Mon Sep 17 00:00:00 2001 From: Scott Lovegrove Date: Wed, 11 Nov 2026 10:27:14 +0000 Subject: [PATCH] fix: keep a Request's own headers in withUsageHeaders When `withUsageHeaders` was called with a Request carrying its own headers and no `init.headers`, it passed `init.headers` made of only the usage headers. `fetch(request, { headers })` replaces the Request's headers, so its `Authorization` and anything else were lost. Start from the Request's headers when `init` brings none. When both are given, `init.headers` still wins, as it does with `fetch`, and the usage headers still win over either. Nothing in the CLI passes a Request today (the OAuth provider calls it with a URL and options), so this was latent. --- src/lib/usage-tracking.test.ts | 77 +++++++++++++++++++++++++++++++++- src/lib/usage-tracking.ts | 8 +++- 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/src/lib/usage-tracking.test.ts b/src/lib/usage-tracking.test.ts index cdda5cd..b774b93 100644 --- a/src/lib/usage-tracking.test.ts +++ b/src/lib/usage-tracking.test.ts @@ -65,7 +65,7 @@ vi.mock('./spinner.js', () => ({ })) const { clearApiClientCache, clearWorkspaceUserCache } = await import('./api.js') -const { trackCommandUsage } = await import('./usage-tracking.js') +const { trackCommandUsage, withUsageHeaders } = await import('./usage-tracking.js') const { registerUserCommand } = await import('../commands/user.js') const { registerThreadCommand } = await import('../commands/thread/index.js') const { registerViewCommand } = await import('../commands/view.js') @@ -136,3 +136,78 @@ describe('CLI usage headers on API requests', () => { expect(getComments?.['request-id']).not.toBe(getThread?.['request-id']) }) }) + +/** + * Calls `fetch` through `withUsageHeaders` and returns the headers the request + * would go out with: the fake `fetch` builds the request from its arguments the + * same way the real one does, so `init.headers` replaces a Request's own. + */ +async function headersFetchedWith( + input: RequestInfo | URL, + init?: RequestInit, +): Promise> { + const sent: Headers[] = [] + const fetchImpl = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { + sent.push(new Request(input, init).headers) + return new Response() + }) + + await withUsageHeaders(fetchImpl)(input, init) + + expect(sent).toHaveLength(1) + return Object.fromEntries(sent[0]?.entries() ?? []) +} + +describe('withUsageHeaders', () => { + const TOKEN_URL = 'https://auth.example.com/oauth/access_token' + + it('adds the usage headers to the headers passed in `init`', async () => { + const headers = await headersFetchedWith(TOKEN_URL, { + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + }) + + expect(headers).toMatchObject({ + 'content-type': 'application/x-www-form-urlencoded', + 'user-agent': `comms-cli/${packageJson.version}`, + 'doist-platform': 'cli', + }) + }) + + it("keeps a Request's own headers when `init` has none", async () => { + const request = new Request(TOKEN_URL, { + headers: { Authorization: 'Bearer request-token' }, + }) + + const headers = await headersFetchedWith(request) + + expect(headers).toMatchObject({ + authorization: 'Bearer request-token', + 'user-agent': `comms-cli/${packageJson.version}`, + 'doist-platform': 'cli', + }) + }) + + it("uses `init.headers` over a Request's own headers, as fetch does", async () => { + const request = new Request(TOKEN_URL, { + headers: { Authorization: 'Bearer request-token', 'X-Request-Only': 'dropped' }, + }) + + const headers = await headersFetchedWith(request, { + headers: { Authorization: 'Bearer init-token' }, + }) + + expect(headers).toMatchObject({ + authorization: 'Bearer init-token', + 'doist-platform': 'cli', + }) + expect(headers).not.toHaveProperty('x-request-only') + }) + + it("sends the usage headers over a Request's header of the same name", async () => { + const request = new Request(TOKEN_URL, { headers: { 'User-Agent': 'other-client/1.0' } }) + + const headers = await headersFetchedWith(request) + + expect(headers['user-agent']).toBe(`comms-cli/${packageJson.version}`) + }) +}) diff --git a/src/lib/usage-tracking.ts b/src/lib/usage-tracking.ts index d45cbf6..ae7a5b8 100644 --- a/src/lib/usage-tracking.ts +++ b/src/lib/usage-tracking.ts @@ -26,6 +26,10 @@ export function trackCommandUsage(program: Command): Command { /** Wraps a `fetch` so requests that bypass the SDK carry the usage headers too. */ export function withUsageHeaders(fetchImpl: typeof fetch): typeof fetch { - return (input, init) => - fetchImpl(input, { ...init, headers: usageTracking.mergeHeaders(init?.headers) }) + return (input, init) => { + // `fetch(request, { headers })` replaces the Request's own headers, so + // start from them unless `init` brings headers, which win as with `fetch`. + const headers = init?.headers ?? (input instanceof Request ? input.headers : undefined) + return fetchImpl(input, { ...init, headers: usageTracking.mergeHeaders(headers) }) + } }