TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Preventing Python "Sandbox" Escape?

3 pointsby high_byte6 months ago
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 comments

zahlman6 months ago
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 months ago
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 months ago
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 months ago
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 months ago
I agree with eesmith. DONT DO IT. I guess you got your answer restrictedPy