You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 27f19e2
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: README.md
+91-22Lines changed: 91 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,49 +12,110 @@ An open-source Python library for code encryption, decryption, and safe evaluati
12
12
> [!NOTE]
13
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
14
15
+
## Security Advisory
16
+
17
+
### Critical Vulnerability Fixed in v0.0.5
18
+
19
+
**Versions 0.0.1 through 0.0.4 contain a known sandbox escape vulnerability in `safe_eval()` that allows arbitrary code execution.**
20
+
21
+
-**Vulnerability**: The `safe_eval()` function in versions 0.0.1-0.0.4 is vulnerable to a sandbox escape via indirect subscript lookup (e.g., `__builtins__['exec'](...)`). This bypasses the AST validation and allows execution of arbitrary Python code.
22
+
-**Impact**: An attacker controlling an expression passed to `safe_eval()` can execute arbitrary Python code with the application's permissions.
**If you are using any version from 0.0.1 to 0.0.4, you should upgrade to version 0.0.5 or later immediately.** These earlier versions should **not** be used in untrusted expression evaluation environments.
27
+
28
+
The fix includes:
29
+
1. Rejecting all `ast.Call` nodes unless the exact target is explicitly whitelisted (including calls via subscript, attribute, and other indirect targets)
30
+
2. Using a minimal built-ins allowlist that excludes dangerous functions like `exec`, `eval`, `compile`, and `__import__`
31
+
3. Added comprehensive regression tests for indirect call targets
CodeSafe v0.0.5+ includes a rigorous test suite (`TestVulnerabilityFixes` and `TestEdgeCases` in `test_poc.py`) to systematically verify that `safe_eval` blocks sandbox escapes while preserving standard evaluation features.
38
+
39
+
### Test Results Summary
40
+
41
+
**49/49 Security & Edge Case Tests Passing** (`100% Pass Rate` across all vector categories)
42
+
43
+
| Attack Vector / Category | Specific Vectors Tested | Status |
* Blocked indirect function calls via `ast.Subscript`, `ast.Attribute`, and other non-`ast.Name` targets
68
+
* Implemented minimal safe builtins whitelist excluding `exec`, `eval`, `compile`, `__import__`, and similar dangerous functions
69
+
* Added regression tests for indirect call bypass attempts
70
+
15
71
### Changelog v0.0.4:
16
-
- Major performance optimizations for encryption and decryption (~10x speedup).
17
-
- Optimized `safe_eval` by pre-filtering builtins and reducing internal overhead.
18
-
- Added support for `bytes` input in `encrypt`.
19
-
- Added support for file-like stream objects in `run` and `decrypt_to_file`.
20
-
- Standardized error handling (replaced `print` with exceptions).
72
+
73
+
* Major performance optimizations for encryption and decryption (~10x speedup).
74
+
* Optimized `safe_eval` by pre-filtering builtins and reducing internal overhead.
75
+
* Added support for `bytes` input in `encrypt`.
76
+
* Added support for file-like stream objects in `run` and `decrypt_to_file`.
77
+
* Standardized error handling (replaced `print` with exceptions).
21
78
22
79
### Changelog v0.0.3:
23
-
- Added an `allow_attributes` parameter to `safe_eval` and set `immediate_termination` to be `True` by default for safer function calling.
80
+
81
+
* Added an `allow_attributes` parameter to `safe_eval` and set `immediate_termination` to be `True` by default for safer function calling.
24
82
25
83
### Changelog v0.0.2:
26
-
- Fixed function returns.
27
-
- Added error handling to `CodeSafe`, removed some print statements with edits from `@0XC7R`.
84
+
85
+
* Fixed function returns.
86
+
* Added error handling to `CodeSafe`, removed some print statements with edits from `@0XC7R`.
28
87
29
88
### Changelog v0.0.1:
30
-
- Initial release
89
+
90
+
* Initial release
31
91
32
92
## Installation
33
93
34
94
You can install CodeSafe using pip:
35
95
36
96
```bash
37
97
pip install codesafe
98
+
38
99
```
39
100
40
101
## Supported Python Versions
41
102
42
103
CodeSafe supports the following Python versions:
43
104
44
-
- Python 3.6
45
-
- Python 3.7
46
-
- Python 3.8
47
-
- Python 3.9
48
-
- Python 3.10
49
-
- Python 3.11/Later (Preferred)
105
+
* Python 3.6
106
+
* Python 3.7
107
+
* Python 3.8
108
+
* Python 3.9
109
+
* Python 3.10
110
+
* Python 3.11/Later (Preferred)
50
111
51
112
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.
52
113
53
114
## Features
54
115
55
-
-**Safe Eval**: Safely allow `eval()` expressions to run, while maintaining complete control over the entire evaluation process.
56
-
-**Code Encryption/Decryption**: Quickly encrypt your code. This is meant for code obfuscation, and not high-level encryption.
57
-
-**Run encrypted code at runtime**: Run your encrypted code files, without needing to expose your code to end-users.
116
+
***Safe Eval**: Safely allow `eval()` expressions to run, while maintaining complete control over the entire evaluation process.
117
+
***Code Encryption/Decryption**: Quickly encrypt your code. This is meant for code obfuscation, and not high-level encryption.
118
+
***Run encrypted code at runtime**: Run your encrypted code files, without needing to expose your code to end-users.
58
119
59
120
> [!NOTE]
60
121
> Running encrypted files at runtime using `run()` are only available in formats that can be understood by Python.
@@ -64,14 +125,20 @@ Please ensure that you have one of these Python versions installed before using
64
125
> ```python
65
126
>if__name__=='__main__':
66
127
># Run eval, etc.
128
+
>
67
129
>```
130
+
>
131
+
>
68
132
> If you're planning on including `safe_eval` in executables:
69
133
>```python
70
134
>import multiprocessing
71
135
>if__name__=='__main__':
72
136
> multiprocessing.freeze_support()
73
137
># Call safe_eval afterwards
138
+
>
74
139
>```
140
+
>
141
+
>
75
142
> 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
> 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).
162
+
> 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?utm_source=gemini).
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).
193
+
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?utm_source=gemini).
125
194
126
195
## License
127
196
128
-
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.
197
+
CodeSafe is released under the terms of the **MIT License (Modified)**. Please see the [LICENSE](https://github.com/infinitode/codesafe/blob/main/LICENSE?utm_source=gemini) file for the full text.
129
198
130
199
**Modified License Clause**
131
200
132
-
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.
201
+
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