HTTP cookie is a key value pair that a web server can send to the user's browser in HTTP response headers. The browser reads the response headers and stores the cookies so that it can automatically return the cookies as request headers in subsequent requests to the same server and path from where the cookie was received.

Cookies are very often used for session management as HTTP is a stateless protocol.

How cookies are created on the browser

ie a domain can create cookies both on the server and the client side.

Adding cookies to the HTTP request

The "Cookie" request header is added by the browser in subsequent requests to the same path from where the cookie was received.

Cookie: yummy_cookie=choco; tasty_cookie=strawberry

Cookie Attributes - Attributes that determine if cookies will be added to a request based on the "origin" of the request and "where" cookies are sent.

Based on the above four rules let us look at a few examples/ramifications.

Cookie Attributes - Attributes that determine the lifetime

Cookie Attributes - Attributes that determine access

Same-origin policy and cookies

In a nutshell, the Same-Origin-Policy says that only a.com may send script requests to a.com.  Cross-origin loading of page resources is generally permitted. For example, the SOP allows embedding of images via the <img> tag. However, javascript will not be able to access the image.

While subdomains are technically different origin, the same-origin policy is relaxed when dealing with cookies. It often does not differentiate between subdomain and domain.

This essentially implies that same-origin-policy prevents cross-origin reads via document.cookie but does not prevent subdomains from creating/overwriting cookies that go to domain. The overwriting of cookies is explained very well in the post https://security.stackexchange.com/questions/12412/what-cookie-attacks-are-possible-between-computers-in-related-dns-domains-exa

So same-origin-policy will block

In web applications where there is a subdomain per customer, there is no good way to stop one customer's website from creating /overwriting cookies that go to another customer's website.ie customerA.blogging.com will always be able to create a cookie that goes to customerB.blogging.com by setting the domain to blogging.com.  Therefore, if you use a different subdomain per customer, you may need your server-side code to be entirely under your control and be free of vulnerabilities .Read more https://security.stackexchange.com/questions/7451/is-security-increased-by-using-a-subdomain-per-customer-in-a-web-app/7567#7567

 

Third party cookies

The cookies that are set on your browser from other domains than the one you are currently in. In other words third party cookies are set by cross origin/site/domain requests.

ie while the server hosting a web page sets first-party cookies, the page may contain images or other components stored on servers in other domains (for example, ad banners), which will set third-party cookies if clicked/accessed .

For example

Some browsers block third party cookies (ie third party cookies cannot be set). Chrome is planning to stop supporting third party cookies in 2022.

Other important points

The XMLHttpRequest.withCredentials property is a boolean value that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates. Setting withCredentials has no effect on same-site requests.In addition, this flag is also used to indicate when cookies are to be ignored in the response. The default is false. Note that this is true regardless of Access-Control- header values.

Cookies do not provide isolation by port Ie, cookies for a given host are shared across all the ports on that host, even though the usual "same-origin policy" used by web browsers isolates content retrieved via different ports.

Cookies do not provide isolation by scheme

Cross-domain cookies are not allowed (i.e. Cookies set by site A.com will never be sent to B.com). But once a cookie is set by site A.com, you can send that cookie even in cross domain requests from site B.com to A.com

 

Test yourself

Question > will a same-site, cross-origin request from subdomain.example.com to example.com go with cookie c1 which is a hostonly cookie?

Question >  https://portswigger.net/web-security/cors/same-origin-policyDue to legacy requirements, the same-origin policy is more relaxed when dealing with cookies, so they are often accessible from all subdomains of a site even though each subdomain is technically a different origin. You can partially mitigate this risk using the HttpOnly cookie flag

How will httponly cookie flag mitigate risk?

Read More