Information Security Stack Exchange is a question and answer site for information security professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have been reading about cross-subdomain cookie attacks here.

A quick overview of how it works (from Wikipedia):

  1. A web site hands out subdomains to untrusted third parties
  2. One such party, Mallory, who now controls, lures Alice to her site
  3. A visit to sets a session cookie with the domain on Alice's browser
  4. When Alice visits, this cookie will be sent with the request, as the specs for cookies states, and Alice will have the session specified by Mallory's cookie.
  5. If Alice now logs on, Mallory can use her account.

We are building a web app where customers will have their own subdomains:

Each customer will be able to upload template files which can contain javascript (for client side interaction).

Since javascript can be used to read/write cookies, how can we prevent the session fixation attack above? Note that we are only storing the session id in the cookie.

Sites like also allows users to inject their own javascript into their on pages on their own subdomain. Since it would be next to impossible to try and filter out javascript statements that set/read cookies in uploaded template files, how can we mitigate this attack vector?

share|improve this question

The specific scenario can be prevented easily: Create a new sessionid on login.

Please note that the issues exists across domains, if the sessionid is an url-parameter.

Generating a new sesionid, however, not be sufficient to prevent other kinds of attack: Once Alice is logged in and visits Mallory's subdomain, will the cookie be transmitted?

It is common practice to use a completely different domain for all trusted activity. For example Google uses for trusted activities and * for untrusted sites.

share|improve this answer
+1 for "use a completely different domain for all trusted activity." It's the only way, and this problem the question is presenting is just another nail in the coffin of shared-domain hosting IMO. – TildalWave Apr 6 '13 at 17:13
I am currently forcing session ids to be stored in cookies. All session data is stored on the server and we are regenerating the id when the user logs on and logs off. The idea of something like * is interesting. But in this case, wouldn't still be able to attack – F21 Apr 6 '13 at 23:49
While this may prevent the attacker from logging in as the victim, it does not prevent the opposite (login CSRF attacks):… – Gili Jun 5 '14 at 19:24

The safest answer is to use entirely different domains.

If you use subdomains of the same domain, start by learning about the details of the possible attacks. There's a lot already written about this, on this site. See, e.g., What cookie attacks are possible between computers in related DNS domains (* , Preventing insecure webapp on subdomain compromise security of main webapp, User-specific Subdomains : JavaScript security, my answer to Is security increased by using a subdomain per customer in a web-app?.

If you use subdomains of the same domain, here are some steps you can take to protect yourself: store all state on the server side (do not use cookies to store state on the client; instead, the only cookie you use should be a session ID); create a new session ID on login and logout; make sure the cookie is scoped to the subdomain (e.g., its scope should be, not; check at the server side to make sure you don't receive multiple cookies with the name; make sure to protect yourself against session fixation; if you use CORS, be very careful with your cross-domain policy to make sure you don't allow cross-subdomain requests.

share|improve this answer

I just had an idea. We can borrow an idea from the Encrypted Token Pattern: encrypt the cookie value!

If the cookie is only accessed by the server, you can use cheap symmetric encryption. Otherwise, use the more expensive public-key encryption.

This will allow you to detect when malicious subdomains delete your cookie and/or detect their attempts to write fake cookies.

share|improve this answer
This wouldn't really help if we store only the session id in the cookie and store session data on the server. – F21 Jun 10 '14 at 23:11
@F21: You're right. You can play all kind of tricks (such as binding each session ID to an IP address) but ultimately is a safer solution. That being said, it is still vulnerable to login CSRF attacks. – Gili Jun 11 '14 at 20:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.