Are HTTP cookies port specific?

前端 未结 8 1898
傲寒
傲寒 2020-11-22 04:14

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.

相关标签:
8条回答
  • 2020-11-22 05:00

    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.

    0 讨论(0)
  • 2020-11-22 05:01

    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

    0 讨论(0)
  • 2020-11-22 05:02

    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 :)

    0 讨论(0)
  • 2020-11-22 05:05

    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.

    0 讨论(0)
  • 2020-11-22 05:06

    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, noone may care at this point but it was easy and helpful to my situation.

    0 讨论(0)
  • 2020-11-22 05:08

    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.

    0 讨论(0)
提交回复
热议问题