Skip to content

Cross subdomain cookie sharing issue #63

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

Conversation

andrewkharook
Copy link
Contributor

@andrewkharook andrewkharook commented Mar 17, 2017

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Related tickets fixes #62
License MIT

What's in this PR?

The changes make cookies set for main domain available for subdomains as well, as described in RFC 6265

Checklist

  • Updated CHANGELOG.md to describe BC breaks / deprecations | new feature | bugfix
  • Documentation pull request created (if not simply a bugfix)

Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! can you please add some unit tests both for cases where the cookie does get shared and cases where it must not be shared?

@@ -69,7 +69,7 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
}

// Restrict setting cookie from another domain
if (false === strpos($cookie->getDomain(), $request->getUri()->getHost())) {
if (false === strpos($request->getUri()->getHost(), $cookie->getDomain())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this correct. what about symfony.slack.com setting the cookie and then we do a request to hacksymfony.slack.com? its an edge case, but i think we should be correct about this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, hack.symfony.slack.com should be correct again. Cookies.... 😕

Copy link
Contributor Author

@andrewkharook andrewkharook Mar 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbu, @sagikazarmark:
What if we add a dot . to both cookie domain and request domain before the comparison?

strpos(
    '.'.$request->getUri()->getHost(),
    '.'.$cookie->getDomain()
)

Here's what's happening in this case:

strpos(
    '.hacksymfony.slack.com',
    '.symfony.slack.com'
); // no match, cookie ignored.

strpos(
    '.hack.symfony.slack.com',
    '.symfony.slack.com'
); // match, cookie used.

strpos(
    '.symfony.slack.com',
    '.symfony.slack.com'
); // match, cookie used.

I wonder if this workaround is appropriate and not counted as dirty code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think that is the right approach. afaik the server could send the leading . already, we should handle that properly as well, not that we end up with "..symfony.slack.com".

are you sure that cookies should propagate down several levels? like test.com cookie being sent to foo.bar.baz.test.com ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaik the server could send the leading . already, we should handle that properly as well, not that we end up with "..symfony.slack.com".

This is already handled by Http\Message\Cookie::normalizeDomain(), so we shouldn't care about it.

are you sure that cookies should propagate down several levels? like test.com cookie being sent to foo.bar.baz.test.com ?

Yes, that's what is said in RFC 6265. If Domain attribute is not specified, it defaults to the host portion of the current document location (but not including subdomains). If a domain is specified, subdomains are always included.

Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great, thanks!

if (false === strpos($cookie->getDomain(), $request->getUri()->getHost())) {
if (false === strpos(
'.'.$request->getUri()->getHost(),
'.'.$cookie->getDomain()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this check too weak (see https://3v4l.org/nXCMW)? You should here test that $request->getUri()->getHost() ends with $cookie->getDomain().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you are right @fbourigault

@andrewkharook can you please adjust this accordingly and add a test that fails with this code but not with the new code? like comparing fake.domain.com.evil.org with domain.com?

if (false === strpos($cookie->getDomain(), $request->getUri()->getHost())) {
if (false === strpos(
'.'.$request->getUri()->getHost(),
'.'.$cookie->getDomain()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you are right @fbourigault

@andrewkharook can you please adjust this accordingly and add a test that fails with this code but not with the new code? like comparing fake.domain.com.evil.org with domain.com?

@Nyholm
Copy link
Member

Nyholm commented Jul 6, 2017

Thank you @andrewkharook for this PR. Would you mind updating it according to the feedback?

@andrewkharook
Copy link
Contributor Author

Hi, @Nyholm!
Sure, the update will be ready over the weekend.

Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me.

@fbourigault do you want to check again as well?

Copy link
Member

@joelwurtz joelwurtz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me also, go merge ?

@Nyholm
Copy link
Member

Nyholm commented Jul 20, 2017

Ping @fbourigault

@joelwurtz joelwurtz merged commit 0f92f97 into php-http:master Jul 20, 2017
@joelwurtz
Copy link
Member

Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cookies from subdomains are ignored by the CookiePlugin
6 participants