-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathtest_security_fix.py
More file actions
77 lines (60 loc) · 2.31 KB
/
test_security_fix.py
File metadata and controls
77 lines (60 loc) · 2.31 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
import os
import tempfile
import pytest
from text_extract_api.files.storage_strategies.local_filesystem import LocalFilesystemStorageStrategy
def test_path_traversal_protection():
"""Test that path traversal attacks are blocked"""
with tempfile.TemporaryDirectory() as temp_dir:
context = {
'settings': {
'root_path': temp_dir,
'create_subfolders': False,
'subfolder_names_format': ''
}
}
storage = LocalFilesystemStorageStrategy(context)
# Test path traversal attempts
malicious_filenames = [
"../../../../../../etc/passwd",
"../../../secret.txt",
"..\\..\\..\\secret.txt",
"file/../../../etc/passwd"
]
for filename in malicious_filenames:
with pytest.raises(ValueError, match="Path traversal detected"):
storage.load(filename)
with pytest.raises(ValueError, match="Path traversal detected"):
storage.save("test.txt", filename, "content")
with pytest.raises(ValueError, match="Path traversal detected"):
storage.delete(filename)
def test_legitimate_operations():
"""Test that legitimate file operations work correctly"""
with tempfile.TemporaryDirectory() as temp_dir:
context = {
'settings': {
'root_path': temp_dir,
'create_subfolders': False,
'subfolder_names_format': ''
}
}
storage = LocalFilesystemStorageStrategy(context)
# Test normal file operations
filename = "test.txt"
content = "Test content"
# Save file
storage.save(filename, filename, content)
# Load file
loaded_content = storage.load(filename)
assert loaded_content == content
# List files
files = storage.list()
assert filename in files
# Delete file
storage.delete(filename)
# Verify file is deleted
with pytest.raises(FileNotFoundError):
storage.load(filename)
if __name__ == "__main__":
test_path_traversal_protection()
test_legitimate_operations()
print("All tests passed!")