An open-source Python library for code encryption, decryption, and safe evaluation using Python's built-in AST module, complete with allowed functions, variables, built-in imports, timeouts, and blocked access to attributes.
Project description
CodeSafe
An open-source Python library for code encryption, decryption, and safe evaluation using Python's built-in AST module, complete with allowed functions, variables, built-in imports, timeouts, and blocked access to attributes.
CodeSafe is an experimental library, and we're still running some tests on it. If you encounter any issues, or have an edge use case, please let us know.
[!NOTE] CodeSafe is intended to quickly encrypt/decrypt code files, and run them (only for Python script files) while in their encrypted form, but not as a means for powerful encryption, just code obfuscation. We have also included a
safe_evalfunction, that can safely evaluate expressions within a safe environment.
Changelog v0.0.3:
- Added an
allow_attributesparameter tosafe_evaland setimmediate_terminationto beTrueby default for safer function calling.
Changelog v0.0.2:
- Fixed function returns.
- Added error handling to
CodeSafe, removed some print statements with edits from@0XC7R.
Changelog v0.0.1:
- Initial release
Installation
You can install CodeSafe using pip:
pip install codesafe
Supported Python Versions
CodeSafe supports the following Python versions:
- Python 3.6
- Python 3.7
- Python 3.8
- Python 3.9
- Python 3.10
- Python 3.11/Later (Preferred)
Please ensure that you have one of these Python versions installed before using CodeSafe. CodeSafe may not work as expected on lower versions of Python than the supported.
Features
- Safe Eval: Safely allow
eval()expressions to run, while maintaining complete control over the entire evaluation process. - Code Encryption/Decryption: Quickly encrypt your code. This is meant for code obfuscation, and not high-level encryption.
- Run encrypted code at runtime: Run your encrypted code files, without needing to expose your code to end-users.
[!NOTE] Running encrypted files at runtime using
run()are only available in formats that can be understood by Python.
[!IMPORTANT] When running
safe_eval, make sure to wait for the Python file to finish its bootstrapping phase. This can be done by simply waiting for:if __name__ == '__main__': # Run eval, etc.If you're planning on including
safe_evalin executables:import multiprocessing if __name__ == '__main__': multiprocessing.freeze_support() # Call safe_eval afterwardsYou can read more about why this needs to be done here: https://pytorch.org/docs/stable/notes/windows.html#multiprocessing-error-without-if-clause-protection
Usage
Safe Eval
from codesafe import safe_eval
if __name__ == '__main__':
# Run a normal, safe expression
expression = "1 + 1"
disallowed_expression = "os.getcwd()"
result1 = safe_eval(expression, timeout=10, immediate_termination=True)
result2 = safe_eval(disallowed_expression, timeout=10, immediate_termination=True)
[!NOTE] Attribute inspection is disabled when using
safe_eval. You can read more about how to usesafe_evalfrom here.
Encrypt & Run Code
from codesafe import encrypt_to_file, decrypt_to_file, run
code = """
greetJohnny = "Hello Johnny!"
def greet_someone(greeting):
print(greeting)
greet_someone(greetJohnny)
"""
# Encrypt the code
encrypted_file_path = "encrypted_code.encrypt"
encrypt_to_file(code, encrypted_file_path)
# Run the encrypted code
run(encrypted_file_path) # Hello Johnny!
# Decrypt code to another file
output_file = "decrypted_code.py"
decrypt_to_file(encrypted_file_path, output_file)
Contributing
Contributions are welcome! If you encounter any issues, have suggestions, or want to contribute to CodeSafe, please open an issue or submit a pull request on GitHub.
License
CodeSafe is released under the terms of the MIT License (Modified). Please see the LICENSE file for the full text.
Modified License Clause
The modified license clause grants users the permission to make derivative works based on the CodeSafe software. However, it requires any substantial changes to the software to be clearly distinguished from the original work and distributed under a different name.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file codesafe-0.0.3.tar.gz.
File metadata
- Download URL: codesafe-0.0.3.tar.gz
- Upload date:
- Size: 14.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae90149f19ea81798362a4400c3ff73faa2fe230935ab01e2bca353ceedec8e8
|
|
| MD5 |
b249d8210bdd7e43268b741d6b843265
|
|
| BLAKE2b-256 |
a16e50a24a39aa9c21c032f5b7b4a64ed43cae906edd599a829723b88d6380b4
|
File details
Details for the file codesafe-0.0.3-py3-none-any.whl.
File metadata
- Download URL: codesafe-0.0.3-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.23
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d97c40a7b3303cab11460d9f4b9ef8c8ebc5d216706f32e6e6d35e742c454ae9
|
|
| MD5 |
d54613725bde40472958889ae968e924
|
|
| BLAKE2b-256 |
a11332693aed542a9b673c219902f2c5f29f5617a9626d59efa06db67854d4d8
|