TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Preventing Python "Sandbox" Escape?

3 点作者 high_byte6 个月前
I&#x27;m using python&#x27;s exec(code, globals, locals)<p>I disable __builtins__ so no imports, exec, eval, open, etc. inside that context<p>but it seems you can still always do object.__subclasses__() and find every system method (eg. open())<p>it can&#x27;t be overwritten but looking at the interpreter code is seems like it&#x27;s possible to hack a workaround for this specific case.<p>are there other known ways to escape exec()?

5 条评论

zahlman6 个月前
Relevant:<p><a href="https:&#x2F;&#x2F;discuss.python.org&#x2F;t&#x2F;extending-subinterpreters-with-sandboxing-capabilities&#x2F;45355" rel="nofollow">https:&#x2F;&#x2F;discuss.python.org&#x2F;t&#x2F;extending-subinterpreters-with-...</a><p><a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3068139" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3068139</a><p><a href="https:&#x2F;&#x2F;wiki.python.org&#x2F;moin&#x2F;SandboxedPython" rel="nofollow">https:&#x2F;&#x2F;wiki.python.org&#x2F;moin&#x2F;SandboxedPython</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;jailctf&#x2F;pyjailbreaker">https:&#x2F;&#x2F;github.com&#x2F;jailctf&#x2F;pyjailbreaker</a><p><a href="https:&#x2F;&#x2F;healeycodes.com&#x2F;running-untrusted-python-code" rel="nofollow">https:&#x2F;&#x2F;healeycodes.com&#x2F;running-untrusted-python-code</a><p><a href="https:&#x2F;&#x2F;lwn.net&#x2F;Articles&#x2F;574215&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lwn.net&#x2F;Articles&#x2F;574215&#x2F;</a>
Terr_6 个月前
I would infer that it&#x27;s insecure, since if it were that easy there wouldn&#x27;t be various abandoned projects trying to sandbox Python.<p>It&#x27;s the curse of any sufficiently useful language. Well, maybe not Lua, but that was specifically designed for embedding. Java also began with that intention back when applets were ahead of their time, though IIRC secure sandboxing is no longer really a feature.
评论 #42244894 未加载
eesmith6 个月前
Don&#x27;t do it. Really, really don&#x27;t do it. People have tried for decades to develop such a sandbox, and it does not work.
评论 #42244912 未加载
billpg6 个月前
I&#x27;m interested in an answer. Is there a way, by design, to run code from an untrusted source in a restricted manner? So the worse the code could do is call me rude names.
评论 #42244662 未加载
评论 #42244910 未加载
PixelNomad_1236 个月前
I agree with eesmith. DONT DO IT. I guess you got your answer restrictedPy