如何限制Python脚本,以便访问本地资源?

发布于 2025-01-20 12:25:32 字数 143 浏览 2 评论 0原文

我正在开发一个项目,该项目允许用户将 python 脚本上传到 API 并按计划运行它。目前,我正在尝试找出一种方法来限制脚本的功能,使其无法访问本地文件、扰乱运行 API 的 Flask 服务器等。您对我如何实现这一目标有什么想法吗?有没有办法让只有特定的库可用于导入?

I am working on a project that allows users to upload a python script to an API and run it on a schedule. Currently, I'm trying to figure out a way to limit the functionality of the script so that it cannot access local files, mess with the flask server running the API, etc. Do you have any ideas on how I can achieve this? Is there anyway to make it so only specific libraries are available for importing?

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

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

发布评论

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

评论(2

赢得她心 2025-01-27 12:25:32

在服务器上运行其他脚本是严重的安全问题。如果您尝试在Web应用程序上部署Python解释器,则可以尝试使用 judge0 -github 。如果您自己部署它,它是免费的,并且可以安全地在容器中运行脚本。

Running other scripts on your server is serious security issue. If you are trying to deploy Python interpreter on your web application, you can try with something like judge0 - GitHub. It is free if you deploy it yourself and it will run scripts safely inside containers.

夜灵血窟げ 2025-01-27 12:25:32

最简单的方法是确保运行脚本的用户不是root,而是专门为此任务设计的用户(例如,只能读取而不写或执行的组的一部分)。这意味着至少应确保所有文件具有适当的模式。然后,您可以使用管道或某些内容来运行脚本。

另外,您可以使用不是“本地”的运行时,例如VM或Compute Service(AWS Lambda等)。后者将是最简单的,并且有很多供应商使用程序化API提供计算服务。

The simplest way is to ensure the user running the script is not root, but a user specifically designed for this task (e.g. part of a group that can only read and not write or execute). This means at minimum you should ensure all files have the appropriate mode. Then you can just use a pipe or something to run the script.

Alternatively, you could use a runtime that’s not “local”, like a VM or compute service (AWS lambda, etc). The latter would be simplest, and there’s lots of vendors who offer compute service with programmatic api.

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