Skip to content

Commit

Permalink
prevent ESC char from ex data
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Feb 9, 2025
1 parent b9e3d01 commit d413020
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
32 changes: 12 additions & 20 deletions src/ExceptionRenderer/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ class Console extends RendererAbstract
private const COLOR_BRIGHT_GREEN = "\e[92m";

/**
* @param non-empty-list<self::FORMAT_*|self::COLOR_*|self::BACKGROUND_COLOR_*> $formats
* @param list<self::FORMAT_*|self::COLOR_*|self::BACKGROUND_COLOR_*> $formats
*/
private function text(string $text, array $formats): string
private function text(string $text, array $formats = []): string
{
assert(!str_contains($text, "\e["));
$text = str_replace("\e", '', $text);

return implode('', $formats) . $text . self::RESET;
return $formats === []
? $text
: implode('', $formats) . $text . self::RESET;
}

private function optimizeText(string $value): string
Expand All @@ -51,20 +53,10 @@ protected function processHeader(): void
$title = $this->getExceptionTitle();
$class = get_class($this->exception);

$tokens = [
'{TITLE}' => $title,
'{CLASS}' => $class,
'{MESSAGE}' => $this->getExceptionMessage(),
'{CODE}' => $this->exception->getCode() ? ' [code: ' . $this->exception->getCode() . ']' : '',
];

$this->output .= $this->replaceTokens(
$this->text('--[ {TITLE} ]', [self::FORMAT_BOLD, self::BACKGROUND_COLOR_RED]) . "\n"
. '{CLASS}: '
. $this->text('{MESSAGE}', [self::FORMAT_BOLD, self::COLOR_BLACK]) . ' '
. $this->text('{CODE}', [self::COLOR_RED]),
$tokens
);
$this->output .= $this->text('--[ ' . $title . ' ]', [self::FORMAT_BOLD, self::BACKGROUND_COLOR_RED]) . "\n"
. $this->text($class . ': ')
. $this->text($this->getExceptionMessage(), [self::FORMAT_BOLD, self::COLOR_BLACK])
. $this->text($this->exception->getCode() ? ' [code: ' . $this->exception->getCode() . ']' : '', [self::COLOR_RED]);
}

#[\Override]
Expand Down Expand Up @@ -132,8 +124,8 @@ protected function processStackTraceInternal(): void
$functionColor = $escapeFrame ? self::COLOR_RED : self::COLOR_YELLOW;

$tokens = [
'{FILE}' => str_pad(mb_substr($call['file_rel'], -40), 40, ' ', \STR_PAD_LEFT),
'{LINE}' => str_pad($call['line'], 4, ' ', \STR_PAD_LEFT),
'{FILE}' => $this->text(str_pad(mb_substr($call['file_rel'], -40), 40, ' ', \STR_PAD_LEFT)),
'{LINE}' => $this->text(str_pad($call['line'], 4, ' ', \STR_PAD_LEFT)),
'{OBJECT}' => $call['object'] !== null ? ' - ' . $this->text($call['object_formatted'], [self::COLOR_GREEN]) : '',
'{CLASS}' => $call['class'] !== null ? $this->text($call['class_formatted'] . '::', [self::COLOR_GREEN]) : '',
'{FUNCTION}' => $call['function'] !== null ? $this->text($call['function'], [$functionColor]) : '',
Expand Down
8 changes: 7 additions & 1 deletion tests/ExceptionRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ public function testFormatConsole(): void

self::assertStringStartsWith("\e[0;", $e->getColorfulText());
self::assertStringEndsWith("\n", $e->getColorfulText());
self::assertStringNotContainsString('\e[', $e->getColorfulText());

self::assertSame(str_replace("\e", '\e', <<<"EOF"
\e[0;1;41m--[ Critical Error ]\e[0m
Expand All @@ -167,6 +166,13 @@ public function testFormatConsole(): void
/a/main.php:\e[31m 20\e[0m \e[32mAtk4\\Core\\Tests\\ExceptionRendererTest::\e[0;33mmain\e[0;33m()\e[0m
EOF), str_replace("\e", '\e', $e->getColorfulText()));

self::assertStringNotContainsString('\e[', $e->getColorfulText());

$e->setMessage("prevent\eESC");
self::assertStringNotContainsString("prevent\e", $e->getColorfulText());
self::assertStringNotContainsString("\eESC", $e->getColorfulText());
self::assertStringContainsString('preventESC', $e->getColorfulText());
}

public function testToSafeString(): void
Expand Down

0 comments on commit d413020

Please sign in to comment.