Skip to content

Commit

Permalink
Fixs PCRE stack limit for context parsing, omitting preg
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Carlson committed May 10, 2024
1 parent 02d5e4f commit 05d0936
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/Logs/LaravelLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,17 @@ protected static function regexPattern(): string

protected function extractContextsFromFullText(): void
{
// The regex pattern to find JSON strings.
$pattern = '/(\{(?:[^{}]|(?R))*\}|\[(?:[^\[\]]|(?R))*\])/';
$contexts = [];

// Find matches.
preg_match_all($pattern, $this->text, $matches);
$json_strings = $this->getJsonStrings();

if (! isset($matches[0])) {
if (empty($json_strings)) {
return;
}

// Loop through the matches.
foreach ($matches[0] as $json_string) {
foreach ($json_strings as $json_string) {
// Try to decode the JSON string. If it fails, json_last_error() will return a non-zero value.
$json_data = json_decode(trim($json_string), true);

Expand Down Expand Up @@ -181,4 +179,28 @@ protected function extractMailPreview(): void
'size_formatted' => Utils::bytesForHumans($message->getSize()),
];
}

protected function getJsonStrings(): array
{
$json = '';
$json_strings = [];
$in = 0;
foreach (str_split($this->text) as $char) {
if ($char == '{' || $char == '[') {
$in++;
}
if ($in) {
$json .= $char;
}
if ($char == '}' || $char == ']') {
$in--;
if ($in == 0) {
$json_strings[] = $json;
$json = '';
}
}
}

return $json_strings;
}
}

0 comments on commit 05d0936

Please sign in to comment.