SecRule RESPONSEHEADERS:Set-Cookie "!@rx HttpOnly" \…
- User
SecRule RESPONSE_HEADERS:Set-Cookie "!@rx HttpOnly"
"id:170001,phase:3,pass,nolog,t:none,setvar:RESPONSE_HEADERS:Set-Cookie=%{RESPONSE_HEADERS.Set-Cookie}; HttpOnly"SecRule RESPONSE_HEADERS:Set-Cookie "!@rx SameSite"
"id:170002,phase:3,pass,nolog,t:none,setvar:RESPONSE_HEADERS:Set-Cookie=%{RESPONSE_HEADERS.Set-Cookie}; SameSite=Strict"nginx -t
2025/05/12 05:20:24 [emerg] 552#552: "modsecurity_rules_file" directive Rules error. File: /etc/nginx/modsec/custom-rules/http-flags.conf. Line: 2. Column: 115. Expecting an action, got: RESPONSE_HEADERS:Set-Cookie=%{RESPONSE_HEADERS.Set-Cookie}; HttpOnly" in /etc/nginx/sites-enabled/website-proxy.conf:7 nginx: configuration file /etc/nginx/nginx.conf test failed
- Assistant
The error message indicates that Nginx (modsecurity) is expecting an action in your SecRule directive, but it encountered what looks like a variable assignment with
RESPONSE_HEADERS:Set-Cookie=%{RESPONSE_HEADERS.Set-Cookie}; HttpOnly. This strongly suggests a syntax or usage error in how you're modifying response headers in ModSecurity.
Core issue:
In ModSecurity, you cannot directly perform string concatenation or modify response headers like that using
setvarin theRESPONSE_HEADERS:Set-Cookievariable. Thesetvaraction sets a variable but does not modify response headers.The syntax:
setvar:RESPONSE_HEADERS:Set-Cookie=%{RESPONSE_HEADERS.Set-Cookie}; HttpOnly...is invalid because:
RESPONSE_HEADERSis a collection representing response headers.setvarsets variables, but to modify response headers directly, you typically need to use theset-headeroradd-headeraction in ModSecurity 3.x or employ transformations accordingly.- Setting
RESPONSE_HEADERS:Set-Cookieto a value of%{RESPONSE_HEADERS.Set-Cookie}; HttpOnlyis not supported as a direct assignment in this way.
How to fix
If your goal is to append
HttpOnlyflag andSameSite=Strictto theSet-Cookieresponse header when these flags are missing, then you need to use the correct ModSecurity actions to modify response headers.
Information about modifying response headers in ModSecurity:
- In ModSecurity 2.x, it's difficult or impossible to modify headers on the fly efficiently.
- In ModSecurity 3.x, you can modify response headers using the following:
set-header RESPONSE_HEADERS:Set-Cookie "value" add-header RESPONSE_HEADERS:Set-Cookie "value"Usually, to replace or set headers,
set-headeris used.
Recommended approach for your use case:
You want to:
- Detect if a
Set-Cookieheader does not containHttpOnly - If missing, append
; HttpOnly - Similarly for
SameSite
A typical way to do this is:
SecRule RESPONSE_HEADERS:Set-Cookie "!@rx HttpOnly" \ "id:170001,phase:3,pass,nolog,t:none,set-header:RESPONSE_HEADERS.Set-Cookie=%{tx.modified_cookie}" SecRule RESPONSE_HEADERS:Set-Cookie "!@rx HttpOnly" \ "phase:3,pass,nolog,t:none,ctl:tx.modified_cookie=%{RESPONSE_HEADERS.Set-Cookie}; HttpOnly"But
ctl:tx.variables can only be used if enabled.However, ModSecurity itself does not provide a simple way to append to existing headers in a single SecRule. You often have to do it in two steps or with Lua scripting.
Minimal solution with ModSecurity:
Using
set-headerresets the header value completely, so you have to build the full new value yourself.Since you can't concatenate in the set-header action directly, you must use a Rule that captures the existing header, appends the
; HttpOnlystring, and sets the header with this new value.Example (pseudo):
SecRule RESPONSE_HEADERS:Set-Cookie "!@rx HttpOnly" \ "id:170001,phase:3,pass,nolog,t:none,set-var:tx.new_cookie=%{RESPONSE_HEADERS.Set-Cookie}";\ set-var:tx.new_cookie=%{tx.new_cookie}; HttpOnly;\ set-header RESPONSE_HEADERS:Set-Cookie %{tx.new_cookie}"However, modsecurity doesn't allow multiple actions with semicolon
;in one rule unless properly quoted and structured.
Correct example syntax adapting your configuration
Try splitting the logic into two rules:
SecRule RESPONSE_HEADERS:Set-Cookie "!@rx HttpOnly" \ "id:170001,phase:3,pass,nolog,t:none,setvar:tx.new_cookie=%{RESPONSE_HEADERS.Set-Cookie}; HttpOnly" SecRule tx.new_cookie "!@eq %{RESPONSE_HEADERS.Set-Cookie}" \ "id:170002,phase:3,pass,nolog,t:none,set-header:RESPONSE_HEADERS:Set-Cookie=%{tx.new_cookie}"But this might not work because
setvardoes not concatenate strings with; HttpOnlydirectly.
Alternative: Using Lua scripting with ModSecurity
If your ModSecurity installation supports Lua, you'd write a Lua script to handle header modification safely.
Summary — immediate problem causing the error:
You cannot use
setvar:RESPONSE_HEADERS:Set-Cookie=%{RESPONSE_HEADERS.Set-Cookie}; HttpOnly— ModSecurity doesn't accept this syntax.You need to replace your rules with ones that do not use
setvarto modify response headers. Useset-headeroradd-headerto actually modify headers.
Example fixed rules (simplified):
SecRule RESPONSE_HEADERS:Set-Cookie "!@rx HttpOnly" \ "id:170001,phase:3,pass,nolog,t:none,set-header:RESPONSE_HEADERS:Set-Cookie=%{RESPONSE_HEADERS.Set-Cookie}; HttpOnly" SecRule RESPONSE_HEADERS:Set-Cookie "!@rx SameSite" \ "id:170002,phase:3,pass,nolog,t:none,set-header:RESPONSE_HEADERS:Set-Cookie=%{RESPONSE_HEADERS.Set-Cookie}; SameSite=Strict"But be careful: This will replace the entire header with the original value plus appended text — but
%{RESPONSE_HEADERS.Set-Cookie}may only return the first Set-Cookie header if multiple exist, or not behave as you expect.
Important note:
RESPONSE_HEADERSis a multi-value collection for headers.- Using
%{RESPONSE_HEADERS.Set-Cookie}returns only one header (usually the first). - If you have multiple
Set-Cookieheaders, this won't handle all. - Be careful with this approach.
Final recommendation:
- Use
set-headeroradd-headerinstead ofsetvar:RESPONSE_HEADERS:Set-Cookie=... - Be aware of multi-set-cookie headers — you might need custom scripting.
- Test the rules in staging before production.
If you want, I can help you write a complete working example based on your ModSecurity version and environment.
- User
Okay, you right. That's not good idea to fix Set-Cookie and SameSite with ModSecurity. So please give nginx config to append SameSite: Strict and Set-Cookie: HttpOnly flags on every response:
server { listen 10.100.2.2:80 default_server;
location / { modsecurity on; modsecurity_rules_file /etc/nginx/modsec/modsecurity.conf; proxy_pass http://10.100.1.2; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }}
- Assistant
Modifying
Set-Cookieheaders to appendHttpOnlyandSameSite=Strictflags directly in nginx can be done using theproxy_cookie_flagsandproxy_cookie_pathdirectives starting from nginx 1.19.3.
Solution: Use
proxy_cookie_flagsdirective (recommended)Nginx added the
proxy_cookie_flagsdirective to controlSecure,HttpOnly,SameSiteflags on cookies returned by the upstream.Example nginx config snippet:
server { listen 10.100.2.2:80 default_server; location / { modsecurity on; modsecurity_rules_file /etc/nginx/modsec/modsecurity.conf; proxy_pass http://10.100.1.2; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # Append HttpOnly and SameSite=Strict to all Set-Cookie headers proxy_cookie_flags ~ HttpOnly; proxy_cookie_flags ~ SameSite=Strict; } }
How does this work?
proxy_cookie_flags <cookie-name> <flags>;applies flags to cookies with the exact name.- Using
~as<cookie-name>acts as a regex matching all cookies, so this adds flags to everySet-Cookieheader. HttpOnlyensures the HttpOnly flag is set.SameSite=Strictadds theSameSite=Strictattribute.
Requirements
- nginx 1.19.3 or newer (introduced
proxy_cookie_flags). - The
HttpOnlyflag is added only if not present. - The
SameSiteattribute is added if missing (or overwritten? Check docs - docs say "sets the flags", so it overwrites if existing). - If you want to avoid overwriting existing
SameSitevalues, you need a more complex solution (nginx doesn’t provide per-cookie conditional flag appending).
Alternative: If you don’t have nginx >=1.19.3
You can try to rewrite cookie headers manually, but it's fragile and complex. Typically that involves:
proxy_pass ...; proxy_cookie_path / "/; HttpOnly; SameSite=Strict";But
proxy_cookie_pathonly manipulates thePathattribute in cookies, so it’s not suitable here.
Summary:
Add these two lines inside your
location /block:proxy_cookie_flags ~ HttpOnly; proxy_cookie_flags ~ SameSite=Strict;This will ensure all cookies returned from upstream get these flags appended.
Let me know if you want help verifying nginx version or alternative solutions!