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_scan_entity_pointer.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, 18 minutes ago History
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
3scans.entity_id declares relation.requires update (app-intake 0.5.2), and
4the record API enforces it. action_scan_ingest writes the scan itself,
5below that API, and copied entity_id from the request body unchecked.
6
7Measured before the fix, with enforcement on and off: any signed-in user
8could POST /objects/action_scan_ingest {"text": ..., "entity_id": <another
9user's entity>} and get a 200; the scan stored that pointer, and
10action_confirm_scan carried it onto the draft expense, which an approver's
11approval posts as a journal in that entity's books. A nonexistent id was a
12500 carrying a traceback, after the document's bytes had been stored, so
13a 200 against a 500 told the caller which entity ids exist.
14
15Now the action asks the record API's own question (object_access.
16Access.relation_refusal, the one implementation object_server uses too)
17before it stores anything: an entity the caller may not see and one that
18does not exist get the same 400, and nothing is written.
19"""
20
21from __future__ import annotations
22
23import pytest
24from test_findings_access_control import _json, _text, make_site # noqa: F401
25
26import object_execution
27import object_records
28from test_document_intake import INTAKE_OBJECTS, RUNTIME
29
30
31def _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
41def _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
46def _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"])
51def 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
71def 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
79def 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"])
92def 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
106def 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
115def 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) == []