Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-php): refuse a request path without a leading slash

Yudhi Armyndharis868825e5c581
diff --git a/sdk/php/src/Http/HttpExecutor.php b/sdk/php/src/Http/HttpExecutor.php
index a22330305..03f68f68b 100644
--- a/sdk/php/src/Http/HttpExecutor.php
+++ b/sdk/php/src/Http/HttpExecutor.php
@@ -130,6 +130,11 @@ class HttpExecutor
*/
private function send(string $method, string $path, array $query, $body): ResponseInterface
{
+ // The path is appended to the base URL, so one without a leading "/" could move the host
+ // (".example.net/x", "@example.net/x") and send the API key there.
+ if (!str_starts_with($path, '/')) {
+ throw new \InvalidArgumentException('OpenWA: path must begin with "/": ' . $path);
+ }
// Auth/JSON headers are applied per-request so they are correct whether
// a default or injected client is used (and never leak Guzzle exceptions:
// http_errors disabled so we translate status into typed SDK exceptions).
diff --git a/sdk/php/tests/HttpExecutorTest.php b/sdk/php/tests/HttpExecutorTest.php
new file mode 100644
index 000000000..b74a0ef47
--- /dev/null
+++ b/sdk/php/tests/HttpExecutorTest.php
@@ -0,0 +1,29 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenWA\Tests;
+
+use PHPUnit\Framework\TestCase;
+
+class HttpExecutorTest extends TestCase
+{
+ /**
+ * The path is appended to the base URL, so one without a leading "/" could move the
+ * host and carry the API key elsewhere. It is refused before anything is sent.
+ */
+ public function testPathWithoutLeadingSlashIsRefusedBeforeSending(): void
+ {
+ $backend = new MockBackend();
+ $client = $backend->makeClient();
+ foreach (['.evil.example/x', '@evil.example/x', 'api/sessions'] as $path) {
+ try {
+ $client->request('GET', $path);
+ $this->fail("Expected InvalidArgumentException for {$path}");
+ } catch (\InvalidArgumentException $e) {
+ $this->assertStringContainsString('path must begin with "/"', $e->getMessage());
+ }
+ }
+ $this->assertSame([], $backend->calls());
+ }
+}