最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What should I know about cookies domain and scope for security purposes? - Stack Overflow

programmeradmin2浏览0评论

Where can I learn (or what is) about a cookie's scope to avoid CSRF and XSS attacks for authenticated users?

For example, if I have a multi-tenant system where a single user can be access to one or more sites what is more secure:

  • pany1.hoster
  • pany2.hoster
  • pany3.hoster

or

  • www.hoster/pany1
  • www.hoster/pany2
  • www.hoster/pany3

What happens if I set a cookie at "hoster"?

Where can I learn (or what is) about a cookie's scope to avoid CSRF and XSS attacks for authenticated users?

For example, if I have a multi-tenant system where a single user can be access to one or more sites what is more secure:

  • pany1.hoster.
  • pany2.hoster.
  • pany3.hoster.

or

  • www.hoster./pany1
  • www.hoster./pany2
  • www.hoster./pany3

What happens if I set a cookie at "hoster."?

Share Improve this question edited Apr 15, 2011 at 16:12 rook 67k38 gold badges166 silver badges246 bronze badges asked Apr 15, 2011 at 13:57 TLDRTLDR 1,2782 gold badges12 silver badges32 bronze badges 2
  • I think you mean XSS (Cross site scripting). CSRF is a request forgery, and it doesn't matter (unless checked by the attacked domain) where the request originated from (and you can spoof the origin anyway). XSS have a same origin policy which is a javascript restriction, read upon that – cyber-guard Commented Apr 15, 2011 at 15:37
  • Thank you. I'll look into how same origin applies to those scenarios above... – TLDR Commented Apr 15, 2011 at 16:05
Add a ment  | 

2 Answers 2

Reset to default 5

You can restrict the validity scope of cookie in the domain and the path separately. So you could set a cookie in both scenarios that is only valid for that specific domain/path bination:

  1. To set a cookie for //pany1.example./ only:

     Set-Cookie: name=value; Path=/
    

    Omitting the Domain attribute makes the cookie only valid for the domain that it was set in. And with Path=/ the cookie is valid for any path that has the prefix /.

  2. To set a cookie for //example./pany1/ only:

     Set-Cookie: name=value; Path=/pany1/
    

    Same explanation as for the example above. The only restriction is that you need to use /pany1/ instead of /pany1 as Path=/pany1 would be equivalent to Path=/ and thus would make the cookie also valid for /pany2 and /pany3.

And to avoid that the cookie can be read via JavaScript (reducing the assets accessible using XSS), set the HttpOnly attribute.

The Open Web application security project publishes lots of valuable information about secure web application development.

Cookie's have a scope and path attributes, you would normally not want ot issue cookies for "/" or wildcard hosts *.hoster. would both be ill-advised.

It's not as simple as this one decision, it's good you thought of security in your design, but security is a process, in every phase of your development.

发布评论

评论列表(0)

  1. 暂无评论