JavaScript 的 eval() 什么时候不是邪恶的? [英] When is JavaScript's eval() not evil?

查看:29
本文介绍了JavaScript 的 eval() 什么时候不是邪恶的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些 JavaScript 代码来解析用户输入的函数(用于类似电子表格的功能).解析公式后,我可以将其转换为 JavaScript 并在其上运行 eval() 以产生结果.

I'm writing some JavaScript code to parse user-entered functions (for spreadsheet-like functionality). Having parsed the formula I could convert it into JavaScript and run eval() on it to yield the result.

然而,如果我可以避免使用 eval(),我总是避免使用它,因为它是邪恶的(无论对错,我一直认为它在 JavaScript 中更加邪恶,因为要评估的代码可能会被用户更改).

However, I've always shied away from using eval() if I can avoid it because it's evil (and, rightly or wrongly, I've always thought it is even more evil in JavaScript, because the code to be evaluated might be changed by the user).

那么,什么时候可以使用它?

So, when it is OK to use it?

推荐答案

我想花点时间解决您问题的前提 - eval() 是邪恶".编程语言人员使用的evil"这个词通常意味着危险",或者更准确地说能够用一个看似简单的命令造成很多伤害".那么,什么时候可以使用危险的东西呢?当您知道危险是什么并采取适当的预防措施时.

I'd like to take a moment to address the premise of your question - that eval() is "evil". The word "evil", as used by programming language people, usually means "dangerous", or more precisely "able to cause lots of harm with a simple-looking command". So, when is it OK to use something dangerous? When you know what the danger is, and when you're taking the appropriate precautions.

说到点子上,我们来看看使用eval()的危害.和其他一切一样,可能存在许多小的隐患,但最大的两个风险——eval() 被认为是邪恶的原因——是性能和代码注入.

To the point, let's look at the dangers in the use of eval(). There are probably many small hidden dangers just like everything else, but the two big risks - the reason why eval() is considered evil - are performance and code injection.

  • 性能 - eval() 运行解释器/编译器.如果您的代码被编译,那么这是一个大打击,因为您需要在运行时调用一个可能很重的编译器.然而,JavaScript 仍然主要是一种解释型语言,这意味着在一般情况下调用 eval() 不会对性能造成很大的影响(但请参阅下面我的具体评论).
  • 代码注入 - eval() 可能会在提升的权限下运行一串代码.例如,以管理员/root 身份运行的程序永远不会想要 eval() 用户输入,因为该输入可能是rm -rf/etc/important-file"或更糟.同样,浏览器中的 JavaScript 没有这个问题,因为无论如何该程序都在用户自己的帐户中运行.服务器端 JavaScript 可能存在这个问题.

根据您的具体情况.据我了解,您自己生成字符串,因此假设您小心不允许生成像rm -rf something-important"这样的字符串,则不存在代码注入风险(但请记住,它是 在一般情况下很难确保这一点).此外,如果您在浏览器中运行,那么代码注入的风险很小,我相信.

On to your specific case. From what I understand, you're generating the strings yourself, so assuming you're careful not to allow a string like "rm -rf something-important" to be generated, there's no code injection risk (but please remember, it's very very hard to ensure this in the general case). Also, if you're running in the browser then code injection is a pretty minor risk, I believe.

至于性能,您必须权衡编码的难易程度.我的观点是,如果您正在解析公式,您最好在解析期间计算结果,而不是运行另一个解析器(eval() 中的那个).但是使用 eval() 编码可能更容易,并且性能下降可能不会引起注意.在这种情况下,看起来 eval() 并不比任何其他可能为您节省一些时间的函数更邪恶.

As for performance, you'll have to weight that against ease of coding. It is my opinion that if you're parsing the formula, you might as well compute the result during the parse rather than run another parser (the one inside eval()). But it may be easier to code using eval(), and the performance hit will probably be unnoticeable. It looks like eval() in this case is no more evil than any other function that could possibly save you some time.

这篇关于JavaScript 的 eval() 什么时候不是邪恶的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆