Skip to content

Commit

Permalink
[1.x] Fixes message and options html output not escaped (#16)
Browse files Browse the repository at this point in the history
* Fixes message and options html output not escaped

* Fixes style
  • Loading branch information
nunomaduro authored Nov 7, 2023
1 parent 5a5e0c9 commit 8e78f27
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/Printers/CliPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ protected function messageHtml(string $message): string
return '<span class="text-gray">No message.</span>';
}

$message = htmlspecialchars($message);

return "<span>$message</span>";
}

Expand Down Expand Up @@ -204,7 +206,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_string($value) ? e($value) : var_export($value, true))
->map(fn (mixed $value) => is_string($value) ? $value : var_export($value, true))
->map(fn (string $value) => htmlspecialchars($value))
->map(fn (string $value, string|int $key) => is_string($key) ? "$key: $value" : $value)
->map(fn (string $value) => "<span class=\"font-bold\">$value</span>")
->implode('');
Expand Down
77 changes: 72 additions & 5 deletions tests/Unit/CliPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,42 @@
);
});

test('escaping message', function () {
$output = output([
'message' => '<div class=3D"gmail-adL" style=3D"box-sizing:border-box">escaping message</div>',
'level_name' => 'info',
'datetime' => '2021-01-01 00:00:00',
'context' => [
'__pail' => [
'origin' => [
'type' => 'http',
'method' => 'GET',
'path' => '/logs',
'auth_id' => null,
'auth_email' => null,
],
],
],
], true);

expect($output)->toBe(<<<'EOF'
┌ 2024-01-01 03:04:05 INFO ───────────────────────
│ <div class=3D"gmail-adL" style=3D"box-sizing:border-box">escaping message</div>
│ 1. app/MyClass.php:12
│ 2. app/MyClass.php:34
└──────────────────── GET: /logs • Auth ID: guest

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' => '<div class=3D"gmail-adL" style=3D"box-sizing:border-box"></div>',
'html' => '<div class=3D"gmail-adL" style=3D"box-sizing:border-box">escaping html options</div>',
'__pail' => [
'origin' => [
'type' => 'http',
Expand All @@ -131,12 +160,50 @@
],
],
],
]);
], true);

expect($output)->toBe(<<<'EOF'
┌ 2024-01-01 03:04:05 INFO ───────────────────────
│ Context that contains html
│ 1. app/MyClass.php:12
│ 2. app/MyClass.php:34
└ GET: /logs • Auth ID: guest • html: <div class=3D"gmail-adL" style=3D"box-sizing:border-box">escaping html options</div>

EOF
);
});

test('escaping html arrayable options', function () {
$output = output([
'message' => 'Context that contains html',
'level_name' => 'info',
'datetime' => '2021-01-01 00:00:00',
'context' => [
'html' => [
'first' => '<span class=3D"gmail-adL">first</span>',
'second' => [
'a' => '<span class=3D"gmail-adL">a</span>',
'b' => '<span class=3D"gmail-adL">b</span>',
],
],
'__pail' => [
'origin' => [
'type' => 'http',
'method' => 'GET',
'path' => '/logs',
'auth_id' => null,
'auth_email' => null,
],
],
],
], true);

expect($output)->toBe(<<<'EOF'
┌ 03:04:05 INFO ─────────────────────────────────┐
│ Context that contains html │
└ GET: /logs • Auth ID: guest • html: <div class=3D"gmail-adL" style=3D"box-sizing:border-box"></div> ┘
┌ 2024-01-01 03:04:05 INFO ───────────────────────
│ Context that contains html
│ 1. app/MyClass.php:12
│ 2. app/MyClass.php:34
└ GET: /logs • Auth ID: guest • html: array ( 'first' => '<span class=3D"gmail-adL">first</span>', 'second' => array ( 'a' => '<span class=3D"gmail-adL">a</span>', 'b' => '<span class=3D"gmail-adL">b</span>', ), )

EOF
);
Expand Down

0 comments on commit 8e78f27

Please sign in to comment.