Skip to content

[ErrorHandler] Don't format binary strings #53006

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

Merged
merged 1 commit into from
Jan 9, 2024
Merged

[ErrorHandler] Don't format binary strings #53006

merged 1 commit into from
Jan 9, 2024

Conversation

aleho
Copy link
Contributor

@aleho aleho commented Dec 12, 2023

Fixes #53005

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #53005
License MIT

This is an attempt to fix memory exhaustion when a binary string is part of a stack trace.

@derrabus
Copy link
Member

Please note that we usually don't merge changes without a test that covers them.

@aleho
Copy link
Contributor Author

aleho commented Dec 12, 2023

I can try to add a test case for that.

Would loading a local, binary file into a string in a test-case be OK?

(Asserting memory exhaustion in var_dump to me seems a bit hard to test ;))

@aleho
Copy link
Contributor Author

aleho commented Dec 13, 2023

I just had a look at the tests and figured it might indeed be hard to test for var_export not being called with a binary string. I hope just a hard-coded check for the replacement string is OK.

@@ -98,7 +98,11 @@ public function formatArgs(array $args): string
} elseif ('resource' === $item[0]) {
$formattedValue = '<em>resource</em>';
} else {
$formattedValue = str_replace("\n", '', htmlspecialchars(var_export($item[1], true), \ENT_COMPAT | \ENT_SUBSTITUTE, $this->charset));
if (preg_match('//u', $item[1])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when ppl don't use the UTF-8 charset, this might lead to false positives
what about checking using preg_match('/[^\x00\x07-\x0d\x1B\x20-\xff]/', $value) instead?

also, please unindent the if else by merging it with the surrouding one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, please unindent the if else by merging it with the surrouding one

The parent if checks the value of $item[0] exclusively. The merged else-if would then check $item[1]. I explicitly wanted to reduce cognitive load here (having X-branches of an if checking the same variable only to have the last one do something different is not very readable in my opinion). If you insist I'll still merge the checks though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you insist I'll still merge the checks though.

We do. It's our code style.

Comment on lines 71 to 72
unset($binaryData);
$this->assertNotNull($exception, 'Could not catch RuntimeException to check for binary string replacement');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be inside a finally block?

Calling var_export on a binary string easily causes memory exhaustion if
it's called with even a small image.

Fixes #53005
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied my patch from #53005 (comment)

Good catch on the ini setting.

@nicolas-grekas
Copy link
Member

Thank you @aleho.

@nicolas-grekas nicolas-grekas merged commit e0f8877 into symfony:5.4 Jan 9, 2024
@aleho
Copy link
Contributor Author

aleho commented Jan 9, 2024

I applied my patch from #53005 (comment)

FYI, originally I wanted to throw the exception to simulate the conditions where I ran into the problem. Finding out the ini setting is what made the difference I guess your changes make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants