Search code examples
configurationload-balancinghaproxy

Haproxy frontend configuration to replace response header depending on query string


I used the following haproxy configuration in frontend to modify the response header of requests depending on a query string:

frontend my-frontend
   acl is-foo urlp(foo) 1
   http-response replace-header Set-Cookie "(.*)" "\1; SameSite=None" if is-foo

Depending on my information from the docs the acl should match for all requests like

example.com?a=b&foo=1&bar=2
example.com?foo=1
example.com?a=b&foo=1

And it should not match for requests like

example.com?a=b&foo=0&bar=2
example.com?a=b
example.com?a=b&foo=bar

The actual result is that the acl matches never. If i invert the if i.e.: if !is-foo the replace-header happens on every request. So the problem must be the acl which matches never.

I use haproxy 2.0.15


Solution

  • I got it working by myself.

    It seems to be the case that urlp(foo) is not present at runtime when it has been executed for http-response.

    So we need to store its value in a temporary variable using set-var(custom.name), before. At runtime in if condition we can access it with var(custom.name) and match it against our condition. I used urlp_val() instead of urlp() here because the value will be casted to int immediately.

    frontend my-frontend
        http-request set-var(txn.foo) urlp_val(foo)
        http-response replace-header Set-Cookie "(.*)" "\1; SameSite=None" if { var(txn.foo) eq 1 }
    

    Thank you for traveling.