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.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_api_key_owner_stamp.py · 3.2 KB · 72 lines
| 1 | """A record created with a per-user API key is owned by that key's user. |
| 2 | |
| 3 | Why it matters: owner-scoped rules (`row_filter: {"owner_id": "$user_id"}`) |
| 4 | are how most apps keep people's records their own. Sessions were stamped |
| 5 | server-side; API keys -- the path agents, scripts and desktop apps use -- |
| 6 | were not, so their records kept whatever owner the client claimed, or none, |
| 7 | and fell outside their own owner's rules (Webmaster publishing a site with |
| 8 | an API key is how this was found). |
| 9 | """ |
| 10 | |
| 11 | import json |
| 12 | |
| 13 | from test_object_server import request, save_permission_policy, write_records |
| 14 | |
| 15 | import object_api_keys |
| 16 | import object_server |
| 17 | |
| 18 | POLICY = { |
| 19 | "access_mode": "role_based", |
| 20 | "rules": [ |
| 21 | {"effect": "allow", "principal": "registered", "actions": ["create", "read", "update", "delete"], |
| 22 | "collection": "things", "row_filter": {"owner_id": "$user_id"}, "reason": "own things"}, |
| 23 | ], |
| 24 | } |
| 25 | |
| 26 | |
| 27 | def env(tmp_path, monkeypatch): |
| 28 | data = tmp_path / "data" |
| 29 | write_records(data, "things", "id\tname\towner_id\n") |
| 30 | schema = data / "schemas" / "things.json" |
| 31 | schema.parent.mkdir(parents=True, exist_ok=True) |
| 32 | schema.write_text(json.dumps({"fields": [{"name": "id"}, {"name": "name"}, {"name": "owner_id"}]})) |
| 33 | save_permission_policy(data, POLICY) |
| 34 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(data)) |
| 35 | monkeypatch.setenv(object_server.PERMISSION_ENFORCEMENT_ENV, "true") |
| 36 | monkeypatch.setenv(object_server.PERMISSION_UNREADY_ENFORCEMENT_ENV, "true") |
| 37 | return data |
| 38 | |
| 39 | |
| 40 | def create(token, body): |
| 41 | return request("/collections/things/records", method="POST", body=json.dumps(body).encode(), |
| 42 | headers=[("authorization", f"Bearer {token}"), ("content-type", "application/json")]) |
| 43 | |
| 44 | |
| 45 | def test_an_api_key_owns_what_it_creates(tmp_path, monkeypatch): |
| 46 | data = env(tmp_path, monkeypatch) |
| 47 | _, token = object_api_keys.create_api_key("dan", "script", base_dir=data) |
| 48 | status, _, payload = create(token, {"name": "mine", "owner_id": "dan"}) |
| 49 | assert status == 201, payload |
| 50 | assert payload["record"]["owner_id"] == "dan" |
| 51 | |
| 52 | |
| 53 | def test_an_api_key_cannot_create_for_someone_else(tmp_path, monkeypatch): |
| 54 | """The owner rule is checked against what the client sent, as for a |
| 55 | session: claiming another owner is refused, never quietly accepted.""" |
| 56 | data = env(tmp_path, monkeypatch) |
| 57 | _, token = object_api_keys.create_api_key("dan", "script", base_dir=data) |
| 58 | status, _, _ = create(token, {"name": "sneaky", "owner_id": "eve"}) |
| 59 | assert status == 403 |
| 60 | |
| 61 | |
| 62 | def test_the_stamp_is_the_keys_user_not_the_clients_claim(monkeypatch, tmp_path): |
| 63 | """Where a rule lets a client write any owner_id (no owner row filter), |
| 64 | the stored owner is still the key's user, as it is for a session.""" |
| 65 | data = env(tmp_path, monkeypatch) |
| 66 | save_permission_policy(data, {"access_mode": "role_based", "rules": [ |
| 67 | {"effect": "allow", "principal": "registered", "actions": ["create", "read"], |
| 68 | "collection": "things", "reason": "anyone signed in"}]}) |
| 69 | _, token = object_api_keys.create_api_key("dan", "script", base_dir=data) |
| 70 | status, _, payload = create(token, {"name": "x", "owner_id": "eve"}) |
| 71 | assert status == 201, payload |
| 72 | assert payload["record"]["owner_id"] == "dan" |