Eval 是邪恶的...那么我应该用什么来代替呢?
ajax 请求返回一个标准 JSON 数组,其中填充了用户的输入。 输入已被清理,并且使用 eval() 函数,我可以轻松创建我的 javascript 对象并更新我的页面......
所以这就是问题。 无论我多么努力地尝试清理输入,我都不愿意使用 eval() 函数。 我已经在 google 上检查了如何使用“AJAX 中的 JSON 而不需要 eval”,并且运行了一堆不同的方法...
我应该使用哪一个? 有没有一种标准的、经过验证的安全方法来做到这一点?
An ajax request returns me a standard JSON array filled with my user's inputs. The input has been sanitized, and using the eval() function, I can easily create my javascript object and update my page...
So here's the problem. No matter how hard I try to sanitize the inputs, I'd rather not use the eval() function. I've checked google for ways to use "JSON in AJAX without eval" and have ran accross a bunch of different methods...
Which one should I use? Is there a standard, proven-secure way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
json.org 有一个很好的 javascript 库
简单用法:
编辑:作为在评论中指出,如果您查看其源代码(尽管它看起来首先被清理),则使用 eval 来
完全避免它,请查看 json_parse 或 json-sans-eval
json2.js 不安全,json_parse.js 速度慢,json-sans-eval.js 不验证
json.org has a nice javascript library
simple usage:
Edit: As pointed out in comments, this uses eval if you look through its source (although it looks to be sanitized first)
to avoid it completely, look at json_parse or json-sans-eval
json2.js is insecure, json_parse.js is slow, json-sans-eval.js is non-validating
在即将推出的 ECMAScript 3.1 版本的 JavaScript 中,建议采用标准方法来执行此操作: JSON.解析。
IE8、Firefox 3.1/3.5 以及未来很可能的其他流行浏览器都将支持它。 同时,您可以回退到或专门使用 eval()。 它可能是邪恶的,也可能不是; 当然它会比 JSON.parse 慢。 但这是当今解析 JSON 的常用方法。
如果攻击者能够将恶意 JavaScript 注入到您通过 JSON 吐出的内容中,那么您需要担心的问题比 eval-is-evil 更大。
There is a proposed standard way of doing this, in the forthcoming ECMAScript 3.1 version of JavaScript: JSON.parse.
It will be supported in IE8, Firefox 3.1/3.5 and most likely the other popular browsers in the future. In the meantime, you can fall back to, or use exclusively, eval(). Evil it may or may not be; certainly it will be slower than JSON.parse. But that's the usual way to parse JSON today.
If an attacker is able to inject malcious JavaScript into content you are spitting out via JSON, you have bigger problems to worry about than eval-is-evil.
我想说,一旦输入被清理,评估就是最好的方法。 如果您的服务器遭到破坏,人们将能够向客户端发送他们想要的任何脚本。 因此进行评估并不是一个很大的安全风险。 如果您担心人们在数据包到达客户端之前对其进行操作,那么可以再次修改脚本本身。
不用担心评估。 但请确保将其包装在 try...catch 块中,这样如果您的 JSON 被破坏,您的用户就不会收到 JS 错误。
:)
I would say, once the input is sanitized, eval is the best way to go. If your server gets compromised, people will be able to send whatever scripts they want to the client anyway. So putting an eval is not a big security risk. If you are worried about people manipulating the packets before they reach the client then, again, the scripts themselves can be modified.
Don't worry about eval. But make sure to wrap it in a try...catch block so your users don't get JS errors if your JSON gets mangled.
:)
为了安全地将 JSON 转换为 JS 对象,您应该使用 JSON 解析器,例如 这个库。
To safely convert JSON to a JS object you should use a JSON parser such as the JSON.parse() function provided by this library.
与命令设计模式比较:http://en.wikipedia.org/wiki/Command_pattern 。 鉴于此,您可以精确定义客户端可以执行的操作,并且您的应用程序将与底层解释一样安全。
Compare to the command design pattern: http://en.wikipedia.org/wiki/Command_pattern. Given this, you can precisely define the operations a client can execute and your application will be as safe as the underlying interpretation.
取决于您想通过卫生设施实现什么目标。 我在 prototype 框架对 JSON 和安全评估的支持方面取得了巨大成功。
Depends on what you're trying to accomplish with the sanitation. I've had great success w/the prototype framework's support for JSON and safe evaluation.
如果您确定不存在注入风险,并且您没有在循环中
eval()ing
,那么请使用eval()
。 与其他选项相比,它无疑会更慢,可能会崩溃,并且需要客户端下载额外的代码。If you're certain there's no injection risk, and you're not
eval()ing
in a loop, then useeval()
. It will compare favorably to other options which will certainly be slower, might break, and will require the client to download additional code.从 jQuery 中“窃取”
"stolen" from jQuery
问题:
eval 带来的问题是它在全局范围内执行
场景:
假设您在各种文档元素的标记代码中使用各个属性,例如属性 onvisible
您会想要获取所有具有此属性的元素,请将 onvisible-content-string 转为闭包并将其放入 EventHandler 队列中。 这就是 JS Function 构造函数发挥作用的地方。
把它们放在一起:
Issue:
The problem eval poses is that it executes in the global scope
Scenario:
Assume you are using individual attributes in the markup code of various document-elements such as an attribute onvisible
You would like to get all elements with this attribute, turn the onvisible-content-string into a closure and put it into an EventHandler queue. This is where the JS Function constructor comes into play.
Putting it all together: