Skip to main content

What are Cookies?

Cookies are small pieces of data that websites store in your browser to remember information between visits. They’re essential for keeping users logged in, storing preferences, and tracking sessions.
MDN Web DocsRead more on HTTP cookies.

Session Cookies

  • Purpose: Temporary storage during browsing session
  • Examples: PHPSESSID, session_id, _session
  • Lifetime: Until browser closes
  • Security: Critical for authentication

Persistent Cookies

  • Purpose: Remember users across visits
  • Examples: remember_me, user_preferences
  • Lifetime: Set expiration date
  • Security: Can be convenience vs. security tradeoff

Tracking Cookies

  • Purpose: Analytics and advertising
  • Examples: _ga (Google Analytics), fbp (Facebook)
  • Lifetime: Often months or years
  • Security: Privacy considerations

Security Cookies

  • Purpose: CSRF protection, security tokens
  • Examples: csrf_token, __Host-session
  • Lifetime: Varies by purpose
  • Security: Critical for application security

How Cookies Work on the Web

  1. Server sends cookie: Via Set-Cookie header
  2. Browser stores it: According to domain/path rules
  3. Browser sends it back: On every matching request
  4. Server reads cookie: To identify user/session

Using Cookies in Policies

Basic Examples

Require authentication cookie:
Detect cookie tampering:

Advanced Patterns

Session fixation detection:
Rate limit by session:
Cookie SecurityNever trust cookie values for authorization decisions! Cookies can be modified by users. Always verify server-side.

Security Patterns

Detecting Session Hijacking

Best Practices

DO:

  • Validate cookie formats - Ensure expected structure
  • Monitor cookie combinations - Unusual sets might indicate attacks
  • Track cookie age - Very old sessions might be compromised
  • Check cookie presence - Missing auth cookies on protected resources
  • Use with other signals - Combine with IP, user agent for better detection

DON’T:

  • Trust cookie values - Users can modify them
  • Store sensitive data - Even encrypted, it’s risky
  • Block missing cookies carelessly - Some pages don’t need them
  • Ignore cookie size - Large cookies can cause issues
  • Forget about subdomains - Cookie scope matters

Common Attack Patterns

Privilege Escalation

Session Fixation

Working with Authentication

Multi-Factor Authentication Check

Remember Me Security

Investigation TipsWhen investigating suspicious activity:
  1. Check cookie creation time vs. activity time
  2. Look for impossible cookie combinations
  3. Compare cookie values with server records
  4. Track cookie modifications over time

Browser Behavior Quirks

Third-Party Cookies

  • Increasingly blocked by browsers
  • May not be sent in embedded contexts
  • Consider when building policies
  • ~4KB per cookie
  • ~50 cookies per domain
  • Browsers handle limits differently

Troubleshooting

“Cookie policy not matching”
  • Check exact cookie name (case-sensitive)
  • Verify cookie is sent on this path
  • Confirm domain/subdomain scope
  • Check SameSite restrictions
“Blocking legitimate users”
  • Users might have cookies disabled
  • Private browsing affects cookies
  • Cookie might be expired
  • Browser extensions might block cookies

Integration Patterns

Combine with IP Tracking

API vs. Browser Detection

  • Headers - Cookies are sent via Cookie header
  • Client IP - Often checked together for session security
  • User Agent - Changes might indicate session hijacking
  • Request Path - Different paths need different cookies