cache-handler icon indicating copy to clipboard operation
cache-handler copied to clipboard

Set-cookie header being cached

Open EdBailey opened this issue 8 months ago • 7 comments

In my testing I've found that set-cookie headers are getting cached by caddy. This is obviously a big problem for us and I've not been able to see any cache-handler config or ways to work around it in the caddy config.

Obviously in an ideal world we wouldn't be sending set-cookie headers on cacheable pages, but I'm working on a legacy platform and it's not going to be possible to change that behaviour in the short term, so I need caddy to handle it. We've been using caching in nginx for some time and not had this problem with nginx.

My preferred behaviour here would be that the initial cache-miss request would send the set-cookie headers, but it wouldn't be stored in the cache so future cache-hits would not receive it.

I'd really appreciate any help with this!

EdBailey avatar Apr 29 '25 08:04 EdBailey

Hey @EdBailey nice edge case, I'll put it on top of my todo list.

darkweak avatar Apr 29 '25 12:04 darkweak

Thanks @darkweak!

EdBailey avatar Apr 29 '25 13:04 EdBailey

yikes same issue here. subscribing

also, related https://github.com/caddyserver/cache-handler/issues/126

punkpeye avatar May 18 '25 18:05 punkpeye

Just in case useful for others, this is what I did in Fastify:

app.addHook('onSend', (request, reply, payload, done) => {
  if (
    reply.hasHeader('set-cookie') ||
    request.headers['cookie']?.includes('user_account')
  ) {
    reply.header('cache-control', 'private');
  }

  done(null, payload);
});

punkpeye avatar May 18 '25 19:05 punkpeye

e.g. With the Caddyfile only

{
	cache {
		ttl 5s
	}
}
localhost:9080 {
	route /cache-set-cookie {
		cache
		reverse_proxy localhost:9087 {
			header_down +Cache-Control no-cache=Set-Cookie
		}
	}
}

darkweak avatar May 25 '25 10:05 darkweak

@darkweak I was just wondering if there's been any progress on this? Setting Cache-Control no-cache=Set-Cookie seemed to make it revalidate every request, making the cache functionally useless unfortunately, unless I've really misunderstood something. Thanks!

EdBailey avatar Aug 08 '25 08:08 EdBailey

@EdBailey You can try with the latest version of Souin (v1.7.8).

darkweak avatar Sep 19 '25 10:09 darkweak