Spring 安全和 CSRF 攻击

Spring Security and CSRF attack

我正在开发一个应该非常安全的 java Web 应用程序,因此我在 SSL 服务器上应用了 spring 安全性和 spring MVC,并启用了 CSRF;我使用 POST 成功提交了所有表单以及生成的 CSRF 令牌,但是某些页面具有 GET 方法,如果任何攻击者从任何浏览器打开任何页面的源代码,他都可以在表单标记内看到生成的 CSRF 令牌,然后他可以用它来 POST 我们网站的任何内容,只要 session 被受到攻击的用户激活!!我说得对吗?

我应该怎么做才能使站点非常安全?我是否应该使用任何其他开放源代码以及 spring 安全措施来覆盖其他攻击,如跨站点脚本等。?我应该强制所有页面使用 POST 以避免任何 CSRF 攻击吗?

更新

我尝试通过客户端工具在相同 session 下使用与登录用户相同的令牌在同一浏览器上提交请求来做更多测试,但失败了,响应说登录失败,并且 header 包含 nosniff

X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY

所以我认为在 spring 安全中使用 GET 来处理 csrf 是安全的,而不用担心有人读取 GET 页面中的令牌,除非攻击者使用 XSS 攻击来提交

however some pages have GET methods and if any attacker open the source of any page from any browser he can see the generated CSRF token inside the Form tag, then he can use it to POST any content to our site as long as the session is active by the user under attack !! am I right?

是的,你是对的,使用 GET 会让你的网站泄露敏感信息。这正是 Spring Security and RFC Specification 不鼓励使用 GET 请求发送敏感信息的原因。如果您要发送 CSRF 令牌,您应该 POST。

What should I do make the site very secure? should I use any other open source along with spring security to cover other attacks like cross site scripting, etc.. ?

Spring 安全框架不是银弹。使用它不会神奇地使您的 Web 应用程序 100% 安全。尽管它涵盖了大部分安全漏洞,但我们必须确保以不引入任何安全漏洞的方式进行编码。

例如:

在跨站点脚本的情况下,即使您使用 Spring 安全性,如果您从 HTML 输入框中获取一个值并直接保存,而不对它进行 HTML 编码,并且将其显示在 JSP 文件中,如 ${value} 您将引入 XSS 的可能性。

攻击者可以看到自己的CSRF token,但是看不到其他用户的CSRF token。不同的用户有不同的token,使用错误的token应该是不行的。

所以这里没有什么可担心的。不要惊慌。