We need to make a clear distinction between a request containing a Cookie header and a response containing a Set-Cookie header.
Assuming a CSRF token was set through a Set-Cookie header of the parent response, you probably want to access that value through a Cookie request header in your ESI subrequest.
Assuming the Cookie header already contains the CSRF token
The req_top.http.Cookie variable has access to the cookies of the parent request, however req_top not accessible in vcl_backend_response where the ESI placeholders are parsed.
You can bypass this limitation with the following VCL snippet:
sub vcl_recv {
if (req.esi_level > 0 ) {
set req.http.X-Parent-Cookie = req_top.http.Cookie;
}
}
This will enable the X-Parent-Cookie header which is available in vcl_backend_response through bereq.http.X-Parent-Cookie.
Assuming the Cookie header does not yet contain the CSRF token
It is realistic to assume that req_top.http.Cookie doesn't yet contain the value of the CSRF token because when ESI subrequests are processed, the Cookie header is not yet set by the client.
The only solution I can think off is storing the parent Set-Cookie value in a variable. Variables aren't supported natively in Varnish and require the official https://github.com/varnish/varnish-modules to be installed.
You'll have to compile this from source.
See https://github.com/varnish/varnish-modules/blob/master/src/vmod_var.vcc for the vmod_var API and code examples.