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_relation_requires_guard.py · 5.1 KB · 116 lines
| 1 | """Forgetting `relation.requires` on a pointer a registered user can write is a CI failure. |
| 2 | |
| 3 | Every shipped money and stock collection had the same hole until 2026-11-30: |
| 4 | the create rule asked that the new row be the writer's own, and nothing asked |
| 5 | about the row it pointed at, so a refund could be filed on anybody's payment, a |
| 6 | line on anybody's order, a stock move on anybody's product |
| 7 | (tests/test_relation_requires.py, tests/test_relation_requires_collections.py). |
| 8 | The fix is a schema key, and a schema key is exactly the kind of thing the next |
| 9 | collection forgets. So: for every shipped collection some non-admin principal |
| 10 | may create or update under the shipped rules, every relation field either |
| 11 | declares `requires` or is listed in EXEMPT with the reason it needs none. |
| 12 | |
| 13 | And because a rollup turns a child into a change to its parent (a refund is |
| 14 | the payment's refunded_cents, a journal line is the journal's balance), a |
| 15 | writable child that a parent rolls up must require `update` on that parent, |
| 16 | not merely `read`. |
| 17 | """ |
| 18 | |
| 19 | import json |
| 20 | from pathlib import Path |
| 21 | |
| 22 | ROOT = Path(__file__).resolve().parents[1] |
| 23 | PACKAGES = ROOT / "packages" |
| 24 | |
| 25 | EXEMPT = { |
| 26 | ("refunds", "invoice_id"): ( |
| 27 | "not the writer's to choose: hook_refunds stamps it from the payment and " |
| 28 | "LOCKED_CHECK refuses any other value for every writer; payment_id carries " |
| 29 | "the requirement"), |
| 30 | ("project_access", "project_id"): ( |
| 31 | "the create rule's own row filter ($owned_projects) is the parent check: " |
| 32 | "only the project's owner may grant access to it"), |
| 33 | ("rate_cards", "project_id"): ( |
| 34 | "written only by role:manager, an org-wide role by the shipped rules (it " |
| 35 | "approves everybody's time and expenses); no rule lets a manager read " |
| 36 | "projects, so a requirement would refuse the one flow the role exists for"), |
| 37 | ("rate_cards", "entity_id"): ( |
| 38 | "written only by role:manager (see project_id above)"), |
| 39 | } |
| 40 | |
| 41 | |
| 42 | def _load(pattern): |
| 43 | for path in sorted(PACKAGES.glob(pattern)): |
| 44 | yield path, json.loads(path.read_text()) |
| 45 | |
| 46 | |
| 47 | SCHEMAS = {schema["name"]: (path, schema) |
| 48 | for path, schema in _load("*/schemas/*.json") |
| 49 | if isinstance(schema, dict) and schema.get("name")} |
| 50 | |
| 51 | |
| 52 | def _writable(): |
| 53 | found = set() |
| 54 | for _path, payload in _load("*/permissions/rules.json"): |
| 55 | for rule in payload.get("rules", []): |
| 56 | if rule.get("effect") != "allow" or not rule.get("collection"): |
| 57 | continue |
| 58 | if rule.get("principal") == "role:admin": |
| 59 | continue |
| 60 | if {"create", "update", "*"} & set(rule.get("actions", [])): |
| 61 | found.add(rule["collection"]) |
| 62 | return found |
| 63 | |
| 64 | |
| 65 | WRITABLE = _writable() |
| 66 | |
| 67 | |
| 68 | def _relations(schema): |
| 69 | for field in schema.get("fields", []): |
| 70 | relation = field.get("relation") if isinstance(field, dict) else None |
| 71 | if isinstance(relation, str): |
| 72 | yield field["name"], relation, None |
| 73 | elif isinstance(relation, dict): |
| 74 | yield field["name"], relation.get("collection"), relation.get("requires") |
| 75 | |
| 76 | |
| 77 | def test_every_writable_pointer_declares_what_its_writer_must_be_allowed(): |
| 78 | missing = [] |
| 79 | for collection in sorted(WRITABLE & set(SCHEMAS)): |
| 80 | path, schema = SCHEMAS[collection] |
| 81 | for name, target, requires in _relations(schema): |
| 82 | if requires is None and (collection, name) not in EXEMPT: |
| 83 | missing.append(f"{path.relative_to(ROOT)}: {name} -> {target}") |
| 84 | assert not missing, ( |
| 85 | "These relations can be written by a non-admin and say nothing about the record " |
| 86 | "they point at. Add \"requires\": \"update\" (the row moves money, stock or state " |
| 87 | "of its target) or \"read\" (a reference), or list it in EXEMPT with the reason " |
| 88 | "(docs/permissions-model.md, 'Pointing at a record you may not act on'):\n" |
| 89 | + "\n".join(missing)) |
| 90 | |
| 91 | |
| 92 | def test_every_exemption_names_a_real_unchecked_pointer(): |
| 93 | for (collection, name), reason in EXEMPT.items(): |
| 94 | assert reason |
| 95 | _path, schema = SCHEMAS[collection] |
| 96 | relations = {field: requires for field, _target, requires in _relations(schema)} |
| 97 | assert name in relations, f"{collection}.{name} is no longer a relation; drop it from EXEMPT" |
| 98 | assert relations[name] is None, ( |
| 99 | f"{collection}.{name} now declares requires; drop it from EXEMPT") |
| 100 | |
| 101 | |
| 102 | def test_a_writable_child_a_parent_rolls_up_requires_update_on_the_parent(): |
| 103 | wrong = [] |
| 104 | for parent, (_path, schema) in SCHEMAS.items(): |
| 105 | for field in schema.get("fields", []): |
| 106 | rollup = field.get("rollup") if isinstance(field, dict) else None |
| 107 | if not isinstance(rollup, dict): |
| 108 | continue |
| 109 | child, fk = rollup.get("collection"), rollup.get("fk_field") |
| 110 | if child not in WRITABLE or child not in SCHEMAS: |
| 111 | continue |
| 112 | for name, target, requires in _relations(SCHEMAS[child][1]): |
| 113 | if name == fk and target == parent and requires != "update" \ |
| 114 | and (child, name) not in EXEMPT: |
| 115 | wrong.append(f"{child}.{name} (rolled up by {parent}.{field['name']}): {requires}") |
| 116 | assert not wrong, wrong |