Cookies often hold sensitive data, such as session and login tokens. If an attacker can run a script on your page through cross-site scripting (XSS), they can read those cookies and hijack a user’s session.
To avoid that, and improve the security of your site and your users, set the HttpOnly flag on your cookies, especially session and login cookies.
Combine that with Secure, so the cookie is only sent over HTTPS, and SameSite, which will limit when the browser sends the cookie on cross-site requests and helps against CSRF (Cross-Site Request Forgery).
A well-protected session cookie looks like this:
Set-Cookie: SESSIONID=abc123; Path=/; Domain=.example.com; Secure; HttpOnly; SameSite=Lax
Note: Omit
HttpOnlyfor cookies your own JavaScript must read, such as some analytics or consent cookies.
PHP
Session cookies (PHPSESSID): in php.ini, or with ini_set() before session_start():
session.cookie_httponly = 1
session.cookie_secure = 1
session.cookie_samesite = Lax
Your own cookies (PHP 7.3+):
setcookie('name', 'value', [
'expires' => time() + 3600,
'path' => '/',
'domain' => '.example.com',
'secure' => true, // only over HTTPS
'httponly' => true, // not readable by JavaScript
'samesite' => 'Lax',
]);
Older PHP versions use the positional form: setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
Setting the header directly:
header('Set-Cookie: name=value; Path=/; Secure; HttpOnly; SameSite=Lax', false);
WordPress
WordPress already sets its login cookies (wordpress_logged_in_*, wordpress_sec_*) with HttpOnly. If a scanner flags a cookie on a WordPress site, it usually comes from a plugin or theme. Find which plugin or theme sets the cookie, update if required, or ask the developer for a solution.
Other platforms
-
Node.js / Express:
res.cookie('name', 'value', { httpOnly: true, secure: true, sameSite: 'lax' }) -
Python / Django:
SESSION_COOKIE_HTTPONLY = True(the default) andSESSION_COOKIE_SECURE = True. Flask:SESSION_COOKIE_HTTPONLY = True,SESSION_COOKIE_SECURE = True. -
ASP.NET:
<httpCookies httpOnlyCookies="true" requireSSL="true" />inweb.config. -
Apache (add the flags to every cookie your application sets, with
mod_headers):Header always edit Set-Cookie ^(.*)$ "$1; HttpOnly; Secure" -
nginx 1.19.3+:
proxy_cookie_flags ~ httponly secure samesite=lax;
Use the server-level options with care, as they also change cookies your JavaScript may need to read.
Multiple cookies
Web scanners, including Sucuri SiteCheck, show a warning if any cookie your site sets is missing HttpOnly, even if others have it. For example, this site sets two cookies and only one is protected:
$ curl -s -o /dev/null -D - https://example.com/
HTTP/2 200
set-cookie: abc_16951=a56f2718; expires=Wed, 31-Dec-2999 23:59:59 GMT; path=/; HttpOnly
set-cookie: PHPSESSID=c543d1b48df7a597947f5556cbe06628; path=/
Here PHPSESSID needs HttpOnly (and Secure) to clear the warning.
Cookies set by the Sucuri Firewall
The firewall doesn’t change your website’s cookies so the addition of HttpOnly has to be done manually on your server or application with assistance from your developer.
However, the firewall does set a few cookies of its own.
|
Cookie |
Set when |
Flags |
|---|---|---|
|
|
A visitor passes a Protected Page or captcha |
|
|
|
A WordPress admin submits a form in |
|
|
|
A visitor passes the JavaScript challenge (bot protection) |
|
A scanner may report sucuri_cloudproxy_uuid_* as missing HttpOnly. That is expected and not a risk: it only proves that the visitor passed the challenge, and it can’t be used to log in or reach any account.