如何通过为此字符串添加前缀来阻止劫持?

时间:2015-08-09 06:25:32

标签: 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劫持?

2 个答案:

答案 0 :(得分:2)

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

{}阻止了这一点:mail.google.com上的AJAX请求可以完全访问文本内容,并可以将其删除。但是标签插入盲目地执行JavaScript而不进行任何处理。由于while(1);是假值,因此永远不会解析实际响应

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

答案 1 :(得分:0)

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

我想您可能想要检查此Stackoverflow问题并对其进行评论: Difference between ")]}',\n" and "{} &&" in avoiding json hijacking