Verhindere, dass Raum-Endpunkte verraten, welche Raum-IDs es gibt, und sichere sie mit einem Test gegen Außenstehende ab.
Unbekannte und fremde Räume antworten bei Mitglieder-, Einladungs-, Eigentümer- und Freigabe-Aktionen gleich mit 403. Der neue Test prüft alle Raum-Endpunkte als Außenstehender, Leser und Mitglied. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ac3c3d81fe
commit
e47f50d513
2 files changed
+47
-5
No files matched your search
@@ -46,8 +46,11 @@ def _created(path: str, payload: dict) -> JSONResponse:
|
|||||||
|
|
||||||
|
|
||||||
def _require_owner(conn, account_id: UUID, room_id: UUID) -> dict:
|
def _require_owner(conn, account_id: UUID, room_id: UUID) -> dict:
|
||||||
room = _room(conn, room_id)
|
# An unknown room and a room owned by someone else answer alike: no way to probe ids.
|
||||||
if room["owner_id"] != account_id:
|
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.")
|
raise forbidden("Das darf nur der Eigentümer des Datenraums.")
|
||||||
return room
|
return room
|
||||||
|
|
||||||
@@ -287,7 +290,6 @@ def transfer_owner(
|
|||||||
@router.get("/api/v1/rooms/{room_id}/exposure")
|
@router.get("/api/v1/rooms/{room_id}/exposure")
|
||||||
def exposure(room_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
|
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`)."""
|
"""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:
|
if room_role(conn, principal.account_id, room_id) is None:
|
||||||
raise forbidden()
|
raise forbidden()
|
||||||
result: dict = {}
|
result: dict = {}
|
||||||
@@ -318,7 +320,6 @@ def set_my_content(
|
|||||||
conn: Connection = Depends(get_conn),
|
conn: Connection = Depends(get_conn),
|
||||||
):
|
):
|
||||||
"""`room`: share every own entry filed in this room with it. `private`: end all own shares to it."""
|
"""`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)
|
role = room_role(conn, principal.account_id, room_id)
|
||||||
if role is None:
|
if role is None:
|
||||||
raise forbidden()
|
raise forbidden()
|
||||||
@@ -475,8 +476,8 @@ def invite(
|
|||||||
principal: Principal = Depends(get_principal),
|
principal: Principal = Depends(get_principal),
|
||||||
conn: Connection = Depends(get_conn),
|
conn: Connection = Depends(get_conn),
|
||||||
):
|
):
|
||||||
|
require_room_manager(conn, principal.account_id, body.room_id)
|
||||||
room = _room(conn, body.room_id)
|
room = _room(conn, body.room_id)
|
||||||
require_room_manager(conn, principal.account_id, room["id"])
|
|
||||||
email = normalize_email(body.email)
|
email = normalize_email(body.email)
|
||||||
if account_by_email(conn, email):
|
if account_by_email(conn, email):
|
||||||
raise unprocessable("Es gibt bereits ein Konto. Füge die Person als Mitglied hinzu.")
|
raise unprocessable("Es gibt bereits ein Konto. Füge die Person als Mitglied hinzu.")
|
||||||
|
|||||||
@@ -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}/members/{_id(db, ben.email)}", json={"role": "reader"})
|
||||||
personal.request("PATCH", f"/api/v1/rooms/{room}", json={"name": "Werkstatt neu"})
|
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"]
|
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()
|
||||||
Reference in new issue
Block a user