Home/Glossary/HTTP Cookie

HTTP Cookie

Small pieces of data stored by web browsers, used for session management, personalization, and tracking.

Web SecurityAlso called: "browser cookie", "web cookie"

Cookies enable stateful interactions over the stateless HTTP protocol.

Cookie attributes

  • Domain: Which domains can access the cookie.
  • Path: Which URL paths can access it.
  • Expires/Max-Age: When the cookie expires.
  • Secure: Only sent over HTTPS.
  • HttpOnly: Inaccessible to JavaScript (XSS protection).
  • SameSite: CSRF protection (Strict, Lax, None).

Types of cookies

  • Session: Deleted when browser closes.
  • Persistent: Saved until expiration date.
  • First-party: Set by the visited domain.
  • Third-party: Set by external domains (ads, analytics).

Use cases

  • Authentication tokens (session IDs).
  • Shopping cart persistence.
  • User preferences.
  • Analytics and tracking.

Security concerns

  • Session hijacking: Steal cookies to impersonate users.
  • CSRF: Force actions using victim's cookies.
  • XSS: Steal cookies via JavaScript.

Best practices

  • Set Secure and HttpOnly flags.
  • Use SameSite=Strict for sensitive cookies.
  • Encrypt sensitive cookie data.
  • Implement CSRF tokens.