Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk): keep the auth and JSON headers winning in any letter case

Yudhi Armyndharis089b3b0cf578
diff --git a/sdk/javascript/src/http.ts b/sdk/javascript/src/http.ts
index 8004cbae4..dfadabbc7 100644
--- a/sdk/javascript/src/http.ts
+++ b/sdk/javascript/src/http.ts
@@ -137,13 +137,15 @@ async function send<T>(
// Auth and JSON content-type WIN over caller-supplied defaults/per-request headers — the SDK only
// ever sends a JSON body, and this matches the Python and PHP SDKs (which force JSON) and the
- // documented "JSON headers win" contract. Put them last so a defaultHeaders Content-Type can't clobber.
- const headers: Record<string, string> = {
- ...config.defaultHeaders,
- ...options.headers,
- 'Content-Type': 'application/json',
- 'X-API-Key': config.apiKey,
- };
+ // documented "JSON headers win" contract. Header names are case-insensitive and fetch joins duplicates,
+ // so drop a caller's copy in any case before adding ours; putting ours last is not enough.
+ const headers: Record<string, string> = {};
+ for (const [name, value] of Object.entries({ ...config.defaultHeaders, ...options.headers })) {
+ const lower = name.toLowerCase();
+ if (lower !== 'content-type' && lower !== 'x-api-key') headers[name] = value;
+ }
+ headers['Content-Type'] = 'application/json';
+ headers['X-API-Key'] = config.apiKey;
try {
const res = await config.fetch(url, {
diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.ts
index ab67f9f95..f70337a3a 100644
--- a/sdk/javascript/test/client.test.ts
+++ b/sdk/javascript/test/client.test.ts
@@ -286,6 +286,25 @@ describe('OpenWAClient', () => {
expect(t.lastCall!.headers['x-trace']).toBe('keep');
});
+ it('keeps the auth and JSON headers winning over a caller header that differs only in case', async () => {
+ // fetch folds header names case-insensitively and joins duplicates, so read what goes on the wire.
+ let wire: Headers | undefined;
+ const recordingFetch: FetchLike = async (_url, init) => {
+ wire = new Headers(init?.headers);
+ return new Response('[]', { status: 200 });
+ };
+ const c = new OpenWAClient({
+ baseUrl: 'http://localhost',
+ apiKey: 'REAL',
+ defaultHeaders: { 'x-api-key': 'EVIL', 'x-trace': 'keep' },
+ fetch: recordingFetch,
+ });
+ await c.request({ method: 'GET', path: '/api/sessions', headers: { 'content-type': 'text/plain' } });
+ expect(wire!.get('x-api-key')).toBe('REAL');
+ expect(wire!.get('content-type')).toBe('application/json');
+ expect(wire!.get('x-trace')).toBe('keep');
+ });
+
it('calls the global fetch unbound from the client config when none is injected', async () => {
// Browsers and Workers reject a fetch invoked as a method of another object ("Illegal invocation").
vi.stubGlobal('fetch', function (this: unknown) {
diff --git a/sdk/python/openwa/_http.py b/sdk/python/openwa/_http.py
index c5cf1f357..9d88aa313 100644
--- a/sdk/python/openwa/_http.py
+++ b/sdk/python/openwa/_http.py
@@ -67,9 +67,9 @@ class HttpExecutor:
) -> None:
# Caller-supplied default headers are applied FIRST so the auth/JSON
# headers below always win and can never be clobbered (mirrors the JS SDK).
- headers: dict[str, str] = {}
- if default_headers:
- headers.update(default_headers)
+ # httpx.Headers matches names case-insensitively, so a lowercase copy is
+ # replaced instead of being sent alongside ours.
+ headers = httpx.Headers(default_headers)
headers["Content-Type"] = "application/json"
headers["X-API-Key"] = api_key
client_kwargs: dict[str, Any] = {
diff --git a/sdk/python/tests/test_sdk.py b/sdk/python/tests/test_sdk.py
index 1bcc86293..38d06323e 100644
--- a/sdk/python/tests/test_sdk.py
+++ b/sdk/python/tests/test_sdk.py
@@ -44,6 +44,19 @@ class TestClientCore:
assert backend.last_call.headers["content-type"] == "application/json"
assert backend.last_call.headers["x-trace"] == "keep" # benign custom headers still pass through
+ def test_default_headers_cannot_override_api_key_in_another_case(self):
+ # Header names are case-insensitive: a lowercase copy must be replaced, not sent alongside.
+ backend = MockBackend().on("GET", "/api/sessions", body=[])
+ client = OpenWAClient(
+ base_url="http://localhost",
+ api_key="REAL_KEY",
+ default_headers={"x-api-key": "EVIL", "content-type": "text/plain"},
+ transport=backend.as_transport(),
+ )
+ client.sessions.list()
+ assert backend.last_call.headers["x-api-key"] == "REAL_KEY"
+ assert backend.last_call.headers["content-type"] == "application/json"
+
def test_non_json_2xx_body_returns_text(self):
import httpx