Skip to content

Commit 94188f8

Browse files
committed
fix: infer ssl option from host protocol in PostHog::init
cleanHost() strips the protocol before QueueConsumer sees the host, so QueueConsumer never detects http:// and defaults ssl to true. This causes requests to http:// hosts (e.g. localhost) to fail silently with curl error 0 as the SDK tries to connect via HTTPS. Now PostHog::init() infers ssl from the host protocol before stripping it, unless the user has explicitly set the ssl option.
1 parent 9af83a0 commit 94188f8

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

lib/PostHog.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@ public static function init(
2828
if (null === $client) {
2929
$apiKey = $apiKey ?: getenv(self::ENV_API_KEY);
3030

31+
$rawHost = null;
3132
if (array_key_exists("host", $options)) {
32-
$options["host"] = self::cleanHost($options["host"]);
33+
$rawHost = $options["host"];
34+
$options["host"] = self::cleanHost($rawHost);
3335
} else {
3436
$envHost = getenv(self::ENV_HOST) ?: null;
3537
if (null !== $envHost) {
36-
$options["host"] = self::cleanHost(getenv(self::ENV_HOST));
38+
$rawHost = $envHost;
39+
$options["host"] = self::cleanHost($rawHost);
40+
}
41+
}
42+
43+
// Infer ssl from the host protocol if the user hasn't explicitly set it
44+
if ($rawHost !== null && !array_key_exists("ssl", $options)) {
45+
if (str_starts_with($rawHost, "http://")) {
46+
$options["ssl"] = false;
47+
} elseif (str_starts_with($rawHost, "https://")) {
48+
$options["ssl"] = true;
3749
}
3850
}
3951

test/PostHogTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,75 @@ public function testInitWithEnvApiKey(): void
6363
putenv(PostHog::ENV_API_KEY);
6464
}
6565

66+
public function testInitWithHttpHostSetsSslFalse(): void
67+
{
68+
PostHog::init("random_key", ["host" => "http://localhost:8010"]);
69+
70+
$client = PostHog::getClient();
71+
$ref = new \ReflectionClass($client);
72+
$consumerProp = $ref->getProperty('consumer');
73+
$consumerProp->setAccessible(true);
74+
$consumer = $consumerProp->getValue($client);
75+
76+
$cRef = new \ReflectionClass($consumer);
77+
$httpProp = $cRef->getProperty('httpClient');
78+
$httpProp->setAccessible(true);
79+
$httpClient = $httpProp->getValue($consumer);
80+
81+
$hRef = new \ReflectionClass($httpClient);
82+
$sslProp = $hRef->getProperty('useSsl');
83+
$sslProp->setAccessible(true);
84+
85+
$this->assertFalse($sslProp->getValue($httpClient), 'HttpClient should use ssl=false for http:// hosts');
86+
}
87+
88+
public function testInitWithHttpsHostSetsSslTrue(): void
89+
{
90+
PostHog::init("random_key", ["host" => "https://app.posthog.com"]);
91+
92+
$client = PostHog::getClient();
93+
$ref = new \ReflectionClass($client);
94+
$consumerProp = $ref->getProperty('consumer');
95+
$consumerProp->setAccessible(true);
96+
$consumer = $consumerProp->getValue($client);
97+
98+
$cRef = new \ReflectionClass($consumer);
99+
$httpProp = $cRef->getProperty('httpClient');
100+
$httpProp->setAccessible(true);
101+
$httpClient = $httpProp->getValue($consumer);
102+
103+
$hRef = new \ReflectionClass($httpClient);
104+
$sslProp = $hRef->getProperty('useSsl');
105+
$sslProp->setAccessible(true);
106+
107+
$this->assertTrue($sslProp->getValue($httpClient), 'HttpClient should use ssl=true for https:// hosts');
108+
}
109+
110+
public function testInitWithEnvHttpHostSetsSslFalse(): void
111+
{
112+
putenv(PostHog::ENV_HOST . "=http://localhost:8010");
113+
PostHog::init("random_key");
114+
115+
$client = PostHog::getClient();
116+
$ref = new \ReflectionClass($client);
117+
$consumerProp = $ref->getProperty('consumer');
118+
$consumerProp->setAccessible(true);
119+
$consumer = $consumerProp->getValue($client);
120+
121+
$cRef = new \ReflectionClass($consumer);
122+
$httpProp = $cRef->getProperty('httpClient');
123+
$httpProp->setAccessible(true);
124+
$httpClient = $httpProp->getValue($consumer);
125+
126+
$hRef = new \ReflectionClass($httpClient);
127+
$sslProp = $hRef->getProperty('useSsl');
128+
$sslProp->setAccessible(true);
129+
130+
$this->assertFalse($sslProp->getValue($httpClient), 'HttpClient should use ssl=false for http:// env host');
131+
132+
putenv(PostHog::ENV_HOST);
133+
}
134+
66135
public function testInitThrowsExceptionWithNoApiKey(): void
67136
{
68137
$this->expectException(Exception::class);

0 commit comments

Comments
 (0)