Skip to content

Commit c12345d

Browse files
bug #62805 [VarDumper] Fix dumper selection for Accept: */* requests (apoca)
This PR was squashed before being merged into the 7.4 branch. Discussion ---------- [VarDumper] Fix dumper selection for Accept: */* requests | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #62787 | License | MIT ## What does this fix? Fixes incorrect dumper selection when `Accept: */*` header is present in HTTP requests, causing `dd()` and `dump()` output to disappear in browsers. ## The Problem Since version 7.4.0 (PR #58070), when an HTTP request includes `Accept: */*` header, the dumper selection logic incorrectly chooses `CliDumper` instead of `HtmlDumper`. This causes dumps to be sent to STDOUT instead of being rendered in the browser response. **Affected scenario:** ```php // Browser makes request with Accept: */* // dd(['test' => 123]) produces blank page instead of showing dump ``` Common with: - AJAX requests without explicit Accept header - API clients (Postman, Insomnia) - cURL without `-H "Accept: text/html"` - PHP-FPM environments ## Root Cause Current code only checks if Accept contains literal string `'html'`: ```php str_contains($_SERVER['HTTP_ACCEPT'], 'html') ? new HtmlDumper() : new CliDumper() ``` According to [RFC 9110 Section 12.5.1](https://www.rfc-editor.org/rfc/rfc9110.html#section-12.5.1), `*/*` means "accept any media type", including `text/html`. ## Solution Added explicit check for wildcard pattern: ```php $accept = $_SERVER['HTTP_ACCEPT'] ?? (\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? 'txt' : 'html'); $dumper = (str_contains($accept, 'html') || str_contains($accept, '*/*')) ? new HtmlDumper() : new CliDumper(); ``` **Before:** `Accept: */*` → ❌ CliDumper (blank page) **After:** `Accept: */*` → ✅ HtmlDumper (dump visible) ## Backward Compatibility ✅ Fully backward compatible - only fixes broken behavior introduced in 7.4.0 **Test coverage:** - `Accept: text/html` → HtmlDumper (unchanged) - `Accept: */*` → HtmlDumper (fixed) - `Accept: application/json` → CliDumper (unchanged) - CLI SAPI → CliDumper (unchanged) Commits ------- bc7b547 [VarDumper] Fix dumper selection for Accept: */* requests
2 parents 1f337e8 + bc7b547 commit c12345d

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
--TEST--
2+
Test dump() with "Accept: */*" uses HTML dumper
3+
--FILE--
4+
<?php
5+
putenv('NO_COLOR=1');
6+
7+
$vendor = __DIR__;
8+
while (!file_exists($vendor.'/vendor')) {
9+
$vendor = \dirname($vendor);
10+
}
11+
require $vendor.'/vendor/autoload.php';
12+
13+
$_SERVER['HTTP_ACCEPT'] = '*/*';
14+
dump('Test with wildcard');
15+
--EXPECTF--
16+
%a>Test with wildcard</%a

src/Symfony/Component/VarDumper/VarDumper.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ private static function register(): void
7676
case 'server' === $format:
7777
case $format && 'tcp' === parse_url($format, \PHP_URL_SCHEME):
7878
$host = 'server' === $format ? $_SERVER['VAR_DUMPER_SERVER'] ?? '127.0.0.1:9912' : $format;
79-
$dumper = str_contains($_SERVER['HTTP_ACCEPT'] ?? (\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? 'txt' : 'html'), 'html') ? new HtmlDumper() : new CliDumper();
79+
$accept = $_SERVER['HTTP_ACCEPT'] ?? (\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? 'txt' : 'html');
80+
$dumper = str_contains($accept, 'html') || str_contains($accept, '*/*') ? new HtmlDumper() : new CliDumper();
8081
$dumper = new ServerDumper($host, $dumper, self::getDefaultContextProviders());
8182
break;
8283
default:
83-
$dumper = str_contains($_SERVER['HTTP_ACCEPT'] ?? (\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? 'txt' : 'html'), 'html') ? new HtmlDumper() : new CliDumper();
84+
$accept = $_SERVER['HTTP_ACCEPT'] ?? (\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true) ? 'txt' : 'html');
85+
$dumper = str_contains($accept, 'html') || str_contains($accept, '*/*') ? new HtmlDumper() : new CliDumper();
8486
}
8587

8688
if (!$dumper instanceof ServerDumper) {

0 commit comments

Comments
 (0)