Skip to content

Commit 139e052

Browse files
committed
Initial commit.
0 parents  commit 139e052

File tree

7 files changed

+647
-0
lines changed

7 files changed

+647
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Publish Python 🐍 distributions 📦 to PyPI
2+
3+
on:
4+
push:
5+
tags:
6+
- '*'
7+
8+
jobs:
9+
build-n-publish:
10+
name: Build and publish Python 🐍 distributions 📦 to PyPI
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@master
14+
- name: Set up Python 3.12
15+
uses: actions/setup-python@v3
16+
with:
17+
python-version: '3.12'
18+
- name: Install pypa/setuptools
19+
run: >-
20+
python -m
21+
pip install setuptools wheel
22+
- name: Extract tag name
23+
id: tag
24+
run: echo ::set-output name=TAG_NAME::$(echo $GITHUB_REF | cut -d / -f 3)
25+
- name: Update version in setup.py
26+
run: >-
27+
sed -i "s/{{VERSION_PLACEHOLDER}}/${{ steps.tag.outputs.TAG_NAME }}/g" setup.py
28+
- name: Build a binary wheel
29+
run: >-
30+
python setup.py sdist bdist_wheel
31+
- name: Publish distribution 📦 to PyPI
32+
uses: pypa/gh-action-pypi-publish@master
33+
with:
34+
password: ${{ secrets.PYPI_API_TOKEN }}

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__pycache__
2+
*.pyc
3+
dist/
4+
build/
5+
venv/

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
MIT License (Modified)
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to make derivative works based on the Software, provided that any substantial changes to the Software are clearly distinguished from the original work and are distributed under a different name.
4+
5+
The original copyright notice and disclaimer must be retained in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# CodeSafe
2+
![Python Version](https://img.shields.io/badge/python-3.12-blue.svg)
3+
[![Code Size](https://img.shields.io/github/languages/code-size/infinitode/codesafe)](https://github.com/infinitode/codesafe)
4+
![Downloads](https://pepy.tech/badge/codesafe)
5+
![License Compliance](https://img.shields.io/badge/license-compliance-brightgreen.svg)
6+
![PyPI Version](https://img.shields.io/pypi/v/codesafe)
7+
8+
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.
9+
10+
*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.*
11+
12+
> [!NOTE]
13+
> **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_eval` function, that can safely evaluate expressions within a safe environment.
14+
15+
## Installation
16+
17+
You can install CodeSafe using pip:
18+
19+
```bash
20+
pip install codesafe
21+
```
22+
23+
## Supported Python Versions
24+
25+
CodeSafe supports the following Python versions:
26+
27+
- Python 3.6
28+
- Python 3.7
29+
- Python 3.8
30+
- Python 3.9
31+
- Python 3.10
32+
- Python 3.11/Later (Preferred)
33+
34+
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.
35+
36+
## Features
37+
38+
- **Safe Eval**: Safely allow `eval()` expressions to run, while maintaining complete control over the entire evaluation process.
39+
- **Code Encryption/Decryption**: Quickly encrypt your code. This is meant for code obfuscation, and not high-level encryption.
40+
- **Run encrypted code at runtime**: Run your encrypted code files, without needing to expose your code to end-users.
41+
42+
> [!NOTE]
43+
> Running encrypted files at runtime using `run()` are only available in formats that can be understood by Python.
44+
45+
> [!IMPORTANT]
46+
> When running `safe_eval`, make sure to wait for the Python file to finish its bootstrapping phase. This can be done by simple waiting for:
47+
> ```python
48+
> if __name__ == '__main__':
49+
> # Run eval, etc.
50+
> ```
51+
> If you're planning on including `safe_eval` in executables:
52+
> ```python
53+
> import multiprocessing
54+
> if __name__ == '__main__':
55+
> multiprocessing.freeze_support()
56+
> # Call safe_eval afterwards
57+
> ```
58+
> You 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
59+
60+
## Usage
61+
62+
### Safe Eval
63+
64+
```python
65+
from codesafe import safe_eval
66+
67+
if __name__ == '__main__':
68+
# Run a normal, safe expression
69+
expression = "1 + 1"
70+
disallowed_expression = "os.getcwd()"
71+
72+
result1 = safe_eval(expression, timeout=10, immediate_termination=True)
73+
result2 = safe_eval(disallowed_expression, timeout=10, immediate_termination=True)
74+
```
75+
76+
> [!NOTE]
77+
> Attribute inspection is disabled when using `safe_eval`. You can read more about how to use `safe_eval` from [here](https://infinitode-docs.gitbook.io/documentation/package-documentation/codesafe-package-documentation).
78+
79+
### Encrypt & Run Code
80+
81+
```python
82+
from codesafe import encrypt_to_file, decrypt_to_file, run
83+
84+
code = """
85+
greetJohnny = "Hello Johnny!"
86+
87+
def greet_someone(greeting):
88+
print(greeting)
89+
90+
greet_someone(greetJohnny)
91+
"""
92+
93+
# Encrypt the code
94+
encrypted_file_path = "encrypted_code.encrypt"
95+
encrypt_to_file(code, encrypted_file_path)
96+
97+
# Run the encrypted code
98+
run(encrypted_file_path) # Hello Johnny!
99+
100+
# Decrypt code to another file
101+
output_file = "decrypted_code.py"
102+
decrypt_to_file(encrypted_file_path, output_file)
103+
```
104+
105+
## Contributing
106+
107+
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](https://github.com/infinitode/codesafe).
108+
109+
## License
110+
111+
CodeSafe is released under the terms of the **MIT License (Modified)**. Please see the [LICENSE](https://github.com/infinitode/codesafe/blob/main/LICENSE) file for the full text.
112+
113+
**Modified License Clause**
114+
115+
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.

0 commit comments

Comments
 (0)