java - 如何通过像这样为字符串添加前缀来防止劫持?

标签 java json spring security web

我在 Spring JSON messageConverter 中看到如下注释:

/**
 * Indicate whether the JSON output by this view should be prefixed with "{} &&". Default is false.
 * <p>Prefixing the JSON string in this manner is used to help prevent JSON Hijacking.
 * The prefix renders the string syntactically invalid as a script so that it cannot be hijacked.
 * This prefix does not affect the evaluation of JSON, but if JSON validation is performed on the
 * string, the prefix would need to be ignored.
 */

字符串前缀如何防止 JSON 劫持?

最佳答案

人为的例子:假设 Google 有一个像 mail.google.com/json?action=inbox 这样的 URL,它以 JSON 格式返回收件箱的前 50 条消息。由于同源策略,其他域上的恶意网站无法通过 AJAX 请求获取此数据,但它们可以通过标签包含 URL。使用您的 cookie 访问 URL,并通过 overriding the global array constructor or accessor methods他们可以在设置对象(数组或散列)属性时调用一个方法,从而允许他们读取 JSON 内容。

{} && 阻止了这种情况:mail.google.com 的 AJAX 请求将具有对文本内容的完全访问权限,并且可以将其删除。但是标签插入会盲目地执行 JavaScript,而不进行任何处理。由于 {} 是一个虚假值,因此永远不会解析实际响应

其他框架会在响应中添加不同的内容,例如 while(1);(来自 Google 的示例),这会导致黑客陷入无限循环,但我们可以在自己的网站上将其删除

关于java - 如何通过像这样为字符串添加前缀来防止劫持?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31901528/

相关文章:

Java 扫描仪的奇怪行为

java - 有没有办法在小程序中获取安全基线?

java - 在 Java 中,检查列表是否包含另一个列表中的项目的最快方法是什么,两个列表是否属于同一类型?

ios - Swift 获取 HTTP JSON 响应并使用 Codable 解析它

javascript - 每次使用angularjs调用函数时如何附加web服务json数据

java - 用java监控系统的平台访问和花费时间

java - JNDI 属性(java.naming.factory.initial 和 java.naming.provider.url)未通过 Spring 的 PropertyPlaceholderConfigurer 设置

java - 如何仅在 JSP 中使用网络摄像头(不使用 Servlet)捕获图像并将其保存到 MS SQL Server 中

javascript - 与 json 文件中的智能表进行数据绑定(bind)

spring - org.springframework.web.client.ResourceAccessException : I/O error on GET request for in Microservices