askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_object_sandbox_status_caller.py · 4.6 KB · 98 lines
| 1 | """`object_sandbox.py --status` run by someone other than the service user. |
| 2 | |
| 3 | The launcher answers only the server's uid, so root inside the container sees |
| 4 | "no launcher" on a host that is serving user objects. The status output then |
| 5 | says to run the check as the service user instead of REFUSED. Only the |
| 6 | operator's verdict line changes: `available` and the exit status do not. |
| 7 | """ |
| 8 | import os |
| 9 | import stat |
| 10 | from types import SimpleNamespace |
| 11 | |
| 12 | import pytest |
| 13 | |
| 14 | import object_sandbox |
| 15 | |
| 16 | pytestmark = pytest.mark.skipif(not hasattr(os, "getuid"), reason="POSIX only") |
| 17 | |
| 18 | SOCKET = SimpleNamespace(st_mode=stat.S_IFSOCK | 0o660, st_uid=0, st_gid=990) |
| 19 | HINT = "run this as the service user (docker compose exec -u dbbasic ..." |
| 20 | |
| 21 | |
| 22 | def refused(**changes): |
| 23 | status = {"available": False, "mode": "strict", "uid_separation": False, |
| 24 | "launcher_socket": "/run/example/launcher.sock", |
| 25 | "reason": "no sandbox launcher answers at /run/example/launcher.sock"} |
| 26 | status.update(changes) |
| 27 | return status |
| 28 | |
| 29 | |
| 30 | def test_root_beside_a_launcher_socket_of_the_service_group_is_the_wrong_caller(): |
| 31 | assert object_sandbox._not_the_service_user(refused(), 0, {0}, SOCKET) is True |
| 32 | |
| 33 | |
| 34 | def test_another_user_outside_the_service_group_is_the_wrong_caller(): |
| 35 | assert object_sandbox._not_the_service_user(refused(), 1001, {1001, 27}, SOCKET) is True |
| 36 | # and one who may not even look into the socket's directory |
| 37 | assert object_sandbox._not_the_service_user(refused(), 1001, {1001}, PermissionError(13, "denied")) is True |
| 38 | |
| 39 | |
| 40 | def test_the_service_user_keeps_the_real_refusal(): |
| 41 | assert object_sandbox._not_the_service_user(refused(), 990, {990}, SOCKET) is False |
| 42 | |
| 43 | |
| 44 | def test_no_launcher_socket_at_all_is_a_real_refusal_for_anyone(): |
| 45 | assert object_sandbox._not_the_service_user(refused(), 0, {0}, None) is False |
| 46 | plain_file = SimpleNamespace(st_mode=stat.S_IFREG | 0o660, st_uid=0, st_gid=990) |
| 47 | assert object_sandbox._not_the_service_user(refused(), 0, {0}, plain_file) is False |
| 48 | |
| 49 | |
| 50 | def test_a_refusal_that_is_not_about_the_launcher_is_never_blamed_on_the_caller(): |
| 51 | # the launcher answered (so the caller IS the server's uid): Landlock too old, server is root, ... |
| 52 | answered = refused(uid_separation=True, reason="user objects are not run while the server runs as root") |
| 53 | assert object_sandbox._not_the_service_user(answered, 0, {0}, SOCKET) is False |
| 54 | # a root-group socket means the configured server user is root: nothing to switch to |
| 55 | root_socket = SimpleNamespace(st_mode=stat.S_IFSOCK | 0o660, st_uid=0, st_gid=0) |
| 56 | assert object_sandbox._not_the_service_user(refused(), 0, {0}, root_socket) is False |
| 57 | assert object_sandbox._not_the_service_user(refused(available=True), 0, {0}, SOCKET) is False |
| 58 | assert object_sandbox._not_the_service_user(refused(mode=object_sandbox.UNSAFE_DEV), 0, {0}, SOCKET) is False |
| 59 | |
| 60 | |
| 61 | def _operator_status(monkeypatch, status, *, uid, socket_info): |
| 62 | monkeypatch.setattr(object_sandbox, "isolation_status", lambda: dict(status)) |
| 63 | monkeypatch.setattr(object_sandbox.os, "getuid", lambda: uid) |
| 64 | monkeypatch.setattr(object_sandbox.os, "getgid", lambda: uid) |
| 65 | monkeypatch.setattr(object_sandbox.os, "getgroups", lambda: [uid]) |
| 66 | |
| 67 | def fake_stat(path, *args, **kwargs): |
| 68 | assert path == status["launcher_socket"] |
| 69 | if isinstance(socket_info, Exception): |
| 70 | raise socket_info |
| 71 | return socket_info |
| 72 | |
| 73 | monkeypatch.setattr(object_sandbox.os, "stat", fake_stat) |
| 74 | return object_sandbox._status_for_operator() |
| 75 | |
| 76 | |
| 77 | def test_status_as_root_says_to_run_as_the_service_user_and_still_fails(monkeypatch): |
| 78 | status = _operator_status(monkeypatch, refused(), uid=0, socket_info=SOCKET) |
| 79 | assert HINT in status["verdict"] and "uid 0" in status["verdict"] |
| 80 | assert "REFUSED" not in status["verdict"] |
| 81 | assert status["available"] is False # --status exits 0 only when available |
| 82 | |
| 83 | |
| 84 | def test_status_as_the_service_user_still_says_refused(monkeypatch): |
| 85 | status = _operator_status(monkeypatch, refused(), uid=990, socket_info=SOCKET) |
| 86 | assert status["verdict"].startswith("user objects REFUSED: no sandbox launcher answers") |
| 87 | assert status["available"] is False |
| 88 | |
| 89 | |
| 90 | def test_status_without_a_launcher_socket_still_says_refused_to_root(monkeypatch): |
| 91 | status = _operator_status(monkeypatch, refused(), uid=0, socket_info=FileNotFoundError(2, "missing")) |
| 92 | assert status["verdict"].startswith("user objects REFUSED") |
| 93 | |
| 94 | |
| 95 | def test_the_servers_own_verdict_line_is_unchanged(): |
| 96 | """The startup banner and the daemon use _verdict directly: a server that cannot reach its launcher |
| 97 | still says REFUSED.""" |
| 98 | assert object_sandbox._verdict(refused()).startswith("user objects REFUSED: ") |