Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk): drop case-variant auth headers in the Java and PHP clients

Yudhi Armyndharis9bb8ac657d2a
diff --git a/sdk/java/src/main/java/com/rmyndharis/openwa/http/Http.java b/sdk/java/src/main/java/com/rmyndharis/openwa/http/Http.java
index 18538461b..129adc616 100644
--- a/sdk/java/src/main/java/com/rmyndharis/openwa/http/Http.java
+++ b/sdk/java/src/main/java/com/rmyndharis/openwa/http/Http.java
@@ -53,6 +53,9 @@ public final class Http {
if (perRequest != null) {
out.putAll(perRequest);
}
+ // Header names are case-insensitive, and the transport sends every entry: drop a caller's
+ // copy in any case, or a lowercase "x-api-key" would travel alongside ours.
+ out.keySet().removeIf(k -> k.equalsIgnoreCase("Content-Type") || k.equalsIgnoreCase("X-API-Key"));
out.put("Content-Type", "application/json");
out.put("X-API-Key", apiKey);
return out;
diff --git a/sdk/java/src/test/java/com/rmyndharis/openwa/http/HttpTest.java b/sdk/java/src/test/java/com/rmyndharis/openwa/http/HttpTest.java
index 5937c9304..ac489a391 100644
--- a/sdk/java/src/test/java/com/rmyndharis/openwa/http/HttpTest.java
+++ b/sdk/java/src/test/java/com/rmyndharis/openwa/http/HttpTest.java
@@ -49,5 +49,13 @@ class HttpTest {
assertEquals("abc", out.get("X-Trace"));
}
+ @Test
+ void mergeHeadersDropsCallerCopiesThatDifferOnlyInCase() {
+ // Header names are case-insensitive: a lowercase copy left in the map is sent alongside ours.
+ Map<String, String> defaults = Map.of("x-api-key", "EVIL", "content-type", "text/plain");
+ Map<String, String> out = Http.mergeHeaders(defaults, null, "owa_k1_secret");
+ assertEquals(Map.of("Content-Type", "application/json", "X-API-Key", "owa_k1_secret"), out);
+ }
+
private record Query(String chatId, Integer limit, String cursor) {}
}
diff --git a/sdk/php/src/Http/HttpExecutor.php b/sdk/php/src/Http/HttpExecutor.php
index 2cdc66599..feb1e054a 100644
--- a/sdk/php/src/Http/HttpExecutor.php
+++ b/sdk/php/src/Http/HttpExecutor.php
@@ -132,8 +132,13 @@ class HttpExecutor
// Never auto-follow redirects: doing so would re-send the X-API-Key
// header to the redirect target (potentially a different origin).
'allow_redirects' => false,
- // Caller default headers first; auth/JSON win so they can't be clobbered.
- 'headers' => array_merge($this->defaultHeaders, [
+ // Caller default headers first; auth/JSON win so they can't be clobbered. Header names are
+ // case-insensitive and PSR-7 keeps every value, so a caller's copy in another case is dropped.
+ 'headers' => array_merge(array_filter(
+ $this->defaultHeaders,
+ fn ($name) => !in_array(strtolower((string) $name), ['x-api-key', 'content-type', 'accept'], true),
+ ARRAY_FILTER_USE_KEY
+ ), [
'X-API-Key' => $this->apiKey,
'Content-Type' => 'application/json',
'Accept' => 'application/json',
diff --git a/sdk/php/tests/ClientTest.php b/sdk/php/tests/ClientTest.php
index 08152a96c..17f9e10a5 100644
--- a/sdk/php/tests/ClientTest.php
+++ b/sdk/php/tests/ClientTest.php
@@ -73,6 +73,23 @@ class ClientTest extends TestCase
$this->assertSame('REAL_KEY', $headers['x-api-key'] ?? ''); // auth still wins
}
+ public function testDefaultHeadersThatDifferOnlyInCaseDoNotReachTheWire(): void
+ {
+ // PSR-7 folds header names case-insensitively and keeps every value, so a lowercase copy
+ // would be sent ahead of ours: "x-api-key: EVIL, REAL_KEY".
+ $backend = (new MockBackend())->on(200, []);
+ $client = new \OpenWA\Client([
+ 'baseUrl' => 'https://x',
+ 'apiKey' => 'REAL_KEY',
+ 'httpClient' => $backend->httpClient(),
+ 'defaultHeaders' => ['x-api-key' => 'EVIL', 'content-type' => 'text/plain'],
+ ]);
+ $client->sessions->list();
+ $headers = $backend->lastCall()['headers'];
+ $this->assertSame('REAL_KEY', $headers['x-api-key'] ?? '');
+ $this->assertSame('application/json', $headers['content-type'] ?? '');
+ }
+
public function testPathSegmentsAreEncoded(): void
{
$backend = (new MockBackend())->on(200, ['id' => 'x']);