bug #26569 [BrowserKit] Fix cookie path handling when $domain is null (dunglas)

This PR was merged into the 2.7 branch.

Discussion
----------

[BrowserKit] Fix cookie path handling when $domain is null

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  |no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? |no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

The bug is highlighted by the new test: there is an inconsistency in the path handling regarding if a domain is set or not. If it is set, and the cookie is set to a subpath of the passed path, the cookie is returned. However if no domain is set, it is not. This PR fixes this bug.

Commits
-------

acc20fc [BrowserKit] Fix cookie path handling when $domain is null
This commit is contained in:
Nicolas Grekas 2018-03-19 21:00:51 +01:00
commit bd49884804
2 changed files with 14 additions and 23 deletions

View File

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

View File

@ -237,6 +237,8 @@ class CookieJarTest extends TestCase
$this->assertEquals($cookie1, $cookieJar->get('foo', '/test', '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', '/', 'example.com'));
$this->assertEquals($cookie2, $cookieJar->get('foo1', '/bar', 'example.com')); $this->assertEquals($cookie2, $cookieJar->get('foo1', '/bar', 'example.com'));
$this->assertEquals($cookie2, $cookieJar->get('foo1', '/bar'));
} }
public function testCookieWithWildcardDomain() public function testCookieWithWildcardDomain()