Skip to content

Commit 0f92f97

Browse files
authored
Merge pull request #63 from andrewkharook/cross-subdomain-cookie-sharing-issue
Cross subdomain cookie sharing issue
2 parents 7b34df8 + f5103f0 commit 0f92f97

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ $plugin = new RetryPlugin(['delay' => function(RequestInterface $request, Except
3737

3838
### Fixed
3939

40+
- `CookiePlugin` allows main domain cookies to be sent/stored for subdomains
4041
- `DecoderPlugin` uses the right `FilteredStream` to handle `deflate` content encoding
4142

4243

spec/Plugin/CookiePluginSpec.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,47 @@ function it_does_not_load_cookie_if_domain_does_not_match(RequestInterface $requ
8282
}, function () {});
8383
}
8484

85+
function it_does_not_load_cookie_on_hackish_domains(RequestInterface $request, UriInterface $uri, Promise $promise)
86+
{
87+
$hackishDomains = [
88+
'hacktest.com',
89+
'test.com.hacked.org',
90+
];
91+
$cookie = new Cookie('name', 'value', 86400, 'test.com');
92+
$this->cookieJar->addCookie($cookie);
93+
94+
foreach ($hackishDomains as $domain) {
95+
$request->getUri()->willReturn($uri);
96+
$uri->getHost()->willReturn($domain);
97+
98+
$request->withAddedHeader('Cookie', 'name=value')->shouldNotBeCalled();
99+
100+
$this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) {
101+
if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) {
102+
return $promise->getWrappedObject();
103+
}
104+
}, function () {});
105+
}
106+
}
107+
108+
function it_loads_cookie_on_subdomains(RequestInterface $request, UriInterface $uri, Promise $promise)
109+
{
110+
$cookie = new Cookie('name', 'value', 86400, 'test.com');
111+
$this->cookieJar->addCookie($cookie);
112+
113+
$request->getUri()->willReturn($uri);
114+
$uri->getHost()->willReturn('www.test.com');
115+
$uri->getPath()->willReturn('/');
116+
117+
$request->withAddedHeader('Cookie', 'name=value')->willReturn($request);
118+
119+
$this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) {
120+
if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) {
121+
return $promise->getWrappedObject();
122+
}
123+
}, function () {});
124+
}
125+
85126
function it_does_not_load_cookie_if_path_does_not_match(RequestInterface $request, UriInterface $uri, Promise $promise)
86127
{
87128
$cookie = new Cookie('name', 'value', 86400, 'test.com', '/sub');

src/Plugin/CookiePlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
6969
}
7070

7171
// Restrict setting cookie from another domain
72-
if (false === strpos($cookie->getDomain(), $request->getUri()->getHost())) {
72+
if (!preg_match("/\.{$cookie->getDomain()}$/", '.'.$request->getUri()->getHost())) {
7373
continue;
7474
}
7575

0 commit comments

Comments
 (0)