Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/tests/test_api_key_owner_stamp.py
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.

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
3Why it matters: owner-scoped rules (`row_filter: {"owner_id": "$user_id"}`)
4are how most apps keep people's records their own. Sessions were stamped
5server-side; API keys -- the path agents, scripts and desktop apps use --
6were not, so their records kept whatever owner the client claimed, or none,
7and fell outside their own owner's rules (Webmaster publishing a site with
8an API key is how this was found).
9"""
10
11import json
12
13from test_object_server import request, save_permission_policy, write_records
14
15import object_api_keys
16import object_server
17
18POLICY = {
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
27def 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
40def 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
45def 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
53def 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
62def 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"