Skip to content

Commit

Permalink
Merge pull request #370 from essell/fix/laravel-context-parsing
Browse files Browse the repository at this point in the history
Fix PCRE stack limit for context parsing, omitting preg
  • Loading branch information
arukompas authored May 11, 2024
2 parents 02d5e4f + 1b76585 commit 4f60ca0
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->getJsonStringsFromFullText();

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 getJsonStringsFromFullText(): 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 4f60ca0

Please sign in to comment.