summaryrefslogtreecommitdiff
path: root/plugins/claude-security/scripts/sandbox_guest.py
blob: 49cd39bb7585b4a34807a6706ede8ca89e416d01 (plain)
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
#!/usr/bin/env python3
"""The supervisor's part inside a virtual machine. `sandbox.py --image` starts it, as root.

It starts the keeper, and carries the keeper's streams, calls and files over stdin and stdout.
It holds no credential.
Python 3.9-compatible, stdlib only.
"""

from __future__ import annotations

import os
import sys
import threading
import time
from contextlib import suppress
from itertools import count
from pathlib import Path
from typing import TYPE_CHECKING, NoReturn, cast

# Under runpy, which starts this program, Python leaves its folder, with lib/, off the import path.
sys.path.insert(0, str(Path(__file__).resolve().parent))
from lib.sandbox.keeper import Door, Keeper
from lib.sandbox.plan import BROKEN, MOST_CALLS, SandboxError, parsed_plan
from lib.sandbox.tunnel import CHUNK, FIRST_CALL, REPORT, STDERR, STDIN, STDOUT, Tunnel

if TYPE_CHECKING:
    import socket
    from io import BufferedReader

_FRAMES = {"exit": b"x", "note": b"n", "removed": b"r"}


def _answer(door: socket.socket, tunnel: Tunnel) -> None:
    try:
        for lane in count(FIRST_CALL):
            # A call that waits for the relay would hold up the one stream.
            while len(tunnel.open_calls()) >= MOST_CALLS:
                time.sleep(0.05)
            tunnel.join(lane, door.accept()[0])
    except EOFError:
        return
    except (OSError, RuntimeError, MemoryError) as error:
        with suppress(OSError):
            tunnel.send(b"e", data=f"the machine cannot take calls: {error}".encode())


def _serve(tunnel: Tunnel) -> int:
    frames = tunnel.frames()
    if not (first := next(frames, None)) or first.kind != b"p":
        msg = "the host sent no plan"
        raise SandboxError(msg)
    keeper = Keeper(parsed_plan(first.data.decode()), sudo=False)

    def listen() -> None:
        try:
            with suppress(SandboxError):
                for _ in frames:
                    pass
        finally:
            keeper.stop()

    relay = -1
    outputs: list[threading.Thread] = []
    threading.Thread(target=listen, daemon=True).start()
    for kind, text, files in keeper.messages():
        if kind == "relay":
            relay = files[0]
        elif kind == "streams":
            tunnel.deliver_to(STDIN, os.fdopen(files[0], "wb"))
            tunnel.send(b"s")
            door = Door(fileno=relay)
            threading.Thread(target=_answer, args=(door, tunnel), daemon=True).start()
            outputs = [
                tunnel.carry(lane, os.fdopen(file, "rb", buffering=0))
                for lane, file in ((STDOUT, files[1]), (STDERR, files[2]))
            ]
        elif kind == "file":
            tunnel.send(b"f", data=os.fsencode(text))
            with os.fdopen(files[0], "rb") as report:
                while data := report.read(CHUNK):
                    tunnel.send(b"d", REPORT, data)
            tunnel.send(b"d", REPORT)
        else:
            tunnel.send(_FRAMES.get(kind, b"e"), data=os.fsencode(text))
    for output in outputs:
        output.join()
    return keeper.wait()


def _pass_zeros(source: BufferedReader) -> None:
    while source.peek(1)[:1] == b"\0":
        source.read(1)


def main() -> NoReturn:
    _pass_zeros(cast("BufferedReader", sys.stdin.buffer))
    tunnel = Tunnel(sys.stdin.buffer, sys.stdout.buffer, guest=True)
    try:
        code = _serve(tunnel)
    except (OSError, SandboxError) as error:
        tunnel.send(b"e", data=str(error).encode())
        code = BROKEN
    # A normal exit fails while a daemon thread is blocked in stdin.
    tunnel.exit(code)


if __name__ == "__main__":
    main()