为什么不用eval()JSON? [英] Why not eval() JSON?
问题描述
据我所知,由于安全性,JavaScript中的JSON对象 eval()
被认为是不好的做法。如果JSON来自另一台服务器,我可以理解这个问题。
As far as I know it is considered bad practice to eval()
JSON objects in JavaScript, because of security. I can understand this concern if the JSON comes from another server.
但是如果JSON是由我自己的服务器提供的并且是使用PHP的 json_encode创建的
(让我们假设它没有错误),简单地使用 eval()
来读取JS中的JSON还是有任何安全性是合法的我目前无法想到的问题?
But if the JSON is provided by my own server and is created using PHP's json_encode
(let us assume it is not buggy), is it legitimate to simply use eval()
to read the JSON in JS or are there any security problem I currently can't think of?
我真的不想处理动态加载JSON解析器,并且很乐意只使用 eval()
。
I really don't want to deal with dynamically loading a JSON parser and would be glad to simply use eval()
.
PS:我显然会使用原生的 JSON
对象如果它可用,但想要回退到IE / Opera的 eval()
。
PS: I will obviously use the native JSON
object if it is available, but want to fall back to eval()
for IE/Opera.
推荐答案
您的安全性可能会受到多种危害。
There are a number of ways that your security may be compromised.
- 中间攻击的人理论上可能会发生变化传送到客户端的数据内容。
- 您的服务器流量可能会在其他地方截获,不同的内容可能会被截获e提供(与MIM攻击不完全相同)
- 您的服务器可能遭到入侵,数据源可能被篡改。
这些只是一个简单的例子。 XSS很讨厌。
and these are just the simple examples. XSS is nasty.
一盎司预防值得一磅治疗
"an ounce of prevention is worth a pound of cure"
这篇关于为什么不用eval()JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!