作为 IronPython 执行用户输入的表达式是否安全

发布于 2024-08-14 11:15:04 字数 456 浏览 1 评论 0原文

我正在开发一个大型 ASP.NET 软件产品。我们希望允许用户为某些字段输入表达式而不是常量。通常是这样的:

(Price * 1.175) + 25

明显的解决方案似乎是嵌入 IronPython,创建一个范围,传入“价格”(和其他)变量,然后将上述内容作为 IronPython 代码执行。

但是,没有什么可以阻止用户输入:

1 / 0

or

def func1():
    func1()
func1()

or

import System.IO
File.Delete(....)

但是,如果我捕获所有异常并在具有 Internet 权限集的应用程序域中运行 IronPython 代码,我安全吗?

I'm working on a large ASP.NET software product. We'd like to allow users to enter expressions rather than constants for certain fields. Typically something like:

(Price * 1.175) + 25

The obvious solution seems to be to embed IronPython, create a Scope, pass in the "Price" (and other) variables and then execute the above as IronPython code.

However, there would be nothing stopping users from entering:

1 / 0

or

def func1():
    func1()
func1()

or

import System.IO
File.Delete(....)

But if I catch all exceptions and run the IronPython code in an Application Domain with the Internet permission set, am I safe?

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

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

发布评论

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

评论(2

枕头说它不想醒 2024-08-21 11:15:04

您通过指出没有什么可以阻止用户输入有效代码来回答您自己的问题。永远不要相信用户输入。曾经。

You answer your own question by noting that there is nothing to stop the user from entering valid code. Never trust user input. Ever.

暗恋未遂 2024-08-21 11:15:04

在类似的情况下,我选择了服务器端 JScript。为了添加另一层保护,我将表达式包装在函数中,然后执行该函数:

function generated123(p1, p2, p3) {
     return
     // user code goes here
     ;
}

这样用户就无法强制导入任何危险的内容。服务器端 JScript 也被编译,这对性能有好处

In a similar situation I opted for server side JScript. To add another layer of protection I am wrapping the expression in a function and then execute the function:

function generated123(p1, p2, p3) {
     return
     // user code goes here
     ;
}

This way the user cannot force importing anything dangerous. Also server side JScript is compiled which is good for perforamnce

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