Skip to content

[WebProfiler] Error "Deprecated: str_contains(): Passing null to parameter #1 ($haystack) of type string ..." in dev environment with no debug #50977

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

Closed
alexbuyanow opened this issue Jul 14, 2023 · 3 comments

Comments

@alexbuyanow
Copy link
Contributor

Symfony version(s) affected

6.*

Description

PHP version: >=8.1
Symfony version: (probably) any with dependes on PHP>=8.1

Sent HTTP request to my service with no header Content-type. Testing in dev (or test) mode with debug disabled.

And have error Deprecated: str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated in /var/www/vendor/symfony/web-profiler-bundle/EventListener/WebDebugToolbarListener.php on line 124

How to reproduce

  • Set environments:
APP_ENV=dev
APP_DEBUG=false
  • WebProfilerBundle enabled and collects data
  • Send HTTP request to any endpoint with no Content-type header

Possible Solution

Its working for me

--- a/vendor/symfony/web-profiler-bundle/EventListener/WebDebugToolbarListener.php	
+++ b/vendor/symfony/web-profiler-bundle/EventListener/WebDebugToolbarListener.php
@@ -121,7 +121,7 @@
         if (self::DISABLED === $this->mode
             || !$response->headers->has('X-Debug-Token')
             || $response->isRedirection()
-            || ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type'), 'html'))
+            || ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type') ?? '', 'html'))
             || 'html' !== $request->getRequestFormat()
             || false !== stripos($response->headers->get('Content-Disposition', ''), 'attachment;')
         ) {

But unfortunly not working

            || ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type', ''), 'html'))

Additional Context

No response

@derrabus
Copy link
Member

|| ($response->headers->has('Content-Type') && !str_contains($response->headers->get('Content-Type') ?? '', 'html'))

I don't understand why this fixes an issue for you. If has() returns true, why would get() return null?

Can you please provide a reproducer for your bug?

@HypeMC
Copy link
Contributor

HypeMC commented Jul 14, 2023

I don't understand why this fixes an issue for you. If has() returns true, why would get() return null?

@derrabus Actually, has() uses array_key_exists() and the $values argument of set() is allowed to be null, so this could happen.

@derrabus
Copy link
Member

I see. Someone send a PR please.

nicolas-grekas added a commit that referenced this issue Jul 19, 2023
…t null in dev environment with no debug (alexbuyanow)

This PR was squashed before being merged into the 5.4 branch.

Discussion
----------

[WebProfilerBundle] Fix error in case of 'Content-Type' set null in dev environment with no debug

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #50977
| License       | MIT
| Doc PR        | -

Commits
-------

6faed31 [WebProfilerBundle] Fix error in case of 'Content-Type' set null in dev environment with no debug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants