Skip to content

Commit 0952740

Browse files
committed
Psr18Client ignore invalid HTTP headers
1 parent 9f9dd96 commit 0952740

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Psr18Client.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ public function sendRequest(RequestInterface $request): ResponseInterface
100100

101101
foreach ($response->getHeaders(false) as $name => $values) {
102102
foreach ($values as $value) {
103-
$psrResponse = $psrResponse->withAddedHeader($name, $value);
103+
try {
104+
$psrResponse = $psrResponse->withAddedHeader($name, $value);
105+
} catch (\InvalidArgumentException $e) {
106+
// ignore invalid header
107+
}
104108
}
105109
}
106110

Tests/Psr18ClientTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313

1414
use Nyholm\Psr7\Factory\Psr17Factory;
1515
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\HttpClient\MockHttpClient;
1617
use Symfony\Component\HttpClient\NativeHttpClient;
1718
use Symfony\Component\HttpClient\Psr18Client;
1819
use Symfony\Component\HttpClient\Psr18NetworkException;
1920
use Symfony\Component\HttpClient\Psr18RequestException;
21+
use Symfony\Component\HttpClient\Response\MockResponse;
2022
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
2123

2224
class Psr18ClientTest extends TestCase
@@ -81,4 +83,22 @@ public function test404()
8183
$response = $client->sendRequest($factory->createRequest('GET', 'http://localhost:8057/404'));
8284
$this->assertSame(404, $response->getStatusCode());
8385
}
86+
87+
public function testInvalidHeaderResponse()
88+
{
89+
$responseHeaders = [
90+
// space in header name not allowed in RFC 7230
91+
' X-XSS-Protection' => '0',
92+
'Cache-Control' => 'no-cache',
93+
];
94+
$response = new MockResponse('body', ['response_headers' => $responseHeaders]);
95+
$this->assertArrayHasKey(' x-xss-protection', $response->getHeaders());
96+
97+
$client = new Psr18Client(new MockHttpClient($response));
98+
$request = $client->createRequest('POST', 'http://localhost:8057/post')
99+
->withBody($client->createStream('foo=0123456789'));
100+
101+
$resultResponse = $client->sendRequest($request);
102+
$this->assertCount(1, $resultResponse->getHeaders());
103+
}
84104
}

0 commit comments

Comments
 (0)