← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Insecure Cookie

Security CWE-1004 OWASP A2:2021 CVSS 5.4 PHP 5.0+ Beginner
debt(d5/e1/b3/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints.tools list includes semgrep, owasp-zap, and lighthouse — all specialist tools rather than default linters or compilers. The issue is not caught by the PHP interpreter or a standard linter; it requires running a SAST scanner (semgrep) or a security proxy (owasp-zap) to detect the missing flags.

e1 Effort Remediation debt — work required to fix once spotted

Closest to 'one-line patch or single-call swap' (e1). The quick_fix is a single call to session_set_cookie_params() with the correct flags. This is a one-line parameterised fix that resolves the core issue immediately, warranting e1.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The applies_to scope is web context only, and the fix is isolated to cookie/session configuration — typically one or two places in the codebase (session start, setcookie calls). It does not spread across the whole codebase or impose a persistent productivity tax on unrelated work streams.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception field directly states the canonical trap: developers believe HttpOnly alone prevents cookie theft, not realising the Secure flag is also required to prevent interception over plain HTTP. This is a well-documented gotcha that many developers learn only after encountering it, but it does not fully contradict intuition about a similar concept elsewhere — it is more of a partial understanding failure.

About DEBT scoring →

Also Known As

cookie security missing Secure flag missing HttpOnly flag

TL;DR

Cookies without HttpOnly, Secure, and SameSite flags are vulnerable to theft via XSS, network interception, and CSRF.

Explanation

HttpOnly prevents JavaScript from reading the cookie, blocking XSS-based session theft. Secure restricts the cookie to HTTPS connections, preventing interception on unencrypted networks. SameSite=Strict prevents the browser from sending the cookie on cross-site requests, mitigating CSRF. All three are required for session cookies. Use PHP's array form of setcookie() to set all flags in one call.

Common Misconception

Setting HttpOnly prevents all cookie theft. HttpOnly blocks JavaScript access but not network interception over plain HTTP. The Secure flag is also required to prevent transmission over unencrypted connections.

Why It Matters

Session cookies without proper flags can be stolen via XSS (missing HttpOnly), downgraded to HTTP and intercepted (missing Secure), or sent in cross-site requests for CSRF (missing SameSite).

Common Mistakes

  • Not setting HttpOnly on session cookies — XSS can then steal them via document.cookie.
  • Not setting the Secure flag — cookies are sent over plain HTTP if available.
  • Missing SameSite attribute — defaults vary by browser and may not prevent CSRF.
  • Setting overly long expiry on session cookies — a stolen cookie remains valid for months.

Code Examples

✗ Vulnerable
// Missing Secure, HttpOnly, SameSite
setcookie('session', $token);
✓ Fixed
setcookie('session', $token, [
    'expires'  => time() + 86400,
    'path'     => '/',
    'secure'   => true,       // HTTPS only
    'httponly' => true,       // not accessible to JavaScript
    'samesite' => 'Strict',   // or 'Lax' for cross-site navigation
]);

Added 15 Mar 2026
Edited 22 Mar 2026
Views 113
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings S 1 ping M 0 pings T 0 pings W 2 pings T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 1 ping T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 2 pings W 1 ping T 0 pings F 0 pings S 1 ping S 0 pings M 2 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 1 ping M
PetalBot 1
No pings yesterday
Scrapy 17 PetalBot 10 Amazonbot 9 Perplexity 7 Ahrefs 7 SEMrush 7 Bing 6 Google 5 Brave Search 2 Applebot 2 Majestic 1 ChatGPT 1 Meta AI 1 Twitter/X 1 Qwen 1
crawler 75 crawler_json 2
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Set session cookie with Secure, HttpOnly, SameSite=Lax flags: session_set_cookie_params(['secure'=>true,'httponly'=>true,'samesite'=>'Lax'])
📦 Applies To
PHP 5.0+ web
🔗 Prerequisites
🔍 Detection Hints
setcookie or session_set_cookie_params without secure=>true and httponly=>true flags
Auto-detectable: ✓ Yes semgrep owasp-zap lighthouse
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Line
CWE-614 CWE-1004 CWE-1275


✓ schema.org compliant