DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Come Play in the Sand with PySandbox

Come Play in the Sand with PySandbox

Chris Smith user avatar by
Chris Smith
·
Mar. 21, 12 · Interview
Like (2)
Save
Tweet
Share
3.83K Views

Join the DZone community and get the full member experience.

Join For Free
If you're looking for a way to test your Python code without exposing your system to untrusted code, then this may be the solution you've been searching for.  PySandbox is just what it sounds like: a Python Sandbox.  It allows you to run Python code while preventing that code from modifying the environment, which means the code cannot write a file, import a module, or use the print function.  Other blocked functions in PySandbox include:

  • Deny access to the file system
  • Deny importing Python modules
  • Deny exiting Python
  • Deny access to stein, stout, or stderr
  • Deny some built-ins symbols like execfile(), reload() or KeyboardInterrupt
  • Deny execution of arbitrary bytecode (creation of arbitrary code object)

All of these features can be enabled in the Sandbox Configuration settings, so the user has control over what functions and modules are permitted inside the sandbox.  The code is run through a subprocess that has the following limitations:

  • timeout = 5 seconds
  • memory limit = 200 MB
  • recursion limit = 50 frames
  • number of child process = 0 (disabled fork or thread at the OS level)
  • pysandbox is able to catch crashed like segmentation fault (SIGSEGV)
  • stdin, stdout, and stderr are redirected to /dev/null (or :NUL on Windows)

There's also a wealth of features available, that can be turned on and off using SandboxConfig, including:

  • "codecs": codecs module
  • "debug_sandbox": allow to display a traceback of the sandbox.
  • "encodings": encodings module with ascii, latin_1, utf_8, utf_16_be, utf_32_be and rot_13 codecs (submodules).
  • "interpreter": give access to standard streams, enable traceback.

PySandbox features a CPython restricted mode, which is currently deemed "unsafe" due to the possible execution of arbitrary bytecode.  Unfortunately, the restricted mode no longer exists in Python 3, as it was removed with the bastion and exec modules.

Currently, it has been tested on Python 2.5 and 2.6, but support for Python 3 is in the works. For more information on PySandbox, or to download the source files, visit the github page.
Python (language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • gRPC on the Client Side
  • Container Security: Don't Let Your Guard Down
  • Tracking Software Architecture Decisions
  • Steel Threads Are a Technique That Will Make You a Better Engineer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: