使用 eval() 执行用户提供的代码存在问题吗?
我有一个计算器小部件 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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)
抛开所有其他“评估是邪恶的”和“代码质量”问题......
安全问题不是关于允许用户提供的代码:用户可以删除他们拥有的每个文件,如果他们喜欢这样。不推荐,但完全有可能。
JavaScript 的危险,无论是
eval()
还是其他方式,都允许攻击者在以下情况下代表用户运行代码(未经同意)用户(ergo 浏览器/域)。这称为 XSS:跨站点脚本:
快乐编码。
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:
Happy coding.
请参阅:高效 JavaScript 代码中的“eval 是邪恶的”:
See: "eval is evil" from Efficient JavaScript code: