🌐
Python
python.org › downloads › release › python-2713
Python Release Python 2.7.13 | Python.org
Warning: Python 2.7.13 reached end-of-life on 2020-01-01. It is no longer supported and does not receive security updates.
🌐
Stack Overflow
stackoverflow.com › questions › 67063888 › google-foobar-question-1-minion-job-handling
python - Google foobar question 1, minion job handling - Stack Overflow
April 12, 2021 - The returned list should retain the same ordering as the original list - you don't want to mix up those carefully-planned shift rotations! For instance, if data was [5, 10, 15, 10, 7] and n was 1, solution(data, n) would return the list [5, 15, 7] because 10 occurs twice, and thus was removed from the list entirely. It would be run on an Python 2.7.13 sandbox
🌐
Pythonsandbox
pythonsandbox.dev
Link to our Python Sandbox
Python Sandbox lets you write, run and debug your code. Our interactive code embed enables better documentation, blog posts, and enhances learning experiences.
🌐
Pypy
doc.pypy.org › en › latest › release-v7.2.0.html
PyPy v7.2.0: release of 2.7, and 3.6, released 2019-10-14 — PyPy documentation
The CFFI backend has been updated to version 1.13.0. We recommend using CFFI rather than c-extensions to interact with C, and cppyy for interacting with C++ code. Thanks to Anvil, we revived the PyPy Sandbox, which allows total control over a python interpreter’s interactions with the external ...
🌐
Cloudera Community
community.cloudera.com › t5 › Support-Questions › Python-2-7-in-sandbox › td-p › 268592
Solved: Python 2.7 in sandbox - Cloudera Community - 268592
August 22, 2019 - Additionally, pip will not work with Python 2.7 after version 19.1. As long as the version of pip you have installed is less than this, you'll still be able to use Python 2.7 for a while, until the repository itself is taken down or reconfigured in a way that breaks old pip. See https://stackoverflow.com/questions/54915381/will-pip-work-for-python-2-7-after-its-end-of-life-on-1...
🌐
Ideone
ideone.com › fork › FAnMhj
Online Compiler and IDE >> C/C++, Java, PHP, Python, Perl and 70+ other compilers and interpreters - Ideone.com
Ideone is something more than a pastebin; it's an online compiler and debugging tool which allows to compile and run code online in more than 40 programming languages.
🌐
Python Sandbox
pythonsandbox.com
Python Sandbox | Text Mode
Python Sandbox · Modes · Text Mode · Turtle Mode · Graphics Mode · Docs · Turtle Docs · Graphics Docs · About Python Sandbox · Contact · × · Close · × · The file you are saving already has a later revision. If you save as normal, the next revision in this file series will be ...
🌐
Stack Overflow
stackoverflow.com › questions › 63582321 › how-can-i-install-python2-7-in-the-windows-sandbox
python 2.7 - How can I install python2.7 in the windows sandbox? - Stack Overflow
The installer always fails in the windows sandbox because it can't install the Visual C++ Redistributable: An error occurred during the installation of assembly 'Microsoft.VC90.CRT,version="9.0.
🌐
Stack Overflow
stackoverflow.com › questions › 73159933 › google-foo-bar-failing-all-test-cases-but-working-in-python-ide
Google foo.bar failing all test cases but working in python IDE - Stack Overflow
I know for a fact that for at least the first two test cases my output matches their output but it still fails all of them. I assumed it could be because its running in python 2.7.13 so I found an online sandbox that runs that version of python but my code still outputs the required output there too.
🌐
Turbo
hub.turbo.net › run › python › python-2.7.13
Run Python 2.7.13 Online - Sign In - Turbo.net
October 24, 2014 - Support: docs.python.org/3 · Current 2.7.13 updated 7 years ago · Details · Updated: Created: File Extensions · .py .pyc .pyo .pyw .pyz .pyzw · Report an Issue × · Submit Submitting · ×Close · Your issue report has been received and will be reviewed shortly.
Find elsewhere
🌐
7-Zip Documentation
documentation.help › Python-2.7.13 › documentation.pdf pdf
Python 2.7.13 documentation
documentation.HELP! Python 2.7.13 Documentation · Python 2.7.13 documentation · Python Module Index · What's New in Python · What's New in Python 2.7 · The Future for Python 2.x · Changes to the Handling of Deprecation Warnings · Python 3.1 Features ·
Top answer
1 of 9
67

This is really non-trivial.

There are two ways to sandbox Python. One is to create a restricted environment (i.e., very few globals etc.) and exec your code inside this environment. This is what Messa is suggesting. It's nice but there are lots of ways to break out of the sandbox and create trouble. There was a thread about this on Python-dev a year ago or so in which people did things from catching exceptions and poking at internal state to break out to byte code manipulation. This is the way to go if you want a complete language.

The other way is to parse the code and then use the ast module to kick out constructs you don't want (e.g. import statements, function calls etc.) and then to compile the rest. This is the way to go if you want to use Python as a config language etc.

Another way (which might not work for you since you're using GAE), is the PyPy sandbox. While I haven't used it myself, word on the intertubes is that it's the only real sandboxed Python out there.

Based on your description of the requirements (The requirements are support for variables, basic conditionals and function calls (not definitions)) , you might want to evaluate approach 2 and kick out everything else from the code. It's a little tricky but doable.

2 of 9
23

Roughly ten years after the original question, Python 3.8.0 comes with auditing. Can it help? Let's limit the discussion to hard-drive writing for simplicity - and see:

from sys import addaudithook
def block_mischief(event,arg):
    if 'WRITE_LOCK' in globals() and ((event=='open' and arg[1]!='r') 
            or event.split('.')[0] in ['subprocess', 'os', 'shutil', 'winreg']): raise IOError('file write forbidden')

addaudithook(block_mischief)

So far exec could easily write to disk:

exec("open('/tmp/FILE','w').write('pwned by l33t h4xx0rz')", dict(locals()))

But we can forbid it at will, so that no wicked user can access the disk from the code supplied to exec(). Pythonic modules like numpy or pickle eventually use the Python's file access, so they are banned from disk write, too. External program calls have been explicitly disabled, too.

WRITE_LOCK = True
exec("open('/tmp/FILE','w').write('pwned by l33t h4xx0rz')", dict(locals()))
exec("open('/tmp/FILE','a').write('pwned by l33t h4xx0rz')", dict(locals()))
exec("numpy.savetxt('/tmp/FILE', numpy.eye(3))", dict(locals()))
exec("import subprocess; subprocess.call('echo PWNED >> /tmp/FILE', shell=True)",     dict(locals()))

An attempt of removing the lock from within exec() seems to be futile, since the auditing hook uses a different copy of locals that is not accessible for the code ran by exec. Please prove me wrong.

exec("print('muhehehe'); del WRITE_LOCK; open('/tmp/FILE','w')", dict(locals()))
...
OSError: file write forbidden

Of course, the top-level code can enable file I/O again.

del WRITE_LOCK
exec("open('/tmp/FILE','w')", dict(locals()))

Sandboxing within Cpython has proven extremely hard and many previous attempts have failed. This approach is also not entirely secure e.g. for public web access:

  1. perhaps hypothetical compiled modules that use direct OS calls cannot be audited by Cpython - whitelisting the safe pure pythonic modules is recommended.

  2. Definitely there is still the possibility of crashing or overloading the Cpython interpreter.

  3. Maybe there remain even some loopholes to write the files on the harddrive, too. But I could not use any of the usual sandbox-evasion tricks to write a single byte. We can say the "attack surface" of Python ecosystem reduces to rather a narrow list of events to be (dis)allowed: https://docs.python.org/3/library/audit_events.html

I would be thankful to anybody pointing me to the flaws of this approach.


EDIT: So this is not safe either! I am very thankful to @Emu for his clever hack using exception catching and introspection:

#!/usr/bin/python3.8
from sys import addaudithook
def block_mischief(event,arg):
    if 'WRITE_LOCK' in globals() and ((event=='open' and arg[1]!='r') or event.split('.')[0] in ['subprocess', 'os', 'shutil', 'winreg']):
        raise IOError('file write forbidden')

addaudithook(block_mischief)
WRITE_LOCK = True
exec("""
import sys
def r(a, b):
    try:
        raise Exception()
    except:
        del sys.exc_info()[2].tb_frame.f_back.f_globals['WRITE_LOCK']
import sys
w = type('evil',(object,),{'__ne__':r})()
sys.audit('open', None, w)
open('/tmp/FILE','w').write('pwned by l33t h4xx0rz')""", dict(locals()))

I guess that auditing+subprocessing is the way to go, but do not use it on production machines:

https://bitbucket.org/fdominec/experimental_sandbox_in_cpython38/src/master/sandbox_experiment.py

🌐
Work at Tech
workat.tech › ide › online-python2-compiler
Python 2.7 Online Compiler and Editor (IDE)
Python2 Online Compiler and Editor (IDE). Online compiler and editor (IDE) for coding/programming languages like C/C++, Java, Python 2 and Python 3. Write, Run and Share your code easily with others.
🌐
Pythonsandbox
pythonsandbox.io
Python Sandbox
Python Sandbox lets you quickly run and test your Python code. This is the fastest online Python sandbox environment since the code executes in browser itself.
🌐
Paiza
paiza.io › en › languages › python
Online PHP/Java/C++... editor and compiler | paiza.IO
Paiza.IO is online editor and compiler. Java, Ruby, Python, PHP, Perl, Swift, JavaScript... You can use for learning programming, scraping web sites, or writing batch
🌐
PyPy
pypy.org › features.html
PyPy - Features | PyPy
December 28, 2019 - You get a fully sandboxed interpreter, in its own filesystem hierarchy (try os.listdir('/')).
🌐
CAE Community
caecommunity.org › sites › default › files › The PyPy Sandbox 20180308.pdf pdf
The PyPy Sandbox
The mission of the National Centers of Academic Excellence in Cybersecurity (NCAE) program is to create and manage a collaborative cybersecurity educational program with community colleges, colleges, and universities.
🌐
Edube
edube.org › sandbox
Edube Sandbox
Python 3.7 · Reset · Console · Clear · × · Link: Embed on your site: Download · × · Please login in to share your code. Log in Download · × · Language: C++ (98) C++ (11) C++ (14) ANSI C · Python 2.7 · Python 3.7 · Python 3.12 · JavaScript ·
🌐
GitHub
github.com › yingted › pysandbox
GitHub - yingted/pysandbox: Python sandbox
Python sandbox. Contribute to yingted/pysandbox development by creating an account on GitHub.
Author   yingted