From bd0746d19779a7ad359c3839ec28b1c472dfb99a Mon Sep 17 00:00:00 2001 From: Inferencebot Date: Wed, 18 Nov 2026 19:27:37 +0000 Subject: [PATCH] =?UTF-8?q?panel:=20split=20read=20from=20write=20?= =?UTF-8?q?=E2=80=94=20any=20admitted=20Cloudron=20account=20reads,=20ADMI?= =?UTF-8?q?N=5FUSERNAMES=20writes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Cloudron access group alone was not enough: the app's own allowlist still 403'd every non-admin, so a steward with panel access saw nothing. Read is now any authenticated identity Cloudron admitted to the app; write (balances, credits, guest invites) stays with ADMIN_USERNAMES, enforced on every POST. The page says which role you have and hides write controls when read-only. --- app/main.py | 104 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 74 insertions(+), 30 deletions(-) diff --git a/app/main.py b/app/main.py index 071dd90..09317a9 100644 --- a/app/main.py +++ b/app/main.py @@ -1,19 +1,22 @@ """Admin Panel — co-op-wide member overview, spend, and balance management. -Security model: -- Authentication is done by Cloudron's proxyAuth wall (SSO). Cloudron injects - the authenticated user's username via X-Remote-User. -- Access is further restricted to a small ADMIN_USERNAMES allowlist (checked - against the injected username), so only designated admins see this panel. -- This app calls the member portal's /admin/overview and /admin/set-balance - endpoints, authenticated with the portal's dedicated ADMIN_TOKEN (the - X-Admin-Token header — NOT the webhook's URL token, which cannot unlock - /admin/*). +Security model — TWO roles, split by the app itself: +- Authentication is Cloudron OIDC. Cloudron only completes the login for users + the app's access restriction admits (the `managers` and `stewards` groups), + so "authenticated here" already means "the operator let this account in". +- READ: any authenticated identity may view the panel (overview, spend, dues, + guests, money series). The page says so and hides every write control. +- WRITE: only the ADMIN_USERNAMES allowlist (Cloudron usernames, comma- + separated) may change balances/credits or provision guests. Every POST route + re-checks this server-side — hiding a button is not access control. +- This app calls the member portal's /admin/* endpoints with the portal's + dedicated ADMIN_TOKEN (the X-Admin-Token header — NOT the webhook's URL + token, which cannot unlock /admin/*). Environment (Cloudron env vars): PORTAL_BASE — base URL of the member portal ADMIN_TOKEN — the portal's admin/webhook token (for /admin/* endpoints) - ADMIN_USERNAMES — comma-separated list of allowed Cloudron usernames + ADMIN_USERNAMES — comma-separated list of Cloudron usernames with WRITE access """ import os @@ -63,12 +66,18 @@ def get_identity(request: Request) -> str: def is_admin(request: Request) -> bool: + """WRITE access: the identity must be in ADMIN_USERNAMES.""" identity = get_identity(request) if not identity: return False return identity.lower() in ADMIN_USERNAMES +def is_reader(request: Request) -> bool: + """READ access: any authenticated Cloudron identity Cloudron admitted.""" + return bool(get_identity(request)) + + async def portal_get(path: str) -> dict: async with httpx.AsyncClient(timeout=20.0) as client: r = await client.get(f"{PORTAL_BASE}{path}", headers={"X-Admin-Token": ADMIN_TOKEN}) @@ -116,28 +125,25 @@ async def index(request: Request): "

Forbidden

This panel is restricted to administrators.

", status_code=403, ) - # Authenticated but not an admin → forbid. - if identity.lower() not in ADMIN_USERNAMES: - return HTMLResponse( - "

Forbidden

This panel is restricted to administrators.

", - status_code=403, - ) + # Authenticated → READ is allowed for everyone Cloudron admitted to the app; + # WRITE stays with the ADMIN_USERNAMES roster (enforced on every POST). + read_only = identity.lower() not in ADMIN_USERNAMES # no-store: the page is one inline script with no validators, so a cached # copy runs the PREVIOUS deploy's JS and makes a working app look broken # (it did, 2026-11-13). - return HTMLResponse(render_page(), headers={"Cache-Control": "no-store"}) + return HTMLResponse(render_page(read_only), headers={"Cache-Control": "no-store"}) @app.get("/api/overview") async def api_overview(request: Request): - if not is_admin(request): + if not is_reader(request): return JSONResponse({"error": "forbidden"}, status_code=403) return await portal_get("/admin/overview") @app.get("/api/model-usage") async def api_model_usage(request: Request): - if not is_admin(request): + if not is_reader(request): return JSONResponse({"error": "forbidden"}, status_code=403) try: return await portal_get("/admin/model-usage") @@ -153,7 +159,7 @@ async def api_metrics(request: Request, days: int = 730): only reads it. Fetched once at the widest range and sliced client-side, so the period toggle never refetches. """ - if not is_admin(request): + if not is_reader(request): return JSONResponse({"error": "forbidden"}, status_code=403) try: return await portal_get(f"/admin/metrics/series?days={max(1, min(int(days), 730))}") @@ -169,7 +175,7 @@ async def api_money_series(request: Request): the panel only reads it. The full history comes back in one payload — the period and spend-mode toggles slice client-side and never refetch. """ - if not is_admin(request): + if not is_reader(request): return JSONResponse({"error": "forbidden"}, status_code=403) try: return await portal_get("/admin/money-series") @@ -293,7 +299,7 @@ async def api_guest_ledger(request: Request): approval gate, removed 2026-11-08: member invitations provision immediately, so there is nothing to approve.) """ - if not is_admin(request): + if not is_reader(request): return JSONResponse({"error": "forbidden"}, status_code=403) try: return await portal_get("/admin/guest-ledger") @@ -358,8 +364,18 @@ def _esc(s: str) -> str: return s.replace("&", "&").replace("<", "<").replace(">", ">").replace('"', """) -def render_page() -> str: - return PAGE_HTML +def render_page(read_only: bool) -> str: + """Render the panel for one role. + + The role is injected into the page's JS as one assignment after + `let READ_ONLY = false;` — the placeholder stays a JS comment, so the + literal is still `node --check`-clean for tests/check-page-js.sh. Hiding + the write controls here is UX only; every POST route re-checks the roster. + """ + html = PAGE_HTML + html = html.replace("@@WRITERS@@", ", ".join(sorted(ADMIN_USERNAMES)) or "the operator") + html = html.replace("/*@@READ_ONLY@@*/", "READ_ONLY = true;" if read_only else "") + return html # The shared chart module (canonical: ops-docs scripts/shared/coop_chart.js, @@ -450,6 +466,10 @@ PAGE_HTML = """ .flash.show { display: block; } .flash.ok { background: #eaf3ea; color: #2f5c30; } .flash.err { background: #f6e9e5; color: var(--danger); } + /* Role banner: the page states which role you have at the top, where it is + unmissable, instead of leaving write controls just absent. */ + .readonly-banner { background: #eef3ee; border: 1px solid #d7e2d6; border-radius: 10px; padding: 12px 16px; font-size: 13px; color: var(--ink); margin: 0 0 4px; } + .readonly-banner strong { color: var(--green-deep); } .adjust { display: inline-flex; gap: 6px; align-items: center; white-space: nowrap; } .adjust input { width: 56px; padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; flex-shrink: 0; } .adjust button { cursor: pointer; border: none; border-radius: 6px; background: var(--green-deep); color: #fff; font-size: 11px; font-weight: 600; padding: 4px 8px; flex-shrink: 0; } @@ -483,6 +503,13 @@ PAGE_HTML = """ + +
—
members
—
guests — guest access
excluded from member counts
@@ -521,7 +548,7 @@ PAGE_HTML = """ Spend Dues Reset - Adjust + Adjust Loading… @@ -643,6 +670,10 @@ PAGE_HTML = """ /*@@CHART_MODULE@@*/ const $ = (id) => document.getElementById(id); +// Role flag, injected per request by render_page() (READ_ONLY = true for an +// account outside ADMIN_USERNAMES). UI only — POST routes enforce it. +let READ_ONLY = false; /*@@READ_ONLY@@*/ + let flashTimer = null; function flash(msg, ok) { const f = $('flash'); @@ -1088,6 +1119,15 @@ function renderMembers() { const org = m.org ? ' org: ' + esc(m.org) + '' : ''; + // Write controls exist only for the writer role. Presentation only — the + // POST routes re-check ADMIN_USERNAMES, so a hand-built request still fails. + const adjustCell = READ_ONLY + ? '—' + : '
' + + '' + + '' + + '' + + '
'; return '' + '' + esc(top) + '' + emailLine + mxidLine + (org ? '
' + org + '
' : '') + '' + @@ -1099,11 +1139,7 @@ function renderMembers() { '' + fmt(memberSpend(m)) + '' + '' + duesCell(m) + '' + '' + esc(reset) + '' + - '
' + - '' + - '' + - '' + - '
' + + adjustCell + ''; }).join(''); clearArrows('members', memberSort); @@ -1287,6 +1323,14 @@ async function sendGuestAccess(ev) { // inviter, credits, expiry and status, exactly like admin-created ones. // The portal's /admin/guest-ledger still feeds the duplicate-send guard. +// Role-dependent chrome: read-only accounts see the banner and no write +// controls. Runs before the first load() so no button ever flashes on screen. +if (READ_ONLY) { + $('readonly-banner').style.display = ''; + $('guest-invite-section').style.display = 'none'; + $('adjust-th').style.display = 'none'; +} + buildMetricButtons(); load(); loadModels();