diff --git a/backend/app/api/rooms.py b/backend/app/api/rooms.py index 037e680..ffc3398 100644 --- a/backend/app/api/rooms.py +++ b/backend/app/api/rooms.py @@ -46,8 +46,11 @@ def _created(path: str, payload: dict) -> JSONResponse: def _require_owner(conn, account_id: UUID, room_id: UUID) -> dict: - room = _room(conn, room_id) - if room["owner_id"] != account_id: + # An unknown room and a room owned by someone else answer alike: no way to probe ids. + room = conn.execute( + "SELECT * FROM data_rooms WHERE id = %s AND kind = 'organization'", (room_id,) + ).fetchone() + if room is None or room["owner_id"] != account_id: raise forbidden("Das darf nur der Eigentümer des Datenraums.") return room @@ -287,7 +290,6 @@ def transfer_owner( @router.get("/api/v1/rooms/{room_id}/exposure") def exposure(room_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): """Own cases and matters: shared with this room (`room`) or filed here but not shared (`private`).""" - _room(conn, room_id) if room_role(conn, principal.account_id, room_id) is None: raise forbidden() result: dict = {} @@ -318,7 +320,6 @@ def set_my_content( conn: Connection = Depends(get_conn), ): """`room`: share every own entry filed in this room with it. `private`: end all own shares to it.""" - _room(conn, room_id) role = room_role(conn, principal.account_id, room_id) if role is None: raise forbidden() @@ -475,8 +476,8 @@ def invite( principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn), ): + require_room_manager(conn, principal.account_id, body.room_id) room = _room(conn, body.room_id) - require_room_manager(conn, principal.account_id, room["id"]) email = normalize_email(body.email) if account_by_email(conn, email): raise unprocessable("Es gibt bereits ein Konto. Füge die Person als Mitglied hinzu.") diff --git a/backend/tests/test_room_access.py b/backend/tests/test_room_access.py index 352fa2b..0ef045c 100644 --- a/backend/tests/test_room_access.py +++ b/backend/tests/test_room_access.py @@ -334,3 +334,44 @@ def test_lists_tell_the_owner_what_is_shared_and_rooms_are_managed_from_any_room personal.request("PATCH", f"/api/v1/rooms/{room}/members/{_id(db, ben.email)}", json={"role": "reader"}) personal.request("PATCH", f"/api/v1/rooms/{room}", json={"name": "Werkstatt neu"}) assert [r["name"] for r in personal.request("GET", "/api/v1/rooms")["items"] if r["id"] == room] == ["Werkstatt neu"] + + +def test_room_endpoints_are_closed_to_outsiders_and_do_not_reveal_which_ids_exist(world, db): + import uuid + + ada, ben, cem, dan, room = world["ada"], world["ben"], world["cem"], world["dan"], world["room"] + foreign = str(uuid.uuid4()) + dan_id = _id(db, dan.email) + ben_id = _id(db, ben.email) + attempts = [ + ("GET", "/members", None), + ("POST", "/members", {"email": dan.email, "role": "manager"}), + ("PATCH", f"/members/{ben_id}", {"role": "manager"}), + ("DELETE", f"/members/{ben_id}", None), + ("GET", "/invitations", None), + ("PATCH", "", {"name": "Übernommen"}), + ("POST", "/owner", {"subject_id": dan_id}), + ("GET", "/exposure", None), + ("POST", "/my-content/visibility", {"visibility": "room"}), + ] + for method, suffix, body in attempts: + # Dan is no member, Cem is a reader, Ben a plain member: none of them administers the room. + for actor in (dan, cem, ben): + if method == "GET" and suffix == "/exposure" and actor in (cem, ben): + continue # members may read their own exposure counts + if method == "POST" and suffix == "/my-content/visibility" and actor is ben: + continue # members may share their own entries + actor.request(method, f"/api/v1/rooms/{room}{suffix}", json=body, status=403) + # A room that does not exist answers like a room that is closed to the caller. + dan.request(method, f"/api/v1/rooms/{foreign}{suffix.replace(ben_id, dan_id)}", json=body, status=403) + dan.request("POST", "/api/v1/invitations", json={"email": "x@example.test", "room_id": room}, status=403) + dan.request("POST", "/api/v1/invitations", json={"email": "x@example.test", "room_id": foreign}, status=403) + dan.request("GET", "/api/v1/admin/rooms", status=403) + dan.request("POST", f"/api/v1/admin/rooms/{room}/owner", json={"email": dan.email}, status=403) + dan.request("GET", "/api/v1/admin/events", status=403) + # Nothing changed. + members = ada.request("GET", f"/api/v1/rooms/{room}/members")["items"] + assert {m["email"]: m["role"] for m in members}[ben.email] == "member" + assert dan.email not in {m["email"] for m in members} + assert db.execute("SELECT name FROM data_rooms WHERE id = %s", (room,)).fetchone()["name"] == "Werkstatt" + db.rollback()