Articles / Go

  • go
  • net-http
  • cookies
  • cookiejar
  • security
  • debugging

Why a Go Cookie Jar Sends a Cookie to Every Subdomain

You log in on app.example.com, then your Go client calls api.example.com and the session cookie turns up there too. The jar is not buggy. Almost every time, the server asked for exactly that, probably without meaning to.

Here is the whole thing in thirty lines, using nothing but the standard library.

Reproduce it without a network

You can feed a jar cookies directly with SetCookies, which behaves as if the URL you pass had just sent a Set-Cookie response.

package main

import (
	"fmt"
	"net/http"
	"net/http/cookiejar"
	"net/url"
)

func main() {
	jar, err := cookiejar.New(nil)
	if err != nil {
		panic(err)
	}
	u, _ := url.Parse("https://app.example.com/")
	jar.SetCookies(u, []*http.Cookie{
		{Name: "hostonly", Value: "1"},
		{Name: "wide", Value: "2", Domain: "example.com"},
	})

	for _, h := range []string{"app.example.com", "api.example.com", "example.com", "example.org"} {
		v, _ := url.Parse("https://" + h + "/")
		fmt.Println(h, jar.Cookies(v))
	}
}

Expected output:

app.example.com [hostonly=1 wide=2]
api.example.com [wide=2]
example.com [wide=2]
example.org []

The cookie without a Domain stayed on the host that set it. The one with Domain=example.com went everywhere underneath that name.

Host-only versus domain cookies

RFC 6265 defines two kinds of cookie, and the only difference is one attribute:

  • No Domain: a host-only cookie, sent back to the exact host that set it and nobody else.
  • Domain=example.com: a domain cookie, sent to example.com and every subdomain of it.

Go's jar records which kind it stored and applies the rule faithfully. So the wrong-subdomain cookie is nearly always a Domain attribute on the server side, set by a framework default, a copy-pasted config line or a single-sign-on setup that genuinely wanted sharing.

Quick detour: the leading dot means nothing

Old advice says to write Domain=.example.com to cover subdomains, and people argue about whether the dot matters. Under RFC 6265 it does not: a leading dot is ignored, and the jar strips it. Domain=example.com and Domain=.example.com behave identically.

The consequence is the one that bites: you cannot write a cookie that applies to the parent but not the children. Setting any Domain attribute opts into all of them.

How to find out who set it

Do not guess from the jar's contents; look at the raw response. The jar's Cookies method hides the Domain attribute (it returns only name and value), so check the headers:

resp, err := client.Get("https://app.example.com/login")
if err != nil {
	return err
}
defer resp.Body.Close()
for _, c := range resp.Cookies() {
	fmt.Printf("%s domain=%q path=%q secure=%v\n", c.Name, c.Domain, c.Path, c.Secure)
}

An empty Domain means host-only. If you see a value, that is your culprit, and the fix belongs on the server: drop the attribute unless you really want cross-subdomain sharing.

The jar can be the one making it worse

There is a second way to get this wrong, and it is on the client. cookiejar.New(nil) gives you a jar with no public suffix list. A public suffix is a name like com or co.uk under which unrelated parties register their own sites.

Without that list, the jar cannot tell that co.uk is not a site. From my reading of the package source, a response from shop.example.co.uk carrying Domain=co.uk would be accepted as a domain cookie, and then offered to any other .co.uk host your client visits. Browsers refuse that; a bare jar does not.

The package documentation points at the fix: pass a list.

import (
	"net/http/cookiejar"

	"golang.org/x/net/publicsuffix"
)

jar, err := cookiejar.New(&cookiejar.Options{
	PublicSuffixList: publicsuffix.List,
})

With the list in place, a Domain attribute that is itself a public suffix is rejected (or treated as host-only if it matches the setting host exactly). It is a small extra module, and worth it for any client that talks to hosts you do not control.

Other places a cookie can travel further than you expect

Cookies are scoped by host and path, not by port. Two services on localhost:8080 and localhost:9090 share a jar's cookies of the same name, and one will overwrite the other. If your tests run several local servers, this is a classic source of baffling logins.

Also check what you share:

  • One http.Client with one jar across all your targets means every host's cookies sit in the same store. Use a jar per trust boundary.
  • Redirects consult the jar on each hop, so a redirect to a sibling subdomain will carry any domain cookie along with it.
  • The Secure flag is honoured, but a domain cookie marked Secure still goes to every HTTPS subdomain.

Hang on, why does the default client not do any of this? Because http.DefaultClient has a nil Jar, so it stores nothing at all. Cookies only appear once you attach a jar yourself.

What to change

  1. Print resp.Cookies() and look for a non-empty Domain.
  2. Remove it server-side unless sharing across subdomains is the point.
  3. Build your client's jar with publicsuffix.List.
  4. Give separate jars to clients that should not see each other's sessions.

The spec behaviour is easy to forget because browsers hide it so well. A Go client makes it visible, which is rather useful when you are auditing what your own servers hand out.