HAProxy community

Best way to delete a cookie?


#1

What is the best way to delete a single cookie?

I tried

http-request replace-header Cookie mycookie=[^;]*(;\s*|$) ""

but then I get an error that http-request replace-header requires three parameters. Apparently it doesn’t recognise the empty string as a parameter :confused:. (at least on 1.7).

I was kind of surprised there doesn’t seem to be any built-in ways to manipulate cookies since haproxy already has to know how to parse cookies for the cook fetch.

What is the best way to delete an individual cookie?


#2

You are right, there is no build-in feature for this. Multiple regexes are required to delete a cookie, I assume the following untested configuration would do the job:

# delete mycookie when 
http-request replace-header Cookie mycookie=[^;]*;\s*(.*) \1

# removing mycookie when at the end of the header
http-request replace-header Cookie (.*);\s*mycookie=[^=]$ \1

# deleting entire Cookie header (single cookie)
reqidel ^Cookie: mycookie=[^=]$