19.09.2017 Views

the-web-application-hackers-handbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7 n Attacking Session Management 245<br />

The cookie mechanism allows a server to specify both <strong>the</strong> domain and <strong>the</strong><br />

URL path to which each cookie will be resubmitted. To do this, it uses <strong>the</strong> domain<br />

and path attributes that may be included in <strong>the</strong> Set-cookie instruction.<br />

Cookie Domain Restrictions<br />

When <strong>the</strong> <strong>application</strong> residing at foo.wahh-app.com sets a cookie, <strong>the</strong> browser<br />

by default resubmits <strong>the</strong> cookie in all subsequent requests to foo.wahh-app<br />

.com, and also to any subdomains, such as admin.foo.wahh-app.com. It does<br />

not submit <strong>the</strong> cookie to any o<strong>the</strong>r domains, including <strong>the</strong> parent domain<br />

wahh-app.com and any o<strong>the</strong>r subdomains of <strong>the</strong> parent, such as bar.wahh-app.com.<br />

A server can override this default behavior by including a domain attribute<br />

in <strong>the</strong> Set-cookie instruction. For example, suppose that <strong>the</strong> <strong>application</strong> at foo<br />

.wahh-app.com returns <strong>the</strong> following HTTP header:<br />

Set-cookie: sessionId=19284710; domain=wahh-app.com;<br />

The browser <strong>the</strong>n resubmits this cookie to all subdomains of wahh-app.com,<br />

including bar.wahh-app.com.<br />

NOTE A server cannot specify just any domain using this attribute. First, <strong>the</strong><br />

domain specified must be ei<strong>the</strong>r <strong>the</strong> same domain that <strong>the</strong> <strong>application</strong> is running<br />

on or a domain that is its parent (ei<strong>the</strong>r immediately or at some remove).<br />

Second, <strong>the</strong> domain specified cannot be a top-level domain such as .com or<br />

.co.uk, because this would enable a malicious server to set arbitrary cookies<br />

on any o<strong>the</strong>r domain. If <strong>the</strong> server violates one of <strong>the</strong>se rules, <strong>the</strong> browser<br />

simply ignores <strong>the</strong> Set-cookie instruction.<br />

If an <strong>application</strong> sets a cookie’s domain scope as unduly liberal, this may<br />

expose <strong>the</strong> <strong>application</strong> to various security vulnerabilities.<br />

For example, consider a blogging <strong>application</strong> that allows users to register,<br />

log in, write blog posts, and read o<strong>the</strong>r people’s blogs. The main <strong>application</strong> is<br />

located at <strong>the</strong> domain wahh-blogs.com. When users log in to <strong>the</strong> <strong>application</strong>,<br />

<strong>the</strong>y receive a session token in a cookie that is scoped to this domain. Each user<br />

can create blogs that are accessed via a new subdomain that is prefixed by his<br />

username:<br />

herman.wahh-blogs.com<br />

solero.wahh-blogs.com<br />

Because cookies are automatically resubmitted to every subdomain within<br />

<strong>the</strong>ir scope, when a user who is logged in browses <strong>the</strong> blogs of o<strong>the</strong>r users,<br />

his session token is submitted with his requests. If blog authors are permitted<br />

to place arbitrary JavaScript within <strong>the</strong>ir own blogs (as is usually <strong>the</strong> case in

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!