Skip to content

Commit

Permalink
fix: do not decode cookie names
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weierophinney committed Dec 3, 2020
1 parent 733af78 commit 0500ad0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Dflydev/FigCookies/StringUtil.php
Expand Up @@ -5,11 +5,11 @@
namespace Dflydev\FigCookies;

use function array_filter;
use function array_map;
use function assert;
use function explode;
use function is_array;
use function preg_split;
use function urldecode;

class StringUtil
{
Expand All @@ -27,8 +27,8 @@ public static function splitOnAttributeDelimiter(string $string) : array
public static function splitCookiePair(string $string) : array
{
$pairParts = explode('=', $string, 2);
$pairParts[1] = $pairParts[1] ?? '';
$pairParts[1] = urldecode($pairParts[1]) ?? '';

return array_map('urldecode', $pairParts);
return $pairParts;
}
}
2 changes: 1 addition & 1 deletion tests/Dflydev/FigCookies/CookieTest.php
Expand Up @@ -52,7 +52,7 @@ public function provideParsesOneFromCookieStringData() : array
{
return [
['someCookie=something', 'someCookie', 'something'],
['hello%3Dworld=how%22are%27you', 'hello=world', 'how"are\'you'],
['hello%3Dworld=how%22are%27you', 'hello%3Dworld', 'how"are\'you'],
['empty=', 'empty', ''],
];
}
Expand Down

0 comments on commit 0500ad0

Please sign in to comment.