How to fix: Rails Session Cookie Missing HttpOnly Flag
A Rails-style session cookie (matching the *_session naming pattern) was found in the Set-Cookie header without the HttpOnly flag. HttpOnly prevents client-side JavaScript from reading the cookie value, which blocks XSS-based session hijacking.
Why it matters
Without HttpOnly, any injected JavaScript on the page can read the session cookie via document.cookie and exfiltrate it to an attacker-controlled server. This enables full session hijacking even when the XSS vulnerability itself has limited reach.
Rails session cookies use a *_session suffix by default (e.g., myapp_session). When the HttpOnly flag is absent, client-side scripts have unrestricted access to the cookie. An attacker who achieves XSS on any page in the same origin can steal the session and impersonate the authenticated user.
How to fix it
- 1In Rails, enable HttpOnly on the session cookie via config.session_store :cookie_store, httponly: true in config/application.rb.
- 2In config/initializers/session_store.rb, verify :httponly => true is set.
- 3For non-Rails cookies, always include HttpOnly in the Set-Cookie header: Set-Cookie: name=value; HttpOnly; Secure; SameSite=Strict.
- 4Audit all cookies set by the application and ensure none are readable by JavaScript unnecessarily.
Code examples
Rails (config/initializers/session_store.rb)
Rails.application.config.session_store :cookie_store,
key: '_myapp_session',
httponly: true,
secure: Rails.env.production?,
same_site: :strictPHP (generic HttpOnly cookie)
// Set session cookie with HttpOnly and Secure flags
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'domain' => '',
'secure' => true,
'httponly' => true,
'samesite' => 'Strict',
]);
session_start();Express.js (express-session)
app.use(session({
name: 'sid',
secret: process.env.SESSION_SECRET,
cookie: {
httpOnly: true,
secure: true,
sameSite: 'strict',
},
resave: false,
saveUninitialized: false,
}));References
- https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/06-Session_Management_Testing/02-Testing_for_Cookies_Attributes
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#httponly
- https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html#httponly-attribute
- CWE-1004 at MITRE
Detected automatically by VulnRadar. The check id is rails-cookie-httponly, stable across every scan so you can filter or diff on it.
More info disclosure checks
- InfoServer header truncated
- InfoPHP session cookie naming exposes runtime
- Infoconfig.js / settings.js referenced (verify contents)
- Infoenv.js / environment.js referenced (verify contents)
- InfoSitemap.xml publicly accessible
- InfoOpenAPI schema version exposed in URL
- LowCORS exposes internal CDN hostname
- InforeCAPTCHA site key exposure
Questions
- How severe is Rails Session Cookie Missing HttpOnly Flag?
- VulnRadar rates this medium severity in the info disclosure 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 rails-cookie-httponly, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does Rails Session Cookie Missing HttpOnly Flag map to?
- It maps to CWE-1004. 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.