Mailing List Archive

Keeping multiple cookies based on regex or prefix, with Varnish 6.0
Hi,

We use Varnish Cache 6.0 LTS, with varnish modules 0.15.0. As far as I can see, this includes a version of the cookie vmod that doesn't support regex. Yet, our use case leans towards using a regex, or at least a substring.

Case in point. Our frontend uses a bunch of cookies. And a handful of them is needed by the backend too. I have done a simple test case, with a just a single cookie, and was able to use the built in cookie vmod to allow that cookie to go through to the backend. But in order for this to be useful for us, we need to allow multiple cookies. Cookies that we don't know the exact name of when writing the VCL. But they will all start with a defined prefix.

Example:
The incoming request contains the following cookie header:

Cookie: _fbp=fb.123; _gid=GA1.2.3; custom-x=qwerty; _ga_ABCD=GS1.123; custom-y=qwerty; _ga=GA1123

And we want to keep all cookies that start with "custom-", hence we want the rewritten cookie header to be:

Cookie: custom-x=qwerty; custom-y=qwerty

How can we achieve this with our version of Varnish and varnish-modules?
Re: Keeping multiple cookies based on regex or prefix, with Varnish 6.0 [ In reply to ]
On 15/11/2021 15:08, Batanun B wrote:
> Hi,
>
> We use Varnish Cache 6.0 LTS, with varnish modules 0.15.0. As far as I
> can see, this includes a version of the cookie vmod that doesn't
> support regex. Yet, our use case leans towards using a regex, or at
> least a substring.
>
> Case in point. Our frontend uses a bunch of cookies. And a handful of
> them is needed by the backend too. I have done a simple test case,
> with a just a single cookie, and was able to use the built in cookie
> vmod to allow that cookie to go through to the backend. But in order
> for this to be useful for us, we need to allow multiple cookies.
> Cookies that we don't know the exact name of when writing the VCL. But
> they will all start with a defined prefix.
>
> Example:
> The incoming request contains the following cookie header:
>
> *Cookie: _fbp=fb.123; _gid=GA1.2.3; custom-x=qwerty; _ga_ABCD=GS1.123;
> custom-y=qwerty; _ga=GA1123*
>
> And we want to keep all cookies that start with "custom-", hence we
> want the rewritten cookie header to be:
>
> *Cookie: custom-x=qwerty; custom-y=qwerty*
>
> How can we achieve this with our version of Varnish and varnish-modules?
>
> _______________________________________________
> varnish-misc mailing list
> varnish-misc@varnish-cache.org
> https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc


Maybe you can use the cookie config from the drupal vcl example on
https://www.varnish-software.com/developers/tutorials/configuring-varnish-drupal/

if (req.http.Cookie) {
        set req.http.Cookie = ";" + req.http.Cookie;
        set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
        set req.http.Cookie = regsuball(req.http.Cookie,
";(S?custom-[a-z0-9]+)=", "; \1=");
        set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
        set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[;
]+$", "");

        if (req.http.cookie ~ "^\s*$") {
            unset req.http.cookie;
        } else {
            return(pass);
        }
}


If my regex foo is not failing me it will keep all cookie's that start
with custom- and what comes after the dash.
For this there is no need to use the cookie module as this works with a
plain varnish-cache install.


regards
Johan Hendriks