Skip to content

[VarDumper] Wrong dumper detection for Accept: */* requests #62787

@alexmerlin

Description

@alexmerlin

Symfony version(s) affected

=7.4.0

Description

Calling dd() or dump() in a HTTP request with Accept: */* will not output $vars, even though */* should accept any media type.

Instead of the HtmlDumper VarDumper incorrectly attributes the request to the CliDumper, therefore the dumped data is sent to the wrong output stream.

How to reproduce

Just added a dd() call to public/index.php:

<?php

use App\Kernel;

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

dd();

return function (array $context) {
    return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};

Possible Solution

The logic is getting too complex for a one-liner, it should be moved to a separate method.
The new method could look something like this:

    private function getDumper(): DataDumperInterface
    {
        if (\array_key_exists('HTTP_ACCEPT', $_SERVER)) {
            if (str_contains($_SERVER['HTTP_ACCEPT'], 'html') || str_contains($_SERVER['HTTP_ACCEPT'], '*/*')) {
                return new HtmlDumper();
            }
        } elseif (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
            return new HtmlDumper();
        } else {
            return new CliDumper();
        }
    }

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions