Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk): check a header value after the trim, redact it in a PHP frame

Yudhi Armyndharis8418b4285ed0
diff --git a/sdk/javascript/src/client.ts b/sdk/javascript/src/client.ts
index ed8120d73..b20287841 100644
--- a/sdk/javascript/src/client.ts
+++ b/sdk/javascript/src/client.ts
@@ -74,7 +74,8 @@ export class OpenWAClient {
throw new Error('OpenWAClient: apiKey contains a control or non-Latin-1 character; check for a stray newline');
}
for (const [name, value] of Object.entries(options.defaultHeaders ?? {})) {
- if (hasUnsendableChar(String(value))) {
+ // fetch trims spaces, tabs and line breaks at both ends of a value, so only what is left can be refused.
+ if (hasUnsendableChar(String(value).replace(/^[\t\n\r ]+|[\t\n\r ]+$/g, ''))) {
throw new Error(
`OpenWAClient: defaultHeaders value for "${name}" contains a control or non-Latin-1 character; check for a stray newline`,
);
diff --git a/sdk/javascript/test/client.test.ts b/sdk/javascript/test/client.test.ts
index e79d53e05..a29f086d4 100644
--- a/sdk/javascript/test/client.test.ts
+++ b/sdk/javascript/test/client.test.ts
@@ -475,10 +475,14 @@ describe('OpenWAClient', () => {
expect(message).toContain(`"${name}"`);
expect(message).not.toContain(value);
}
- // A tab, printable ASCII and a Latin-1 character are sent as before.
+ // A tab, printable ASCII and a Latin-1 character are sent as before, and a line break at either end of a
+ // value read from a file is what fetch trims.
expect(
() => new OpenWAClient({ baseUrl: 'http://x', apiKey: 'k', defaultHeaders: { 'X-Trace': 'a\tb caf\u00e9' } }),
).not.toThrow();
+ expect(
+ () => new OpenWAClient({ baseUrl: 'http://x', apiKey: 'k', defaultHeaders: { 'X-Proxy-Token': 'tok\n' } }),
+ ).not.toThrow();
});
it('keeps X-API-Key winning over defaultHeaders', async () => {
diff --git a/sdk/php/src/Http/HttpExecutor.php b/sdk/php/src/Http/HttpExecutor.php
index 684d19c3b..87a5b37bd 100644
--- a/sdk/php/src/Http/HttpExecutor.php
+++ b/sdk/php/src/Http/HttpExecutor.php
@@ -74,7 +74,7 @@ class HttpExecutor
*
* @throws OpenWAException
*/
- public static function checkDefaultHeaders(array $headers): void
+ public static function checkDefaultHeaders(#[\SensitiveParameter] array $headers): void
{
foreach ($headers as $name => $value) {
if (!is_string($name) || preg_match('/\A[!#$%&\'*+\-.^_`|~0-9A-Za-z]+\z/', $name) !== 1) {
diff --git a/sdk/php/tests/ClientTest.php b/sdk/php/tests/ClientTest.php
index ccf2099ff..ecbf6d10e 100644
--- a/sdk/php/tests/ClientTest.php
+++ b/sdk/php/tests/ClientTest.php
@@ -109,6 +109,10 @@ class ClientTest extends TestCase
$this->fail('Expected an exception for an illegal header value');
} catch (\OpenWA\Exceptions\OpenWAException $e) {
$this->assertStringNotContainsString(substr($key, 0, 10), $e->getTraceAsString());
+ // From PHP 8.2 the frames carry no argument values at all: the refused value is not in one.
+ if (PHP_VERSION_ID >= 80200) {
+ $this->assertStringNotContainsString('tok', (string) json_encode($e->getTrace()));
+ }
}
}