-
Notifications
You must be signed in to change notification settings - Fork 34
fix: preserve escaped characters in fork-curl gzip payloads #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "posthog-php": patch | ||
| --- | ||
|
|
||
| Preserve escaped characters in gzip-compressed fork-curl event payloads. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| <?php | ||
|
|
||
| namespace PostHog\Test; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\TestCase; | ||
| use PostHog\Consumer\ForkCurl; | ||
|
|
||
| class ForkCurlCompressionTest extends TestCase | ||
| { | ||
| public static function compressionCases(): array | ||
| { | ||
| return ['plain JSON' => [false], 'gzip' => [true]]; | ||
| } | ||
|
|
||
| #[DataProvider('compressionCases')] | ||
| public function testEscapedCharactersSurviveDelivery(bool $compressed): void | ||
| { | ||
| $server = new LocalHttpServer(); | ||
| try { | ||
| $consumer = new ForkCurl('test-key', [ | ||
| 'host' => $server->address(), | ||
| 'ssl' => false, | ||
| 'debug' => true, | ||
| 'compress_request' => $compressed, | ||
| ]); | ||
| $message = [ | ||
| 'event' => 'quoted "event" with an apostrophe\'', | ||
| 'distinct_id' => 'test-user', | ||
| 'properties' => [ | ||
| 'controls' => "line one\nline two\tvalue\rreturn", | ||
| 'literal_backslashes' => '\\n \\t C:\\path\\file', | ||
| 'format_string' => '%s %b %n', | ||
| 'unicode' => 'café 🌍', | ||
| ], | ||
| ]; | ||
|
|
||
| self::assertTrue($consumer->flushBatch([$message])); | ||
| $requests = $server->requests(); | ||
| self::assertCount(1, $requests); | ||
| self::assertSame('POST /batch/ HTTP/1.1', $requests[0]['requestLine']); | ||
| self::assertSame($compressed ? 'gzip' : null, $requests[0]['headers']['content-encoding'] ?? null); | ||
| self::assertSame( | ||
| ['batch' => [$message], 'api_key' => 'test-key'], | ||
| json_decode($requests[0]['body'], true, 512, JSON_THROW_ON_ERROR) | ||
| ); | ||
| } finally { | ||
| $server->stop(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php | ||
|
|
||
| namespace PostHog\Test; | ||
|
|
||
| use RuntimeException; | ||
|
|
||
| final class LocalHttpServer | ||
| { | ||
| private $process; | ||
| private string $requestsFile; | ||
| private string $errorsFile; | ||
| private string $address; | ||
|
|
||
| public function __construct(array $responses = []) | ||
| { | ||
| $this->requestsFile = tempnam(sys_get_temp_dir(), 'posthog-requests-'); | ||
| $this->errorsFile = tempnam(sys_get_temp_dir(), 'posthog-server-'); | ||
| $this->process = proc_open( | ||
| [PHP_BINARY, __DIR__ . '/fixtures/http-server.php', $this->requestsFile, json_encode($responses)], | ||
| [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['file', $this->errorsFile, 'a']], | ||
| $pipes | ||
| ); | ||
| if (!is_resource($this->process)) { | ||
| $this->stop(); | ||
| throw new RuntimeException('Could not start local HTTP server'); | ||
| } | ||
| fclose($pipes[0]); | ||
| stream_set_timeout($pipes[1], 5); | ||
| $this->address = trim(fgets($pipes[1]) ?: ''); | ||
| fclose($pipes[1]); | ||
| if ($this->address === '') { | ||
| $error = file_get_contents($this->errorsFile); | ||
| $this->stop(); | ||
| throw new RuntimeException('Local HTTP server did not become ready: ' . $error); | ||
| } | ||
| } | ||
|
|
||
| public function address(): string | ||
| { | ||
| return $this->address; | ||
| } | ||
|
|
||
| public function requests(): array | ||
| { | ||
| return array_map( | ||
| static fn(string $line): array => json_decode($line, true, 512, JSON_THROW_ON_ERROR), | ||
| file($this->requestsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) | ||
| ); | ||
| } | ||
|
|
||
| public function stop(): void | ||
| { | ||
| if (is_resource($this->process)) { | ||
| proc_terminate($this->process); | ||
| proc_close($this->process); | ||
| } | ||
| foreach ([$this->requestsFile, $this->errorsFile] as $file) { | ||
| if (is_file($file)) { | ||
| unlink($file); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public function __destruct() | ||
| { | ||
| $this->stop(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
|
|
||
| $server = stream_socket_server('tcp://127.0.0.1:0', $errno, $error); | ||
| if ($server === false) { | ||
| throw new RuntimeException($error, $errno); | ||
| } | ||
| $responses = json_decode($argv[2], true, 512, JSON_THROW_ON_ERROR); | ||
| echo stream_socket_get_name($server, false) . "\n"; | ||
| flush(); | ||
|
|
||
| while ($connection = stream_socket_accept($server, 30)) { | ||
| stream_set_timeout($connection, 5); | ||
| $requestLine = trim(fgets($connection)); | ||
| $headers = []; | ||
| while (($line = fgets($connection)) !== false && trim($line) !== '') { | ||
| [$name, $value] = explode(':', $line, 2); | ||
| $headers[strtolower($name)] = trim($value); | ||
| } | ||
| if (strtolower($headers['expect'] ?? '') === '100-continue') { | ||
| fwrite($connection, "HTTP/1.1 100 Continue\r\n\r\n"); | ||
| } | ||
| $length = (int) ($headers['content-length'] ?? 0); | ||
| $body = ''; | ||
| while (strlen($body) < $length) { | ||
| $chunk = fread($connection, $length - strlen($body)); | ||
| if ($chunk === false || $chunk === '') { | ||
| throw new RuntimeException('Incomplete request body'); | ||
| } | ||
| $body .= $chunk; | ||
| } | ||
| if (($headers['content-encoding'] ?? '') === 'gzip') { | ||
| $body = gzdecode($body); | ||
| } | ||
| file_put_contents($argv[1], json_encode([ | ||
| 'requestLine' => $requestLine, | ||
| 'headers' => $headers, | ||
| 'body' => $body, | ||
| ], JSON_THROW_ON_ERROR) . "\n", FILE_APPEND); | ||
|
|
||
| $response = array_shift($responses) ?? []; | ||
| $status = $response['status'] ?? 200; | ||
| $body = $response['body'] ?? '{"status":1}'; | ||
| $headers = ''; | ||
| foreach ($response['headers'] ?? [] as $name => $value) { | ||
| $headers .= "$name: $value\r\n"; | ||
| } | ||
| $wire = "HTTP/1.1 $status Response\r\n" . $headers | ||
| . 'Content-Length: ' . strlen($body) . "\r\nConnection: close\r\n\r\n" . $body; | ||
| while ($wire !== '') { | ||
| $written = fwrite($connection, $wire); | ||
| if ($written === false || $written === 0) { | ||
| break; | ||
| } | ||
| $wire = substr($wire, $written); | ||
| } | ||
| fclose($connection); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.