使用 Request.ApplicationPath 作为 cookie 路径是否安全

发布于 2024-08-26 11:26:50 字数 135 浏览 2 评论 0原文

使用这样的代码安全吗?

Response.Cookies[cookieName].Path = Request.ApplicationPath + "/";

我想了解所有极端情况,请...

Is it safe to use such code?

Response.Cookies[cookieName].Path = Request.ApplicationPath + "/";

I want to know about all corner cases, please...

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

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

发布评论

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

评论(3

゛清羽墨安 2024-09-02 11:26:50

简而言之,不,这不安全。使用 cookie 路径充满了问题,因为它们在 IE 和 Chrome 中区分大小写,但在 FF 中不区分大小写。这意味着路径大小写的任何不匹配都会导致问题。

  1. 生成 Cookie 时,如果您设置的路径与用户输入的路径不同,浏览器将不会存储该路径。

    生成 Cookie 时,如果您设置的路径与用户输入的路径不同,浏览器将不会存储该路径

  2. 当用户返回时,如果他们输入的路径与第一次访问不同,浏览器将不会在请求中提供 cookie。

    当用户返回时,如果他们输入的路径与第一次访问不同,浏览器将不会提供 cookie

你想解决什么问题?

In short, no, it's not safe. Using cookie paths is fraught with problems as they are case sensitive in IE and Chrome, but not FF. This means any mismatch in path case will stuff things up.

  1. When generating a cookie, if the path you set differs in case from what the user typed, browsers won't store it.

  2. When the user returns, if the path they enter differs in case from the first trip, the browser won't supply the cookie with the request.

What problem are you trying to solve?

七七 2024-09-02 11:26:50

如果您的应用程序在域的根目录中运行,则 Request.ApplicationPath == "/"。因此,对于您的代码,cookie 的路径将是 //。您可以通过这样做来回避这个问题:

cookie.Path = Request.ApplicationPath;
if (cookie.Path.Length > 1) cookie.Path += '/';

Will 正确指出,您需要确保您的应用程序强制执行一致的 URL 大小写(即将 URL 中包含大写字母的所有请求重定向为相应的小写字母)。

除此之外,我相信你这样做应该没问题。如果您希望所有 cookie 都处于“应用程序范围”,请考虑使用如下代码创建自定义 IHttpModule(或扩展 global.asax.cs):

private void Application_EndRequest(object sender, EventArgs e)
{
    var app = (HttpApplication)sender;

    var cookiePath = app.Request.ApplicationPath;
    if (cookiePath.Length > 1) cookiePath += '/';

    foreach (string name in app.Response.Cookies.AllKeys)
    {
        var cookie = app.Response.Cookies[name];
        cookie.Path = cookiePath;
    }
}

If your application runs in the root of a domain, Request.ApplicationPath == "/". Hence, with your code, the path of your cookie will be //. You can dodge around this problem by doing this:

cookie.Path = Request.ApplicationPath;
if (cookie.Path.Length > 1) cookie.Path += '/';

As Will correctly points out, you will want to make sure that your application enforces a consistent casing of URLs (i.e. redirect all requests with URLs containing uppercase letters to their lowercase equivalent).

Other than that, I believe you should be fine doing this. If you want all of your cookies to be "application scoped", consider creating a custom IHttpModule with code like this (or extend global.asax.cs):

private void Application_EndRequest(object sender, EventArgs e)
{
    var app = (HttpApplication)sender;

    var cookiePath = app.Request.ApplicationPath;
    if (cookiePath.Length > 1) cookiePath += '/';

    foreach (string name in app.Response.Cookies.AllKeys)
    {
        var cookie = app.Response.Cookies[name];
        cookie.Path = cookiePath;
    }
}
魔法少女 2024-09-02 11:26:50

不,这不安全,原因如威尔所指出的。

但是...您可能想采用此技术来实现你的意图。

No, it's not safe, for the reasons that Will specified.

But... You may want to employ this technique to fulfill your intent.

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