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_scan_entity_pointer.py · 6.0 KB · 125 lines
| 1 | """A scan sent in through intake points only at books its sender may update. |
| 2 | |
| 3 | scans.entity_id declares relation.requires update (app-intake 0.5.2), and |
| 4 | the record API enforces it. action_scan_ingest writes the scan itself, |
| 5 | below that API, and copied entity_id from the request body unchecked. |
| 6 | |
| 7 | Measured before the fix, with enforcement on and off: any signed-in user |
| 8 | could POST /objects/action_scan_ingest {"text": ..., "entity_id": <another |
| 9 | user's entity>} and get a 200; the scan stored that pointer, and |
| 10 | action_confirm_scan carried it onto the draft expense, which an approver's |
| 11 | approval posts as a journal in that entity's books. A nonexistent id was a |
| 12 | 500 carrying a traceback, after the document's bytes had been stored, so |
| 13 | a 200 against a 500 told the caller which entity ids exist. |
| 14 | |
| 15 | Now the action asks the record API's own question (object_access. |
| 16 | Access.relation_refusal, the one implementation object_server uses too) |
| 17 | before it stores anything: an entity the caller may not see and one that |
| 18 | does not exist get the same 400, and nothing is written. |
| 19 | """ |
| 20 | |
| 21 | from __future__ import annotations |
| 22 | |
| 23 | import pytest |
| 24 | from test_findings_access_control import _json, _text, make_site # noqa: F401 |
| 25 | |
| 26 | import object_execution |
| 27 | import object_records |
| 28 | from test_document_intake import INTAKE_OBJECTS, RUNTIME |
| 29 | |
| 30 | |
| 31 | def _box(make_site, monkeypatch, mode): |
| 32 | site = make_site("app-intake", "app-finance", "app-entities") |
| 33 | users = {name: site.user(name) for name in ("alice", "bob")} |
| 34 | site.create("entities", {"id": "e-alice", "name": "Alice Books", "owner_id": "alice"}) |
| 35 | site.create("entities", {"id": "e-bob", "name": "Bob Books", "owner_id": "bob"}) |
| 36 | if mode == "off": |
| 37 | monkeypatch.delenv("DBBASIC_ENABLE_PERMISSION_ENFORCEMENT") |
| 38 | return site, users |
| 39 | |
| 40 | |
| 41 | def _stored(site, user): |
| 42 | folder = site.data / "user_files" / user |
| 43 | return sorted(p.name for p in folder.iterdir()) if folder.exists() else [] |
| 44 | |
| 45 | |
| 46 | def _ingest(site, headers, entity_id, text="CAFE\nTOTAL 8.99\n"): |
| 47 | return site.execute("action_scan_ingest", {"text": text, "entity_id": entity_id}, headers) |
| 48 | |
| 49 | |
| 50 | @pytest.mark.parametrize("mode", ["enforced", "off"]) |
| 51 | def test_ingest_cannot_point_a_scan_at_books_the_caller_may_not_update( |
| 52 | make_site, monkeypatch, mode): |
| 53 | site, users = _box(make_site, monkeypatch, mode) |
| 54 | hidden = _ingest(site, users["bob"], "e-alice") |
| 55 | missing = _ingest(site, users["bob"], "e-nobody", text="OTHER\nTOTAL 1.00\n") |
| 56 | |
| 57 | assert hidden.status == 400, _text(hidden) |
| 58 | assert missing.status == 400, _text(missing) |
| 59 | # One answer for "not yours" and "not there", the same words the record |
| 60 | # API gives: no oracle for which entity ids exist. |
| 61 | assert _json(hidden)["error"] == ("Record field 'entity_id' references a missing " |
| 62 | "record: entities/e-alice") |
| 63 | assert _json(missing)["error"] == ("Record field 'entity_id' references a missing " |
| 64 | "record: entities/e-nobody") |
| 65 | assert set(_json(hidden)) == set(_json(missing)) |
| 66 | assert "Traceback" not in _text(missing) |
| 67 | assert site.rows("scans") == [] |
| 68 | assert _stored(site, "bob") == [] # refused before any bytes were stored |
| 69 | |
| 70 | |
| 71 | def test_the_record_api_gives_the_same_answer(make_site, monkeypatch): |
| 72 | site, users = _box(make_site, monkeypatch, "enforced") |
| 73 | api = site.call("/collections/scans/records", "POST", |
| 74 | {"filename": "x", "entity_id": "e-alice"}, users["bob"]) |
| 75 | action = _ingest(site, users["bob"], "e-alice") |
| 76 | assert (api.status, _json(api)["error"]) == (action.status, _json(action)["error"]) |
| 77 | |
| 78 | |
| 79 | def test_nothing_reaches_an_expense_in_somebody_elses_books(make_site, monkeypatch): |
| 80 | """The chain the pointer bought: ingest, read, confirm into a draft |
| 81 | expense whose entity_id is another user's books.""" |
| 82 | site, users = _box(make_site, monkeypatch, "enforced") |
| 83 | assert _ingest(site, users["bob"], "e-alice").status == 400 |
| 84 | assert site.execute("system_scan_processor", {"engine": "text"}, site.admin).status == 200 |
| 85 | for row in site.rows("scans"): |
| 86 | site.execute("action_confirm_scan", {"scan_id": row["id"], "amount_cents": "899"}, |
| 87 | users["bob"]) |
| 88 | assert not [row for row in site.rows("expenses") if row.get("entity_id") == "e-alice"] |
| 89 | |
| 90 | |
| 91 | @pytest.mark.parametrize("mode", ["enforced", "off"]) |
| 92 | def test_a_scan_may_point_at_the_callers_own_books(make_site, monkeypatch, mode): |
| 93 | site, users = _box(make_site, monkeypatch, mode) |
| 94 | response = _ingest(site, users["bob"], "e-bob") |
| 95 | assert response.status == 200, _text(response) |
| 96 | scan_id = _json(response)["scan_id"] |
| 97 | row = site.row("scans", scan_id) |
| 98 | assert (row["owner_id"], row["entity_id"], row["file_id"]) == ("bob", "e-bob", f"scan-{scan_id}") |
| 99 | assert _stored(site, "bob") == [f"scan-{scan_id}"] |
| 100 | |
| 101 | unpointed = _ingest(site, users["bob"], "", text="NO ENTITY\nTOTAL 2.00\n") |
| 102 | assert unpointed.status == 200, _text(unpointed) |
| 103 | assert site.row("scans", _json(unpointed)["scan_id"])["entity_id"] == "" |
| 104 | |
| 105 | |
| 106 | def test_an_admin_may_point_a_scan_at_any_books(make_site, monkeypatch): |
| 107 | site, _users = _box(make_site, monkeypatch, "enforced") |
| 108 | response = site.execute("action_scan_ingest", |
| 109 | {"text": "TOTAL 1.00", "owner_id": "alice", "entity_id": "e-alice"}, |
| 110 | site.admin) |
| 111 | assert response.status == 200, _text(response) |
| 112 | assert site.row("scans", _json(response)["scan_id"])["entity_id"] == "e-alice" |
| 113 | |
| 114 | |
| 115 | def test_an_in_process_call_with_no_identity_names_no_books(make_site, monkeypatch): |
| 116 | """No `_identity` is an in-process caller, which may name its owner but |
| 117 | is anonymous to the permission engine: it may not point at books.""" |
| 118 | site, _users = _box(make_site, monkeypatch, "enforced") |
| 119 | result = object_execution.execute_object( |
| 120 | RUNTIME, object_execution.ObjectExecutionRequest( |
| 121 | "action_scan_ingest", method="POST", |
| 122 | payload={"text": "TOTAL 3.00", "owner_id": "alice", "entity_id": "e-alice"}), |
| 123 | roots=[INTAKE_OBJECTS]).result |
| 124 | assert result["status"] == 400, result |
| 125 | assert object_records.read_collection_records("scans", base_dir=site.data) == [] |