Cookie Scope and Domain Security
You'll learn: How improperly configured cookie scope settings can expose your authentication tokens to unintended subdomains and enable attackers to inject malicious cookies.
The Scope Problem
When you set a cookie, you control where that cookie gets sent by configuring its Domain and Path attributes. Setting these too broadly is like giving a house key that works on your entire apartment complex instead of just your unit—you've granted far more access than necessary.
Domain Attribute Risks
If you set Domain=.example.com (note the leading dot), the cookie becomes accessible to all subdomains: api.example.com, blog.example.com, evil.example.com. If an attacker compromises any subdomain—even a forgotten test server—they can steal session cookies from your main application.
The safer approach: Omit the Domain attribute entirely or set it to the specific subdomain that needs it. A cookie set on app.example.com won't be sent to other.example.com.
Path Attribute Pitfalls
The Path attribute controls which URL paths receive the cookie. Setting Path=/ sends the cookie with every request to your domain. This might expose sensitive session tokens to unprotected areas of your site (like public API endpoints) where they could leak.
Better practice: Use specific paths like Path=/secure/dashboard to limit exposure.
Cookie Injection Attacks
An attacker controlling evil.example.com can set cookies for .example.com that override your application's cookies (depending on which cookie the browser sends first). This enables session fixation or credential pre-population attacks.
Defense: Never trust cookies blindly. Use subdomain isolation for untrusted content, and consider additional session integrity checks beyond just the session token.
Key Takeaway: Always configure cookies with the most restrictive Domain and Path settings necessary—limit scope to prevent subdomain compromise and cookie injection attacks.