Skip to content

Commit 5894f24

Browse files
author
fisk
committed
Fix handling of multiple cookie headers and comma values
1 parent 5f2ce4d commit 5894f24

2 files changed

Lines changed: 27 additions & 5 deletions

File tree

src/Io/ServerRequest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,13 @@ public function __construct(
5757
\parse_str($query, $this->queryParams);
5858
}
5959

60-
$this->cookies = $this->parseCookie($this->getHeaderLine('Cookie'));
60+
// Multiple cookie headers are not allowed according
61+
// to https://tools.ietf.org/html/rfc6265#section-5.4
62+
$cookieHeaders = $this->getHeader("Cookie");
63+
64+
if (count($cookieHeaders) === 1) {
65+
$this->cookies = $this->parseCookie($cookieHeaders[0]);
66+
}
6167
}
6268

6369
public function getServerParams()
@@ -146,10 +152,7 @@ public function withoutAttribute($name)
146152
*/
147153
private function parseCookie($cookie)
148154
{
149-
// PSR-7 `getHeaderLine('Cookie')` will return multiple
150-
// cookie header comma-seperated. Multiple cookie headers
151-
// are not allowed according to https://tools.ietf.org/html/rfc6265#section-5.4
152-
if ($cookie === '' || \strpos($cookie, ',') !== false) {
155+
if ($cookie === '') {
153156
return array();
154157
}
155158

tests/StreamingServerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2790,6 +2790,25 @@ public function testRequestCookieWithSeparatorWillBeAddedToServerRequest()
27902790
$this->assertEquals(array('hello' => 'world', 'test' => 'abc'), $requestValidation->getCookieParams());
27912791
}
27922792

2793+
public function testRequestCookieWithCommaValueWillBeAddedToServerRequest() {
2794+
$requestValidation = null;
2795+
$server = new StreamingServer(function (ServerRequestInterface $request) use (&$requestValidation) {
2796+
$requestValidation = $request;
2797+
});
2798+
2799+
$server->listen($this->socket);
2800+
$this->socket->emit('connection', array($this->connection));
2801+
2802+
$data = "GET / HTTP/1.1\r\n";
2803+
$data .= "Host: example.com:80\r\n";
2804+
$data .= "Connection: close\r\n";
2805+
$data .= "Cookie: test=abc,def; hello=world\r\n";
2806+
$data .= "\r\n";
2807+
2808+
$this->connection->emit('data', array($data));
2809+
$this->assertEquals(array('test' => 'abc,def', 'hello' => 'world'), $requestValidation->getCookieParams());
2810+
}
2811+
27932812
private function createGetRequest()
27942813
{
27952814
$data = "GET / HTTP/1.1\r\n";

0 commit comments

Comments
 (0)