Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
232 lines (189 loc) · 10.2 KB
/
Copy pathtest.py
File metadata and controls
232 lines (189 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import unittest
import os
from codesafe import safe_eval, encrypt_to_file, encrypt, decrypt_to_file, decrypt, run, EvaluationTimeoutError, UnsafeExpressionError
from time import sleep
LEGACY_PAYLOAD = "NDc7Z2g8ZjhkOmQ3N2c0PGg4OWZnPGRoNGQ4ODdlaTk6Ozc6ZGllM2Y4O2k5aDQ1aWQ1PGRmOmdnaWZkPDw3Mzc4NzY3PGg3NTVpMzg1PDo0PDRoZGc0Nmg1NGc2Z2U4NTNoOGRlaGk4NTM4OGg3PDk3ZTs1aWU1NDZpODw2ZDRnaDpnNGU6NTRkNGgzOTY1ZTpmaTM3aGdpODM2NWY7aGZpaWQ8aTxkMzs3PDU0ODVlPDU5aTRkOGQ6aDo5OGc6NGU0OWU0Z2k4NjtlZDQ1Z2Y2aTw6aGdlZTs4ZmRkOjM4M2c3OWY0Nzs0Njc1PDNpaGVkOzNpOzU2OWY7NmdlPGg2ZTw7ZDdnZD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pZjllOzMzOTszNjdkOTg8ZDxkZzc3PDw0ZTM7Zjg7aTVnNTc4NzY3PGg3NTVpMzg1PDo0PDRoZGc0Nmg1NGc2Z2U4NTNoOGRlaGk4NTM4OGg3PDk3ZTs1aWU1NDZpODw2ZDQ7NTg3ZjY1PGQ8NTs4M2k5Zzg2PGdnNjo5aTc7NDloaDU6OTc4NDpnZDhoMzU2ODg0N2RpNzY2NDk3NzszZzpkMzc2ZDo0Ozo6N2Y4OjVlZztkNThkZ2VoZTRlaWZnOGYzNTg5ZGg0NGZoZmk8aTxmNmk8NThnM2g4NWVoZGk7PDU5OGlnZDQ6ZDY3OTQ0ZTQ4NjZnO2Q1OzRpaTk7M2dmODo8NGUzZmgzZDQ0ZjU4ZTY4aDQ0ZjtoOjg5Ozg4MzxlZDhoZjg0ZzM6ZDdkZjNoPDg0OTM7OjM3NzY0Zzg8ZDM1ZTU0ZDdoPDg0ZGZmNDM4MzhkO2dmNzM6ZjgzNGho" # produced by CodeSafe <= v0.0.5 encrypt()
# Custom function defined for testing
def custom_sum(a, b):
return a + b
def long_running_function():
while True:
pass # This will run indefinitely until the timeout occurs
class TestCodeSafe(unittest.TestCase):
def setUp(self):
# Set up the safe environment with custom variables and functions
self.safe_builtins = {"abs": abs, "max": max}
self.safe_vars = {"x": 10, "y": 20, "custom_sum": custom_sum, "long_running_function": long_running_function, "sleep": sleep}
self.allowed_function_calls = ['custom_sum', 'max', 'abs', 'long_running_function', 'sleep']
# Sample Python code for testing encryption and decryption
self.python_code = """
def greet(name):
print(f"Hello, {name}!")
greet('Johnny')
"""
# File names for testing
self.encrypted_file = "encrypted_code.encrypt"
self.decrypted_file = "decrypted.py"
def test_custom_sum(self):
expression = "custom_sum(x, y)"
result = safe_eval(
expression,
allowed_builtins=self.safe_builtins,
allowed_vars=self.safe_vars,
timeout=3,
allowed_function_calls=self.allowed_function_calls,
file_access=False
)
self.assertEqual(result, 30)
def test_safe_attribute_access(self):
expression = "'hello'.upper()"
result = safe_eval(expression, allow_attributes=True)
self.assertEqual(result, "HELLO")
def test_unsafe_attribute_access(self):
expression = "().__class__.__bases__[0]"
with self.assertRaises(UnsafeExpressionError):
safe_eval(expression, allow_attributes=True)
def test_attribute_access_disabled(self):
expression = "'hello'.upper()"
with self.assertRaises(UnsafeExpressionError):
safe_eval(
expression,
allow_attributes=False # Explicitly disable
)
def test_security_error_on_unsafe_function(self):
expression = "os.getcwd()" # Attempting to call an unsafe function
with self.assertRaises(UnsafeExpressionError):
safe_eval(
expression,
allowed_builtins=self.safe_builtins,
allowed_vars=self.safe_vars,
timeout=3,
allowed_function_calls=self.allowed_function_calls
)
def test_timeout_error(self):
expression = "long_running_function()" # This will run indefinitely
# Check if EvaluationTimeoutError is raised
with self.assertRaises(EvaluationTimeoutError):
safe_eval(
expression,
allowed_builtins=self.safe_builtins,
allowed_vars=self.safe_vars,
timeout=1, # Setting a short timeout
allowed_function_calls=self.allowed_function_calls,
immediate_termination=True
)
def test_encryption(self):
# Encrypt and save the code to a file
encrypt_to_file(self.python_code, self.encrypted_file)
# Check if the encrypted file was created
self.assertTrue(os.path.exists(self.encrypted_file))
def test_decryption_and_run(self):
# Encrypt the code first
encrypt_to_file(self.python_code, self.encrypted_file)
# Decrypt and run the code from the file
run(self.encrypted_file)
# Decrypt to a file
decrypt_to_file(self.encrypted_file, self.decrypted_file)
encrypted = encrypt("print('Hello.')")
decrypted = decrypt(encrypted)
print(f"Encrypted: {encrypted}\nDecrypted: {decrypted}")
# Check if the decrypted file was created
self.assertTrue(os.path.exists(self.decrypted_file))
# Optionally: Check if the decrypted file content matches the original code
with open(self.decrypted_file, 'r') as f:
decrypted_content = f.read()
self.assertEqual(decrypted_content.strip(), self.python_code.strip())
def test_bytes_and_streams(self):
import io
code = b"print('Bytes work')"
enc = encrypt(code)
self.assertIsInstance(enc, str)
# Stream run
stream = io.StringIO(f"# {enc}")
run(stream)
# Stream decrypt
out_stream = io.StringIO()
decrypt_to_file(io.StringIO(f"# {enc}"), out_stream)
self.assertEqual(out_stream.getvalue(), "print('Bytes work')")
def test_encrypt_supports_unicode(self):
# v2 crypto payloads are UTF-8 safe: any Unicode code round-trips.
code = "print('héllo, ✓, 你好')"
encrypted = encrypt(code)
self.assertTrue(encrypted.startswith("codesafe:v2:"))
self.assertEqual(decrypt(encrypted), code)
encrypted_bytes = encrypt(code.encode("utf-8"))
self.assertEqual(decrypt(encrypted_bytes), code)
def test_encrypt_rejects_mapping_argument(self):
# The Caesar mapping parameter was removed in v0.0.6.
with self.assertRaises(TypeError):
encrypt("print('hi')", mapping={"p": "too-short"})
with self.assertRaises(TypeError):
encrypt("print('hi')", {"p": "too-short"})
def test_custom_key_roundtrip_and_wrong_key(self):
from codesafe import generate_key
key = generate_key()
encrypted = encrypt("print('secret')", key=key)
self.assertEqual(decrypt(encrypted, key=key), "print('secret')")
with self.assertRaises(ValueError):
decrypt(encrypted, key=generate_key())
def test_environment_key_roundtrip(self):
import os
from codesafe import generate_key
key = generate_key()
os.environ["CODESAFE_KEY"] = key
try:
encrypted = encrypt("print('env secret')")
self.assertEqual(decrypt(encrypted), "print('env secret')")
finally:
del os.environ["CODESAFE_KEY"]
def test_legacy_payload_still_decrypts(self):
# A payload produced by CodeSafe <= v0.0.5 (Base64 Caesar) must keep
# working. Generated with codesafe.encrypt_with_mapping() and
# base64.b64encode(), exactly as <= v0.0.5 produced it.
legacy = LEGACY_PAYLOAD
self.assertEqual(decrypt(legacy), "print('legacy works')")
def test_legacy_file_still_runs_and_decrypts(self):
import io
legacy = LEGACY_PAYLOAD
out = io.StringIO()
decrypt_to_file(io.StringIO(f"# {legacy}"), out)
self.assertEqual(out.getvalue(), "print('legacy works')")
def test_v2_file_roundtrip(self):
import io
encrypt_to_file("print('v2 works')", "v2_test_prog.encrypt")
with open("v2_test_prog.encrypt", "r") as f:
data = f.read()
self.assertTrue(data.startswith("# codesafe:v2:"))
out = io.StringIO()
decrypt_to_file("v2_test_prog.encrypt", out)
self.assertEqual(out.getvalue(), "print('v2 works')")
def test_decrypt_rejects_invalid_input(self):
with self.assertRaises(ValueError):
decrypt("!!! definitely not base64 !!!")
with self.assertRaises(ValueError):
decrypt("codesafe:v2:bm90LWEtdmFsaWQtdG90ZW4")
def test_exception_propagates_from_evaluation(self):
with self.assertRaises(ZeroDivisionError):
safe_eval("1/0")
with self.assertRaises(NameError):
safe_eval("this_name_is_not_defined")
def test_timeout_must_be_positive(self):
for invalid in (0, -1):
with self.assertRaises(ValueError):
safe_eval("1 + 1", timeout=invalid)
def test_dir_and_id_are_not_available(self):
# Removed from the default safe builtins: pure information leakage.
with self.assertRaises(UnsafeExpressionError):
safe_eval("dir()")
with self.assertRaises(UnsafeExpressionError):
safe_eval("id(1)")
def tearDown(self):
# Clean up: remove the files created during the tests
if os.path.exists(self.encrypted_file):
os.remove(self.encrypted_file)
if os.path.exists(self.decrypted_file):
os.remove(self.decrypted_file)
if os.path.exists("v2_test_prog.encrypt"):
os.remove("v2_test_prog.encrypt")
if __name__ == '__main__':
unittest.main()