4

I need to remove specific cookies from the backend response in varnish.

My backend server sets a bunch of cookies that I don't need and unfortunately I can not control, so I want to delete them.

However I need some of the cookies, so I want to be able to remove cookies by their name.

For example I want to rename a cookie named bad_cookie, but at the same time keep a cookie named good_cookie.

I have found a lot of resources about removing specific request cookies, but none about removing backend response cookies.

Is this possible in Varnish?

mrege
  • 1,821
  • 1
  • 13
  • 15
Martin Taleski
  • 6,033
  • 10
  • 40
  • 78

1 Answers1

5

If you want to rename I think it would be something like:

sub vcl_fetch {
    #renamed after receiving the backend
    set beresp.http.set-cookie = regsuball(beresp.http.set-cookie, "bad_cookie", "good_cookie"); 
    set beresp.http.cookie = regsuball(beresp.http.cookie, "bad_cookie", "good_cookie"); }
}

sub vcl_deliver {
    #renamed before sending the client
    set resp.http.set-cookie = regsuball(beresp.http.set-cookie, "bad_cookie", "good_cookie"); 
    set resp.http.cookie = regsuball(beresp.http.cookie, "bad_cookie", "good_cookie"); }
}

If you want to delete all cookies:

sub vcl_fetch {
    #deleted after receiving the backend
    remove beresp.http.set-cookie;
    remove beresp.http.cookie;
}

sub vcl_deliver {
    #deleted before sending the client
    remove resp.http.set-cookie;
    remove resp.http.cookie;
}

beresp.http.set-cookie reads only the first Set-Cookie header, If you want to delete some and keep others can use: github.com/varnish/libvmod-header**

Community
  • 1
  • 1
mrege
  • 1,821
  • 1
  • 13
  • 15
  • removing beresp.http.set-cookie in vcl_fetch will remove all cookies. It looks like you can not delete some of the response cookies and keep others because remove beresp.http.set-cookie reads only the first Set-Cookie header. Still haven't found a solution – Martin Taleski Jan 05 '13 at 16:29
  • You are right, for multiple set-cookie headers can use: https://github.com/varnish/libvmod-header – mrege Jan 07 '13 at 13:24
  • yeah vmod_header did exactly what I needed... Thanks. – Martin Taleski Jan 10 '13 at 00:51
  • @MartinTaleski Great! I changed the answer! – mrege Jan 10 '13 at 14:29