简体   繁体   English

https 是否安全 cookies 可以防止 XSS 攻击?

[英]Does https secure cookies prevent XSS attacks?

Does https connection secure cookies and prevents XSS attacks. https 连接是否保护 cookies 并防止 XSS 攻击。 I have a simple blog that allows users to enter JavaScript code as an input.我有一个简单的博客,允许用户输入 JavaScript 代码作为输入。 I want to allow Javascript input by the user while still preventing XSS attacks and cookie stealing.我想允许用户输入 Javascript,同时仍然防止 XSS 攻击和 cookie 窃取。 Does https help secure cookies. https 是否有助于保护 cookies。 I only found few sites that talks about this and still a bit unclear.我只发现很少有网站讨论这个问题,但还是有点不清楚。

HTTPS can prevent a man-in-the-middle attack, not XSS. HTTPS 可以防止中间人攻击,而不是 XSS。 Unfortunately the session cookie is not secure with this alone, one can request a page with HTTP and then the same cookie will be sent unprotected.不幸的是,session cookie 本身并不安全,可以使用 HTTP 请求页面,然后相同的 cookie 将不受保护地发送。

To ensure that the session cookie is sent only on HTTPS connections, you can use the function session_set_cookie_params() before starting the session: To ensure that the session cookie is sent only on HTTPS connections, you can use the function session_set_cookie_params() before starting the session:

session_set_cookie_params(0, '/', '', true, true);
session_start();

Note the first true , it means that the cookie will be sent only to HTTPS pages.注意第一个true ,这意味着 cookie 将仅发送到 HTTPS 页面。 The second true tells the browser, that JavaScript must not access the session cookie, it depends on the browser if that is done correctly.第二个true告诉浏览器,JavaScript 不能访问 session cookie,这取决于浏览器是否正确完成。

Another good way to make your site safer is, to use the session cookie only for maintaining the session, and using a second cookie to take care of the authentication.使您的站点更安全的另一个好方法是,使用 session cookie 仅用于维护 session,并使用第二个 cookie 来处理身份验证。 I can provide an example if you are interested.如果你有兴趣,我可以提供一个例子。

The HTTP protocol (HTTPS or HTTP) does not help with XSS or really have any relation. HTTP 协议(HTTPS 或 HTTP)对 XSS 没有帮助或确实有任何关系。 You'll need to add preventative measures and be careful where you output the javascript to the client.您需要添加预防措施并小心您的 output 和 javascript 到客户端。

Once you've allowed someone to dynamically store and execute arbitrary JavaScript on your site, they have access to a lot of stuff you would want them to leave alone.一旦您允许某人在您的站点上动态存储和执行任意 JavaScript,他们就可以访问很多您希望他们不要管的东西。 At a minimum, they can hijack your PHP Session ID (they'll have access to your cookies, after all) and then use Ajax to forward it to some remote server. At a minimum, they can hijack your PHP Session ID (they'll have access to your cookies, after all) and then use Ajax to forward it to some remote server. Once they have that, they can then do all sorts of crap to your users.一旦他们有了这个,他们就可以对你的用户做各种各样的废话。

If you have to let them add their own JavaScript, I would recommend that you specifically disable all Ajax functionality ( XMLHTTPRequest = function(){} prevents all Ajax pretty easily on most browsers, but you might need to look into what IE needs (I don't know what ActiveXObject = function(){} will do...)). If you have to let them add their own JavaScript, I would recommend that you specifically disable all Ajax functionality ( XMLHTTPRequest = function(){} prevents all Ajax pretty easily on most browsers, but you might need to look into what IE needs (I不知道ActiveXObject = function(){}会做什么...))。 Unfortunately, you can't prevent access to cookies if you have any expectation of using them (ie if you have a session), so you will need to find some other workaround.不幸的是,如果您对使用 cookies 有任何期望(即如果您有会话),则无法阻止对它们的访问,因此您需要找到其他一些解决方法。

If you want users to be able to input JavaScript code , but not have it parsed, run it through htmlspecialchars.如果您希望用户能够输入 JavaScript代码,但不对其进行解析,请通过 htmlspecialchars 运行它。 If you want them to be able to execute code, then no, HTTPS won't help, and you're going to need to parse the code and remove anything bad from it.如果您希望他们能够执行代码,那么不,HTTPS 将无济于事,您将需要解析代码并从中删除任何不好的东西。

You can use a Web Application Firewall to scan and block XSS, including in cookies though the latter can cause false positives https://medium.com/p/5d4b1d33219a/edit您可以使用 Web 应用程序防火墙来扫描和阻止 XSS,包括在 cookies 中,尽管后者可能导致误报Z5E056C500A1C4B6A7110B50D807BADE45Z://medium219a/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM