Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-php): saturate an oversized retryAfterSeconds

Yudhi Armyndharis15018b2adf6e
diff --git a/sdk/php/src/Exceptions/OpenWAApiException.php b/sdk/php/src/Exceptions/OpenWAApiException.php
index ab4fb234d..04604dad5 100644
--- a/sdk/php/src/Exceptions/OpenWAApiException.php
+++ b/sdk/php/src/Exceptions/OpenWAApiException.php
@@ -97,7 +97,8 @@ class OpenWAApiException extends OpenWAException
private static function retryAfter($fromBody, ?string $header): ?int
{
if ((is_int($fromBody) || is_float($fromBody)) && $fromBody >= 0) {
- return (int) ceil($fromBody);
+ // Past the integer range a float cast wraps to a negative or short wait.
+ return $fromBody >= PHP_INT_MAX ? PHP_INT_MAX : (int) ceil($fromBody);
}
$value = trim((string) $header);
if ($value === '') {
diff --git a/sdk/php/tests/ClientTest.php b/sdk/php/tests/ClientTest.php
index 16ec0eac3..f796a3af0 100644
--- a/sdk/php/tests/ClientTest.php
+++ b/sdk/php/tests/ClientTest.php
@@ -539,6 +539,19 @@ class ClientTest extends TestCase
$this->assertSame([], (new OpenWARateLimitException('m', 429))->getHeaders());
}
+ public function testAnOversizedRetryAfterBodyDoesNotWrapAround(): void
+ {
+ foreach ([9.3e18, 1.5e19, 1e20] as $seconds) {
+ $backend = (new MockBackend())->on(429, ['statusCode' => 429, 'message' => 'x', 'retryAfterSeconds' => $seconds]);
+ try {
+ $backend->makeClient()->sessions->list();
+ $this->fail('Expected exception');
+ } catch (OpenWARateLimitException $e) {
+ $this->assertSame(PHP_INT_MAX, $e->getRetryAfterSeconds(), "retryAfterSeconds {$seconds}");
+ }
+ }
+ }
+
public function testRetryAfterParsesWithoutTheCtypeExtension(): void
{
// The SDK does not require ext-ctype, so a build without it must still