File size: 3,340 Bytes
1049942
 
 
 
 
 
 
 
 
 
 
 
80833e8
1049942
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Run corpus and worker checks without ever executing puzzle code on the host."""

import json
import subprocess
import time
import urllib.request
import uuid
from pathlib import Path

import yaml

root = Path(__file__).resolve().parents[1]
task = next((root / "tasks").glob("agentescape-*/environment/docker-compose.yaml"))
compose = yaml.safe_load(task.read_text())
image = compose["services"]["worker"]["image"]
name = "aeb-port-validation-" + uuid.uuid4().hex[:10]
subprocess.run(
    ["docker", "compose", "-p", name, "-f", str(task), "build", "worker"], check=True
)
subprocess.run(
    [
        "docker",
        "run",
        "--rm",
        "--detach",
        "--name",
        name,
        "--read-only",
        "--tmpfs",
        "/tmp:size=64m,mode=1777",
        "--pids-limit",
        "64",
        "--memory",
        "384m",
        "--security-opt",
        "no-new-privileges:true",
        "--cap-drop",
        "ALL",
        "--cap-add",
        "SETUID",
        "--cap-add",
        "SETGID",
        "--cap-add",
        "KILL",
        "--cap-add",
        "CHOWN",
        "--cap-add",
        "DAC_OVERRIDE",
        "-p",
        "127.0.0.1::8001",
        image,
    ],
    check=True,
)
try:
    url = (
        "http://"
        + subprocess.check_output(
            ["docker", "port", name, "8001/tcp"], text=True
        ).strip()
    )
    for attempt in range(40):
        try:
            urllib.request.urlopen(url + "/health", timeout=2).close()
            break
        except OSError:
            if attempt == 39:
                raise
            time.sleep(0.25)
    results = []
    checks = [
        ("normal Python", "print(7 * 6)", lambda s: s == "42\n"),
        (
            "no scenario files",
            "print(open('/private/scenario.json').read())",
            lambda s: "FileNotFoundError" in s,
        ),
        (
            "no server module",
            "import server; print(server.SCENARIOS)",
            lambda s: "ModuleNotFoundError" in s,
        ),
        (
            "no network sockets",
            "import socket; socket.socket()",
            lambda s: "PermissionError" in s,
        ),
        (
            "no parent environment",
            "print(open('/proc/1/environ').read())",
            lambda s: "PermissionError" in s,
        ),
        (
            "CPU limit",
            "while True: pass",
            lambda s: "resource limit" in s or "timed out" in s,
        ),
    ]
    for label, code, check in checks:
        req = urllib.request.Request(
            url + "/execute",
            data=json.dumps({"code": code}).encode(),
            headers={"Content-Type": "application/json"},
        )
        with urllib.request.urlopen(req, timeout=15) as response:
            result = json.load(response)
        assert check(result["stdout"]), (label, result)
        results.append({"check": label, "passed": True})
    (root / "reports/worker-isolation.json").write_text(json.dumps(results, indent=2))
    subprocess.run(
        [
            str(root / ".venv/bin/python"),
            "-m",
            "agentescape_port.validate",
            "--worker-url",
            url,
        ],
        cwd=root,
        check=True,
    )
finally:
    subprocess.run(["docker", "stop", name], check=True, capture_output=True)