-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] Send cookies using header() to fix "SameSite" ones #25348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,13 +162,13 @@ public function testCookieIsCleared() | |
public function testToString() | ||
{ | ||
$cookie = new Cookie('foo', 'bar', $expire = strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true); | ||
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; max-age='.($expire - time()).'; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie'); | ||
$this->assertEquals('foo=bar; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() returns string representation of the cookie'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please update |
||
|
||
$cookie = new Cookie('foo', 'bar with white spaces', strtotime('Fri, 20-May-2011 15:25:52 GMT'), '/', '.myfoodomain.com', true); | ||
$this->assertEquals('foo=bar%20with%20white%20spaces; expires=Fri, 20-May-2011 15:25:52 GMT; max-age='.($expire - time()).'; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)'); | ||
$this->assertEquals('foo=bar%20with%20white%20spaces; expires=Fri, 20-May-2011 15:25:52 GMT; Max-Age=0; path=/; domain=.myfoodomain.com; secure; httponly', (string) $cookie, '->__toString() encodes the value of the cookie according to RFC 3986 (white space = %20)'); | ||
|
||
$cookie = new Cookie('foo', null, 1, '/admin/', '.myfoodomain.com'); | ||
$this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', $expire = time() - 31536001).'; max-age='.($expire - time()).'; path=/admin/; domain=.myfoodomain.com; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL'); | ||
$this->assertEquals('foo=deleted; expires='.gmdate('D, d-M-Y H:i:s T', $expire = time() - 31536001).'; Max-Age=0; path=/admin/; domain=.myfoodomain.com; httponly', (string) $cookie, '->__toString() returns string representation of a cleared cookie if value is NULL'); | ||
|
||
$cookie = new Cookie('foo', 'bar', 0, '/', ''); | ||
$this->assertEquals('foo=bar; path=/; httponly', (string) $cookie); | ||
|
@@ -194,7 +194,7 @@ public function testGetMaxAge() | |
$this->assertEquals($expire - time(), $cookie->getMaxAge()); | ||
|
||
$cookie = new Cookie('foo', 'bar', $expire = time() - 100); | ||
$this->assertEquals($expire - time(), $cookie->getMaxAge()); | ||
$this->assertEquals(0, $cookie->getMaxAge()); | ||
} | ||
|
||
public function testFromString() | ||
|
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/HttpFoundation/Tests/Fixtures/response-functional/common.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
$parent = __DIR__; | ||
while (!@file_exists($parent.'/vendor/autoload.php')) { | ||
if (!@file_exists($parent)) { | ||
// open_basedir restriction in effect | ||
break; | ||
} | ||
if ($parent === dirname($parent)) { | ||
echo "vendor/autoload.php not found\n"; | ||
exit(1); | ||
} | ||
|
||
$parent = dirname($parent); | ||
} | ||
|
||
require $parent.'/vendor/autoload.php'; | ||
|
||
error_reporting(-1); | ||
ini_set('html_errors', 0); | ||
ini_set('display_errors', 1); | ||
|
||
header_remove('X-Powered-By'); | ||
header('Content-Type: text/plain; charset=utf-8'); | ||
|
||
register_shutdown_function(function () { | ||
echo "\n"; | ||
session_write_close(); | ||
print_r(headers_list()); | ||
echo "shutdown\n"; | ||
}); | ||
ob_start(); | ||
|
||
$r = new Response(); | ||
$r->headers->set('Date', 'Sat, 12 Nov 1955 20:04:00 GMT'); | ||
|
||
return $r; |
11 changes: 11 additions & 0 deletions
11
...mfony/Component/HttpFoundation/Tests/Fixtures/response-functional/cookie_max_age.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
Warning: Expiry date cannot have a year greater than 9999 in %scookie_max_age.php on line 10 | ||
|
||
Array | ||
( | ||
[0] => Content-Type: text/plain; charset=utf-8 | ||
[1] => Cache-Control: no-cache, private | ||
[2] => Date: Sat, 12 Nov 1955 20:04:00 GMT | ||
[3] => Set-Cookie: foo=bar; expires=Sat, 01-Jan-10000 02:46:40 GMT; Max-Age=%d; path=/ | ||
) | ||
shutdown |
10 changes: 10 additions & 0 deletions
10
src/Symfony/Component/HttpFoundation/Tests/Fixtures/response-functional/cookie_max_age.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
use Symfony\Component\HttpFoundation\Cookie; | ||
|
||
$r = require __DIR__.'/common.inc'; | ||
|
||
$r->headers->setCookie(new Cookie('foo', 'bar', 253402310800, '', null, false, false)); | ||
$r->sendHeaders(); | ||
|
||
setcookie('foo2', 'bar', 253402310800, '/'); |
10 changes: 10 additions & 0 deletions
10
...Component/HttpFoundation/Tests/Fixtures/response-functional/cookie_raw_urlencode.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
Array | ||
( | ||
[0] => Content-Type: text/plain; charset=utf-8 | ||
[1] => Cache-Control: no-cache, private | ||
[2] => Date: Sat, 12 Nov 1955 20:04:00 GMT | ||
[3] => Set-Cookie: ?*():@&+$/%#[]=?*():@&+$/%#[]; path=/ | ||
[4] => Set-Cookie: ?*():@&+$/%#[]=?*():@&+$/%#[]; path=/ | ||
) | ||
shutdown |
12 changes: 12 additions & 0 deletions
12
...fony/Component/HttpFoundation/Tests/Fixtures/response-functional/cookie_raw_urlencode.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
use Symfony\Component\HttpFoundation\Cookie; | ||
|
||
$r = require __DIR__.'/common.inc'; | ||
|
||
$str = '?*():@&+$/%#[]'; | ||
|
||
$r->headers->setCookie(new Cookie($str, $str, 0, '/', null, false, false, true)); | ||
$r->sendHeaders(); | ||
|
||
setrawcookie($str, $str, 0, '/', null, false, false); |
9 changes: 9 additions & 0 deletions
9
.../Component/HttpFoundation/Tests/Fixtures/response-functional/cookie_samesite_lax.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
Array | ||
( | ||
[0] => Content-Type: text/plain; charset=utf-8 | ||
[1] => Cache-Control: no-cache, private | ||
[2] => Date: Sat, 12 Nov 1955 20:04:00 GMT | ||
[3] => Set-Cookie: CookieSamesiteLaxTest=LaxValue; path=/; httponly; samesite=lax | ||
) | ||
shutdown |
8 changes: 8 additions & 0 deletions
8
...mfony/Component/HttpFoundation/Tests/Fixtures/response-functional/cookie_samesite_lax.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
use Symfony\Component\HttpFoundation\Cookie; | ||
|
||
$r = require __DIR__.'/common.inc'; | ||
|
||
$r->headers->setCookie(new Cookie('CookieSamesiteLaxTest', 'LaxValue', 0, '/', null, false, true, false, Cookie::SAMESITE_LAX)); | ||
$r->sendHeaders(); |
9 changes: 9 additions & 0 deletions
9
...mponent/HttpFoundation/Tests/Fixtures/response-functional/cookie_samesite_strict.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
Array | ||
( | ||
[0] => Content-Type: text/plain; charset=utf-8 | ||
[1] => Cache-Control: no-cache, private | ||
[2] => Date: Sat, 12 Nov 1955 20:04:00 GMT | ||
[3] => Set-Cookie: CookieSamesiteStrictTest=StrictValue; path=/; httponly; samesite=strict | ||
) | ||
shutdown |
8 changes: 8 additions & 0 deletions
8
...ny/Component/HttpFoundation/Tests/Fixtures/response-functional/cookie_samesite_strict.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
use Symfony\Component\HttpFoundation\Cookie; | ||
|
||
$r = require __DIR__.'/common.inc'; | ||
|
||
$r->headers->setCookie(new Cookie('CookieSamesiteStrictTest', 'StrictValue', 0, '/', null, false, true, false, Cookie::SAMESITE_STRICT)); | ||
$r->sendHeaders(); |
10 changes: 10 additions & 0 deletions
10
...ony/Component/HttpFoundation/Tests/Fixtures/response-functional/cookie_urlencode.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
Array | ||
( | ||
[0] => Content-Type: text/plain; charset=utf-8 | ||
[1] => Cache-Control: no-cache, private | ||
[2] => Date: Sat, 12 Nov 1955 20:04:00 GMT | ||
[3] => Set-Cookie: %3F%2A%28%29%3A%40%26%2B%24%2F%25%23%5B%5D=%3F%2A%28%29%3A%40%26%2B%24%2F%25%23%5B%5D; path=/ | ||
[4] => Set-Cookie: ?*():@&+$/%#[]=%3F%2A%28%29%3A%40%26%2B%24%2F%25%23%5B%5D; path=/ | ||
) | ||
shutdown |
12 changes: 12 additions & 0 deletions
12
src/Symfony/Component/HttpFoundation/Tests/Fixtures/response-functional/cookie_urlencode.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
use Symfony\Component\HttpFoundation\Cookie; | ||
|
||
$r = require __DIR__.'/common.inc'; | ||
|
||
$str = '?*():@&+$/%#[]'; | ||
|
||
$r->headers->setCookie(new Cookie($str, $str, 0, '', null, false, false)); | ||
$r->sendHeaders(); | ||
|
||
setcookie($str, $str, 0, '/'); |
6 changes: 6 additions & 0 deletions
6
.../Component/HttpFoundation/Tests/Fixtures/response-functional/invalid_cookie_name.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
The cookie name "Hello + world" contains invalid characters. | ||
Array | ||
( | ||
[0] => Content-Type: text/plain; charset=utf-8 | ||
) | ||
shutdown |
11 changes: 11 additions & 0 deletions
11
...mfony/Component/HttpFoundation/Tests/Fixtures/response-functional/invalid_cookie_name.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
use Symfony\Component\HttpFoundation\Cookie; | ||
|
||
$r = require __DIR__.'/common.inc'; | ||
|
||
try { | ||
$r->headers->setCookie(new Cookie('Hello + world', 'hodor')); | ||
} catch (\InvalidArgumentException $e) { | ||
echo $e->getMessage(); | ||
} |
58 changes: 58 additions & 0 deletions
58
src/Symfony/Component/HttpFoundation/Tests/ResponseFunctionalTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpFoundation\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @requires PHP 7.0 | ||
*/ | ||
class ResponseFunctionalTest extends TestCase | ||
{ | ||
private static $server; | ||
|
||
public static function setUpBeforeClass() | ||
{ | ||
$spec = array( | ||
1 => array('file', '/dev/null', 'w'), | ||
2 => array('file', '/dev/null', 'w'), | ||
); | ||
if (!self::$server = @proc_open('exec php -S localhost:8054', $spec, $pipes, __DIR__.'/Fixtures/response-functional')) { | ||
self::markTestSkipped('PHP server unable to start.'); | ||
} | ||
sleep(1); | ||
} | ||
|
||
public static function tearDownAfterClass() | ||
{ | ||
if (self::$server) { | ||
proc_terminate(self::$server); | ||
proc_close(self::$server); | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider provideCookie | ||
*/ | ||
public function testCookie($fixture) | ||
{ | ||
$result = file_get_contents(sprintf('http://localhost:8054/%s.php', $fixture)); | ||
$this->assertStringMatchesFormatFile(__DIR__.sprintf('/Fixtures/response-functional/%s.expected', $fixture), $result); | ||
} | ||
|
||
public function provideCookie() | ||
{ | ||
foreach (glob(__DIR__.'/Fixtures/response-functional/*.php') as $file) { | ||
yield array(pathinfo($file, PATHINFO_FILENAME)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current cookie code distinguishes between urlencoding the cookie value and not doing that (raw), the
setcookie()
function also performs some filtering and escaping as I understand it. As far as I can see in the code, theallPreserveCase()
function doesn't do that but just casts theCookie
object tostring
and as such isn't a 1-to-1 substitute forsetcookie()
andsetrawcookie()
, right?Edit: Ah, I see in Cookie.php that it does distinguish between raw/non-raw and performs some checks. However a double-check to see what PHP does in
setcookie()
might still be useful so there's no regressions, right?