Skip to content

Instantly share code, notes, and snippets.

TreeBox: Escaping a Python AST Sandbox

Python is a versatile and easy-to-learn programming language. As such, developers are keen to use it as a "mini-programming language" for users to write custom code that will run within an application's sandbox. The usual idea is to do the following:

  1. read the user's custom code;
  2. verify that the code is safe to execute (i.e. sandbox it)
  3. exec() the verifed safe code.