Write ModSecurity rule to set Cookie HttpOnly flag if it not present in response
Assistant
User
Write ModSecurity rule to set Cookie SameSite Strict attribute if it not present in response
Assistant
ModSecurity can inspect and even attempt to modify response headers, but as with setting HttpOnly, adding the SameSite=Strict attribute to Set-Cookie headers that lack it is somewhat limited and tricky because:
There may be multiple Set-Cookie headers.
ModSecurity’s ability to rewrite response headers is limited.
User
Merge HttpOnly and SameSite rules to one
Assistant
Certainly! Here is a merged ModSecurity rule that checks each Set-Cookie header in the response, and if it does have the or attribute, appends the missing ones:
ModSecurity is primarily a web application firewall designed to inspect and manipulate HTTP requests and responses. While it offers some ability to modify response headers, setting the HttpOnly flag on cookies in response headers is a bit tricky and limited because ModSecurity processes response headers mostly for monitoring and blocking purposes rather than modifying them arbitrarily.
However, you can try to detect when a Set-Cookie header does not have the HttpOnly flag, and then modify or set it accordingly. This requires using the RESPONSE_HEADERS collection and the tx variable to capture or modify headers, combined with the actions like setenv, setvar, and edit.
Here is a basic example to detect Set-Cookie header(s) without the HttpOnly flag and to add it. Note that the ability to modify multiple Set-Cookie headers or complex cookies may be limited.
phase:3 means this runs during the response header phase.
The first rule checks if Set-Cookie header does not contain HttpOnly.
The chained rule captures the entire Set-Cookie header.
setvar tries to overwrite the Set-Cookie header by appending ; HttpOnly.
Important notes:
The syntax RESPONSE_HEADERS:Set-Cookie represents the full Set-Cookie header(s).
If multiple Set-Cookie headers are present, this rule may only catch the first or a combined value — ModSecurity has some limitations handling multiple headers of the same name.
ModSecurity is not primarily designed to modify response headers extensively. For guaranteed results, managing cookies on your backend or via your web server (e.g., Apache Header directive or Nginx proxy_cookie_flags) is preferable.
Test carefully to avoid duplicating flags or corrupting cookie strings.
Alternative for Apache:
If you're using Apache and want to set HttpOnly flag on cookies reliably at the web server level (recommended), you could use:
apache
Header edit Set-Cookie ^(.*)$ $1;HttpOnly
Or, with mod_headers:
apache
Header always edit Set-Cookie ^(.*)$ $1;HttpOnly
If you want I can help you with a more complex ModSecurity rule or server-level approach.
Risk of duplicating or corrupting cookies if not carefully handled.
Below is a basic example that attempts to detect Set-Cookie headers missing the SameSite attribute, and appends; SameSite=Strict to them.
If you want, I can help draft a more complex ModSecurity rule with chained transformations or Lua scripting (if enabled) for safer cookie modification.