8000 Merge branch '4.3' into 4.4 · Simperfit/symfony@55843f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 55843f2

Browse files
Merge branch '4.3' into 4.4
* 4.3: sync phpunit script with master [HttpFoundation] allow additinal characters in not raw cookies
2 parents e627989 + d8a026b commit 55843f2

File tree

7 files changed

+47
-16
lines changed

7 files changed

+47
-16
lines changed

phpunit

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ if (!file_exists(__DIR__.'/vendor/symfony/phpunit-bridge/bin/simple-phpunit')) {
99
}
1010
if (!getenv('SYMFONY_PHPUNIT_VERSION')) {
1111
if (\PHP_VERSION_ID >= 70200) {
12+
if (false === getenv('SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT') && false !== strpos(@file_get_contents(__DIR__.'/src/Symfony/Component/HttpKernel/Kernel.php'), 'const MAJOR_VERSION = 3;')) {
13+
putenv('SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT=1');
14+
}
1215
putenv('SYMFONY_PHPUNIT_VERSION=8.3');
1316
} elseif (\PHP_VERSION_ID >= 70000) {
1417
putenv('SYMFONY_PHPUNIT_VERSION=6.5');

src/Symfony/Component/HttpFoundation/Cookie.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,25 @@
1818
*/
1919
class Cookie
2020
{
21+
const SAMESITE_NONE = 'none';
22+
const SAMESITE_LAX = 'lax';
23+
const SAMESITE_STRICT = 'strict';
24+
2125
protected $name;
2226
protected $value;
2327
protected $domain;
2428
protected $expire;
2529
protected $path;
2630
protected $secure;
2731
protected $httpOnly;
32+
2833
private $raw;
2934
private $sameSite;
3035
private $secureDefault = false;
3136

32-
const SAMESITE_NONE = 'none';
33-
const SAMESITE_LAX = 'lax';
34-
const SAMESITE_STRICT = 'strict';
37+
private static $reservedCharsList = "=,; \t\r\n\v\f";
38+
private static $reservedCharsFrom = ['=', ',', ';', ' ', "\t", "\r", "\n", "\v", "\f"];
39+
private static $reservedCharsTo = ['%3D', '%2C', '%3B', '%20', '%09', '%0D', '%0A', '%0B', '%0C'];
3540

3641
/**
3742
* Creates cookie from raw header string.
@@ -93,7 +98,7 @@ public function __construct(string $name, string $value = null, $expire = 0, ?st
9398
}
9499

95100
// from PHP source code
96-
if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
101+
if ($raw && false !== strpbrk($name, self::$reservedCharsList)) {
97102
throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
98103
}
99104

@@ -141,7 +146,13 @@ public function __construct(string $name, string $value = null, $expire = 0, ?st
141146
*/
142147
public function __toString()
143148
{
144-
$str = ($this->isRaw() ? $this->getName() : urlencode($this->getName())).'=';
149+
if ($this->isRaw()) {
150+
$str = $this->getName();
151+
} else {
152+
$str = str_replace(self::$reservedCharsFrom, self::$reservedCharsTo, $this->getName());
153+
}
154+
155+
$str .= '=';
145156

146157
if ('' === (string) $this->getValue()) {
147158
$str .= 'deleted; expires='.gmdate('D, d-M-Y H:i:s T', time() - 31536001).'; Max-Age=0';

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public function sendHeaders()
344344

345345
// cookies
346346
foreach ($this->headers->getCookies() as $cookie) {
347-
header('Set-Cookie: '.$cookie->getName().strstr($cookie, '='), false, $this->statusCode);
347+
header('Set-Cookie: '.$cookie, false, $this->statusCode);
348348
}
349349

350350
// status

src/Symfony/Component/HttpFoundation/Tests/CookieTest.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
*/
2525
class CookieTest extends TestCase
2626
{
27-
public function invalidNames()
27+
public function namesWithSpecialCharacters()
2828
{
2929
return [
30-
[''],
3130
[',MyName'],
3231
[';MyName'],
3332
[' MyName'],
@@ -40,12 +39,26 @@ public function invalidNames()
4039
}
4140

4241
/**
43-
* @dataProvider invalidNames
42+
* @dataProvider namesWithSpecialCharacters
4443
*/
45-
public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidCharacters($name)
44+
public function testInstantiationThrowsExceptionIfRawCookieNameContainsSpecialCharacters($name)
4645
{
4746
$this->expectException('InvalidArgumentException');
48-
Cookie::create($name);
47+
Cookie::create($name, null, 0, null, null, null, false, true);
48+
}
49+
50+
/**
51+
* @dataProvider namesWithSpecialCharacters
52+
*/
53+
public function testInstantiationSucceedNonRawCookieNameContainsSpecialCharacters($name)
54+
{
55+
$this->assertInstanceOf(Cookie::class, Cookie::create($name));
56+
}
57+
58+
public function testInstantiationThrowsExceptionIfCookieNameIsEmpty()
59+
{
60+
$this->expectException('InvalidArgumentException');
61+
Cookie::create('');
4962
}
5063

5164
public function testInvalidExpiration()

src/Symfony/Component/HttpFoundation/Tests/Fixtures/response-functional/cookie_urlencode.expected

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Array
44
[0] => Content-Type: text/plain; charset=utf-8
55
[1] => Cache-Control: no-cache, private
66
[2] => Date: Sat, 12 Nov 1955 20:04:00 GMT
7-
[3] => Set-Cookie: ?*():@&+$/%#[]=%3F%2A%28%29%3A%40%26%2B%24%2F%25%23%5B%5D; path=/
7+
[3] => Set-Cookie: %3D%2C%3B%20%09%0D%0A%0B%0C=%3D%2C%3B%20%09%0D%0A%0B%0C; path=/
88
[4] => Set-Cookie: ?*():@&+$/%#[]=%3F%2A%28%29%3A%40%26%2B%24%2F%25%23%5B%5D; path=/
9+
[5] => Set-Cookie: ?*():@&+$/%#[]=%3F%2A%28%29%3A%40%26%2B%24%2F%25%23%5B%5D; path=/
910
)
1011
shutdown

src/Symfony/Component/HttpFoundation/Tests/Fixtures/response-functional/cookie_urlencode.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
$r = require __DIR__.'/common.inc';
66

7-
$str = '?*():@&+$/%#[]';
7+
$str1 = "=,; \t\r\n\v\f";
8+
$r->headers->setCookie(new Cookie($str1, $str1, 0, '', null, false, false, false, null));
89

9-
$r->headers->setCookie(new Cookie($str, $str, 0, '', null, false, false, false, null));
10+
$str2 = '?*():@&+$/%#[]';
11+
12+
$r->headers->setCookie(new Cookie($str2, $str2, 0, '', null, false, false, false, null));
1013
$r->sendHeaders();
1114

12-
setcookie($str, $str, 0, '/');
15+
setcookie($str2, $str2, 0, '/');

src/Symfony/Component/HttpFoundation/Tests/Fixtures/response-functional/invalid_cookie_name.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
$r = require __DIR__.'/common.inc';
66

77
try {
8-
$r->headers->setCookie(Cookie::create('Hello + world', 'hodor'));
8+
$r->headers->setCookie(new Cookie('Hello + world', 'hodor', 0, null, null, null, false, true));
99
} catch (\InvalidArgumentException $e) {
1010
echo $e->getMessage();
1111
}

0 commit comments

Comments
 (0)
0