What is Cookie?

What is Cookie

Cookies are a key part of the HTTP protocol that most web applications rely on. Frequently they can be used as a vehicle for exploiting vulnerabilities. The cookie mechanism enables the server to send items of data to the client, which the client stores and resubmits to the server. Unlike the other types of request parameters (those within the URL query string or the message body), cookies continue to be resubmitted in each subsequent request without any particular action required by the application or the user.

A server issues a cookie using the Set-Cookie response header, as you have seen:

Set-Cookie: tracking=tI8rk7joMx44S2Uu85nSWc

The user’s browser then automatically adds the following header to subsequent requests back to the same server:

Cookie: tracking=tI8rk7joMx44S2Uu85nSWc

Cookies normally consist of a name/value pair, as shown, but they may consist of any string that does not contain a space. Multiple cookies can be issued by using multiple Set-Cookie headers in the server’s response. These are submitted back to the server in the same Cookie header, with a semicolon separating different individual cookies.

In addition to the cookie’s actual value, the Set-Cookie header can include any of the following optional attributes, which can be used to control how the browser handles the cookie:

expires: sets a date until which the cookie is valid. This causes the browser to save the cookie to persistent storage, and it is reused in subsequent browser sessions until the expiration date is reached. If this attribute is not set, the cookie is used only in the current browser session.

domain: specifies the domain for which the cookie is valid. This must be the same or a parent of the domain from which the cookie is received.

path: specifies the URL path for which the cookie is valid.

secure: If this attribute is set, the cookie will be submitted only in HTTPS requests.

HttpOnly: If this attribute is set, the cookie cannot be directly accessed via client-side JavaScript.

Note: Each of these cookie attributes can impact the application’s security. The primary impact is on the attacker’s ability to directly target other users of the application.