-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_security.py
More file actions
40 lines (34 loc) · 1.05 KB
/
Copy pathtest_security.py
File metadata and controls
40 lines (34 loc) · 1.05 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
# test_security.py
import cryptography
from cryptography.fernet import Fernet
import schema
import bleach
# Test cryptography
key = Fernet.generate_key()
cipher = Fernet(key)
encrypted = cipher.encrypt(b'Hello, secure Python!')
decrypted = cipher.decrypt(encrypted)
print(' Cryptography test:')
print(f' Original: Hello, secure Python!')
print(f' Encrypted: {encrypted[:50]}...')
print(f' Decrypted: {decrypted.decode()}')
print()
# Test schema validation
user_schema = schema.Schema({
'name': str,
'age': int,
'email': str
})
valid_data = {'name': 'John', 'age': 30, 'email': 'john@example.com'}
validated = user_schema.validate(valid_data)
print(' Schema validation test: PASSED')
print(f' Validated data: {validated}')
print()
# Test bleach HTML sanitization
dirty_html = '<script>alert("XSS")</script><p>Safe content</p>'
clean_html = bleach.clean(dirty_html)
print(' Bleach sanitization test:')
print(f' Dirty HTML: {dirty_html}')
print(f' Clean HTML: {clean_html}')
print()
print(' All security tests passed! Your environment is ready.')