Package: safepyeval
safepyevalsafepyevalis a Python library designed for the safe evaluation of a restricted subset of Python expressions.
This tool is particularly useful in environments where running arbitrary Python code can pose a security risk,
such as in any application requiring controlled execution of user-submitted code.The library specifically excludes support for loops (forandwhile) due to their potential for creating harmful scenarios,
such as infinite loops or overly complex computations that can drain system resources.The controlled environment is achieved by parsing the submitted code into an Abstract Syntax Tree (AST) (using the Python ast module), allowing for a granular inspection and execution of code elements. Importantly, the Python interpreter is not used to execute the code, which completely prevents the execution of unsafe operations. Therefore it is extremely safe to usesafepyevalin any environment.InstallationYou can install SafePyEval using pip:pipinstallsafepyevalUsageImport the library and use the evaluate function to safely execute Python code. Here's a basic example:importsafepyevalcode='''admin_user_ids = ['admin', 'user1']if userId in admin_user_ids:return Trueelse:return Falseresult = safepyeval.evaluate(code, {'userId': 'user1'})# result is TrueCapabilitiessafepyevalsupports a variety of Python features while ensuring a secure execution environment:Variable Assignments: You can define and use variables within the code.Conditional Statements: if, else, and elif statements are supported.Comparisons and Boolean Operations: Including ==, !=, <, <=, >, >=, and, or, and not.Mathematical and String Operations: Basic operations like +, -, *, /, and string manipulation.Data Structures: Use of lists, dictionaries, and access to their elements.LimitationsTo maintain safety and prevent abuse, safepyeval does not implement certain parts of Python:Loops: for and while loops are not implemented, as they can lead to dangerous behavior like infinite loops.File I/O: No file reading or writing to prevent unauthorized access to the file system.Network Operations: Disabled to prevent network-based attacks or unauthorized data transmission.Importing Modules: Importing of modules is disabled.Executing Shell Commands: Disabled to avoid executing operating system commands.Advanced Exampleimportsafepyevalcode='''admin_user_ids = ['magland', 'admin']max_num_cpus_for_admin = 8max_num_cpus_for_privileged_users = 4other_users = {'user3': {'max_num_cpus': 2},'user4': {'max_num_cpus': 1}}if userId in admin_user_ids and not userId == 'magland':if nCpus <= max_num_cpus_for_admin:return Trueelse:return Falseelif userId in ['user1', 'user2']:if nCpus <= max_num_cpus_for_privileged_users:return Trueelse:return Falseelse:if userId in other_users:if nCpus <= other_users[userId]['max_num_cpus']:return Trueelse:return Falseelse:return False'''result1=safepyeval.evaluate(code,{'userId':'user1','nCpus':4})# result1 is Trueresult2=safepyeval.evaluate(code,{'userId':'user1','nCpus':8})# result2 is False# etc.Size of codebaseThe library is very small, consisting of a single Python file (safepyeval.py) with around 100 or so lines of code.LicenseThis package is released under Apache License 2.0.AuthorCreated by Jeremy Magland