mirror of
https://github.com/rommapp/romm.git
synced 2026-12-02 10:08:40 +01:00
* fix(auth): bound new client tokens by the caller's scopes and stop delegates removing admins - Creating a client token, approving a device, and regenerating or pairing an existing token now check against the scopes of the credential making the request rather than the whole account's. A narrow client token can no longer mint, or recover the secret of, a broader one. Session and basic auth carry the account's scopes, so they behave as before. - Deleting an admin requires the caller to be an admin, mirroring the existing rule for creating one, since users.write can be delegated. - Editing another user requires the users.write scope on top of the admin role, so an admin's me.write-only token can't change other accounts. - own_only is rejected (422) on grants and overrides for entities with no owner (platforms, roms, firmware, users, tasks, logs), where it silently meant "all". The catalog exposes the ownable entities and the permission matrices only offer "own" on those. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(permissions): keep owner-less own_only grants' access on save Addresses Greptile review on #5346: - An own_only delete grant on an entity with no owner allows nothing today (delete has no coarse scope and the fine check runs with owned=False), so it is now dropped from group grants, or turned into a revoke for overrides, instead of being saved as full delete access. The matrices show it the same way. - The group dialog no longer saves when the permission catalog failed to load, which would have stripped every own_only grant. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(auth): require users.write in the credential to act on another user (pr-ready: code-review) Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * refactor(auth): share client token effective scopes and canOwn (pr-ready: simplify) Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs(permissions): tighten grantEffect docstring and matrix comment (pr-ready: review-polish) Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(auth): make user, token and streaming admin rules consistent - Acting on another user (edit, delete, RA refresh) needs users.write in the credential, and only admins may target an admin or grant the admin role. One assert_admin_for_admin helper now backs add_user, invite links, update, delete and admin token revocation. - Re-issuing a client token's secret (regenerate, pair) needs every scope it stores; deleting one needs every scope it currently grants, so a narrow token can't remove a broader sibling. - Streaming admin overrides need users.read or users.write in the credential, not just the admin role. - Migration 0154 rewrites stored own_only grants and overrides on owner-less entities to the access they already gave, so the frontend normalisation and the catalog save gate are gone. - own_only is no longer rejected on revoke overrides. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
359 lines
12 KiB
Python
359 lines
12 KiB
Python
"""Admin CRUD for groups, memberships, overrides and hidden entities, and the
|
|
end-to-end effect on a member's /permissions/me."""
|
|
|
|
from datetime import timedelta
|
|
from unittest.mock import AsyncMock
|
|
|
|
from tests.factories import make_device_token
|
|
|
|
from config import OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS
|
|
from endpoints import permissions as permissions_endpoints
|
|
from handler.auth.base_handler import oauth_handler
|
|
from handler.database import db_user_handler
|
|
from handler.database.base_handler import sync_session
|
|
from handler.database.permissions_handler import DBPermissionsHandler
|
|
from models.permission import PermissionGroup
|
|
|
|
|
|
def _bearer(token):
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
def _fresh_user_auth(user_id):
|
|
# Mint a token from the user's CURRENT (post-change) projected scopes.
|
|
user = db_user_handler.get_user(user_id)
|
|
assert user is not None
|
|
return _bearer(
|
|
oauth_handler.create_access_token(
|
|
data={
|
|
"sub": user.username,
|
|
"iss": "romm:oauth",
|
|
"scopes": " ".join(user.oauth_scopes),
|
|
},
|
|
expires_delta=timedelta(seconds=OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS),
|
|
)
|
|
)
|
|
|
|
|
|
def _me_actions(client, user_id):
|
|
body = client.get("/api/permissions/me", headers=_fresh_user_auth(user_id)).json()
|
|
return {g["action"] for g in body["grants"]}, body
|
|
|
|
|
|
def _cleanup():
|
|
with sync_session.begin() as s:
|
|
s.query(PermissionGroup).filter(PermissionGroup.system_key.is_(None)).delete(
|
|
synchronize_session="evaluate"
|
|
)
|
|
|
|
|
|
def test_non_admin_forbidden(client, viewer_access_token):
|
|
h = _bearer(viewer_access_token)
|
|
assert client.get("/api/permissions/groups", headers=h).status_code == 403
|
|
assert client.get("/api/permissions/catalog", headers=h).status_code == 403
|
|
assert (
|
|
client.post(
|
|
"/api/permissions/groups", headers=h, json={"name": "x"}
|
|
).status_code
|
|
== 403
|
|
)
|
|
|
|
|
|
def test_catalog_lists_vocabulary(client, access_token):
|
|
body = client.get("/api/permissions/catalog", headers=_bearer(access_token)).json()
|
|
assert "roms" in body["entities"] and "platforms" in body["entities"]
|
|
assert set(body["actions"]) == {"read", "write", "delete"}
|
|
|
|
|
|
def test_catalog_lists_the_ownable_entities(client, access_token):
|
|
body = client.get("/api/permissions/catalog", headers=_bearer(access_token)).json()
|
|
assert "collections" in body["ownable_entities"]
|
|
assert not {"users", "tasks", "logs", "roms"} & set(body["ownable_entities"])
|
|
|
|
|
|
def test_own_only_is_rejected_on_an_entity_without_owners(client, access_token):
|
|
h = _bearer(access_token)
|
|
own_users = {"entity": "users", "action": "write", "own_only": True}
|
|
try:
|
|
created = client.post(
|
|
"/api/permissions/groups",
|
|
headers=h,
|
|
json={"name": "x", "grants": [own_users]},
|
|
)
|
|
assert created.status_code == 422
|
|
|
|
gid = client.post(
|
|
"/api/permissions/groups",
|
|
headers=h,
|
|
json={
|
|
"name": "Own collections",
|
|
"grants": [
|
|
{"entity": "collections", "action": "write", "own_only": True}
|
|
],
|
|
},
|
|
).json()["id"]
|
|
updated = client.put(
|
|
f"/api/permissions/groups/{gid}", headers=h, json={"grants": [own_users]}
|
|
)
|
|
assert updated.status_code == 422
|
|
finally:
|
|
_cleanup()
|
|
|
|
|
|
def test_own_only_override_is_rejected_on_an_entity_without_owners(
|
|
client, access_token, viewer_user
|
|
):
|
|
response = client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=_bearer(access_token),
|
|
json={
|
|
"overrides": [
|
|
{
|
|
"entity": "tasks",
|
|
"action": "write",
|
|
"granted": True,
|
|
"own_only": True,
|
|
}
|
|
]
|
|
},
|
|
)
|
|
assert response.status_code == 422
|
|
|
|
|
|
def test_own_only_revoke_override_is_accepted_on_an_entity_without_owners(
|
|
client, access_token, viewer_user
|
|
):
|
|
response = client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=_bearer(access_token),
|
|
json={
|
|
"overrides": [
|
|
{
|
|
"entity": "tasks",
|
|
"action": "write",
|
|
"granted": False,
|
|
"own_only": True,
|
|
}
|
|
]
|
|
},
|
|
)
|
|
assert response.status_code == 200
|
|
|
|
|
|
def test_groups_expose_the_system_key(client, access_token):
|
|
groups = client.get("/api/permissions/groups", headers=_bearer(access_token)).json()
|
|
keys = {g["name"]: g["system_key"] for g in groups}
|
|
assert keys["Viewer"] == "viewer"
|
|
assert keys["Editor"] == "editor"
|
|
assert "is_system" not in groups[0]
|
|
|
|
|
|
def test_group_create_assign_and_effective_permissions(
|
|
client, access_token, viewer_user
|
|
):
|
|
try:
|
|
created = client.post(
|
|
"/api/permissions/groups",
|
|
headers=_bearer(access_token),
|
|
json={
|
|
"name": "Curators",
|
|
"description": "roms + platform read",
|
|
"grants": [
|
|
{"entity": "roms", "action": "read"},
|
|
{"entity": "roms", "action": "write"},
|
|
{"entity": "platforms", "action": "read"},
|
|
],
|
|
},
|
|
)
|
|
assert created.status_code == 201
|
|
group = created.json()
|
|
assert group["member_count"] == 0
|
|
assert {(g["entity"], g["action"]) for g in group["grants"]} == {
|
|
("roms", "read"),
|
|
("roms", "write"),
|
|
("platforms", "read"),
|
|
}
|
|
gid = group["id"]
|
|
|
|
assigned = client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=_bearer(access_token),
|
|
json={"set_group": True, "permission_group_id": gid},
|
|
)
|
|
assert assigned.status_code == 200
|
|
assert assigned.json()["permission_group_id"] == gid
|
|
|
|
actions, _ = _me_actions(client, viewer_user.id)
|
|
# Group grants -> these actions; nothing from the default viewer matrix.
|
|
assert {"rom.view", "rom.upload", "platform.view"} <= actions
|
|
assert "collection.create" not in actions # group grants no collections
|
|
finally:
|
|
_cleanup()
|
|
|
|
|
|
def test_user_override_adds_capability(client, access_token, viewer_user):
|
|
try:
|
|
gid = client.post(
|
|
"/api/permissions/groups",
|
|
headers=_bearer(access_token),
|
|
json={"name": "RO", "grants": [{"entity": "roms", "action": "read"}]},
|
|
).json()["id"]
|
|
client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=_bearer(access_token),
|
|
json={
|
|
"set_group": True,
|
|
"permission_group_id": gid,
|
|
"overrides": [{"entity": "roms", "action": "delete", "granted": True}],
|
|
},
|
|
)
|
|
actions, _ = _me_actions(client, viewer_user.id)
|
|
assert "rom.delete" in actions # granted by override on top of read-only group
|
|
finally:
|
|
_cleanup()
|
|
|
|
|
|
def test_a_permission_change_closes_the_users_device_sockets(
|
|
mocker, client, access_token, viewer_user
|
|
):
|
|
token, _ = make_device_token(viewer_user, None, scopes="devices.read")
|
|
close = mocker.patch.object(
|
|
permissions_endpoints, "close_client_token_sockets", AsyncMock()
|
|
)
|
|
|
|
response = client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=_bearer(access_token),
|
|
json={"overrides": [{"entity": "roms", "action": "delete", "granted": True}]},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
close.assert_awaited_once_with([token.id])
|
|
|
|
|
|
def test_a_group_edit_closes_every_members_device_sockets_at_once(
|
|
mocker, client, access_token, viewer_user, editor_user
|
|
):
|
|
try:
|
|
gid = client.post(
|
|
"/api/permissions/groups",
|
|
headers=_bearer(access_token),
|
|
json={"name": "Handhelds"},
|
|
).json()["id"]
|
|
for user in (viewer_user, editor_user):
|
|
client.put(
|
|
f"/api/permissions/users/{user.id}",
|
|
headers=_bearer(access_token),
|
|
json={"set_group": True, "permission_group_id": gid},
|
|
)
|
|
viewer_token, _ = make_device_token(viewer_user, None, scopes="devices.read")
|
|
editor_token, _ = make_device_token(editor_user, None, scopes="devices.read")
|
|
close = mocker.patch.object(
|
|
permissions_endpoints, "close_client_token_sockets", AsyncMock()
|
|
)
|
|
|
|
response = client.put(
|
|
f"/api/permissions/groups/{gid}",
|
|
headers=_bearer(access_token),
|
|
json={"grants": [{"entity": "roms", "action": "read"}]},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
close.assert_awaited_once()
|
|
assert close.await_args is not None
|
|
assert sorted(close.await_args.args[0]) == sorted(
|
|
[viewer_token.id, editor_token.id]
|
|
)
|
|
finally:
|
|
_cleanup()
|
|
|
|
|
|
def test_hide_entity_for_user(client, access_token, viewer_user):
|
|
resp = client.post(
|
|
"/api/permissions/hidden",
|
|
headers=_bearer(access_token),
|
|
json={"entity": "platforms", "entity_id": 11, "user_id": viewer_user.id},
|
|
)
|
|
assert resp.status_code == 200
|
|
|
|
_, body = _me_actions(client, viewer_user.id)
|
|
assert body["hidden"]["platforms"] == [11]
|
|
|
|
# Idempotent + removable.
|
|
client.post(
|
|
"/api/permissions/hidden",
|
|
headers=_bearer(access_token),
|
|
json={"entity": "platforms", "entity_id": 11, "user_id": viewer_user.id},
|
|
)
|
|
removed = client.request(
|
|
"DELETE",
|
|
"/api/permissions/hidden",
|
|
headers=_bearer(access_token),
|
|
json={"entity": "platforms", "entity_id": 11, "user_id": viewer_user.id},
|
|
)
|
|
assert removed.status_code == 204
|
|
_, body = _me_actions(client, viewer_user.id)
|
|
assert body["hidden"]["platforms"] == []
|
|
|
|
|
|
def test_hidden_requires_exactly_one_principal(client, access_token):
|
|
resp = client.post(
|
|
"/api/permissions/hidden",
|
|
headers=_bearer(access_token),
|
|
json={"entity": "roms", "entity_id": 1, "user_id": 1, "group_id": 1},
|
|
)
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_cannot_delete_default_group(client, access_token):
|
|
groups = client.get("/api/permissions/groups", headers=_bearer(access_token)).json()
|
|
default = next(g for g in groups if g["is_default"])
|
|
resp = client.delete(
|
|
f"/api/permissions/groups/{default['id']}", headers=_bearer(access_token)
|
|
)
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_update_group_deleted_mid_update_returns_404(client, access_token, mocker):
|
|
try:
|
|
gid = client.post(
|
|
"/api/permissions/groups",
|
|
headers=_bearer(access_token),
|
|
json={"name": "Vanishing"},
|
|
).json()["id"]
|
|
mocker.patch.object(DBPermissionsHandler, "update_group", return_value=None)
|
|
|
|
resp = client.put(
|
|
f"/api/permissions/groups/{gid}",
|
|
headers=_bearer(access_token),
|
|
json={"description": "renamed"},
|
|
)
|
|
assert resp.status_code == 404
|
|
finally:
|
|
_cleanup()
|
|
|
|
|
|
def test_delete_group_falls_members_back(client, access_token, viewer_user):
|
|
gid = client.post(
|
|
"/api/permissions/groups",
|
|
headers=_bearer(access_token),
|
|
json={"name": "Temp", "grants": [{"entity": "roms", "action": "read"}]},
|
|
).json()["id"]
|
|
client.put(
|
|
f"/api/permissions/users/{viewer_user.id}",
|
|
headers=_bearer(access_token),
|
|
json={"set_group": True, "permission_group_id": gid},
|
|
)
|
|
|
|
resp = client.delete(
|
|
f"/api/permissions/groups/{gid}", headers=_bearer(access_token)
|
|
)
|
|
assert resp.status_code == 200
|
|
|
|
# FK SET NULL -> user falls back to the default (viewer) matrix.
|
|
user = db_user_handler.get_user(viewer_user.id)
|
|
assert user is not None
|
|
assert user.permission_group_id is None
|
|
actions, _ = _me_actions(client, viewer_user.id)
|
|
assert "collection.create" in actions # default viewer behaviour restored
|