Skip to main content
Home/Blog/What Should I Do if I Find Insecure Cookies on My Website?
Security

What Should I Do if I Find Insecure Cookies on My Website?

Step-by-step remediation guide for fixing insecure cookie configurations, from adding security attributes to testing implementations across frameworks.

By Inventive HQ Team•
What Should I Do if I Find Insecure Cookies on My Website?

Cookie Analyzer

Analyze HTTP cookies for security issues and privacy concerns

Open the full Cookie Analyzer tool →
Loading interactive tool...

If a scanner or audit flagged insecure cookies on your site, fix them in priority order: add HttpOnly to session and authentication cookies first (it blocks XSS-based session theft), add the Secure flag next so cookies only travel over HTTPS, then set an appropriate SameSite value to reduce CSRF risk while retaining separate CSRF defenses. After that, cap session lifetimes at 30 to 120 minutes, move any sensitive data out of cookie values into server-side storage, and lock critical cookies with a __Host- prefix. Almost all of this is a configuration change in your framework's session settings, not a rewrite, and you can verify every fix in browser DevTools under Application then Cookies.

That is the summary an AI Overview gives you. What it cannot show you is which fix to reach for first when a scanner dumps twenty findings at once, why the Secure flag can silently log your users out, or which order is safe to deploy without breaking sessions. This guide carries the decision map, a symptom-to-fix table for the four things that break after you tighten cookies, a copy-paste remediation matrix, and framework-complete configs for Express, Django, PHP, ASP.NET, and Rails.

The Three Attributes and the Attack Each One Blocks

Every cookie remediation comes down to three attributes plus a lifetime. Each one closes a different attack. This is the map to keep open while you work.

How HttpOnly, Secure, and SameSite each block a different cookie attack HttpOnly blocks XSS-based session theft, the Secure flag blocks man-in-the-middle interception over HTTP, and SameSite=Lax reduces some CSRF exposure. A short session lifetime shrinks the window a stolen cookie stays valid. One cookie, three defenses Each attribute stops a distinct attack. Session cookies need all three. HttpOnly Hides cookie from JavaScript Blocks: XSS session theft Malicious script runs document.cookie → empty Attacker gets nothing Secure Sends cookie over HTTPS only Blocks: MITM interception HTTP request No cookie on the wire to sniff SameSite=Lax Not sent on cross-site subrequests Helps limit: CSRF evil.com forges request cookie withheld Forged action rejected Then: shrink the lifetime A short expiry shrinks the window a stolen cookie stays valid. 30-120 min Safe Never-expiring = open door

Immediate Action: Prioritize Critical Issues

Not all cookie security issues carry equal risk. Prioritize remediation based on severity and exploitability.

Critical (Fix Immediately)

1. Session Cookies Without HttpOnly

  • Risk: XSS-based session hijacking
  • Impact: Complete account compromise
  • Timeline: Fix within 24-48 hours

2. Sensitive Data in Cookie Values

  • Risk: Data exposure, regulatory violation
  • Impact: Privacy breach, compliance penalties
  • Timeline: Fix immediately (hours)

3. Missing Secure Flags on HTTPS Sites

  • Risk: Man-in-the-middle attacks
  • Impact: Session theft on unsecured networks
  • Timeline: Fix within 48-72 hours

High Priority (Fix Within 1 Week)

4. Session Cookies With SameSite=None

  • Risk: CSRF attacks
  • Impact: Unauthorized actions on behalf of users
  • Timeline: Fix within 1 week

5. Extremely Long Cookie Expiration Times

  • Risk: Extended exploitation window
  • Impact: Stolen cookies remain valid indefinitely
  • Timeline: Fix within 1 week

Medium Priority (Fix Within 1 Month)

6. Missing SameSite Attributes (Pre-2020 Code)

  • Risk: CSRF in older browsers
  • Impact: Limited (modern browsers default to Lax)
  • Timeline: Fix during next maintenance window

7. Preference Cookies Without Secure Flag

  • Risk: Low (non-sensitive data)
  • Impact: Minimal
  • Timeline: Fix when convenient

Step-by-Step Remediation Process

Follow this systematic approach to fix cookie security issues safely.

Step 1: Audit All Cookies

Enumerate Every Cookie:

  1. Open browser DevTools (Application > Cookies)
  2. Navigate through all website pages and user flows
  3. Document each cookie's current attributes
  4. Identify which cookies are security-critical

Create Remediation Matrix:

Cookie Name | Current State | Issues | Required Changes | Priority
------------|---------------|---------|------------------|----------
session_id  | No HttpOnly   | XSS risk| Add HttpOnly     | Critical
csrf_token  | No SameSite   | CSRF    | Add SameSite     | High
preferences | No Secure     | Minor   | Add Secure       | Low

Step 2: Add Secure Flag

Add Secure to ALL cookies on HTTPS websites.

Express.js (Node.js):

// Before:
res.cookie('session_id', sessionId);

// After:
res.cookie('session_id', sessionId, {
  secure: true,  // HTTPS only
  httpOnly: true,
  sameSite: 'lax'
});

PHP:

// Before:
setcookie('session_id', $sessionId);

// After:
setcookie('session_id', $sessionId, [
    'secure' => true,
    'httponly' => true,
    'samesite' => 'Lax',
    'expires' => time() + 3600
]);

Django (Python):

# settings.py

# Before: (defaults may be insecure)

# After:
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

ASP.NET Core (C#):

// Startup.cs or Program.cs

services.AddSession(options =>
{
    options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
    options.Cookie.HttpOnly = true;
    options.Cookie.SameSite = SameSiteMode.Lax;
});

Ruby on Rails:

# config/initializers/session_store.rb

Rails.application.config.session_store :cookie_store,
  key: '_app_session',
  secure: true,
  httponly: true,
  same_site: :lax

Step 3: Add HttpOnly to Authentication Cookies

Critical for preventing XSS-based session theft.

Identify Authentication Cookies:

  • Session identifiers (session_id, PHPSESSID, etc.)
  • Authentication tokens (auth_token, jwt, etc.)
  • Remember-me tokens
  • Any cookie used for maintaining logged-in state

Add HttpOnly:

Express.js:

app.use(session({
  secret: process.env.SESSION_SECRET,
  cookie: {
    httpOnly: true,  // JavaScript cannot access
    secure: true,
    sameSite: 'lax',
    maxAge: 3600000
  }
}));

PHP:

session_set_cookie_params([
    'httponly' => true,
    'secure' => true,
    'samesite' => 'Lax',
    'lifetime' => 3600
]);
session_start();

Django:

SESSION_COOKIE_HTTPONLY = True

ASP.NET Core:

options.Cookie.HttpOnly = true;

Important: DO NOT add HttpOnly to cookies that require JavaScript access (preferences, UI state, client-side analytics). Only authentication/session cookies need HttpOnly.

Step 4: Configure SameSite Appropriately

Choose based on cookie purpose and security requirements.

For Session Cookies (Recommended: Lax or Strict):

Express.js:

cookie: {
  sameSite: 'lax',  // or 'strict' for maximum security
  secure: true,
  httpOnly: true
}

PHP:

[
    'samesite' => 'Lax',  // or 'Strict'
    'secure' => true,
    'httponly' => true
]

Django:

SESSION_COOKIE_SAMESITE = 'Lax'  # or 'Strict'
CSRF_COOKIE_SAMESITE = 'Strict'   # CSRF tokens should be Strict

ASP.NET Core:

options.Cookie.SameSite = SameSiteMode.Lax;  // or Strict

Choosing Between Lax and Strict:

Use Lax when:

  • Users commonly arrive via links from other sites
  • You need reasonable UX without friction
  • Standard web application

Use Strict when:

  • Maximum security required
  • Banking, financial, administrative applications
  • Can tolerate users being logged out when arriving via external links

Use None only when:

  • Cookie must work in third-party contexts
  • Embedded widgets requiring authentication
  • Always pair with Secure flag
sameSite: 'none',
secure: true  // Required with SameSite=None

Implement reasonable timeouts for sensitive cookies.

Session Cookies:

// Express.js - 1 hour expiration
maxAge: 3600000  // milliseconds

// PHP - 1 hour
'expires' => time() + 3600

// Django - 1 hour
SESSION_COOKIE_AGE = 3600

// ASP.NET Core - 1 hour
options.IdleTimeout = TimeSpan.FromMinutes(60);

Recommended Expiration Times:

  • Session cookies: 30-120 minutes
  • Remember-me tokens: 1-2 weeks
  • Preference cookies: 6-12 months
  • Analytics cookies: Follow provider recommendations

Step 6: Remove Sensitive Data from Cookies

Never store sensitive information directly in cookies.

Bad Pattern:

// NEVER DO THIS
res.cookie('user_data', JSON.stringify({
  username: 'john',
  password: 'abc123',     // NEVER store passwords
  ssn: '123-45-6789',     // NEVER store SSNs
  credit_card: '4111...'  // NEVER store payment data
}));

Good Pattern:

// Store only session ID
res.cookie('session_id', generateRandomSessionId(), {
  secure: true,
  httpOnly: true,
  sameSite: 'lax',
  maxAge: 3600000
});

// Store sensitive data server-side
req.session.userData = {
  username: 'john',
  userId: 12345
  // Other data stored server-side, referenced by session ID
};

Migration Strategy:

  1. Identify cookies containing sensitive data
  2. Create server-side session storage (Redis, database, memory)
  3. Store only session IDs in cookies
  4. Reference server-side data using session ID
  5. Delete old insecure cookies

Add __Secure- or __Host- prefixes for additional security.

For Critical Session Cookies:

// Express.js
res.cookie('__Host-session', sessionId, {
  secure: true,
  httpOnly: true,
  sameSite: 'strict',
  path: '/'  // Required for __Host- prefix
});

Framework-Specific Cookie Naming:

Express.js:

app.use(session({
  name: '__Host-session',
  // ...other options
}));

Django:

SESSION_COOKIE_NAME = '__Host-sessionid'
SESSION_COOKIE_PATH = '/'  # Do not set SESSION_COOKIE_DOMAIN

ASP.NET Core:

options.Cookie.Name = "__Host-session";

Benefits:

  • Browser enforces security requirements
  • Prevents misconfiguration
  • Signals security-conscious implementation

Testing Your Changes

Thorough testing ensures fixes don't break functionality.

Testing Checklist

1. Verify Cookie Attributes:

  • Open DevTools > Application > Cookies
  • Check each modified cookie has correct attributes
  • Confirm Secure, HttpOnly, SameSite set as intended

2. Test Core Functionality:

  • User login/logout
  • Session persistence across pages
  • Shopping cart functionality
  • User preference saving
  • Form submissions

3. Test Cross-Browser:

  • Chrome/Edge (Chromium)
  • Firefox
  • Safari
  • Mobile browsers (iOS Safari, Chrome Mobile)

4. Test Cross-Site Scenarios:

  • Links from external sites
  • Embedded content (if applicable)
  • Form submissions
  • AJAX requests

5. Test HTTPS Configuration:

  • All pages load over HTTPS
  • No mixed content warnings
  • Cookies only sent over HTTPS

Common Issues After Changes

Tightening cookies breaks four things, predictably. Match the symptom to the cause before you start reverting attributes at random.

SymptomRoot causeFix
Users can't stay logged inSecure flag added but some pages still serve over HTTP, so the cookie is never sent backForce HTTPS site-wide and 301-redirect HTTP to HTTPS; confirm no mixed content, then keep Secure
A front-end feature broke right after adding HttpOnlyJavaScript was reading the cookie via document.cookie; HttpOnly now hides itMove that value to a non-HttpOnly cookie or a server API; keep HttpOnly on the session cookie only
A third-party embed or SSO iframe stopped authenticatingSameSite=Lax/Strict blocks the cookie in cross-site contextSet SameSite=None; Secure on that specific integration cookie only, never site-wide
Users show as logged out when arriving from an external linkSameSite=Strict withholds the cookie on top-level cross-site navigationSwitch that cookie to SameSite=Lax for the standard UX/security balance
Everyone got logged out after deployYou renamed the cookie (e.g. added a __Host- prefix) or set Strict, which withholds cookies on inbound cross-site navigationCheck whether the cookie name changed; Strict alone does not invalidate the session

Framework-Specific Complete Examples

Express.js (Node.js) - Complete Secure Configuration

const express = require('express');
const session = require('express-session');
const helmet = require('helmet');

const app = express();

// Helmet for additional security headers
app.use(helmet());

// Session configuration
app.use(session({
  name: '__Host-session',
  secret: process.env.SESSION_SECRET,
  resave: false,
  saveUninitialized: false,
  cookie: {
    secure: true,  // Required for __Host-; serve over HTTPS
    httpOnly: true,
    sameSite: 'strict',
    path: '/',  // Required for __Host-; omit Domain
    maxAge: 3600000  // 1 hour
  }
}));

// Custom cookie setting
app.get('/set-preference', (req, res) => {
  res.cookie('preferences', req.body.prefs, {
    secure: true,
    sameSite: 'lax',
    maxAge: 31536000000  // 1 year
  });
  res.send('Preferences saved');
});

app.listen(3000);

Django (Python) - Complete Secure Configuration

# settings.py

# Force HTTPS
SECURE_SSL_REDIRECT = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Session cookie security
SESSION_COOKIE_NAME = '__Host-sessionid'
SESSION_COOKIE_PATH = '/'  # Do not set SESSION_COOKIE_DOMAIN
SESSION_COOKIE_SECURE = True
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SAMESITE = 'Strict'
SESSION_COOKIE_AGE = 3600  # 1 hour

# CSRF cookie security
CSRF_COOKIE_NAME = '__Host-csrftoken'
CSRF_COOKIE_PATH = '/'  # Do not set CSRF_COOKIE_DOMAIN
CSRF_COOKIE_SECURE = True
CSRF_COOKIE_HTTPONLY = False  # CSRF needs JS access for AJAX
CSRF_COOKIE_SAMESITE = 'Strict'

# Force cookie security
SESSION_EXPIRE_AT_BROWSER_CLOSE = False

PHP - Complete Secure Configuration

<?php
// config.php

// Secure session configuration
ini_set('session.cookie_httponly', 1);
ini_set('session.cookie_secure', 1);
ini_set('session.cookie_samesite', 'Lax');
ini_set('session.use_strict_mode', 1);
ini_set('session.cookie_lifetime', 3600);  // 1 hour

// Or using session_set_cookie_params (PHP 7.3+)
session_set_cookie_params([
    'lifetime' => 3600,
    'path' => '/',
    'secure' => true,
    'httponly' => true,
    'samesite' => 'Lax'
]);

session_name('__Host-session');
session_start();

// Custom cookie
setcookie('preferences', $value, [
    'expires' => time() + 31536000,
    'path' => '/',
    'secure' => true,
    'httponly' => false,
    'samesite' => 'Lax'
]);
?>

Deployment Strategy

Roll out cookie security changes safely to avoid disrupting users.

Phased Deployment Approach

Phase 1: Testing (1-2 days)

  • Deploy to development/staging environment
  • Test all functionality thoroughly
  • Fix any issues discovered

Phase 2: Canary Deployment (1-3 days)

  • Deploy to 5-10% of production users
  • Monitor error rates and user reports
  • Verify cookie attributes correct in production

Phase 3: Gradual Rollout (3-7 days)

  • Increase to 25%, then 50%, then 100%
  • Monitor at each stage
  • Roll back if significant issues arise

Phase 4: Monitoring (Ongoing)

  • Track session-related errors
  • Monitor authentication failures
  • Review security scanner reports

Rollback Plan

Prepare rollback procedure in case of issues:

  1. Revert to previous cookie configuration
  2. Clear affected users' cookies (force re-login)
  3. Diagnose issue in staging
  4. Fix and redeploy

Documentation and Communication

Update Documentation:

  • Document new cookie security configuration
  • Update developer guidelines
  • Create runbook for cookie security changes

Communicate Changes:

  • Inform development team
  • Update security documentation
  • Note changes in release notes

Train Team:

  • Educate developers on cookie security
  • Review common pitfalls
  • Establish code review standards

Conclusion

Fixing insecure cookies requires systematic approach: prioritize critical issues, implement security attributes correctly, test thoroughly, and deploy carefully. Most cookie security issues can be resolved through configuration changes in your web framework's session and cookie settings, requiring minimal code modifications.

The key attributes—Secure, HttpOnly, and SameSite—address different attack vectors and should be applied based on cookie purpose. Session cookies need all three, preference cookies need Secure and SameSite, and only third-party integration cookies should use SameSite=None.

After implementing fixes, verify changes with browser DevTools, test core functionality, and monitor production deployment carefully. Cookie security is foundational web security—investing time to fix these issues properly protects users and reduces attack surface significantly.

Need help identifying insecure cookies on your website? Use our Cookie Analyzer tool to automatically scan all cookies, flag security issues, and receive specific remediation recommendations for your cookie configuration.

Don't wait for a breach to act

Get a free security assessment. Our experts will identify your vulnerabilities and create a protection plan tailored to your business.