我在 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/