Skip to main content
MediumAI codeOWASP A05:2021CWE-284

How to fix: Cookie Set Client-Side with Overly Broad Domain

A page script sets a cookie via document.cookie with a broad leading-dot domain attribute (e.g., domain=.example.com) that shares it across all subdomains. If any subdomain is compromised or allows user content, this exposes the cookie to that subdomain. The equivalent Set-Cookie response header case is covered separately by the cookie-domain-broad check.

Why it matters

A cookie scoped to .example.com is accessible from attacker.example.com, staging.example.com, and any other subdomain. A subdomain takeover or user-content subdomain (user.example.com) can steal the cookie from all other subdomains.

AI-generated client-side session code sometimes sets cookies with the parent domain via document.cookie for 'SSO across subdomains'. While this has legitimate uses, it significantly expands the attack surface for cookie theft.

How to fix it

  1. 1Scope cookies to the specific domain that needs them unless cross-subdomain sharing is explicitly required.
  2. 2For session cookies: prefer Domain=example.com (without dot prefix is fine in modern browsers).
  3. 3Audit all subdomains for subdomain takeover risks before setting broad domain cookies.
  4. 4Prefer setting session cookies server-side (Set-Cookie with HttpOnly) over document.cookie.

Code examples

Scoped cookie

TypeScript
// Bad (AI-generated, shared across all subdomains)
document.cookie = 'session=' + token + '; domain=.example.com; path=/';

// Good (scoped to this host)
document.cookie = 'session=' + token + '; path=/; Secure; SameSite=Strict';
// Omitting the domain attribute scopes the cookie to the exact host

References

Detected automatically by VulnRadar. The check id is vibe-insecure-cookie-domain, stable across every scan so you can filter or diff on it.

See every ai code check

Questions

How severe is Cookie Set Client-Side with Overly Broad Domain?
VulnRadar rates this medium severity in the ai code category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id vibe-insecure-cookie-domain, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does Cookie Set Client-Side with Overly Broad Domain map to?
It maps to CWE-284. In the OWASP Top 10 it falls under A05:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for cookie set client-side with overly broad domain

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

Cookie Set Client-Side with Overly Broad Domain | VulnRadar