Configure secure cookie settings
✓Works with OpenClaudeYou are a web security engineer. The user wants to configure secure cookie settings to prevent common vulnerabilities like XSS, CSRF, and cookie theft.
What to check first
- Verify your framework's cookie middleware or session library is installed (e.g.,
npm list express-sessionorpip list flask-session) - Check your current environment: development vs. production (cookies should differ between these)
- Identify where cookies are being set: middleware, route handlers, or authentication libraries
Steps
- Set
Secureflag totrueso cookies only transmit over HTTPS (never HTTP) - Set
HttpOnlyflag totrueto block JavaScript access viadocument.cookie, preventing XSS theft - Set
SameSiteattribute to'Strict'or'Lax'to prevent cross-site request forgery (CSRF) - Define
Domainexplicitly if needed; omit it to restrict to current domain only - Set an appropriate
Max-AgeorExpiresto avoid persistent cookies when not necessary - Use
Path=/or a specific path to limit which URLs receive the cookie - Test with browser DevTools (Application → Cookies) to verify all flags are present
- Configure different settings per environment: stricter in production, relaxed in development if needed
Code
// Express.js with express-session example
const express = require('express');
const session = require('express-session');
const app = express();
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
// Core security flags
secure: process.env.NODE_ENV === 'production', // HTTPS only in production
httpOnly: true, // Block JavaScript access
sameSite: 'strict', // Prevent CSRF: use 'lax' for less strict
// Additional restrictions
domain: process.env.NODE_ENV === 'production' ? '.example.com' : undefined,
path: '/',
maxAge: 1000 * 60 * 60 * 24 // 24 hours in milliseconds
}
}));
// Alternative: Set cookie manually in route handler
app.get('/login', (req, res) => {
res.cookie('authToken', 'token_value', {
secure: true, // HTTPS only
httpOnly: true, // JavaScript cannot access
sameSite: 'strict', // No cross-site requests
maxAge: 3600000, // 1 hour
path: '/'
});
res.send('Cookie set securely');
});
// Python Flask example
from flask import Flask, session
from datetime import timedelta
import os
app = Flask(__name__)
app.config['SESSION_COOKIE_SECURE'] = os.getenv('FLASK_ENV') == 'production'
app.config['SESSION_
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Security Skills
Other Claude Code skills in the same category — free to download.
Dependency Audit
Audit dependencies for known vulnerabilities
Secret Scanner
Scan codebase for leaked secrets and credentials
CSP Generator
Generate Content Security Policy headers
Input Sanitizer
Add input sanitization to prevent injection attacks
Auth Middleware
Create authentication middleware
RBAC Setup
Implement role-based access control
CSRF Protection
Add CSRF protection to forms and APIs
Security Headers
Configure security headers (HSTS, X-Frame-Options, etc.)
Want a Security skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.