We started using a new affiliate partner that is setting cookies that contain unencoded JSON, so what we receive looks like this: Cookie: [other cookies]; _aw_j_29283={"id":"2f44a001-378b-436d-89a2-93672703c238-1","expiration":1693705859}; [other cookies]
Our backend rejects these (properly, imho) as a 400, but this has been causing substantial support workload of people complaining our site is broken.
I tried filtering these out in my haproxy config, but the comma in the JSON semantically breaks the one Cookie header into multiple headers.
but that yielded: [other cookies]; _aw_j_29283=OBJECT,BUG=CONTINUES; [other cookies]
i.e. the comma persists, and our backend still coughs up a 400. I can turn strict headers off, but that will involve touching dozens of services. I’m stuck on haproxy 1.8. Is there any way I can just selectively nuke a cookie containing a comma in haproxy?
"replace-value" works like "replace-header" except that it matches the
regex against every comma-delimited value of the header field <name>
instead of the entire header.
It will never match a comma because it’s using commas as a delimiter.
You’d need to use replace header instead, something like:
ooh, awesome, missed that replace-header works on the whole thing!
Just out of curiosity, wouldn’t that regex only replace one single JSON cookie in the header? If it’s like sed /g then I’d think you’d need to isolate it down to the per-cookie level instead of allowing it to match the whole thing.
Well my first try was just '\{[^\}]+\}' REPLACED without anything else, which in my mind would have matched the JSON and replaced it, even multiple times, and not touch everything else.
However when actually testing, it ended up replacing the entire header value with REPLACED (so erasing all cookies). I don’t really understand why.
But yeah, you’re right, if you have multiple cookies like that, it wouldn’t work. In that case, we would have to troubleshoot while the header replacement is erasing it all.
I just look for any cookie value that contains excluded chars, flag them, and truncate up to the end of this cookie. As we discovered, it only applies to the first one, but you can repeat the rule however many times you feel like being tolerant of crap requests: