Skip to content

Commit

Permalink
Psr18Client ignore invalid HTTP headers
Browse files Browse the repository at this point in the history
  • Loading branch information
nuryagdym committed Sep 4, 2022
1 parent 9f9dd96 commit 0952740
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Psr18Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ public function sendRequest(RequestInterface $request): ResponseInterface

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
20 changes: 20 additions & 0 deletions Tests/Psr18ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@

use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpClient\Psr18NetworkException;
use Symfony\Component\HttpClient\Psr18RequestException;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\Test\TestHttpServer;

class Psr18ClientTest extends TestCase
Expand Down Expand Up @@ -81,4 +83,22 @@ public function test404()
$response = $client->sendRequest($factory->createRequest('GET', 'http://localhost:8057/404'));
$this->assertSame(404, $response->getStatusCode());
}

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 Psr18Client(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 0952740

Please sign in to comment.