使用 eval() 执行用户提供的代码存在问题吗?

发布于 2024-12-15 05:41:14 字数 370 浏览 1 评论 0原文

我有一个计算器小部件 (jsfiddle),它使用 javascript 的 eval() 函数来计算用户的输入作为计算器工作。它是 Chrome 扩展中的嵌入式小部件,因此它没有任何数据库或任何其他可能受到损害的附加内容,并且它不发送或接收任何数据。

显然,由于它使用了 javascript 的 eval 函数,因此任何 javascript 都可以由该框执行。这有什么风险吗?我对 javascript 相当陌生,所以我不确定用户能够在这个小部件中评估自己的 javascript 会产生什么结果。他们所做的任何事情难道不会在刷新后恢复吗?

I have a calculator widget (jsfiddle) that uses javascript's eval() function to evaluate the user's input to work as a calculator. It's an embedded widget in a chrome extension, so it doesn't have any database or anything else attached that could be hurt, and it doesn't send or receive any data.

Obviously, since it uses javascript's eval function, any javascript can be executed by this box. Is there any risk involved with this? I'm fairly new to javascript so I'm not sure what could result from the user being able to evaluate their own javascript inside this widget. Wouldn't anything they do just be reverted upon refresh?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伏妖词 2024-12-22 05:41:14

JavaScript 在客户端运行,因此您的服务器不会面临任何迫在眉睫的危险。

但是,如果用户可以以某种方式保存他们的输入并提供给其他用户的链接,这可能会成为一个问题,因为这将允许执行任意 JavaScript(即:跨站脚本又名 XSS)

JavaScript runs on the client side, so your server is not in any imminent danger.

But this could be a problem if users could save their inputs somehow and give a link to other users, as this would allow for the execution of arbitrary JavaScript (ie: Cross-site scripting aka XSS)

笨死的猪 2024-12-22 05:41:14

抛开所有其他“评估是邪恶的”和“代码质量”问题......

安全问题不是关于允许用户提供的代码:用户可以删除他们拥有的每个文件,如果他们喜欢这样。不推荐,但完全有可能。

JavaScript 的危险,无论是 eval() 还是其他方式,都允许攻击者在以下情况下代表用户运行代码(未经同意)用户(ergo 浏览器/域)。

这称为 XSS:跨站点脚本

跨站点脚本漏洞是 Web 应用程序漏洞,允许攻击者通过寻找将恶意脚本注入网页的方法来绕过客户端安全性...[这可能涉及也可能不涉及评估],攻击者可以获得对敏感页面内容、会话 cookie 以及浏览器代表用户维护的各种其他信息的提升访问权限。因此,跨站脚本攻击是代码注入的一种特殊情况。

快乐编码。

All other "eval is evil" and "quality of code" concerns aside...

...the security concern isn't about allowing user-supplied code: the user can delete every file they own if they feel like it. Not recommended, but entirely possible.

The danger with JavaScript, be it eval() or otherwise, is allowing an attacker to run code on the users behalf (without consent), in the context of said user (ergo browser/domain).

This is known as XSS: Cross-Site Scripting:

Cross-site scripting holes are web-application vulnerabilities which allow attackers to bypass client-side security ... by finding ways of injecting malicious scripts into web pages [which may or may not involve eval], an attacker can gain elevated access-privileges to sensitive page-content, session cookies, and a variety of other information maintained by the browser on behalf of the user. Cross-site scripting attacks are therefore a special case of code injection.

Happy coding.

难如初 2024-12-22 05:41:14

请参阅:高效 JavaScript 代码中的“eval 是邪恶的”

“eval”方法和相关结构(例如“new Function”)非常浪费。它们实际上要求浏览器创建一个全新的脚本环境(就像创建一个新的网页一样),从当前范围导入所有变量,执行脚本,收集垃圾,并将变量导出回原始环境。此外,无法出于优化目的而缓存代码。如果可能的话,应该避免 eval 及其亲属。

See: "eval is evil" from Efficient JavaScript code:

The 'eval' method, and related constructs such as 'new Function', are extremely wasteful. They effectively require the browser to create an entirely new scripting environment (just like creating a new web page), import all variables from the current scope, execute the script, collect the garbage, and export the variables back into the original environment. Additionally, the code cannot be cached for optimisation purposes. eval and its relatives should be avoided if at all possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文