javascript - JSON 安全最佳实践?

标签 javascript ajax security json

在研究 JSON vs XML 的问题时,我遇到了this question .现在,首选 JSON 的原因之一被列为在 Javascript 中易于转换,即使用 eval()。现在,从安全 Angular 来看,这立即让我觉得可能存在问题。

所以我开始对 JSON 的安全方面进行一些研究,并在这篇博客文章中了解 JSON is not as safe as people think it is .这部分突出:

Update: If you are doing JSON 100% properly, then you will only have objects at the top level. Arrays, Strings, Numbers, etc will all be wrapped. A JSON object will then fail to eval() because the JavaScript interpreter will think it's looking at a block rather than an object. This goes a long way to protecting against these attacks, however it's still best to protect your secure data with un-predictable URLs.

好的,这是一个很好的开始规则:顶层的 JSON 对象应该始终是对象,而不是数组、数字或字符串。听起来对我来说是一个很好的规则。

对于 JSON 和 AJAX 相关的安全性,还有什么需要做或避免的吗?

上面引用的最后一部分提到了不可预测的 URL。有没有人有这方面的更多信息,特别是你如何在 PHP 中做到这一点?我在 Java 方面比 PHP 更有经验,在 Java 中这很容易(因为您可以将整个范围的 URL 映射到单个 servlet),而我所做的所有 PHP 都将单个 URL 映射到 PHP 脚本。

另外,您究竟如何使用不可预测的 URL 来提高安全性?

最佳答案

有许多针对 JSON 的安全攻击,尤其是 XSRF。

当 Web 服务使用 cookie 进行身份验证,并使用包含敏感数据的 JSON 数组响应 GET 请求时,就会出现该漏洞。

如果攻击者可以诱骗登录服务 naive-webapp.com 的用户访问他们的网站(或任何嵌入了他们控制的 IFRAME 的网站,例如通过嵌入式广告),那么他们可以插入 <script> 标签与 naive-webapp.com 的 SRC,并可能窃取用户的数据。 这取决于 JavaScript Array 构造函数的 javascript 怪癖,如下所示:

 <script>
   // Overload the Array constructor so we can intercept data
   var stolenArrays = [];
   var RealArray = Array;
   Array = function () {
     var arr = RealArray.apply(arguments);
     stolenArrays.push(arr);
     return arr;
   }
 </script>
 <!-- even though the attacker can't access the cookies,
   - he can cause the browser to send them to naive-webapp.com -->
 <script src="//naive-webapp.com/..."></script>
 <script>
   // now stolenArrays contains any data from the parsed JSON
 </script>

EcmaScript 5 修复了导致 [] 在全局对象上查找 Array 的令人困惑的行为,并且许多现代浏览器不再容易受到这种攻击。

顺便说一句,Oil 对不可预测的 URL 的看法是错误的。 URL 中的加密安全随机标识符是保护资源的好方法。正如石油所说,基于身份的安全并不是万能的。 请参阅 http://waterken.sourceforge.net/,了解基于 URL 中加密安全标识符的安全分布式应用程序方案示例,不需要身份概念。

编辑:

在考虑 JSON 与 XML 时,您还应该了解 XML 特定的攻击媒介。

XXE , XML 外部实体攻击,使用精心制作的 XML 通过防火墙访问文件系统和网络资源。

<!DOCTYPE root 
[
<!ENTITY foo SYSTEM "file:///c:/winnt/win.ini">
]>
...
<in>&foo;</in>

The Application embeds the input (parameter "in", which contains the win.ini file) to the web service response.

关于javascript - JSON 安全最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/395592/

相关文章:

jquery - AJAX - 选择具有多个值的字段填充

php - PHP 和 Mysql 的编码陷阱

security - 证书 - .cer + .pfx

javascript - 如何将带有日期的 JSON 文件加载到 Google Charts

javascript - Jasmine:测试 toHaveBeenCalledWith(params) 其中 params 是一个对象

php - 带有 php 响应的 ajax 请求按顺序,而不是在数组中

java - 如何保护 GlassFish 2 上的 Web 服务?

javascript - 如何在每个新条目中包含日期/时间戳?

javascript - 使用选择框进行 jquery 表单验证

jquery - AdSense 与 AJAX