Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk-php): check default headers before the key is in a frame
Yudhi Armyndharis2bdd4a8871b6
diff --git a/sdk/php/src/Client.php b/sdk/php/src/Client.phpindex d1ce454d5..b9fa5cd92 100644--- a/sdk/php/src/Client.php+++ b/sdk/php/src/Client.php@@ -84,7 +84,9 @@ class Client* allowInsecureHttp?:bool* } $config*- * @throws OpenWAException If baseUrl or apiKey is missing.+ * @throws OpenWAException If baseUrl or apiKey is missing, the key holds a control or non-ASCII+ * character (a surrounding space or newline is trimmed), or a default header name is not an+ * HTTP token or its value holds such a character.*/public function __construct(#[\SensitiveParameter] array $config){@@ -108,6 +110,10 @@ class Clientself::warnIfInsecureHttp($config['baseUrl']);}+ // Before the executor exists: its constructor frame holds the key, which a trace shows in part on+ // PHP before 8.2, where #[\SensitiveParameter] does nothing.+ HttpExecutor::checkDefaultHeaders(is_array($config['defaultHeaders'] ?? null) ? $config['defaultHeaders'] : []);+$this->http = new HttpExecutor($config['baseUrl'],$apiKey,diff --git a/sdk/php/src/Http/HttpExecutor.php b/sdk/php/src/Http/HttpExecutor.phpindex 69debe4a5..684d19c3b 100644--- a/sdk/php/src/Http/HttpExecutor.php+++ b/sdk/php/src/Http/HttpExecutor.php@@ -65,12 +65,16 @@ class HttpExecutor/*** Refuse a default header the HTTP stack would refuse, naming the header and never its value: the* stack's own message quotes the value, which would put a proxy credential in the application's log.+ * Public so that the client can run it before it builds an executor, whose constructor frame holds the+ * key and, before PHP 8.2, shows its first characters in a trace.+ *+ * A number, a boolean or null is sent as its text and a list as several values, as the stack does.** @param array<mixed> $headers** @throws OpenWAException*/- private static function checkDefaultHeaders(array $headers): void+ public static function checkDefaultHeaders(array $headers): void{foreach ($headers as $name => $value) {if (!is_string($name) || preg_match('/\A[!#$%&\'*+\-.^_`|~0-9A-Za-z]+\z/', $name) !== 1) {@@ -78,14 +82,16 @@ class HttpExecutor'OpenWA Client: defaultHeaders has a header name that is not a valid HTTP token');}- // A number is text to the HTTP stack, which sends its decimal form; the docblock says string.- $text = is_int($value) || is_float($value) ? (string) $value : $value;- if (!is_string($text) || preg_match('/\A[\t\x20-\x7e]*\z/', $text) !== 1) {- throw new OpenWAException(sprintf(- 'OpenWA Client: defaultHeaders value for "%s" must be text without control or non-ASCII '- . 'characters; check for a stray newline',- $name- ));+ foreach (is_array($value) ? $value : [$value] as $each) {+ if (is_scalar($each) || $each === null) {+ if (preg_match('/\A[\t\x20-\x7e]*\z/', (string) $each) === 1) {+ continue;+ }+ $reason = 'must be text without control or non-ASCII characters; check for a stray newline';+ } else {+ $reason = 'must be text';+ }+ throw new OpenWAException(sprintf('OpenWA Client: defaultHeaders value for "%s" %s', $name, $reason));}}}diff --git a/sdk/php/tests/ClientTest.php b/sdk/php/tests/ClientTest.phpindex 0a1d7d70a..ccf2099ff 100644--- a/sdk/php/tests/ClientTest.php+++ b/sdk/php/tests/ClientTest.php@@ -71,16 +71,45 @@ class ClientTest extends TestCase$this->assertSame("a\tb c", $backend->lastCall()['headers']['x-trace'] ?? null);}- public function testANumericDefaultHeaderValueIsSentAsItsText(): void+ public function testANumberABooleanOrNullDefaultHeaderValueIsSentAsItsText(): void{$backend = (new MockBackend())->on(200, []);(new \OpenWA\Client(['baseUrl' => 'http://localhost:2785','apiKey' => 'k','httpClient' => $backend->httpClient(),- 'defaultHeaders' => ['X-Tenant-Id' => 42],+ 'defaultHeaders' => ['X-Tenant-Id' => 42, 'X-Ratio' => 1.5, 'X-Debug' => true, 'X-Empty' => null],]))->request('GET', '/api/anything');- $this->assertSame('42', $backend->lastCall()['headers']['x-tenant-id'] ?? null);+ $headers = $backend->lastCall()['headers'];+ $this->assertSame('42', $headers['x-tenant-id'] ?? null);+ $this->assertSame('1.5', $headers['x-ratio'] ?? null);+ $this->assertSame('1', $headers['x-debug'] ?? null);+ }++ public function testADefaultHeaderThatIsNeitherTextNorAListOfItIsRefusedByName(): void+ {+ foreach ([new \stdClass(), [['nested']]] as $value) {+ try {+ new \OpenWA\Client(['baseUrl' => 'http://localhost:2785', 'apiKey' => 'k', 'defaultHeaders' => ['X-Bad' => $value]]);+ $this->fail('Expected an exception for a header value that is not text');+ } catch (\OpenWA\Exceptions\OpenWAException $e) {+ $this->assertStringContainsString('X-Bad', $e->getMessage());+ $this->assertStringContainsString('must be text', $e->getMessage());+ }+ }+ }++ // On PHP before 8.2 #[\SensitiveParameter] does nothing, and the frame of the executor's constructor+ // lists the key: a refused header is refused before that frame exists.+ public function testARefusedDefaultHeaderLeavesTheKeyOutOfItsTrace(): void+ {+ $key = 'owa_k1_0123456789abcdefghij';+ try {+ new \OpenWA\Client(['baseUrl' => 'https://wa.example.com', 'apiKey' => $key, 'defaultHeaders' => ['X-Proxy' => "tok\n"]]);+ $this->fail('Expected an exception for an illegal header value');+ } catch (\OpenWA\Exceptions\OpenWAException $e) {+ $this->assertStringNotContainsString(substr($key, 0, 10), $e->getTraceAsString());+ }}public function testRequiresBaseUrlAndApiKey(): void