| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
""" |
| 4 |
Graphviz sandbox |
| 5 |
|
| 6 |
This program is a wrapper around Graphviz. It aims to provide a safe environment for the |
| 7 |
processing of untrusted input graphs and command line options. More precisely: |
| 8 |
1. No network access will be allowed. |
| 9 |
2. The file system will be read-only. Command line options like `-o …` and `-O` will |
| 10 |
not work. It is expected that the caller will render to stdout and pipe the output |
| 11 |
to their desired file. |
| 12 |
""" |
| 13 |
|
| 14 |
import abc |
| 15 |
import platform |
| 16 |
import shutil |
| 17 |
import subprocess as sp |
| 18 |
import sys |
| 19 |
from pathlib import Path |
| 20 |
from typing import Optional, Type, Union |
| 21 |
|
| 22 |
|
| 23 |
class Sandbox: |
| 24 |
""" |
| 25 |
API for sandbox interaction |
| 26 |
|
| 27 |
Specific sandbox mechanisms should be implemented as derived classes of this. |
| 28 |
""" |
| 29 |
|
| 30 |
@staticmethod |
| 31 |
@abc.abstractmethod |
| 32 |
def is_usable() -> bool: |
| 33 |
"""is this sandbox available on the current platform?""" |
| 34 |
raise NotImplementedError |
| 35 |
|
| 36 |
@staticmethod |
| 37 |
@abc.abstractmethod |
| 38 |
def _run(args: list[Union[Path, str]]) -> int: |
| 39 |
"""run the given command line within the sandbox""" |
| 40 |
raise NotImplementedError |
| 41 |
|
| 42 |
@classmethod |
| 43 |
def run(cls, args: list[Union[Path, str]]) -> int: |
| 44 |
"""wrapper around `_run` to perform common sanity checks""" |
| 45 |
assert cls.is_usable(), "attempted to use unusable sandbox" |
| 46 |
return cls._run(args) |
| 47 |
|
| 48 |
|
| 49 |
class Bubblewrap(Sandbox): |
| 50 |
""" |
| 51 |
Bubblewrap¹-based sandbox |
| 52 |
|
| 53 |
¹ https://github.com/containers/bubblewrap |
| 54 |
""" |
| 55 |
|
| 56 |
@staticmethod |
| 57 |
def is_usable() -> bool: |
| 58 |
return shutil.which("bwrap") is not None |
| 59 |
|
| 60 |
@staticmethod |
| 61 |
def _run(args: list[Union[Path, str]]) -> sp.CompletedProcess: |
| 62 |
prefix = ["bwrap", "--ro-bind", "/", "/", "--unshare-all", "--"] |
| 63 |
return sp.call(prefix + args) |
| 64 |
|
| 65 |
|
| 66 |
class MacSandbox(Sandbox): |
| 67 |
""" |
| 68 |
macOS’ sandbox API |
| 69 |
""" |
| 70 |
|
| 71 |
@staticmethod |
| 72 |
def is_usable() -> bool: |
| 73 |
return platform.system() == "Darwin" |
| 74 |
|
| 75 |
@staticmethod |
| 76 |
def _run(args: list[Union[Path, str]]) -> sp.CompletedProcess: |
| 77 |
|
| 78 |
# create a sandbox policy that allows only read-only file access |
| 79 |
policy = ( |
| 80 |
'(version 1)(allow default)(deny file-write* (subpath "/"))(deny network*)' |
| 81 |
) |
| 82 |
|
| 83 |
# run Graphviz under this policy |
| 84 |
prefix = ["sandbox-exec", "-p", policy] |
| 85 |
return sp.call(prefix + args) |
| 86 |
|
| 87 |
|
| 88 |
def main(args: list[str]) -> int: |
| 89 |
"""entry point""" |
| 90 |
|
| 91 |
# available sandboxes in order of preference |
| 92 |
SANDBOXES: tuple[Type[Sandbox]] = (Bubblewrap, MacSandbox) |
| 93 |
|
| 94 |
# locate Graphviz, preferring the version collocated with us |
| 95 |
exe = ".exe" if platform.system() == "Windows" else "" |
| 96 |
dot = Path(__file__).parent / f"dot{exe}" |
| 97 |
if not dot.exists(): |
| 98 |
dot = shutil.which("dot") |
| 99 |
if dot is None: |
| 100 |
sys.stderr.write("Graphviz (`dot`) not found\n") |
| 101 |
return -1 |
| 102 |
|
| 103 |
# find a usable sandbox |
| 104 |
sandbox: Optional[Type[Sandbox]] = None |
| 105 |
for box in SANDBOXES: |
| 106 |
if not box.is_usable(): |
| 107 |
continue |
| 108 |
sandbox = box |
| 109 |
break |
| 110 |
if sandbox is None: |
| 111 |
sys.stderr.write("no usable sandbox found\n") |
| 112 |
return -1 |
| 113 |
|
| 114 |
dot_args = args[1:] |
| 115 |
|
| 116 |
# run Graphviz |
| 117 |
return sandbox.run([dot] + dot_args) |
| 118 |
|
| 119 |
|
| 120 |
if __name__ == "__main__": |
| 121 |
sys.exit(main(sys.argv)) |