Cookies can be created by a server that sends one or more Set-Cookie headers with the response. The browser usually stores the cookie and sends it with requests made to the same server inside a Cookie HTTP header. 🥷🏻
Set-Cookie: <cookie-name>=<cookie-value>
Cookies are Mainly of 2 Types:
Expires attribute, or after a period of time specified by the Max-Age attribute.<aside>
🍪 When you set an Expires date and time, they're relative to the client the cookie is being set on, not the server.
</aside>
If the sum of the lengths of cookie-name and cookie-value is more than 4096 bytes, the cookie is ignored entirely.
__Secure- prefix: Cookies with names starting with __Secure- (dash is part of the prefix) must be set with the secure flag from a secure page (HTTPS). Else the cookie will be Rejected.
__Host- prefix: Cookies with names starting with __Host- must be set with the secure flag, must be from a secure page (HTTPS), must not have a domain specified (and therefore, are not sent to subdomains), and the path must be /.
Additionally the server is vulnerable to an attacker that purposefully mis-capitalizes a cookie in order to impersonate a prefixed cookie.
For example: A site already has a cookie __Secure-SID=12345 and by some means an attacker sends the following Set-Cookie header field for the site (Hacker found some bug by which he can make the server send desired set cookie header) to a UserAgent which checks prefixes case-sensitively. (Any browser that checks Case Sensitivity for cookies)
Set-Cookie: __SeCuRe-SID=evil
The next time a user visits the site the UserAgent will send both cookies:
Cookie: __Secure-SID=12345; __SeCuRe-SID=evil
The server, being case-insensitive, won't be able to tell the difference between the two cookies allowing the attacker to compromise the site.
To prevent these issues, UA’s MUST match cookie name prefixes case-insensitive.
Note: Cookies with different names are still considered separate by UA’s. So both
__Secure-foo=barand__secure-foo=bazcan exist as distinct cookies simultaneously and both would have the requirements of the__Secure-prefix applied.
If a Site sets a cookie without any Expiration Date/Time it is treated as a Session cookie:
A session cookie is a temporary cookie that is stored in the browser's memory during a user's browsing session. It does not have an expiration date specified, and it is discarded once the browser is closed or the session ends. Session cookies are commonly used to maintain user-specific information or session state while the user interacts with a website.
A normal cookie, unlike a session cookie, has an expiration date set in the future. It remains on the user's device even after the browsing session ends and persists until the specified expiration date is reached.