Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PCRE stack limit for context parsing, omitting preg #370

Merged
merged 2 commits into from
May 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading