Cookie options are attributes on Set-Cookie that control lifetime, scope, and security. Learn Expires vs Max-Age, Domain and Path scoping, Secure/HttpOnly/SameSite protections, and when to use Partitioned cookies.
What are the options (attributes) in a cookie?
The core idea
A cookie is not just name=value. Real behavior is controlled by attributes in the Set-Cookie header: lifetime, which paths/domains can receive it, and how strongly it is protected against client-side attacks and cross-site request abuse.
In interviews, high-quality answers group cookie options into three buckets: lifetime, scope, and security.
Set-Cookie: sid=abc123; Path=/; HttpOnly; Secure; SameSite=Lax
Attribute | What it controls | Common values | Key note |
|---|---|---|---|
| Absolute expiry date/time | HTTP-date string | Creates persistent cookie until that date. |
| Relative lifetime in seconds |
| Often preferred; when both are present, Max-Age takes precedence. |
| Which hosts receive the cookie |
| If omitted, cookie is host-only (more restrictive). |
| URL path scope |
| Limits sending by path, but is not a security boundary by itself. |
| Transport requirement | flag | Cookie sent only over HTTPS. |
| JavaScript access | flag | Blocks |
| Cross-site sending policy |
| Reduces CSRF risk; |
| Storage partitioning for cross-site contexts | flag | Used for CHIPS-style isolation; requires |
Lifetime options: Expires vs Max-Age
- No
Expires/Max-Agemeans a session cookie (typically removed when browser session ends). Expiresuses an absolute timestamp.Max-Ageuses relative seconds and is less timezone-sensitive.
Set-Cookie: theme=dark; Max-Age=2592000; Path=/; SameSite=Lax
Set-Cookie: promo=on; Expires=Wed, 31 Dec 2026 23:59:59 GMT; Path=/
Scope options: Domain and Path
Use the narrowest scope possible. Over-broad cookies increase accidental leakage across subdomains or app sections.
Set-Cookie: hostOnly=1; Path=/
# No Domain attribute -> only current host receives it
Set-Cookie: shared=1; Domain=example.com; Path=/
# Sent to example.com and subdomains (broader scope)
Security options that matter most
For auth/session cookies, baseline hardening is usually:
HttpOnlyto reduce token exposure via XSS.Secureto avoid plaintext transport.SameSitepolicy aligned with your cross-site needs (oftenLaxorStrict).
Set-Cookie: sessionId=abc123; Path=/; HttpOnly; Secure; SameSite=Lax
Important frontend limitation
document.cookie can set basic attributes like Path, Expires, and SameSite (browser support dependent), but it cannot set HttpOnly.
If you need secure auth cookies, set them server-side via Set-Cookie response headers.
// Client-side cookie write (limited control)
document.cookie = 'theme=dark; Path=/; Max-Age=2592000; SameSite=Lax';
// HttpOnly cannot be set from JavaScript.
Use case | Suggested attributes | Why |
|---|---|---|
Session authentication |
| Balances usability with strong baseline CSRF/XSS protection. |
Cross-site embedded app/session |
| Allows cross-site sending with explicit secure transport. |
UI preference cookie |
| Persistent behavior without over-scoping sensitive state. |
Interview one-liner
Cookie options are Set-Cookie attributes that define lifetime, scope, and security; strong answers specifically mention Max-Age/Expires, Domain/Path, and Secure/HttpOnly/SameSite.
Practical scenario
An app stores a login session and a theme preference. The session cookie must be locked down for security, while the theme cookie can be broader and persistent.
Common pitfalls
- Missing
SecureorHttpOnlyon auth cookies. - Using
SameSite=NonewithoutSecure(rejected by modern browsers). - Setting overly broad
Domainvalues that expose cookies across subdomains. - Assuming
Pathalone provides strong security isolation.
Test cookie behavior in real browser flows: same-site navigation, cross-site redirect, embedded iframe context, and HTTPS-only environments to verify attributes behave as intended.
Think of cookie attributes like delivery rules on a package: when it expires, which addresses can receive it, and what security checks are required before handoff.
Use the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.