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_user_access_to_system_objects.py · 5.3 KB · 139 lines
| 1 | """What user code may call, and what a customer's GET /objects shows. |
| 2 | |
| 3 | Two design questions from the sandbox simulation, answered with reversible |
| 4 | (closed) defaults: user code may only call system/package objects that opt in |
| 5 | with ``USER_CALLABLE = True``, and a non-admin's object list shows their own |
| 6 | objects plus system objects that say ``LISTED = True``. Admins see everything. |
| 7 | """ |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | import pytest |
| 11 | |
| 12 | import object_sandbox |
| 13 | import object_user_access as access |
| 14 | from runtime_modes import MODES, configure, run_object, write_object |
| 15 | from test_object_server import ( |
| 16 | create_identity_session, |
| 17 | enable_admin_token, |
| 18 | auth_headers, |
| 19 | request, |
| 20 | session_headers, |
| 21 | ) |
| 22 | |
| 23 | PING = "def GET(request):\n return {'pong': True}\n" |
| 24 | CALLER = """ |
| 25 | def GET(request): |
| 26 | return _runtime.load_object("tools_target").execute("GET", {}) |
| 27 | """ |
| 28 | |
| 29 | |
| 30 | @pytest.fixture(autouse=True) |
| 31 | def _cleanup(): |
| 32 | yield |
| 33 | object_sandbox.shutdown() |
| 34 | |
| 35 | |
| 36 | # -- the flag reader ------------------------------------------------------------ |
| 37 | |
| 38 | |
| 39 | def test_flags_are_read_statically_and_only_literal_true_counts(tmp_path): |
| 40 | src = tmp_path / "x.py" |
| 41 | src.write_text("USER_CALLABLE = True\nLISTED = 'yes'\nraise SystemExit('never executed')\n") |
| 42 | assert access.declared_flags(src) == {"USER_CALLABLE"} |
| 43 | src.write_text("LISTED: bool = True\nUSER_CALLABLE = True\nUSER_CALLABLE = False\n") |
| 44 | assert access.declared_flags(src) == {"LISTED"} # mtime/size changed: re-read |
| 45 | src.write_text("def broken(:\n") |
| 46 | assert access.declared_flags(src) == frozenset() |
| 47 | |
| 48 | |
| 49 | # -- _runtime.load_object --------------------------------------------------------- |
| 50 | |
| 51 | |
| 52 | @pytest.mark.parametrize("mode", MODES) |
| 53 | def test_user_code_cannot_call_a_system_object_that_has_not_opted_in(mode, monkeypatch, tmp_path): |
| 54 | _, root = configure(monkeypatch, tmp_path) |
| 55 | write_object(root, "tools_target", PING) |
| 56 | write_object(root, "u_7_main", CALLER) |
| 57 | result = run_object(mode, root, "u_7_main") |
| 58 | assert not result.ok |
| 59 | assert "USER_CALLABLE" in result.error.message and "tools_target" in result.error.message |
| 60 | |
| 61 | |
| 62 | @pytest.mark.parametrize("mode", MODES) |
| 63 | def test_user_code_can_call_a_system_object_that_opted_in(mode, monkeypatch, tmp_path): |
| 64 | _, root = configure(monkeypatch, tmp_path) |
| 65 | write_object(root, "tools_target", "USER_CALLABLE = True\n" + PING) |
| 66 | write_object(root, "u_7_main", CALLER) |
| 67 | result = run_object(mode, root, "u_7_main") |
| 68 | assert result.ok, result.error |
| 69 | assert result.result == {"pong": True} |
| 70 | |
| 71 | |
| 72 | def test_a_system_object_may_still_call_a_system_object_freely(monkeypatch, tmp_path): |
| 73 | _, root = configure(monkeypatch, tmp_path) |
| 74 | write_object(root, "tools_target", PING) |
| 75 | write_object(root, "tools_main", CALLER) |
| 76 | assert run_object("in_process", root, "tools_main").result == {"pong": True} |
| 77 | |
| 78 | |
| 79 | def test_the_refusal_reaches_the_daemon_path_too(monkeypatch, tmp_path): |
| 80 | """A user object run by the daemon (a scheduled method) goes through the |
| 81 | same sandbox proxy, so the same rule applies.""" |
| 82 | import json |
| 83 | |
| 84 | import object_daemon |
| 85 | import object_state |
| 86 | from python_object_runtime import PythonObjectRuntime |
| 87 | |
| 88 | monkeypatch.setenv(object_sandbox.ISOLATION_ENV, object_sandbox.UNSAFE_DEV) |
| 89 | data_dir, root = configure(monkeypatch, tmp_path) |
| 90 | (root / "triggers").mkdir(parents=True, exist_ok=True) |
| 91 | (root / "triggers" / "scheduler.py").write_text("def POST(request):\n return {}\n") |
| 92 | write_object(root, "tools_target", PING) |
| 93 | write_object(root, "u_7_job", CALLER.replace("def GET(request):", "def work():") + ( |
| 94 | "\ndef GET(request):\n return _schedule(60, 'work')\n")) |
| 95 | assert run_object("in_process", root, "u_7_job").ok |
| 96 | manager = object_state.ObjectStateManager("scheduler", base_dir=data_dir) |
| 97 | key = "task_obj.u_7_job.work" |
| 98 | row = json.loads(manager.get(key)) |
| 99 | row["next_run"] = 1 |
| 100 | manager.set(key, json.dumps(row)) |
| 101 | object_daemon.process_scheduler(PythonObjectRuntime()) |
| 102 | manager.reload() |
| 103 | assert "USER_CALLABLE" in json.loads(manager.get(key))["last_error"] |
| 104 | |
| 105 | |
| 106 | # -- GET /objects ----------------------------------------------------------------- |
| 107 | |
| 108 | |
| 109 | @pytest.fixture |
| 110 | def listing_box(tmp_path, monkeypatch): |
| 111 | _, root = configure(monkeypatch, tmp_path) |
| 112 | enable_admin_token(monkeypatch) |
| 113 | write_object(root, "tools_internal", PING) |
| 114 | write_object(root, "tools_public", "LISTED = True\n" + PING) |
| 115 | write_object(root, "u_1_mine", PING) |
| 116 | write_object(root, "u_2_theirs", PING) |
| 117 | token, _ = create_identity_session({"user_id": "1"}) |
| 118 | return session_headers(token) |
| 119 | |
| 120 | |
| 121 | def ids(headers): |
| 122 | status, _, payload = request("/objects", query_string="format=json", headers=headers) |
| 123 | assert status == 200 |
| 124 | return {obj["object_id"] for obj in payload["objects"]} |
| 125 | |
| 126 | |
| 127 | def test_a_customer_sees_their_own_and_listed_system_objects_only(listing_box): |
| 128 | assert ids(listing_box) == {"u_1_mine", "tools_public"} |
| 129 | |
| 130 | |
| 131 | def test_the_admin_sees_everything(listing_box): |
| 132 | assert ids(auth_headers()) >= {"tools_internal", "tools_public", "u_1_mine", "u_2_theirs"} |
| 133 | |
| 134 | |
| 135 | def test_listing_is_discovery_only_an_unlisted_object_is_still_governed_by_permissions(listing_box): |
| 136 | """Unlisted does not mean forbidden or hidden-by-404: this flag changes |
| 137 | what is listed, nothing about who may run it.""" |
| 138 | status, _, _ = request("/objects/tools_internal", headers=listing_box) |
| 139 | assert status == request("/objects/tools_public", headers=listing_box)[0] |