From 9b67ba1477cf8397a6eb62ec55ff0d293075e44d Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Fri, 13 Oct 2023 13:25:10 +0200 Subject: [PATCH 1/6] Escape value --- src/Printers/CliPrinter.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Printers/CliPrinter.php b/src/Printers/CliPrinter.php index d7405b3..0e19a76 100644 --- a/src/Printers/CliPrinter.php +++ b/src/Printers/CliPrinter.php @@ -204,6 +204,7 @@ public function optionsHtml(MessageLogged $messageLogged): string return collect($options)->merge( $messageLogged->context() // @phpstan-ignore-line )->reject(fn (mixed $value, string|int $key) => is_int($key) && is_null($value)) + ->map(fn (mixed $value) => is_string($value) ? e($value): $value) ->map(fn (mixed $value) => is_string($value) ? $value : var_export($value, true)) ->map(fn (string $value, string|int $key) => is_string($key) ? "$key: $value" : $value) ->map(fn (string $value) => "$value") From ce779ef72ca217dc7db54e1b4dd9dda4f53f841c Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Fri, 13 Oct 2023 14:56:59 +0200 Subject: [PATCH 2/6] Test if html with Gmail class doesn't throw exception --- tests/Unit/CliPrinterTest.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tests/Unit/CliPrinterTest.php b/tests/Unit/CliPrinterTest.php index ce0791a..69cc59b 100644 --- a/tests/Unit/CliPrinterTest.php +++ b/tests/Unit/CliPrinterTest.php @@ -113,3 +113,31 @@ EOF, ); }); + +test('escaping html options', function () { + $output = output([ + 'message' => 'Context that contains html', + 'level_name' => 'info', + 'datetime' => '2021-01-01 00:00:00', + 'context' => [ + 'html' => '
', + '__pail' => [ + 'origin' => [ + 'type' => 'http', + 'method' => 'GET', + 'path' => '/logs', + 'auth_id' => null, + 'auth_email' => null + ], + ], + ], + ]); + + expect($output)->toBe(<<<'EOF' + ┌ 03:04:05 INFO ─────────────────────────────────┐ + │ Context that contains html │ + └ GET: /logs • Auth ID: guest • html:
┘ + + EOF + ); +}); \ No newline at end of file From 2048c0346e962e9792d93cf3ee45fe081a650a6d Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Tue, 24 Oct 2023 16:45:41 +0200 Subject: [PATCH 3/6] Escape array values --- src/Printers/CliPrinter.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Printers/CliPrinter.php b/src/Printers/CliPrinter.php index 0e19a76..795a908 100644 --- a/src/Printers/CliPrinter.php +++ b/src/Printers/CliPrinter.php @@ -204,6 +204,10 @@ public function optionsHtml(MessageLogged $messageLogged): string return collect($options)->merge( $messageLogged->context() // @phpstan-ignore-line )->reject(fn (mixed $value, string|int $key) => is_int($key) && is_null($value)) + ->map(fn (mixed $value) => is_array($value) ? + collect($value)->map(fn (mixed $value) => is_string($value) ? e($value) : var_export($value, true))->implode(', ') : + $value + ) ->map(fn (mixed $value) => is_string($value) ? e($value): $value) ->map(fn (mixed $value) => is_string($value) ? $value : var_export($value, true)) ->map(fn (string $value, string|int $key) => is_string($key) ? "$key: $value" : $value) From b34b8f6cf5e7e512a06b4f46b5cbb043d957e641 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Wed, 25 Oct 2023 08:35:33 +0200 Subject: [PATCH 4/6] Fix types --- src/Printers/CliPrinter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Printers/CliPrinter.php b/src/Printers/CliPrinter.php index 795a908..bb8c41c 100644 --- a/src/Printers/CliPrinter.php +++ b/src/Printers/CliPrinter.php @@ -204,8 +204,8 @@ public function optionsHtml(MessageLogged $messageLogged): string return collect($options)->merge( $messageLogged->context() // @phpstan-ignore-line )->reject(fn (mixed $value, string|int $key) => is_int($key) && is_null($value)) - ->map(fn (mixed $value) => is_array($value) ? - collect($value)->map(fn (mixed $value) => is_string($value) ? e($value) : var_export($value, true))->implode(', ') : + ->map(fn (string|null|array $value) => is_array($value) ? + collect($value)->map(fn (string|null $value) => is_string($value) ? e($value) : var_export($value, true))->implode(', ') : $value ) ->map(fn (mixed $value) => is_string($value) ? e($value): $value) From 23d0d6fc4724ccbbf29a1dba109666b6c6c7d8e0 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Wed, 25 Oct 2023 08:38:42 +0200 Subject: [PATCH 5/6] Change type order --- src/Printers/CliPrinter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Printers/CliPrinter.php b/src/Printers/CliPrinter.php index bb8c41c..dc8a2d0 100644 --- a/src/Printers/CliPrinter.php +++ b/src/Printers/CliPrinter.php @@ -204,7 +204,7 @@ public function optionsHtml(MessageLogged $messageLogged): string return collect($options)->merge( $messageLogged->context() // @phpstan-ignore-line )->reject(fn (mixed $value, string|int $key) => is_int($key) && is_null($value)) - ->map(fn (string|null|array $value) => is_array($value) ? + ->map(fn (array|string|null $value) => is_array($value) ? collect($value)->map(fn (string|null $value) => is_string($value) ? e($value) : var_export($value, true))->implode(', ') : $value ) From d988b3ab633a8d1307d9852c055c59d6735c4c5b Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Wed, 25 Oct 2023 08:53:59 +0200 Subject: [PATCH 6/6] Add test --- tests/Unit/CliPrinterTest.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/Unit/CliPrinterTest.php b/tests/Unit/CliPrinterTest.php index b7add12..7bec266 100644 --- a/tests/Unit/CliPrinterTest.php +++ b/tests/Unit/CliPrinterTest.php @@ -141,3 +141,33 @@ EOF ); }); + +test('escaping an array of html options', function () { + $output = output([ + 'message' => 'Context that contains html', + 'level_name' => 'info', + 'datetime' => '2021-01-01 00:00:00', + 'context' => [ + 'html' => ['
'], + '__pail' => [ + 'origin' => [ + 'type' => 'http', + 'method' => 'GET', + 'path' => '/logs', + 'auth_id' => null, + 'auth_email' => null, + ], + ], + ], + ]); + + $html = e('
'); + + expect($output)->toBe(<<