Back to cheatsheets
Web Security
Cookie Security Flags
Set-Cookie security best practices — HttpOnly, Secure, SameSite, and __Host- prefix.
What you'll learn
- Secure cookie settings
- Express.js cookie config
How to use these snippets
- Click the Copy button next to any snippet to copy it to your clipboard.
- Paste the code into your project, IDE, or terminal.
- Replace placeholder values (like
example.comoryour-secret) with your actual data. - Review the code comments for important security notes and caveats.
- Try the interactive related tool to see the concept in action.
Code Snippets
Copy-paste code snippets
2 ready-to-use snippets for cookie security flags. Click copy and paste into your project.
Secure cookie settings
// Recommended Set-Cookie header
Set-Cookie: session=abc123; HttpOnly; Secure; SameSite=Strict; Path=/; Max-Age=86400; __Host- prefix
// SameSite values:
// Strict — never send on cross-site requests (most secure)
// Lax — send on top-level navigation (default in modern browsers)
// None — always send (requires Secure, least secure)
Express.js cookie config
res.cookie('session', token, {
httpOnly: true, // no JS access
secure: true, // HTTPS only
sameSite: 'strict', // no cross-site
path: '/',
maxAge: 86400000, // 24h
signed: true // prevent tampering
});
// __Host- prefix requires:
// - Secure flag
// - Path=/
// - No Domain attribute
Tool
Related ShieldX tool
Try this concept hands-on with the interactive tool.
100% Free · No signup · No cost
Generate something unguessable right now
148 bits of entropy, a crack-time estimate you can trust, and a clipboard that clears itself after 30 seconds.
Part of the Toolly ecosystem · secure.toolly.site