Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk-php): keep the API key out of the executor's properties
Yudhi Armyndharisf4f905a47e72
diff --git a/sdk/php/src/Http/HttpExecutor.php b/sdk/php/src/Http/HttpExecutor.phpindex 20ccf6d8f..7041ac971 100644--- a/sdk/php/src/Http/HttpExecutor.php+++ b/sdk/php/src/Http/HttpExecutor.php@@ -20,9 +20,16 @@ use Psr\Http\Message\ResponseInterface;*/class HttpExecutor{+ /**+ * The API keys of the executors, outside their property tables: print_r, var_dump, var_export, a+ * (array) cast and a framework's dumper all read properties, and the key is bearer-equivalent.+ *+ * @var \WeakMap<HttpExecutor,string>|null+ */+ private static ?\WeakMap $keys = null;+private ClientInterface $http;private float $timeout;- private string $apiKey;private string $baseUrl;/** @var array<string,string> */private array $defaultHeaders;@@ -39,7 +46,8 @@ class HttpExecutorarray $defaultHeaders = []) {$this->timeout = $timeout;- $this->apiKey = $apiKey;+ self::$keys ??= new \WeakMap();+ self::$keys[$this] = $apiKey;$this->baseUrl = rtrim($baseUrl, '/');$this->defaultHeaders = $defaultHeaders;// Auth/JSON headers are applied per-request (in request()). Request URLs@@ -52,8 +60,8 @@ class HttpExecutor}/**- * What print_r and var_dump show of the executor, and so of every client and resource that holds it:- * the API key is bearer-equivalent and must not reach a log line written while a call is debugged.+ * What print_r and var_dump show of the executor, and so of every client and resource that holds it.+ * The key is not among the properties at all (see $keys), so no dump, export or cast can reach it.** @return array<string,mixed>*/@@ -67,6 +75,11 @@ class HttpExecutor];}+ /** A copy would have no key, since the key belongs to the object it was given to. */+ private function __clone()+ {+ }+/*** Percent-encode a single path segment (e.g. a chat/message id) so a value* containing /, # or ? can't break out of its path position. WhatsApp-id@@ -173,7 +186,7 @@ class HttpExecutor&& ($idempotencyKey === null || strtolower((string) $name) !== 'idempotency-key'),ARRAY_FILTER_USE_KEY), [- 'X-API-Key' => $this->apiKey,+ 'X-API-Key' => (self::$keys ?? new \WeakMap())[$this] ?? '','Content-Type' => 'application/json','Accept' => 'application/json',]),diff --git a/sdk/php/tests/HttpExecutorTest.php b/sdk/php/tests/HttpExecutorTest.phpindex 15901cc95..9a8c14025 100644--- a/sdk/php/tests/HttpExecutorTest.php+++ b/sdk/php/tests/HttpExecutorTest.php@@ -39,6 +39,16 @@ class HttpExecutorTest extends TestCaseob_start();var_dump($client);$this->assertStringNotContainsString($key, (string) ob_get_clean());+ // The other ways a value is written out: an export, a cast, and what a framework dumper reads.+ $this->assertStringNotContainsString($key, var_export($client, true));+ $executor = (new \ReflectionObject($client))->getProperty('http')->getValue($client);+ $this->assertStringNotContainsString($key, var_export($executor, true));+ $this->assertStringNotContainsString($key, json_encode((array) $executor, JSON_PARTIAL_OUTPUT_ON_ERROR) ?: '');+ $this->assertStringNotContainsString($key, print_r((array) $executor, true));+ // and the key is still what goes out on the wire+ $backend = (new MockBackend())->on(200, []);+ $backend->makeClient('http://localhost:2785', $key)->request('GET', '/api/anything');+ $this->assertSame($key, $backend->lastCall()['headers']['x-api-key'] ?? null);}public function testQueryParamsExtendAQueryAlreadyInThePath(): void