Great question!
If the request almost always has the same set of cookies, you could simplify this to:
set req.http.cookie = "" +
if (req.http.cookie:csrftoken, "csrftoken=" + req.http.cookie:csrftoken + "; ", "") +
if (req.http.cookie:config-sessionid, "config-sessionid=" + req.http.cookie:config-sessionid, "")
;
Filtering cookies like this is a great idea. However, not sending cookies to origin at all is much better. Even filtered, the cookie header combines multiple unrelated pieces of high granularity data, and if you use that data on the origin, you’ll then have to include Vary: cookie
in the HTTP response, forcing Fastly to store a separate variant of the object for each possible cookie value. And that will likely reduce cache hit ratio dramatically.
Obviously one solution is to remove any cookies that you don’t actually use, and you’re doing that. But what about cookies that are important?
Use the value at the edge
One solution is to consume the cookie value at the edge. One of your examples is a CSRF token, which is actually something you can often implement entirely edge-side. For example, use a stateless CSRF token format that includes a timestamp and hash of the URL, timestamp and client IP, and then re-run that hash at the edge to check that it’s valid.
Once you’ve consumed that cookie, you can then remove it from the request.
Decompose the cookie into separate headers
If you still have multiple cookies you need to pass to origin, consider spreading them over multiple custom headers:
set req.http.Session-ID = req.http.cookie:session-id;
set req.http.Edition = req.http.cookie:edition;
unset req.http.cookie;
Now, imagine session-id
is a very granular value that is usually different for every user, while edition
has only 2 possible values, “local” and “international”. If a request goes to your origin and it pays no attention to the session ID but uses the edition value, it can now return a response with a vary header like this:
Vary: Edition
Now Fastly only needs to store two variants of this response in order to satisfy all possible requests.