From 0d094e1c4a1d8d25a4d9a638be578feafe4faa20 Mon Sep 17 00:00:00 2001 From: Aaron de Mello Date: Wed, 4 Nov 2026 15:42:57 -0500 Subject: [PATCH] NYL-476 preserve opaque upload-session header names --- CHANGELOG.md | 1 + src/apiClient.ts | 11 ++++++++ tests/apiClient.spec.ts | 61 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 964c47f6..19009456 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Fixed +- Preserve upload-session HTTP header map keys while decoding API responses (NYL-476). - Patch vulnerable SDK tooling and example HTTP dependencies while preserving TypeDoc compatibility (NYL-471). - Redact request bodies, provider errors, and stack traces from AWS Lambda example errors and logs (NYL-473). - Support validated completed-upload attachment references in JSON and multipart message sends and draft requests (NYL-469). diff --git a/src/apiClient.ts b/src/apiClient.ts index 75f77019..8eedf878 100644 --- a/src/apiClient.ts +++ b/src/apiClient.ts @@ -371,6 +371,17 @@ export default class APIClient { }, ['metadata'] ); + // Upload-session headers are an opaque HTTP header map. Preserve their + // names while keeping the deprecated top-level response headers camelCase. + const dataHeaders = parsed?.data?.headers; + if ( + dataHeaders !== null && + typeof dataHeaders === 'object' && + !Array.isArray(dataHeaders) && + Object.values(dataHeaders).every((value) => typeof value === 'string') + ) { + payload.data.headers = dataHeaders; + } // Attach rawHeaders as a non-enumerable property to avoid breaking deep equality Object.defineProperty(payload, 'rawHeaders', { value: headers, diff --git a/tests/apiClient.spec.ts b/tests/apiClient.spec.ts index 40e9d668..1d12d65a 100644 --- a/tests/apiClient.spec.ts +++ b/tests/apiClient.spec.ts @@ -338,6 +338,67 @@ describe('APIClient', () => { }); describe('requestWithResponse', () => { + it('should preserve opaque data header maps without changing response header compatibility', async () => { + const uploadHeaders = { + 'Content-Type': 'application/octet-stream', + 'x-goog-upload-protocol': 'resumable', + }; + const response = mockResponse( + JSON.stringify({ + request_id: 'upload-request', + data: { + attachment_id: 'upload-id', + content_type: 'application/octet-stream', + headers: uploadHeaders, + metadata: { Keep_Exact: 'value' }, + }, + }) + ); + response.headers.set('content-type', 'application/json'); + response.headers.set('x-fixture-response', 'upload-session'); + + const result: any = await client.requestWithResponse(response); + + expect(result.requestId).toBe('upload-request'); + expect(result.data.attachmentId).toBe('upload-id'); + expect(result.data.contentType).toBe('application/octet-stream'); + expect(result.data.headers).toEqual(uploadHeaders); + expect(result.data.metadata).toEqual({ Keep_Exact: 'value' }); + expect(result.headers.contentType).toBe('application/json'); + expect(result.headers.xFixtureResponse).toBe('upload-session'); + expect(result.rawHeaders['content-type']).toBe('application/json'); + expect(result.rawHeaders['x-fixture-response']).toBe('upload-session'); + expect(Object.keys(result)).not.toContain('rawHeaders'); + }); + + it('should continue converting structured header array fields', async () => { + const response = mockResponse( + JSON.stringify({ + data: { + headers: [{ header_name: 'Subject', header_value: 'Example' }], + }, + }) + ); + + const result: any = await client.requestWithResponse(response); + + expect(result.data.headers).toEqual([ + { headerName: 'Subject', headerValue: 'Example' }, + ]); + }); + + it('should continue converting header objects that are not string maps', async () => { + const response = mockResponse( + JSON.stringify({ + data: { headers: { custom_field: { inner_value: 1 } } }, + }) + ); + + const result: any = await client.requestWithResponse(response); + + expect(result.data.headers).toEqual({ customField: { innerValue: 1 } }); + }); + it('should redact malformed response bodies while retaining request context', async () => { const response = { status: 200,