-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_guard.py
More file actions
241 lines (219 loc) · 6.21 KB
/
Copy pathscript_guard.py
File metadata and controls
241 lines (219 loc) · 6.21 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
233
234
235
236
237
238
239
240
241
# Copyright (c) 2026 Compute Field Lab, LLC, Abu-Dhabi. All rights reserved.
"""Fail-closed syntax and capability gate for Workbench programs."""
import ast
import builtins
MAX_SOURCE_BYTES = 128 * 1024
MAX_AST_NODES = 20_000
_ALLOWED_IMPORTS = {
"accelerate",
"collections",
"dataclasses",
"functools",
"hashlib",
"itertools",
"math",
"random",
"safetensors",
"statistics",
"time",
"timm",
"torch",
"torchvision",
"transformers",
"typing",
}
_BANNED_NAMES = {
"__builtins__",
"__import__",
"breakpoint",
"compile",
"delattr",
"eval",
"exec",
"getattr",
"globals",
"help",
"input",
"locals",
"open",
"setattr",
"vars",
}
_BANNED_ATTRIBUTES = {
"builtins",
"classes",
"connect",
"ctypes",
"distributed",
"dump",
"dumps",
"from_file",
"fromfile",
"from_pretrained",
"hub",
"importlib",
"jit",
"load",
"multiprocessing",
"ops",
"package",
"load_inline",
"load_library",
"os",
"pathlib",
"popen",
"remove",
"removedirs",
"rename",
"renames",
"replace",
"request",
"rmdir",
"rmtree",
"save",
"serialization",
"shutil",
"socket",
"subprocess",
"sys",
"system",
"tofile",
"unlink",
"urlopen",
}
_BANNED_CHAINS = {
"torch.classes",
"torch.distributed",
"torch.hub",
"torch.jit",
"torch.multiprocessing",
"torch.ops",
"torch.package",
"torch.serialization",
"torch.utils.cpp_extension",
}
class UnsafeScript(ValueError):
pass
def _chain(node: ast.Attribute) -> str:
parts = [node.attr]
value = node.value
while isinstance(value, ast.Attribute):
parts.append(value.attr)
value = value.value
if isinstance(value, ast.Name):
parts.append(value.id)
return ".".join(reversed(parts))
def _matches_module(name: str, roots: set[str]) -> bool:
return any(name == root or name.startswith(root + ".") for root in roots)
def _validate_import(node: ast.Import | ast.ImportFrom) -> None:
modules = [alias.name for alias in node.names] if isinstance(node, ast.Import) else [node.module or ""]
level = node.level if isinstance(node, ast.ImportFrom) else 0
if level or any(not _matches_module(name, _ALLOWED_IMPORTS) for name in modules):
raise UnsafeScript("import is not available in Machine scripts")
if any(_matches_module(name, _BANNED_CHAINS) for name in modules):
raise UnsafeScript("unsafe module import is not allowed")
if isinstance(node, ast.ImportFrom) and any(
alias.name.startswith("_") or alias.name in _BANNED_ATTRIBUTES for alias in node.names
):
raise UnsafeScript("private or unsafe imported attributes are not allowed")
def _validate_node(node: ast.AST) -> None:
if isinstance(node, (ast.Global, ast.Nonlocal)):
raise UnsafeScript(f"{type(node).__name__.lower()} declarations are not allowed")
if isinstance(node, ast.Name) and (node.id in _BANNED_NAMES or node.id.startswith("__")):
raise UnsafeScript(f"name {node.id!r} is not allowed")
if isinstance(node, ast.Constant) and isinstance(node.value, str) and node.value.startswith("__"):
raise UnsafeScript("dunder attribute access is not allowed")
if isinstance(node, (ast.Import, ast.ImportFrom)):
_validate_import(node)
if not isinstance(node, ast.Attribute):
return
chain = _chain(node)
if node.attr.startswith("_") or node.attr in _BANNED_ATTRIBUTES:
raise UnsafeScript(f"attribute {node.attr!r} is not allowed")
if _matches_module(chain, _BANNED_CHAINS):
raise UnsafeScript(f"API {chain!r} is not allowed")
def _parse(code: str) -> list[ast.AST]:
try:
tree = ast.parse(code, filename="<workbench:machine>", mode="exec")
except SyntaxError as exc:
raise UnsafeScript(f"invalid Python: {exc.msg} at line {exc.lineno}") from exc
nodes = list(ast.walk(tree))
if len(nodes) > MAX_AST_NODES:
raise UnsafeScript("script is too complex")
return nodes
def validate_script(code: str) -> ast.Module:
"""Parse and validate Machine code against the explicit capability set."""
if len(code.encode("utf-8")) > MAX_SOURCE_BYTES:
raise UnsafeScript("script exceeds the 128 KiB safety limit")
nodes = _parse(code)
for node in nodes:
_validate_node(node)
return nodes[0]
def _builtins() -> dict:
def guarded_import(name, globals=None, locals=None, fromlist=(), level=0):
if level or not any(name == root or name.startswith(root + ".") for root in _ALLOWED_IMPORTS):
raise ImportError(f"module {name!r} is not available in Machine scripts")
return builtins.__import__(name, globals, locals, fromlist, level)
names = {
"ArithmeticError",
"AssertionError",
"AttributeError",
"BaseException",
"Exception",
"IndexError",
"KeyError",
"MemoryError",
"RuntimeError",
"StopIteration",
"TypeError",
"ValueError",
"ZeroDivisionError",
"__build_class__",
"abs",
"all",
"any",
"bool",
"bytes",
"callable",
"dict",
"enumerate",
"filter",
"float",
"frozenset",
"hasattr",
"hash",
"int",
"isinstance",
"issubclass",
"iter",
"len",
"list",
"map",
"max",
"min",
"next",
"object",
"property",
"range",
"repr",
"reversed",
"round",
"set",
"slice",
"sorted",
"str",
"sum",
"super",
"tuple",
"type",
"zip",
}
values = {name: getattr(builtins, name) for name in names}
values["__import__"] = guarded_import
return values
def execute_script(code: str, namespace: dict) -> None:
"""Execute validated code with a reduced builtin and import namespace."""
tree = validate_script(code)
namespace["__builtins__"] = _builtins()
namespace.setdefault("__name__", "workbench_machine")
exec(compile(tree, "<workbench:machine>", "exec"), namespace) # nosec B102