forked from jevinskie/jevutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrew-sandbox.py
executable file
·71 lines (60 loc) · 3.07 KB
/
brew-sandbox.py
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
#!/usr/bin/env python3
import os
import sys
def build_sandbox_policy(packages: list[str]) -> str:
lines = [
"(version 1)",
"(allow default)",
# '(deny file* (literal "/opt/homebrew"))',
# '(deny file* (subpath "/opt/homebrew"))',
# '(allow file-read* (literal "/opt/homebrew"))',
'(deny file* (literal "/opt/homebrew/include"))',
'(deny file* (subpath "/opt/homebrew/include"))',
# '(allow (with report) file* (literal "/opt/homebrew/include"))',
# '(allow (with report) file* (subpath "/opt/homebrew/include"))',
# '(allow file-read* (literal "/opt/homebrew/bin"))',
# '(allow file-read* (literal "/opt/homebrew/lib"))',
# '(allow file-read* (literal "/opt/homebrew/opt"))',
# '(allow file-read* (literal "/opt/homebrew/Cellar"))',
# '(allow file-map-executable (subpath "/opt/homebrew"))',
# '(allow file-read* (subpath "/opt/homebrew/bin"))',
# '(allow file-read* (subpath "/opt/homebrew/lib"))',
# '(allow file-read* (subpath "/opt/homebrew/opt/pcre"))',
# '(allow file-read* (literal "/opt/homebrew/Cellar/pcre/8.45/lib"))',
# '(allow file-read* (literal "/opt/homebrew/Cellar/pcre"))',
# '(allow file-read* (literal "/opt/homebrew/Cellar/pcre/8.45"))',
# '(allow file-read* (literal "/opt/homebrew/Cellar/pcre/8.45/lib"))',
# '(allow file-read* (subpath "/opt/homebrew/Cellar/pcre/8.45/lib"))',
# '(allow file-read* (regex ".*\.dylib"))',
# '(allow file-map-executable (regex ".*\.dylib"))',
]
for pkg in packages:
# Note: subpath directive is quoted, but parentheses, etc. will parse fine when passed as one argument.
brew_opt_path = f"/opt/homebrew/opt/{pkg}"
brew_cellar_path = os.readlink(brew_opt_path)
brew_cellar_path = os.path.join(os.path.dirname(brew_opt_path), brew_cellar_path)
brew_cellar_path = os.path.abspath(brew_cellar_path)
lines.append(f'(allow file-read* (literal "{brew_opt_path}"))')
lines.append(f'(allow file-read* (literal "{os.path.dirname(brew_cellar_path)}"))')
lines.append(f'(allow file-read* (literal "{brew_cellar_path}"))')
lines.append(f'(allow file* (subpath "{brew_opt_path}"))')
lines.append(f'(allow file* (subpath "{brew_cellar_path}"))')
# Flatten into a single space-separated string
print("\n".join(lines))
return " ".join(lines)
def main() -> None:
if "--" not in sys.argv:
print(f"Usage: {sys.argv[0]} <packages...> -- <command...>", file=sys.stderr)
sys.exit(1)
dash_index = sys.argv.index("--")
packages = sys.argv[1:dash_index]
command = sys.argv[dash_index + 1 :]
if not command:
print("No command specified after --", file=sys.stderr)
sys.exit(1)
# Build the single-line sandbox policy
policy_str = build_sandbox_policy(packages)
# Execute sandbox-exec with inline policy, replacing this process
os.execv("/usr/bin/sandbox-exec", ["sandbox-exec", "-p", policy_str, *command])
if __name__ == "__main__":
main()