Are HTTP cookies port specific?
Asked Answered
H

8

494

I have two HTTP services running on one machine. I just want to know if they share their cookies or whether the browser distinguishes between the two server sockets.

Hegarty answered 23/10, 2009 at 8:53 Comment(0)
W
463

No, cookies do not provide isolation by port.

The current cookie specification is RFC 6265, which replaces RFC 2109 and RFC 2965 (both RFCs are now marked as "Historic") and formalizes the syntax for real-world usages of cookies. It clearly states:

  1. Introduction

...

For historical reasons, cookies contain a number of security and privacy infelicities. For example, a server can indicate that a given cookie is intended for "secure" connections, but the Secure attribute does not provide integrity in the presence of an active network attacker. Similarly, cookies for a given host are shared across all the ports on that host, even though the usual "same-origin policy" used by web browsers isolates content retrieved via different ports.

And also:

8.5. Weak Confidentiality

Cookies do not provide isolation by port. If a cookie is readable by a service running on one port, the cookie is also readable by a service running on another port of the same server. If a cookie is writable by a service on one port, the cookie is also writable by a service running on another port of the same server. For this reason, servers SHOULD NOT both run mutually distrusting services on different ports of the same host and use cookies to store security sensitive information.

Wier answered 2/5, 2013 at 0:9 Comment(3)
/etc/hosts can be used to create more cookie domains for 127.0.0.1 than localhostMylor
CORS changes the game rules: #46288937 With it you have server side: Access-Control-Allow-Credentials, Access-Control-Allow-Origin, Access-Control-Allow-Headers + cookie setting Secure, SameSite=None and client side: XMLHttpRequest.withCredentials + ES6 fetch() credentials: 'include'Valerio
In case it helps anyone, I've written about this davidklempfner.medium.com/…Fibroid
B
153

According to RFC2965 3.3.1 (which might or might not be followed by browsers), unless the port is explicitly specified via the port parameter of the Set-Cookie header, cookies might or might not be sent to any port.

Google's Browser Security Handbook says: by default, cookie scope is limited to all URLs on the current host name - and not bound to port or protocol information. and some lines later There is no way to limit cookies to a single DNS name only [...] likewise, there is no way to limit them to a specific port. (Also, keep in mind, that IE does not factor port numbers into its same-origin policy at all.)

So it does not seem to be safe to rely on any well-defined behavior here.

Babbling answered 18/11, 2010 at 8:41 Comment(4)
RFC 6265, which replaces RFC 2965, eliminates the Port parameter in the Set-Cookie header (because almost nobody actually used it in practice), and makes it very explicit that cookies on the same host ARE NOT distinquishable by ports anymore.Wier
IE 9 won't even send the cookie back on subsequent requests if the domain has a port in itAsdic
Is there any browser that is still considering port in its cookies' SOP?Pyrogenic
Chrome won't even set the cookie if it has the domain has a port in it.Darbee
L
120

This is a really old question but I thought I would add a workaround I used.

I have two services running on my laptop (one on port 3000 and the other on 4000). When I would jump between (http://localhost:3000 and http://localhost:4000), Chrome would pass in the same cookie, each service would not understand the cookie and generate a new one.

I found that if I accessed http://localhost:3000 and http://127.0.0.1:4000, the problem went away since Chrome kept a cookie for localhost and one for 127.0.0.1.

Again, no one may care at this point but it was easy and helpful to my situation.

Leakage answered 1/5, 2013 at 23:57 Comment(9)
Yes, because cookies are associated with host/domain names, so a cookie on localhost cannot by shared with 127.0.0.1 and vice versa. But cookies on the same host/domain, regardless of port, are sharable.Wier
This probably works only in Chrome, as most other browsers don't send cookies to "localhost" (=a string without two dots)Daveen
Of course they do. I (and probably million other developers) use localhost for testing all the time. Unless the added port makes a difference: localhost:8080Lorettalorette
Also, you can use 127.0.0.1, 127.0.0.2, 127.0.0.3 etc... they all mean localhost.Lorettalorette
If you're willing to edit your hosts file (/etc/hosts on Unix) you can have as many meaningful names as you like for localhost.Alexine
@DavidBalažic No, "localhost" means 127.0.0.1. 127.0.0.2 usually doesn't have a name. These addresses are distinct unicast addresses and treated just as any different unicast addresses by the TCP protocol, but they are all local.Greenfield
@SilasS.Brown Please don't say "for localhost". "localhost" is the name associated with a particular IP address. Say "the local host".Greenfield
Also it can be handy to use subdomains like foo.localhost:3000 and bar.localhost:4000Inconvenience
You can set local domains of your choosing that alias to 0.0.0.0, 127.0.0.1, etc. in the /etc/hosts file. You're not confined to localhost. The syntax is pretty straight forward <ip> <domain_alias1> <domain_alias2> ... e.g. 127.0.0.1 api.foo.bar spa.foo.bar admin.foo.bar.Colombes
C
23

This is a big gray area in cookie SOP (Same Origin Policy).

Theoretically, you can specify port number in the domain and the cookie will not be shared. In practice, this doesn't work with several browsers and you will run into other issues. So this is only feasible if your sites are not for general public and you can control what browsers to use.

The better approach is to get 2 domain names for the same IP and not relying on port numbers for cookies.

Castorina answered 23/10, 2009 at 14:15 Comment(1)
It is not a gray area anymore. RFC 6265, which is the current cookie standard, eliminates any confusion about it by simply eliminating the ability to separate cookies on the same host using different ports.Wier
S
20

An alternative way to go around the problem, is to make the name of the session cookie be port related. For example:

  • mysession8080 for the server running on port 8080
  • mysession8000 for the server running on port 8000

Your code could access the webserver configuration to find out which port your server uses, and name the cookie accordingly.

Keep in mind that your application will receive both cookies, and you need to request the one that corresponds to your port.

There is no need to have the exact port number in the cookie name, but this is more convenient.

In general, the cookie name could encode any other parameter specific to the server instance you use, so it can be decoded by the right context.

Sande answered 20/9, 2011 at 9:47 Comment(1)
This works for mutually trusting web services sharing the host name on different port numbers. But all host-related cookies will be sent to all web services of that host regardless of their names. If the host allows malicious unprivileged operators run web services on separate port numbers, the operators can abuse browsers of customers of the original web service into sending all host-related cookies, including the secret ones, to the malicious web service that shares the host.Donelson
B
10

In IE 8, cookies (verified only against localhost) are shared between ports. In FF 10, they are not.

I've posted this answer so that readers will have at least one concrete option for testing each scenario.

Burden answered 16/2, 2012 at 15:51 Comment(0)
G
6

I was experiencing a similar problem running (and trying to debug) two different Django applications on the same machine.

I was running them with these commands:

./manage.py runserver 8000
./manage.py runserver 8001

When I did login in the first one and then in the second one I always got logged out the first one and viceversa.

I added this on my /etc/hosts

127.0.0.1    app1
127.0.0.1    app2

Then I started the two apps with these commands:

./manage.py runserver app1:8000
./manage.py runserver app2:8001

Problem solved :)

Garrity answered 21/8, 2013 at 10:32 Comment(2)
you can probably use 127.0.0.1:8000 for one, localhost:8000 for a second, and possibly ::1:8000 (maybe [::1]:8080) for a third without ever having to touch the hosts file.Delorisdelorme
you can put it into one line: ::1 app1 app2 app3 app4 app5 appNElysian
H
-1

It's optional.

The port may be specified so cookies can be port specific. It's not necessary, the web server / application must care of this.

Source: German Wikipedia article, RFC2109, Chapter 4.3.1

Hegarty answered 23/10, 2009 at 8:57 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.