Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

SecRule RESPONSEHEADERS:Set-Cookie "!@rx HttpOnly" \…

  1. 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

  2. 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 setvar in the RESPONSE_HEADERS:Set-Cookie variable. The setvar action 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_HEADERS is a collection representing response headers.
    • setvar sets variables, but to modify response headers directly, you typically need to use the set-header or add-header action in ModSecurity 3.x or employ transformations accordingly.
    • Setting RESPONSE_HEADERS:Set-Cookie to a value of %{RESPONSE_HEADERS.Set-Cookie}; HttpOnly is not supported as a direct assignment in this way.

    How to fix

    If your goal is to append HttpOnly flag and SameSite=Strict to the Set-Cookie response 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-header is used.


    Recommended approach for your use case:

    You want to:

    • Detect if a Set-Cookie header does not contain HttpOnly
    • 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-header resets 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 ; HttpOnly string, 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 setvar does not concatenate strings with ; HttpOnly directly.


    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 setvar to modify response headers. Use set-header or add-header to 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_HEADERS is a multi-value collection for headers.
    • Using %{RESPONSE_HEADERS.Set-Cookie} returns only one header (usually the first).
    • If you have multiple Set-Cookie headers, this won't handle all.
    • Be careful with this approach.

    Final recommendation:

    1. Use set-header or add-header instead of setvar:RESPONSE_HEADERS:Set-Cookie=...
    2. Be aware of multi-set-cookie headers — you might need custom scripting.
    3. 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.

  3. 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;
        }
    

    }

  4. Assistant

    Modifying Set-Cookie headers to append HttpOnly and SameSite=Strict flags directly in nginx can be done using the proxy_cookie_flags and proxy_cookie_path directives starting from nginx 1.19.3.


    Solution: Use proxy_cookie_flags directive (recommended)

    Nginx added the proxy_cookie_flags directive to control Secure, HttpOnly, SameSite flags 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 every Set-Cookie header.
    • HttpOnly ensures the HttpOnly flag is set.
    • SameSite=Strict adds the SameSite=Strict attribute.

    Requirements

    • nginx 1.19.3 or newer (introduced proxy_cookie_flags).
    • The HttpOnly flag is added only if not present.
    • The SameSite attribute 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 SameSite values, 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_path only manipulates the Path attribute 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!