Skip to content

Commit f405d3a

Browse files
bug #60547 [HttpFoundation] Fixed 'Via' header regex (thecaliskan)
This PR was merged into the 6.4 branch. Discussion ---------- [HttpFoundation] Fixed 'Via' header regex | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT #### 📝 Purpose This MR updates the regular expression used to extract the HTTP protocol version from the `Via` header in the `Request::getProtocolVersion()` method. #### 🎯 Summary of Changes **Old regex:** ```php ~^(HTTP/)?([1-9]\.[0-9]) ~ ``` This pattern failed to match valid values like HTTP/1.1 or 2.0 when there was no trailing space. As a result, values passed from proxies (e.g., via proxy_set_header Via $server_protocol;) were not detected correctly. New regex: ```php ~^(HTTP/)?([1-9]\.[0-9])\b~ ``` #### ✅ Benefits - Replaces dependency on a trailing space with `\b` (word boundary), allowing the regex to match both space-terminated and non-space-terminated inputs. - Correctly handles common `Via` header formats such as: - `HTTP/1.1` - `2.0` - `HTTP/2.0 nginx-proxy` - `1.1 custom-hop` - Ensures compatibility with reverse proxy configurations (e.g., Nginx, Traefik) where the `Via` header may vary in format. - Improves robustness and reliability of HTTP version detection in proxied environments. Commits ------- 4bfb8da fixed Via regex
2 parents de0773c + 4bfb8da commit f405d3a

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,7 @@ public function isMethodCacheable(): bool
14661466
public function getProtocolVersion(): ?string
14671467
{
14681468
if ($this->isFromTrustedProxy()) {
1469-
preg_match('~^(HTTP/)?([1-9]\.[0-9]) ~', $this->headers->get('Via') ?? '', $matches);
1469+
preg_match('~^(HTTP/)?([1-9]\.[0-9])\b~', $this->headers->get('Via') ?? '', $matches);
14701470

14711471
if ($matches) {
14721472
return 'HTTP/'.$matches[2];

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2402,6 +2402,8 @@ public static function protocolVersionProvider()
24022402
'trusted with via and protocol name' => ['HTTP/2.0', true, 'HTTP/1.0 fred, HTTP/1.1 nowhere.com (Apache/1.1)', 'HTTP/1.0'],
24032403
'trusted with broken via' => ['HTTP/2.0', true, 'HTTP/1^0 foo', 'HTTP/2.0'],
24042404
'trusted with partially-broken via' => ['HTTP/2.0', true, '1.0 fred, foo', 'HTTP/1.0'],
2405+
'trusted with simple via' => ['HTTP/2.0', true, 'HTTP/1.0', 'HTTP/1.0'],
2406+
'trusted with only version via' => ['HTTP/2.0', true, '1.0', 'HTTP/1.0'],
24052407
];
24062408
}
24072409

0 commit comments

Comments
 (0)