Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

9 downloads

dbbasic-object-server/tests/test_object_sandbox_status_caller.py
Install on DigitalOcean Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Daniel Quellhorst Changelog for this batch. 65ef78d 3 hours, 19 minutes ago History
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
3The 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
5says to run the check as the service user instead of REFUSED. Only the
6operator's verdict line changes: `available` and the exit status do not.
7"""
8import os
9import stat
10from types import SimpleNamespace
11
12import pytest
13
14import object_sandbox
15
16pytestmark = pytest.mark.skipif(not hasattr(os, "getuid"), reason="POSIX only")
17
18SOCKET = SimpleNamespace(st_mode=stat.S_IFSOCK | 0o660, st_uid=0, st_gid=990)
19HINT = "run this as the service user (docker compose exec -u dbbasic ..."
20
21
22def 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
30def 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
34def 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
40def test_the_service_user_keeps_the_real_refusal():
41 assert object_sandbox._not_the_service_user(refused(), 990, {990}, SOCKET) is False
42
43
44def 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
50def 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
61def _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
77def 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
84def 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
90def 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
95def 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: ")