当我在 Catalyst 中使用 Cache-Control 标头时,如何不发送 cookie?

发布于 2024-07-29 04:41:59 字数 401 浏览 5 评论 0原文

我通过 SessionSession::Store::DBICSession::State::Cookie 在 Catalyst 应用程序中使用会话。

我有一些控制器和方法使用 Cache-Control: public 标头发送数据,因此 Set-Cookie: 标头不 带着这些响应出去(否则,它会被缓存并发送到其他客户端,从而导致可能的安全问题)。 我还没有找到一个好的方法来完成这个任务。

如何告诉 SessionSession::State::Cookie 不要发送 cookie 来响应给定的请求?

I'm using sessions in my Catalyst app via Session, Session::Store::DBIC, and Session::State::Cookie.

I have a few controllers and methods that send out data with a Cache-Control: public header, so its essential that the Set-Cookie: header not go out with those responses (otherwise, it'd be cached and sent to other clients, leading to possible security issues). I haven't found a good way to accomplish this.

How can I tell Session or Session::State::Cookie to not send a cookie in response to a given request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花想c 2024-08-05 04:41:59

执行一些 RTFS,Session.pm 覆盖 Catalyst 的 finalize_headers
方法并通过相当深的调用链在那里设置 cookie:

finalize_header
⇒ _save_session_expires
⇒ session_expires
⇒ _extended_session_expires
⇒ extend_session_id (…::Session::State::Cookie)
⇒ update_session_cookie (…::Session::State::Cookie)

似乎没有任何方法可以将链中的任何内容标记为
停止。 唯一的检查是 Cookie.pm 中的一个方法,称为
cookie_is_rejecting 它只是将配置的 cookie 路径与
请求路径。

所以,看起来最好的方法是将我自己的覆盖添加到
update_session_cookiecookie_is_rejecting。 我想我会
使用cookie_is_rejecting

这是我最终使用的代码。 请注意,这相当笨拙,但它有效......

package Catalyst::Plugin::Session::State::Cookie::Sanity;
use base qw/Catalyst::Plugin::Session::State::Cookie/;

use MRO::Compat;

use strict;

sub cookie_is_rejecting {
    my ($c, $cookie) = @_;

    ($c->stash->{cache_control_time} // 0) > 0
        or $c->maybe::next::method( $c, $cookie );
}

1;

Doing a little RTFS, Session.pm overrides Catalyst's finalize_headers
method and sets the cookie there, through a rather deep call chain:

finalize_header
⇒ _save_session_expires
⇒ session_expires
⇒ _extended_session_expires
⇒ extend_session_id (…::Session::State::Cookie)
⇒ update_session_cookie (…::Session::State::Cookie)

There does not appear to be any way to flag anything in the chain to
stop. The only check is a method in Cookie.pm called
cookie_is_rejecting which just compares the configured cookie path to
the request path.

So, it looks like the best way to do this is to add my own override to
either update_session_cookie or cookie_is_rejecting. I think I'll
use cookie_is_rejecting.

Here is the code I finally used. Note that this is rather klugy, but it works...

package Catalyst::Plugin::Session::State::Cookie::Sanity;
use base qw/Catalyst::Plugin::Session::State::Cookie/;

use MRO::Compat;

use strict;

sub cookie_is_rejecting {
    my ($c, $cookie) = @_;

    ($c->stash->{cache_control_time} // 0) > 0
        or $c->maybe::next::method( $c, $cookie );
}

1;
咽泪装欢 2024-08-05 04:41:59

这种方式似乎有点不寻常,但我想我明白你在做什么。

如果我没记错的话,Cookie 状态模块只是在响应对象中设置 cookie:

$c->response->cookies

我不认为那里有什么魔力,它只是将其设置为创建或延长会话时间。 如果您想确保不发送 cookie。 清除根端的 $c->response->cookies ,你应该可以开始了。

杰克

It seems a bit unusual to approach it this way but I think I get what you are doing.

If I recall correctly, the Cookie State module simply sets the cookie in the response object:

$c->response->cookies

I don't think there's any magic there, and it only sets it to create or extend the session time. If you want to ensure that no cookies are sent. Clear out $c->response->cookies in Root end and you should be good to go.

JayK

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文