Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-php): saturate an oversized Retry-After header

Yudhi Armyndharise35c5993bac1
diff --git a/sdk/php/src/Exceptions/OpenWAApiException.php b/sdk/php/src/Exceptions/OpenWAApiException.php
index 04604dad5..31dd78878 100644
--- a/sdk/php/src/Exceptions/OpenWAApiException.php
+++ b/sdk/php/src/Exceptions/OpenWAApiException.php
@@ -106,7 +106,8 @@ class OpenWAApiException extends OpenWAException
}
// Not ctype_digit(): ext-ctype is optional and this SDK does not require it.
if (preg_match('/^\d+$/D', $value) === 1) {
- return (int) $value;
+ // A numeric string of 309 digits or more converts through INF, which casts to 0.
+ return strlen(ltrim($value, '0')) > 19 ? PHP_INT_MAX : (int) $value;
}
$at = \DateTimeImmutable::createFromFormat('D, d M Y H:i:s \G\M\T', $value, new \DateTimeZone('UTC'));
return $at === false ? null : max(0, $at->getTimestamp() - time());
diff --git a/sdk/php/tests/ClientTest.php b/sdk/php/tests/ClientTest.php
index f796a3af0..15b472ea4 100644
--- a/sdk/php/tests/ClientTest.php
+++ b/sdk/php/tests/ClientTest.php
@@ -552,6 +552,29 @@ class ClientTest extends TestCase
}
}
+ public function testAnOversizedRetryAfterHeaderSaturatesInsteadOfReadingAsZero(): void
+ {
+ // A numeric string of 309 digits or more converts through INF, which casts to 0.
+ $cases = [
+ [str_repeat('9', 19), PHP_INT_MAX],
+ [str_repeat('9', 25), PHP_INT_MAX],
+ [str_repeat('9', 308), PHP_INT_MAX],
+ [str_repeat('9', 309), PHP_INT_MAX],
+ [str_repeat('9', 400), PHP_INT_MAX],
+ [str_repeat('0', 400) . '7', 7],
+ ['0', 0],
+ ];
+ foreach ($cases as [$header, $expected]) {
+ $backend = (new MockBackend())->on(503, ['statusCode' => 503, 'message' => 'x'], ['Retry-After' => $header]);
+ try {
+ $backend->makeClient()->sessions->list();
+ $this->fail('Expected exception');
+ } catch (OpenWAApiException $e) {
+ $this->assertSame($expected, $e->getRetryAfterSeconds(), 'Retry-After of ' . strlen($header) . ' characters');
+ }
+ }
+ }
+
public function testRetryAfterParsesWithoutTheCtypeExtension(): void
{
// The SDK does not require ext-ctype, so a build without it must still