Setting cookies correctly to prevent attacks

The video is hosted by YouTube (Google). When played, YouTube may set cookies.

Cookies are small text files that the browser stores and sends back to the web server with every subsequent request. They can be used to store session information, preferences or other data about the user.

To protect users and their data, cookies can be marked with certain “flags” that tell the browser how to handle and use them. Here are some important flags for cookie security:

HttpOnly: the cookie cannot be accessed from JavaScript. This mitigates Cross-Site Scripting (XSS) attacks, in which an attacker tries to take over a user account by injecting JavaScript into a site.

Secure: the cookie can only be sent over encrypted HTTPS connections. This prevents man-in-the-middle (MITM) attacks, in which an attacker intercepts and reads or modifies the traffic between the browser and the server.

SameSite: the cookie is only sent to the same site that originally set it. This prevents Cross-Site Request Forgery (CSRF) attacks, in which an attacker sends unauthorised requests from a user’s browser to another site on the user’s behalf.

All these flags are important to protect users and their data against different types of attacks. In addition, web developers should use cookies only when needed and limit how much they store and for how long. By using cookies and their security flags properly, developers help keep their users’ data secure and private.

Remediation:

Apache2

  1. Add the following line to the Apache2 configuration file:
    Header always edit Set-Cookie ^(.*)$ $1;HttpOnly;Secure
  2. Restart the web server

Nginx

  1. Make sure the "nginx_cookie_flag_module" module is installed (--add-module=/path/to/nginx_cookie_flag_module)
  2. Add the following line to the Nginx configuration file:
    set_cookie_flag secure;
    the syntax is: set_cookie_flag [HttpOnly] [secure] [SameSite|SameSite=[Lax|Strict]];
  3. Restart the web server
Examples:
location / {
set_cookie_flag Secret HttpOnly secure SameSite;
set_cookie_flag * HttpOnly;
set_cookie_flag SessionID SameSite=Lax secure;
set_cookie_flag SiteToken SameSite=Strict;
}

PHP
Cookies can be set with these flags directly in the code that creates them.
If the application is written in PHP, the following code can be added before the session is started and headers are sent, so that all session cookies are created with these flags automatically:

session_set_cookie_params([“SameSite” => “Strict”]); // possible values: none, lax, strict
session_set_cookie_params([“Secure” => “true”]); // possible values: false, true
session_set_cookie_params([“HttpOnly” => “true”]); // possible values: false, true

Also in PHP, the “setcookie()” function that creates cookies accepts parameters for these flags:

setcookie(
string $name,
string $value = "",
int $expires_or_options = 0,
string $path = "",
string $domain = "",
bool $secure = false,
bool $httponly = false
): bool

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://www.php.net/manual/en/function.setcookie.php
  • cookie
  • flags
  • secure
  • httponly
  • same site
  • cybersecurity
Iulian Băncău

Security Engineer · All Safe

More than CyberSecurity

We offer a variety of services to help companies be prepared for real cyber attacks.