bug #9769 [BrowserKit] fixes #8311 CookieJar is totally ignorant of RFC 6265 edge cases (jzawadzki)

This PR was squashed before being merged into the 2.3 branch (closes #9769).

Discussion
----------

[BrowserKit] fixes #8311 CookieJar is totally ignorant of RFC 6265 edge cases

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #8311
| License       | MIT
| Doc PR        |

Commits
-------

3132b04 [BrowserKit] fixes #8311 CookieJar is totally ignorant of RFC 6265 edge cases
This commit is contained in:
Fabien Potencier 2013-12-17 09:03:37 +01:00
commit ba856e60bf
2 changed files with 43 additions and 1 deletions

View File

@ -55,7 +55,25 @@ class CookieJar
$this->flushExpiredCookies();
if (!empty($domain)) {
return isset($this->cookieJar[$domain][$path][$name]) ? $this->cookieJar[$domain][$path][$name] : null;
foreach ($this->cookieJar as $cookieDomain => $pathCookies) {
if ($cookieDomain) {
$cookieDomain = '.'.ltrim($cookieDomain, '.');
if ($cookieDomain != substr('.'.$domain, -strlen($cookieDomain))) {
continue;
}
}
foreach ($pathCookies as $cookiePath => $namedCookies) {
if ($cookiePath != substr($path, 0, strlen($cookiePath))) {
continue;
}
if (isset($namedCookies[$name])) {
return $namedCookies[$name];
}
}
}
return null;
}
// avoid relying on this behavior that is mainly here for BC reasons

View File

@ -196,6 +196,30 @@ class CookieJarTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array('foo' => 'bar2'), $cookieJar->allValues('http://bar.example.com/'));
}
public function testCookieGetWithSubdomain()
{
$cookieJar = new CookieJar();
$cookieJar->set($cookie1 = new Cookie('foo', 'bar', null, '/', '.example.com'));
$cookieJar->set($cookie2 = new Cookie('foo1', 'bar', null, '/', 'test.example.com'));
$this->assertEquals($cookie1, $cookieJar->get('foo','/','foo.example.com'));
$this->assertEquals($cookie1, $cookieJar->get('foo','/','example.com'));
$this->assertEquals($cookie2, $cookieJar->get('foo1','/','test.example.com'));
}
public function testCookieGetWithSubdirectory()
{
$cookieJar = new CookieJar();
$cookieJar->set($cookie1 = new Cookie('foo', 'bar', null, '/test', '.example.com'));
$cookieJar->set($cookie2 = new Cookie('foo1', 'bar1', null, '/', '.example.com'));
$this->assertNull($cookieJar->get('foo','/','.example.com'));
$this->assertNull($cookieJar->get('foo','/bar','.example.com'));
$this->assertEquals($cookie1, $cookieJar->get('foo','/test','example.com'));
$this->assertEquals($cookie2, $cookieJar->get('foo1','/','example.com'));
$this->assertEquals($cookie2, $cookieJar->get('foo1','/bar','example.com'));
}
public function testCookieWithWildcardDomain()
{
$cookieJar = new CookieJar();