Skip to content

Commit

Permalink
Revert skipping invalid cookies
Browse files Browse the repository at this point in the history
Returning some cookies might lead to incorrect cookie values if the header is malformed, so reverting to returning an empty array.
  • Loading branch information
trowski committed Dec 29, 2023
1 parent a9bfd3a commit a19f480
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/Cookie/RequestCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public static function fromHeader(string $string): array
$cookies = \explode(";", $string);
$result = [];

foreach ($cookies as $cookie) {
try {
try {
foreach ($cookies as $cookie) {
// Ignore zero-length cookie.
if (\trim($cookie) === '') {
continue;
Expand All @@ -39,21 +39,21 @@ public static function fromHeader(string $string): array
$parts = \explode('=', $cookie, 2);

if (\count($parts) !== 2) {
continue;
return [];
}

[$name, $value] = $parts;

$name = \trim($name);
if ($name === '') {
continue;
return [];
}

// We can safely trim quotes, as they're not allowed within cookie values
$result[] = new self($name, \trim($value, " \t\""));
} catch (InvalidCookieException) {
// Skip invalid cookie.
}
} catch (InvalidCookieException) {
return [];
}

return $result;
Expand Down
6 changes: 3 additions & 3 deletions test/Cookie/RequestCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public function testParsing()
$this->assertEquals([new RequestCookie("a", "1"), new RequestCookie("b", "2")], RequestCookie::fromHeader("a=1; b=2"));
$this->assertEquals([new RequestCookie("a", "1"), new RequestCookie("b", "2")], RequestCookie::fromHeader("a=1 ;b=2"));
$this->assertEquals([new RequestCookie("a", "1"), new RequestCookie("b", "-2")], RequestCookie::fromHeader("a=1; b = -2"));
$this->assertEquals([new RequestCookie("a", "1")], RequestCookie::fromHeader("a=1; b=2,2"));
$this->assertEquals([new RequestCookie("a", "1")], RequestCookie::fromHeader("a=1; b=2 2"));
$this->assertEquals([new RequestCookie("a", "1")], RequestCookie::fromHeader("a=1; b"));
$this->assertEquals([], RequestCookie::fromHeader("a=1; b=2,2"));
$this->assertEquals([], RequestCookie::fromHeader("a=1; b=2 2"));
$this->assertEquals([], RequestCookie::fromHeader("a=1; b"));
}

/**
Expand Down

0 comments on commit a19f480

Please sign in to comment.