Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-php): log the insecure http:// warning instead of raising it

Yudhi Armyndharis01602a3edc9a
diff --git a/sdk/php/README.md b/sdk/php/README.md
index d347a86ca..4ae557ab2 100644
--- a/sdk/php/README.md
+++ b/sdk/php/README.md
@@ -38,7 +38,7 @@ For tests, inject a Guzzle client whose handler is a `MockHandler` — no networ
```php
$client = new Client([
- 'baseUrl' => 'http://x',
+ 'baseUrl' => 'http://localhost',
'apiKey' => 'k',
'httpClient' => $mockGuzzleClient,
]);
@@ -72,6 +72,8 @@ try {
## Notes
- **Use HTTPS in production** — the API key is sent as `X-API-Key` and is bearer-equivalent.
+ Over plaintext `http://` to a non-localhost host the client writes a warning with `error_log()`;
+ pass `'allowInsecureHttp' => true` to skip it (for example on a private Docker network).
- The SDK does **not** retry, and **never follows redirects** (so the key is never re-sent to
a redirect target). Path segments are percent-encoded; a base-URL path prefix (e.g. behind a
reverse proxy) is preserved.
diff --git a/sdk/php/src/Client.php b/sdk/php/src/Client.php
index a1608b51f..8c93a2e4b 100644
--- a/sdk/php/src/Client.php
+++ b/sdk/php/src/Client.php
@@ -76,7 +76,8 @@ class Client
* apiKey:string,
* timeout?:float,
* httpClient?:?\GuzzleHttp\ClientInterface|null,
- * defaultHeaders?:array<string,string>
+ * defaultHeaders?:array<string,string>,
+ * allowInsecureHttp?:bool
* } $config
*
* @throws OpenWAException If baseUrl or apiKey is missing.
@@ -90,7 +91,9 @@ class Client
throw new OpenWAException('OpenWA Client: apiKey is required');
}
- self::warnIfInsecureHttp($config['baseUrl']);
+ if (empty($config['allowInsecureHttp'])) {
+ self::warnIfInsecureHttp($config['baseUrl']);
+ }
$this->http = new HttpExecutor(
$config['baseUrl'],
@@ -122,7 +125,9 @@ class Client
* Warn (not throw) when baseUrl is http:// and the host is not localhost. The API key is sent
* as an X-API-Key header on every request — over plaintext http to a non-local host that's
* cleartext on the wire. Warning (not refusing) keeps local dev and TLS-terminating-proxy
- * topologies working.
+ * topologies working. It goes to error_log(), not trigger_error(): Laravel, Symfony and
+ * PHPUnit's failOnWarning turn a PHP warning into an exception, which would make the client
+ * impossible to construct. Set allowInsecureHttp to skip the check.
*/
private static function warnIfInsecureHttp(string $url): void
{
@@ -131,10 +136,9 @@ class Client
if ($scheme === 'http' && $host !== null && $host !== false) {
$host = \trim($host, '[]');
if (!\in_array($host, ['localhost', '127.0.0.1', '::1'], true)) {
- \trigger_error(
+ \error_log(
"OpenWA Client: baseUrl uses an insecure http:// URL (host: {$host}). "
- . 'The API key will be sent in cleartext. Use https:// in production.',
- \E_USER_WARNING
+ . 'The API key will be sent in cleartext. Use https:// in production.'
);
}
}
diff --git a/sdk/php/tests/ClientTest.php b/sdk/php/tests/ClientTest.php
index a3d3c30a9..08152a96c 100644
--- a/sdk/php/tests/ClientTest.php
+++ b/sdk/php/tests/ClientTest.php
@@ -25,6 +25,29 @@ class ClientTest extends TestCase
new \OpenWA\Client(['baseUrl' => 'https://x', 'apiKey' => '']);
}
+ public function testInsecureHttpBaseUrlLogsInsteadOfRaisingAPhpError(): void
+ {
+ // Laravel, Symfony and PHPUnit's failOnWarning turn an E_USER_WARNING into an exception, so a
+ // raised warning left the client unbuildable for an http:// host on a private network.
+ $log = (string) tempnam(sys_get_temp_dir(), 'openwa');
+ $previousLog = ini_set('error_log', $log);
+ set_error_handler(static function (int $errno, string $errstr): bool {
+ throw new \ErrorException($errstr, 0, $errno);
+ });
+ try {
+ new \OpenWA\Client(['baseUrl' => 'http://openwa:2785', 'apiKey' => 'k']);
+ $this->assertStringContainsString('insecure http://', (string) file_get_contents($log));
+
+ file_put_contents($log, '');
+ new \OpenWA\Client(['baseUrl' => 'http://openwa:2785', 'apiKey' => 'k', 'allowInsecureHttp' => true]);
+ $this->assertSame('', file_get_contents($log));
+ } finally {
+ restore_error_handler();
+ ini_set('error_log', (string) $previousLog);
+ unlink($log);
+ }
+ }
+
public function testSendsApiKeyHeader(): void
{
$backend = (new MockBackend())->on(200, []);