Pass the menu and read the page

SignupFolio

Cookie domain rules: 4 checks across subdomains

To share a signup cookie across subdomains, set its Domain to a valid shared parent and test that the app receives it.

Key takeaways

  • Check that the shared parent is not a public suffix.
  • Set only one Domain value; ignore any leading dot.
  • Review cookie lifetime separately from session timeout.
  • Test signup through arrival at the app in the same browser.

One way to carry the login across is a cookie, and the Domain attribute of that cookie controls which hosts receive it. Set that attribute wrong and a new user can finish signing up, reach the app, and still look logged out.

This post covers 4 rules for the Domain attribute, written from MDN's description of the Set-Cookie header. Each rule is easy to get wrong with a small typo or a value copied from an old example. After the 4 rules, I cover two settings that decide how long the login lasts and one check on session IDs. Throughout, I use signup.example.com for the signup page and app.example.com for the app. Those names are illustrative. MDN's examples use api.example.com, and I apply the same logic to them.

If you lead growth rather than writing the headers yourself, you do not need to memorize these rules. You need to know what the signup and login depend on, and to ask whether the team has checked them. The goal is simple: a new account that finished signing up should still be logged in when the user reaches the app.

Read the rules in order. Rules 1 and 2 decide which values are allowed at all. Rules 3 and 4 describe what a valid value does once it is set.

Rule 1: the Domain value cannot be a public suffix

The Domain value cannot be a public suffix such as com, co.uk, or github.io. MDN's Set-Cookie reference states this rule directly, and those three names are its examples.

MDN makes the same point with a UK example. A response from shop.example.co.uk can set Domain=shop.example.co.uk or Domain=example.co.uk, but not Domain=co.uk, because co.uk is a public suffix. The value you choose should be a name under the public suffix, not the suffix itself.

For your hosts, the ending is com. That means Domain=com is off the table, and so is any value that is only a public suffix. Before you ship, check every Domain value in your code against that test.

Rule 1 rules out the ending. Rule 2 then limits you to the host or one of the names above it.

Rule 2: the Domain value can be the host itself or a parent of it

A response can set Domain to its own host or to a parent of that host. MDN's Set-Cookie reference gives an example: a response from api.example.com can set Domain=api.example.com or Domain=example.com, but not Domain=beta.api.example.com, Domain=other.example.com, or Domain=com.

The allowed values are the host and its parent. The rejected values fall into three groups: a deeper name under the host, a host in another branch, and the ending itself.

Applied to our hosts, signup.example.com can set Domain=signup.example.com or Domain=example.com. The shared parent is example.com, and rule 4 explains why that value can reach app.example.com. Domain=app.example.com is the value to avoid. MDN lists other.example.com as not allowed from api.example.com, and app.example.com is the same kind of value when set from signup.example.com: a host under the same parent, not a parent itself.

A quick check helps. For each host that sends a response, list the host itself and its parents. If a name is missing from that list, it fails the rule before you test anything.

The shared parent is the longest name that both hosts end in. For signup.example.com and app.example.com, that is example.com. If your hosts are deeper, such as signup.eu.example.com and app.eu.example.com, the shared parent is eu.example.com. Rule 2 still applies: the value must be the host or one of its parents.

Rule 3: a leading dot in the Domain value is ignored

A leading dot in the Domain value, such as .example.com, is ignored. MDN's Set-Cookie reference puts it this way: "Contrary to earlier specifications, leading dots in domain names (.example.com) are ignored."

Under that description, Domain=.example.com and Domain=example.com are read the same way. If you copy a value from an older example, the leading dot does not change the result. What matters is that a Domain is specified, which rule 4 covers.

The dot also does not rescue a bad value. Because it is ignored, .com is the same as com, and rule 1 still rules it out. When you review headers, treat .example.com and example.com as the same value, and say so in the review notes, so nobody spends time trying to make the dot matter.

Rule 4: set one Domain value, and it includes every subdomain

A cookie can have only one Domain value, and once a domain is specified, every subdomain is included. MDN's Set-Cookie reference states the rule in one line: "Multiple host/domain values are not allowed, but if a domain is specified, then subdomains are always included."

For the signup flow, Domain=example.com covers signup.example.com, app.example.com, and any other name under example.com. That is what lets the login reach the app host. The same rule also makes the cookie broad. Any other name under example.com receives it too, whatever that name is used for.

Pick the value on purpose and write it down. A sign-in cookie that reaches every subdomain has a wider reach than a cookie meant for one host, so review each new subdomain against the cookies that already reach it.

If a name under example.com does not need the login, a cookie that covers it anyway is worth a second look. Once you specify a Domain value, the rule gives no way to leave out the subdomains it covers.

Max-Age sets the lifetime when both attributes are present. MDN's Set-Cookie reference says: "If both Expires and Max-Age are set, Max-Age has precedence." For a signup flow, this matters when the response sets both values. The Max-Age value is the one that counts.

A short lifetime may send that user back to the login page sooner than you planned.

If you set both, make them agree, so the header does not carry an Expires value that Max-Age overrides. A user who returns after the lifetime ends may be logged out, so set the lifetime to match how long the first session should last.

In ASP.NET Core, a session uses a default timeout of 20 minutes unless the app sets one

The app either sets the session timeout or uses the default value of 20 minutes. Microsoft's guide to session in ASP.NET Core gives that wording. This is a default for that framework. If your signup page and app run on a different framework, the number does not apply to you, so check that framework's own documentation.

Treat the session timeout as separate from the cookie lifetime. When a new user reports being logged out, check both. For an ASP.NET Core app, find out whether your code sets a timeout or relies on the default.

Suppose an app uses the default session timeout of 20 minutes. Start by checking whether the signup cookie reaches the app host. The elapsed time alone does not establish whether cookie scope, cookie lifetime, or the session caused the problem.

A session ID needs at least 64 bits of entropy, according to OWASP

OWASP's Session Management cheat sheet says: "Session identifiers must have at least 64 bits of entropy to prevent brute-force session guessing attacks."

The same page explains the arithmetic: "Each “bit” of entropy doubles the number of possible outcomes, meaning a session ID with 64 bits of entropy can have 2^64 possible values." Each extra bit doubles the count, so the number of possible IDs grows quickly as the bits grow.

Count the bits, not the characters. OWASP notes that with an encoding such as Base64, or Microsoft's encoding for ASP.NET session IDs, a different number of characters may be required to represent the minimum 64 bits of entropy. So when you judge a session ID, check the bits it carries, not how many characters it has.

Do not write your own session ID generator from a counter or a timestamp. Use the session mechanism your framework provides, and confirm that it produces IDs with at least 64 bits of entropy. For a closer review, use the session ID length checks.

The shared parent of signup.example.com and app.example.com is example.com

The shared parent is example.com, and that is the name the signup response can use as its Domain value. The signup page runs on signup.example.com, and the app runs on app.example.com. Both hosts end in example.com. In app.example.com, app is a subdomain label; app.example.com is the full host name.

Walk through the flow in order. A user submits the signup form, and the server creates the account and a session. The response that finishes the signup carries a Set-Cookie header for the session. If that header uses Domain=example.com, rule 2 allows it, because example.com is the parent of the signup host. Rule 4 then means the cookie covers subdomains, including app.example.com.

Now consider one specific mistake. If the signup response sets Domain=signup.example.com, the cookie covers the signup host and names under it, but not app.example.com. The app would not receive the cookie, so the new user may look logged out there.

The fix is to change the Domain value to example.com and leave everything else as it was. Changing the value is a small edit, but its effect reaches every subdomain under example.com, so retest the whole flow after it.

The test is simple. After signup, the user should reach app.example.com and still be logged in, without a second login. If that is not what happens, check the Domain value on the signup response first, because each of the 4 rules is about that value.

These rules only cover the Domain attribute, not the whole login

The 4 rules in this post describe the Domain attribute of a Set-Cookie header, and they say nothing about how the app checks a login. A correct Domain value gets the cookie to the right hosts, but it does not, by itself, decide whether the session behind the cookie is valid.

Those questions belong to your authentication design. Check them separately, so a passing Domain test does not hide a problem with the session itself. Use the signup authentication test for that broader review.

The OWASP and Microsoft notes above cover the session side in part. The OWASP note is about the strength of the session ID, and the Microsoft note is about the default timeout in one framework. Neither one is a complete login design.

Decide the Domain value, the lifetime, and the session timeout in one place, and write them down where the signup and app teams can both see them. A cookie that reaches every subdomain is a shared decision, so it should not be set by one team alone.

Ask the team to review every Set-Cookie header in the signup flow, not only the one for the session. Each header with a Domain value should be checked against the 4 rules, and each lifetime should be checked against the Max-Age rule.

Record the decision with its reason. A future change to the app or to the signup host may tempt someone to set a new Domain value. A short note that names the shared parent and the rule it satisfies gives that reviewer a starting point.

Plan a retest for every change to the signup host, the app host, or the shared parent. A change to any of them can change which hosts receive the cookie, so the flow needs a fresh check each time.

Start with the Domain value on the signup response, then check the lifetime and the session timeout.

Use the 4 rules as a checklist. Does the value sit on the shared parent? Is it a name below a public suffix, not the suffix itself? Is there only one value? If the Domain value passes, look at the lifetime next, and then the session timeout. Each step narrows the problem before you move on.

Run the test in a fresh browser, and record which host the user was on when the logout happened. That detail helps you see which host did not receive the cookie.

Before release, check the 4 rules against your real hostnames

Run these checks against your real hostnames before the release goes out. Each item points back to a section above.

  1. Write down the signup host and the app host, such as signup.example.com and app.example.com.
  2. Find the shared parent, such as example.com, and confirm it is not a public suffix such as com or co.uk (MDN, rule 1).
  3. Confirm that you set only one Domain value, and treat any leading dot as ignored (MDN, rules 3 and 4).
  4. Confirm the Domain value is the host itself or one of its parents, not a deeper name or a host in another branch (rule 2).
  5. Check whether each cookie sets Expires, Max-Age, or both, and that any values you set agree.
  6. If you use ASP.NET Core, check whether the app sets a session timeout or uses the default value of 20 minutes (Microsoft).
  7. Confirm that session IDs have at least 64 bits of entropy, counted in bits rather than characters (OWASP).

Then test the flow in a fresh browser. Sign up on the signup host, open the app host in the same browser, and check that you are still logged in. Repeat the test after any change to a cookie's Domain value, lifetime, or session settings.

Ask four questions before the launch, and expect a clear answer to each

Check the host of the response carrying the Set-Cookie header. That host determines which parent Domain values it can set.

What exact Domain value does that response use, and is it the shared parent?

For signup.example.com and app.example.com, the shared parent is example.com. Check the actual header rather than assuming its value.

Each cookie can have only one Domain value. Review each cookie separately.

Max-Age takes precedence when both are present. Cookie lifetime can affect how long the user stays logged in; check the session separately.

Sources and check date

Related posts

More posts