From d0f09830bb31ffad83a7061ba5211e817e3c54c3 Mon Sep 17 00:00:00 2001 From: Stommer Date: Thu, 22 Oct 2026 17:26:06 +0200 Subject: [PATCH] =?UTF-8?q?Behebe=20R-11:=20=C3=9Cbersicht=20und=20Suche?= =?UTF-8?q?=20lesen=20in=20Bl=C3=B6cken=20und=20pr=C3=BCfen=20Rechte=20vor?= =?UTF-8?q?=20der=20Begrenzung.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 --- backend/app/api/routes.py | 60 +++++++++++++++++++++++--------- backend/tests/test_views_scan.py | 46 ++++++++++++++++++++++++ docs/review/06_BEFUNDE.md | 4 ++- 3 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 backend/tests/test_views_scan.py diff --git a/backend/app/api/routes.py b/backend/app/api/routes.py index 0779174..b40adab 100644 --- a/backend/app/api/routes.py +++ b/backend/app/api/routes.py @@ -1182,37 +1182,61 @@ def post_fulfill( return deadline_payload(conn, principal.account_id, deadline) +VIEW_LIMIT = 200 +SEARCH_LIMIT = 50 +SCAN_BATCH = 200 +SCAN_CAP = 20000 + + +def _scan(conn, sql: str, params: tuple = ()): + """Rows in the order of the query, read in batches. + + The rights are checked per row after reading. A fixed LIMIT in the query would count rows + of other people too, so the own ones could fall off the end. The reader stops once it has + enough. `SCAN_CAP` bounds the work on very large data sets. + """ + offset = 0 + while offset < SCAN_CAP: + rows = conn.execute(f"{sql} LIMIT %s OFFSET %s", (*params, SCAN_BATCH, offset)).fetchall() + yield from rows + if len(rows) < SCAN_BATCH: + return + offset += SCAN_BATCH + + @router.get("/api/v1/views/open-tasks") def open_tasks(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): - rows = conn.execute( + items = [] + for task in _scan( + conn, """ SELECT * FROM tasks WHERE deleted_at IS NULL AND task_status = 'open' ORDER BY due_on NULLS LAST, created_at, id - LIMIT 200 - """ - ).fetchall() - items = [] - for task in rows: + """, + ): if _task_visible(conn, principal.account_id, task): items.append(task_payload(conn, principal.account_id, task)) + if len(items) >= VIEW_LIMIT: + break return {"items": items, "next_cursor": None} @router.get("/api/v1/views/open-deadlines") def open_deadlines(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)): - rows = conn.execute( + items = [] + for deadline in _scan( + conn, """ SELECT * FROM deadlines WHERE deleted_at IS NULL AND deadline_status = 'open' ORDER BY due_on, created_at, id - LIMIT 200 - """ - ).fetchall() - items = [] - for deadline in rows: + """, + ): if _deadline_visible(conn, principal.account_id, deadline): items.append(deadline_payload(conn, principal.account_id, deadline)) + if len(items) >= VIEW_LIMIT: + break return {"items": items, "next_cursor": None} @@ -1266,19 +1290,21 @@ def search(q: str, principal: Principal = Depends(get_principal), conn: Connecti ], } ) - elements = conn.execute( + elements = _scan( + conn, """ SELECT e.* FROM elements e LEFT JOIN note_bodies n ON n.element_id = e.id WHERE e.deleted_at IS NULL AND (e.title ILIKE %s ESCAPE '\\' OR COALESCE(n.document_json::text, '') ILIKE %s ESCAPE '\\') - ORDER BY e.updated_at DESC - LIMIT 100 + ORDER BY e.updated_at DESC, e.id """, (pattern, pattern), - ).fetchall() + ) for element in elements: + if len(items) >= SEARCH_LIMIT: + break from app.access.policy import visible_bindings_for_element bindings = visible_bindings_for_element(conn, principal.account_id, element) @@ -1306,7 +1332,7 @@ def search(q: str, principal: Principal = Depends(get_principal), conn: Connecti ], } ) - return {"items": items[:50], "next_cursor": None} + return {"items": items[:SEARCH_LIMIT], "next_cursor": None} @router.patch("/api/v1/cases/{case_id}/content-order") diff --git a/backend/tests/test_views_scan.py b/backend/tests/test_views_scan.py new file mode 100644 index 0000000..36d777c --- /dev/null +++ b/backend/tests/test_views_scan.py @@ -0,0 +1,46 @@ +"""R-11: other people's rows must not push your own out of the overview and the search.""" + +from __future__ import annotations + +from tests.support import Actor, case_of, note_on, register + + +def test_own_entries_are_found_behind_many_foreign_ones(client, db, monkeypatch): + from fastapi.testclient import TestClient + + from app.api import routes + from app.main import app + + # A tiny batch size makes the paging visible with few rows. + monkeypatch.setattr(routes, "SCAN_BATCH", 2) + register(db, "ada-scan@example.test", "Ada") + register(db, "ben-scan@example.test", "Ben") + ada = Actor(client, "ada-scan@example.test", "Ada") + ben = Actor(TestClient(app), "ben-scan@example.test", "Ben") + ada.login() + ben.login() + + ada_case = case_of(ada, "Adas Akte") + for index in range(7): + matter = ada.request("POST", "/api/v1/matters", json={"title": f"Adas Vorgang {index}"}, status=201) + ada.request("POST", "/api/v1/tasks", json={"title": f"Frühe Aufgabe {index}", "due_on": "2026-01-0%d" % (index + 1), "matter_id": matter["id"]}, status=201) + ada.request("POST", "/api/v1/deadlines", json={"title": f"Frühe Frist {index}", "due_on": "2026-01-0%d" % (index + 1), "matter_id": matter["id"]}, status=201) + note_on(ada, f"Gemeinsames Stichwort {index}", "Zebrastreifen", ada_case["id"]) + + ben_matter = ben.request("POST", "/api/v1/matters", json={"title": "Bens Vorgang"}, status=201) + ben.request("POST", "/api/v1/tasks", json={"title": "Bens späte Aufgabe", "due_on": "2027-01-01", "matter_id": ben_matter["id"]}, status=201) + ben.request("POST", "/api/v1/deadlines", json={"title": "Bens späte Frist", "due_on": "2027-01-01", "matter_id": ben_matter["id"]}, status=201) + ben_case = case_of(ben, "Bens Akte") + note_on(ben, "Bens Zebrastreifen-Notiz", "Zebrastreifen", ben_case["id"]) + + assert [item["title"] for item in ben.request("GET", "/api/v1/views/open-tasks")["items"]] == ["Bens späte Aufgabe"] + assert [item["title"] for item in ben.request("GET", "/api/v1/views/open-deadlines")["items"]] == ["Bens späte Frist"] + found = ben.request("GET", "/api/v1/search", params={"q": "Zebrastreifen"})["items"] + assert [item["title"] for item in found] == ["Bens Zebrastreifen-Notiz"] + + # The own entries still come in order and the cap on the list holds. + monkeypatch.setattr(routes, "VIEW_LIMIT", 3) + first = ada.request("GET", "/api/v1/views/open-tasks")["items"] + assert [item["title"] for item in first] == ["Frühe Aufgabe 0", "Frühe Aufgabe 1", "Frühe Aufgabe 2"] + monkeypatch.setattr(routes, "SEARCH_LIMIT", 4) + assert len(ada.request("GET", "/api/v1/search", params={"q": "Zebrastreifen"})["items"]) == 4 diff --git a/docs/review/06_BEFUNDE.md b/docs/review/06_BEFUNDE.md index 108a6c0..0cf958e 100644 --- a/docs/review/06_BEFUNDE.md +++ b/docs/review/06_BEFUNDE.md @@ -133,7 +133,9 @@ Die Paperless-Vorschau lädt mit dem zuletzt angelegten Paperless-Zugang **der a **Empfehlung:** Zugang fest an die Referenz binden (`connector_id` als Spalte, wie im Connectoren-Befund vorgeschlagen). Den Abruf über den Zugang des Element-Owners führen, nachdem das Leserecht im Kontext geprüft ist. Bis dahin die Grenze in der Oberfläche benennen. -## R-11 – `LIMIT` vor der Rechteprüfung (Hoch) +## R-11 – `LIMIT` vor der Rechteprüfung (Hoch) – behoben am 23.10.2026 + +**Stand:** Übersicht (offene Aufgaben und Fristen) und Suche lesen die Kandidaten jetzt in Blöcken von 200 in der Reihenfolge der Abfrage, prüfen je Zeile die Rechte und hören auf, sobald genug sichtbare Einträge da sind (`_scan` in `routes.py`). Fremde Zeilen verdrängen eigene nicht mehr. Die Arbeit ist bei sehr großen Beständen auf 20 000 gelesene Zeilen begrenzt; eine Vorfilterung im SQL und Cursor-Seiten bleiben eine spätere Optimierung. Test: `tests/test_views_scan.py`. Der Befund unten beschreibt den früheren Zustand. **Fundstelle:** [routes.py:1328–1359](../../backend/app/api/routes.py#L1328-L1359), [routes.py:1412–1423](../../backend/app/api/routes.py#L1412-L1423)