Skip to content

Commit

Permalink
bug #50226 [HttpClient] Ensure HttplugClient ignores invalid HTTP hea…
Browse files Browse the repository at this point in the history
…ders (nicolas-grekas)

This PR was merged into the 5.4 branch.

Discussion
----------

[HttpClient] Ensure HttplugClient ignores invalid HTTP headers

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

Something we forgot in #47415

Commits
-------

f702e66 [HttpClient] Ensure HttplugClient ignores invalid HTTP headers
  • Loading branch information
nicolas-grekas committed May 3, 2023
2 parents 3683d73 + f702e66 commit 1d52937
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
},
"config": {
"allow-plugins": {
"php-http/discovery": false,
"symfony/runtime": true
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ public function createPsr7Response(ResponseInterface $response, bool $buffer = f

foreach ($response->getHeaders(false) as $name => $values) {
foreach ($values as $value) {
$psrResponse = $psrResponse->withAddedHeader($name, $value);
try {
$psrResponse = $psrResponse->withAddedHeader($name, $value);
} catch (\InvalidArgumentException $e) {
// ignore invalid header
}
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttplugClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,22 @@ function (\Exception $exception) use ($errorMessage, &$failureCallableCalled, $c
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('OK', (string) $response->getBody());
}

public function testInvalidHeaderResponse()
{
$responseHeaders = [
// space in header name not allowed in RFC 7230
' X-XSS-Protection' => '0',
'Cache-Control' => 'no-cache',
];
$response = new MockResponse('body', ['response_headers' => $responseHeaders]);
$this->assertArrayHasKey(' x-xss-protection', $response->getHeaders());

$client = new HttplugClient(new MockHttpClient($response));
$request = $client->createRequest('POST', 'http://localhost:8057/post')
->withBody($client->createStream('foo=0123456789'));

$resultResponse = $client->sendRequest($request);
$this->assertCount(1, $resultResponse->getHeaders());
}
}

0 comments on commit 1d52937

Please sign in to comment.