Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 30fcd25

Browse filesBrowse files
authored
Merge pull request #1 from trvrnrth/fix-cookie-name-checks
Encode non-reserved cookie name chars only
2 parents f537db2 + 1601ccd commit 30fcd25
Copy full SHA for 30fcd25

File tree

Expand file treeCollapse file tree

1 file changed

+15
-2
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+15
-2
lines changed

‎src/Symfony/Component/HttpFoundation/Cookie.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Cookie.php
+15-2Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Cookie
2929
private $sameSite;
3030
private $secureDefault = false;
3131

32+
private static $charNameReserved = "=,; \t\r\n\013\014";
33+
3234
const SAMESITE_NONE = 'none';
3335
const SAMESITE_LAX = 'lax';
3436
const SAMESITE_STRICT = 'strict';
@@ -93,7 +95,7 @@ public function __construct(string $name, string $value = null, $expire = 0, ?st
9395
}
9496

9597
// from PHP source code
96-
if ($raw && preg_match("/[=,; \t\r\n\013\014]/", $name)) {
98+
if ($raw && preg_match('/['.self::$charNameReserved.']/', $name)) {
9799
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
98100
}
99101

@@ -141,7 +143,13 @@ public function __construct(string $name, string $value = null, $expire = 0, ?st
141143
*/
142144
public function __toString()
143145
{
144-
$str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'=';
146+
if ($this->isRaw()) {
147+
$str = $this->getName();
148+
} else {
149+
$str = preg_replace_callback('/['.self::$charNameReserved.']/', [$this, 'rawurlencodeMatchZero'], $this->getName());
150+
}
151+
152+
$str .= '=';
145153

146154
if ('' === (string) $this->getValue()) {
147155
$str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0';
@@ -176,6 +184,11 @@ public function __toString()
176184
return $str;
177185
}
178186

187+
private function rawurlencodeMatchZero(array $match)
188+
{
189+
return rawurlencode($match[0]);
190+
}
191+
179192
/**
180193
* Gets the name of the cookie.
181194
*

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.