Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk): keep the API key out of a printed JavaScript or PHP client
Yudhi Armyndharis5bc6d0be4192
diff --git a/sdk/javascript/src/client.ts b/sdk/javascript/src/client.tsindex ecf4f1589..08986aad9 100644--- a/sdk/javascript/src/client.ts+++ b/sdk/javascript/src/client.ts@@ -60,21 +60,26 @@ export interface OpenWAClientOptions {}export class OpenWAClient {- private readonly config: Required<Omit<ClientConfig, 'fetch'>> & { fetch: FetchLike };+ // Defined non-enumerable in the constructor, so the API key does not appear when a client is logged,+ // inspected or serialized (`declare` keeps the class field from creating an enumerable one).+ private declare readonly config: Required<Omit<ClientConfig, 'fetch'>> & { fetch: FetchLike };constructor(options: OpenWAClientOptions) {if (!options.baseUrl) throw new Error('OpenWAClient: baseUrl is required');if (!options.apiKey) throw new Error('OpenWAClient: apiKey is required');- this.config = {- baseUrl: options.baseUrl,- apiKey: options.apiKey,- timeoutMs: toTimeoutMs(options.timeoutMs ?? 30000),- defaultHeaders: options.defaultHeaders ?? {},- // Looked up on each call rather than captured here, so a fetch installed on globalThis after- // the client is built (a polyfill or a test stub) is the one used.- fetch: options.fetch ?? ((input, init) => globalThis.fetch(input, init)),- };+ Object.defineProperty(this, 'config', {+ enumerable: false,+ value: {+ baseUrl: options.baseUrl,+ apiKey: options.apiKey,+ timeoutMs: toTimeoutMs(options.timeoutMs ?? 30000),+ defaultHeaders: options.defaultHeaders ?? {},+ // Looked up on each call rather than captured here, so a fetch installed on globalThis after+ // the client is built (a polyfill or a test stub) is the one used.+ fetch: options.fetch ?? ((input, init) => globalThis.fetch(input, init)),+ },+ });warnIfInsecureHttpUrl(options.baseUrl);}diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.tsindex 5b2820f2b..eb20a6a99 100644--- a/sdk/javascript/test/client.test.ts+++ b/sdk/javascript/test/client.test.ts@@ -23,6 +23,24 @@ function client(transport: MockTransport): OpenWAClient {}describe('OpenWAClient', () => {+ // A client that is logged, inspected or captured by an error tracker while a call is debugged must not+ // write its bearer-equivalent key out.+ it('does not show the API key when the client is printed or serialized', () => {+ const secret = 'owa_k1_do_not_print';+ const printed = new OpenWAClient({ baseUrl: 'http://localhost:2785', apiKey: secret });++ // What a logger, util.inspect and an error tracker walk is the enumerable own properties.+ const seen = new Set<unknown>();+ const reachable = (value: unknown): boolean => {+ if (typeof value === 'string') return value.includes(secret);+ if (typeof value !== 'object' || value === null || seen.has(value)) return false;+ seen.add(value);+ return Object.values(value).some(reachable);+ };+ expect(reachable(printed)).toBe(false);+ expect(Object.getOwnPropertyDescriptor(printed, 'config')?.enumerable).toBe(false);+ });+it('requires baseUrl and apiKey', () => {expect(() => new OpenWAClient({ baseUrl: '', apiKey: 'x' })).toThrow();expect(() => new OpenWAClient({ baseUrl: 'http://x', apiKey: '' })).toThrow();diff --git a/sdk/php/src/Http/HttpExecutor.php b/sdk/php/src/Http/HttpExecutor.phpindex 00b8c5307..43d68d125 100644--- a/sdk/php/src/Http/HttpExecutor.php+++ b/sdk/php/src/Http/HttpExecutor.php@@ -51,6 +51,22 @@ 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.+ *+ * @return array<string,mixed>+ */+ public function __debugInfo(): array+ {+ return [+ 'baseUrl' => $this->baseUrl,+ 'timeout' => $this->timeout,+ 'apiKey' => '[redacted]',+ 'defaultHeaders' => array_keys($this->defaultHeaders),+ ];+ }+/*** 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-iddiff --git a/sdk/php/tests/HttpExecutorTest.php b/sdk/php/tests/HttpExecutorTest.phpindex 2f3d203cd..15901cc95 100644--- a/sdk/php/tests/HttpExecutorTest.php+++ b/sdk/php/tests/HttpExecutorTest.php@@ -27,6 +27,20 @@ class HttpExecutorTest extends TestCase$this->assertSame([], $backend->calls());}+ /**+ * A client that is dumped while a call is debugged must not write its bearer-equivalent key out.+ */+ public function testTheApiKeyDoesNotAppearWhenTheClientIsDumped(): void+ {+ $key = 'owa_k1_do_not_print';+ $client = (new MockBackend())->makeClient('http://localhost:2785', $key);++ $this->assertStringNotContainsString($key, print_r($client, true));+ ob_start();+ var_dump($client);+ $this->assertStringNotContainsString($key, (string) ob_get_clean());+ }+public function testQueryParamsExtendAQueryAlreadyInThePath(): void{$backend = (new MockBackend())->on(200, [])->on(200, []);