-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVuln-example.py
More file actions
79 lines (63 loc) · 2.32 KB
/
Vuln-example.py
File metadata and controls
79 lines (63 loc) · 2.32 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
import os
import json
import zlib
import struct
from typing import Dict, Any, Optional
class DataSerializer:
VERSION = 2
MAGIC = b'DS02'
def __init__(self):
self.compression_enabled = True
self.max_size = 1024 * 1024 # 1MB
def serialize(self, data: Dict[str, Any]) -> bytes:
json_data = json.dumps(data, separators=(',', ':'))
json_bytes = json_data.encode('utf-8')
if self.compression_enabled and len(json_bytes) > 100:
compressed = zlib.compress(json_bytes, 6)
if len(compressed) < len(json_bytes) * 0.9:
json_bytes = compressed
is_compressed = 1
else:
is_compressed = 0
else:
is_compressed = 0
header = struct.pack('<4sHBBI',
self.MAGIC,
self.VERSION,
is_compressed,
0, # reserved
len(json_bytes)
)
return header + json_bytes
def deserialize(self, data: bytes) -> Optional[Dict[str, Any]]:
if len(data) < 13:
return None
magic, version, is_compressed, reserved, size = struct.unpack('<4sHBBI', data[:13])
if magic != self.MAGIC or version != self.VERSION:
return None
if size > self.max_size:
return None
payload = data[13:13+size]
if len(payload) != size:
return None
try:
json_bytes = zlib.decompress(payload) if is_compressed else payload
return json.loads(json_bytes.decode('utf-8'))
except:
return None
class ConfigStore:
def __init__(self, base_path: str = "./configs"):
self.base_path = base_path
self.serializer = DataSerializer()
os.makedirs(base_path, exist_ok=True)
def save(self, name: str, config: Dict[str, Any]) -> bool:
if not name.replace('_', '').replace('-', '').isalnum():
return False
path = os.path.join(self.base_path, f"{name}.dat")
try:
data = self.serializer.serialize(config)
with open(path, 'wb') as f:
f.write(data)
return True
except:
return False