Skip to content

Commit 0500ad0

Browse files
committed
fix: do not decode cookie names
Per CVE-2020-7070, reported to the PHP project at https://bugs.php.net/bug.php?id=79699, cookie names SHOULD NOT be urldecoded. The cookie specification is not entirely clear on whether _names_ should be urldecoded, though it does indicate _values_ should be. The behavior of PHP until that patch was made, and, in fact, several other languages/frameworks, such as Rack, was to urldecode the names as well. However, this can lead to security implications when urlencoding is used to create key names with cookie prefixes such as `__Host-` and `__Secure-`, which are supposed to be controlled entirely by the browser, and which are only to be set when specific conditions are met. This patch modifies the logic used to split cookie pairs (i.e., cookie name/value pairs). Previously, it passed both values to `urldecode`; now it only passes the cookie value.
1 parent 733af78 commit 0500ad0

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/Dflydev/FigCookies/StringUtil.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
namespace Dflydev\FigCookies;
66

77
use function array_filter;
8-
use function array_map;
98
use function assert;
109
use function explode;
1110
use function is_array;
1211
use function preg_split;
12+
use function urldecode;
1313

1414
class StringUtil
1515
{
@@ -27,8 +27,8 @@ public static function splitOnAttributeDelimiter(string $string) : array
2727
public static function splitCookiePair(string $string) : array
2828
{
2929
$pairParts = explode('=', $string, 2);
30-
$pairParts[1] = $pairParts[1] ?? '';
30+
$pairParts[1] = urldecode($pairParts[1]) ?? '';
3131

32-
return array_map('urldecode', $pairParts);
32+
return $pairParts;
3333
}
3434
}

tests/Dflydev/FigCookies/CookieTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function provideParsesOneFromCookieStringData() : array
5252
{
5353
return [
5454
['someCookie=something', 'someCookie', 'something'],
55-
['hello%3Dworld=how%22are%27you', 'hello=world', 'how"are\'you'],
55+
['hello%3Dworld=how%22are%27you', 'hello%3Dworld', 'how"are\'you'],
5656
['empty=', 'empty', ''],
5757
];
5858
}

0 commit comments

Comments
 (0)