【问题标题】:How is hijack prevented by prefixing the string like this?如何通过像这样为字符串添加前缀来防止劫持?
【发布时间】:2015-08-09 06:25:32
【问题描述】:

我在Spring JSONmessageConverter看到了如下评论:

/**
 * 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 劫持?

【问题讨论】:

  • 太好了,我不知道 Spring 支持这个。你能分享一下这个评论在哪个类和包中吗?
  • @Sanjay 它是org.springframework.http.converter.json.MappingJackson2HttpMessageConverter - 请参阅setPrefixJson() 方法

标签: java json spring security web


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    添加前缀会使字符串失效。

    我想你可能想检查这个 Stackoverflow 问题并对其进行 cmets: Difference between ")]}',\n" and "{} &&" in avoiding json hijacking

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 2017-12-22
      • 2010-11-28
      • 1970-01-01
      相关资源
      最近更新 更多