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_composition_helper.py · 5.7 KB · 160 lines
| 1 | """``_runtime.load_object``: composition, namespace rule, recursion limit.""" |
| 2 | import pytest |
| 3 | |
| 4 | import object_runtime_helpers |
| 5 | from runtime_modes import MODES, configure, run_object, write_object |
| 6 | |
| 7 | CALLEE = """ |
| 8 | def GET(request): |
| 9 | _state_manager.increment("calls") |
| 10 | return {"hello": request.get("who", "world"), "calls": _state_manager.get("calls")} |
| 11 | """ |
| 12 | |
| 13 | |
| 14 | @pytest.mark.parametrize("mode", MODES) |
| 15 | def test_object_calls_object_and_reads_its_state_and_files(mode, monkeypatch, tmp_path): |
| 16 | _, root = configure(monkeypatch, tmp_path) |
| 17 | write_object(root, "tools_callee", CALLEE.replace( |
| 18 | 'return {', '_files.put("f.txt", b"callee-file")\n return {')) |
| 19 | write_object(root, "tools_caller", """ |
| 20 | def GET(request): |
| 21 | other = _runtime.load_object("tools_callee") |
| 22 | out = other.execute("GET", {"who": "caller"}) |
| 23 | return { |
| 24 | "out": out, |
| 25 | "id": other.object_id, |
| 26 | "state": other.state_manager.get("calls"), |
| 27 | "file": other.file_manager.get("f.txt").decode(), |
| 28 | } |
| 29 | """) |
| 30 | result = run_object(mode, root, "tools_caller") |
| 31 | assert result.ok, result.error |
| 32 | assert result.result == { |
| 33 | "out": {"hello": "caller", "calls": 1}, |
| 34 | "id": "tools_callee", |
| 35 | "state": 1, |
| 36 | "file": "callee-file", |
| 37 | } |
| 38 | |
| 39 | |
| 40 | @pytest.mark.parametrize("mode", MODES) |
| 41 | def test_user_object_may_load_own_namespace_and_opted_in_system_objects(mode, monkeypatch, tmp_path): |
| 42 | _, root = configure(monkeypatch, tmp_path) |
| 43 | write_object(root, "tools_callee", "USER_CALLABLE = True\n" + CALLEE) |
| 44 | write_object(root, "u_7_helper", CALLEE) |
| 45 | write_object(root, "u_7_main", """ |
| 46 | def GET(request): |
| 47 | return { |
| 48 | "own": _runtime.load_object("u_7_helper").execute("GET", {})["hello"], |
| 49 | "system": _runtime.load_object("tools_callee").execute("GET", {})["hello"], |
| 50 | } |
| 51 | """) |
| 52 | result = run_object(mode, root, "u_7_main") |
| 53 | assert result.ok, result.error |
| 54 | assert result.result == {"own": "world", "system": "world"} |
| 55 | |
| 56 | |
| 57 | ATTACK = """ |
| 58 | def GET(request): |
| 59 | out = {} |
| 60 | for oid in ("u_8_secret", "u_08_secret", "u_8_nothere"): |
| 61 | try: |
| 62 | obj = _runtime.load_object(oid) |
| 63 | out[oid] = "LOADED" |
| 64 | except PermissionError: |
| 65 | out[oid] = "denied" |
| 66 | except LookupError: |
| 67 | out[oid] = "notfound" |
| 68 | for bad in ("../u_8/secret", "/etc/passwd", "u_8_secret.py", 5, None): |
| 69 | try: |
| 70 | _runtime.load_object(bad) |
| 71 | out[repr(bad)] = "LOADED" |
| 72 | except LookupError: |
| 73 | out[repr(bad)] = "refused" |
| 74 | return out |
| 75 | """ |
| 76 | |
| 77 | |
| 78 | @pytest.mark.parametrize("mode", MODES) |
| 79 | def test_user_object_cannot_load_another_users_object(mode, monkeypatch, tmp_path): |
| 80 | _, root = configure(monkeypatch, tmp_path) |
| 81 | write_object(root, "u_8_secret", "def GET(request):\n return {'secret': 1}\n") |
| 82 | write_object(root, "u_7_evil", ATTACK) |
| 83 | result = run_object(mode, root, "u_7_evil") |
| 84 | assert result.ok, result.error |
| 85 | out = result.result |
| 86 | assert out["u_8_secret"] == "denied" |
| 87 | assert out["u_08_secret"] == "denied" |
| 88 | assert out["u_8_nothere"] == "denied" # fail closed: no existence oracle |
| 89 | assert all(v == "refused" for k, v in out.items() if k not in ("u_8_secret", "u_08_secret", "u_8_nothere")) |
| 90 | |
| 91 | |
| 92 | def test_system_object_may_load_user_object(monkeypatch, tmp_path): |
| 93 | _, root = configure(monkeypatch, tmp_path) |
| 94 | write_object(root, "u_8_data", "def GET(request):\n return {'v': 8}\n") |
| 95 | write_object(root, "tools_admin", """ |
| 96 | def GET(request): |
| 97 | return _runtime.load_object("u_8_data").execute("GET", {}) |
| 98 | """) |
| 99 | result = run_object("in_process", root, "tools_admin") |
| 100 | assert result.ok, result.error |
| 101 | assert result.result == {"v": 8} |
| 102 | |
| 103 | |
| 104 | @pytest.mark.parametrize("mode", MODES) |
| 105 | def test_recursion_is_stopped_with_a_clear_error(mode, monkeypatch, tmp_path): |
| 106 | _, root = configure(monkeypatch, tmp_path) |
| 107 | write_object(root, "tools_selfie", """ |
| 108 | def GET(request): |
| 109 | return _runtime.load_object("tools_selfie").execute("GET", {}) |
| 110 | """) |
| 111 | write_object(root, "tools_ping", """ |
| 112 | def GET(request): |
| 113 | return _runtime.load_object("tools_pong").execute("GET", {}) |
| 114 | """) |
| 115 | write_object(root, "tools_pong", """ |
| 116 | def GET(request): |
| 117 | return _runtime.load_object("tools_ping").execute("GET", {}) |
| 118 | """) |
| 119 | for object_id in ("tools_selfie", "tools_ping"): |
| 120 | result = run_object(mode, root, object_id) |
| 121 | assert not result.ok |
| 122 | assert "depth limit" in result.error.message |
| 123 | assert str(object_runtime_helpers.MAX_COMPOSITION_DEPTH) in result.error.message |
| 124 | |
| 125 | |
| 126 | def test_depth_resets_after_a_failed_chain(monkeypatch, tmp_path): |
| 127 | _, root = configure(monkeypatch, tmp_path) |
| 128 | write_object(root, "tools_selfie", """ |
| 129 | def GET(request): |
| 130 | return _runtime.load_object("tools_selfie").execute("GET", {}) |
| 131 | """) |
| 132 | write_object(root, "tools_ok", "def GET(request):\n return 1\n") |
| 133 | assert not run_object("in_process", root, "tools_selfie").ok |
| 134 | assert run_object("in_process", root, "tools_ok").result == 1 |
| 135 | |
| 136 | |
| 137 | def test_a_bounded_chain_within_the_limit_works(monkeypatch, tmp_path): |
| 138 | _, root = configure(monkeypatch, tmp_path) |
| 139 | write_object(root, "tools_chain", """ |
| 140 | def GET(request): |
| 141 | n = request.get("n", 0) |
| 142 | if n >= 5: |
| 143 | return n |
| 144 | return _runtime.load_object("tools_chain").execute("GET", {"n": n + 1}) |
| 145 | """) |
| 146 | assert run_object("in_process", root, "tools_chain").result == 5 |
| 147 | |
| 148 | |
| 149 | def test_locked_check_may_not_load_objects(monkeypatch, tmp_path): |
| 150 | import object_records |
| 151 | |
| 152 | _, root = configure(monkeypatch, tmp_path) |
| 153 | write_object(root, "tools_callee", CALLEE) |
| 154 | composer = object_runtime_helpers.ObjectComposer("tools_x", tmp_path / "data") |
| 155 | object_records._LOCK_STATE.in_check = "some_check" |
| 156 | try: |
| 157 | with pytest.raises(object_records.LockedCheckWriteError): |
| 158 | composer.load_object("tools_callee") |
| 159 | finally: |
| 160 | object_records._LOCK_STATE.in_check = None |