The _RedactWebhookPath filter now also strips email-shaped strings from every log record — messages, %-format args (recursing into tuple/list/ dict args), and any exception text already attached to the record — the same retention discipline the webhook token got in ops/security#2. _send_email recipients, listmonk failures, Loomio invite lists and provisioning/credit lines carry member addresses into container logs, which persist on the host and ride the backups. MXIDs, counts, slugs and amounts stay readable; the daily digest's own sanitisation is unchanged. Filter keeps its never-break-logging guarantee (all redaction inside try/except; non-string args untouched). Live pre-state measured: 99 email-bearing lines in the current app log.
266 lines
11 KiB
Python
266 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
"""Regression tests for M5 (atomic held-credit grant) + M6 (Cloudron user
|
|
pagination) — audit 2026-11-05.
|
|
|
|
Runnable:
|
|
* from the repo root: python3 tests/test_portal_hardening.py
|
|
* inside the portal app: PORTAL_CODE_DIR=/app/code python3 /tmp/test_portal_hardening.py
|
|
|
|
Covered:
|
|
M5 — grant_held_credits
|
|
- matched holds (by email AND by slug) flip + credit in one transaction
|
|
- a re-run moves no money (held:<id> reference exists-check)
|
|
- a hold whose ledger row already exists is reconciled, never re-credited
|
|
M6 — cloudron_fetch_all_users (async) / _sync
|
|
- pages past the first: >100 users returned in full (the old code silently
|
|
truncated at 100)
|
|
- page-1 failure -> None / raise (callers keep failing loudly)
|
|
- later-page failure -> keep what was read
|
|
- the hard page cap holds even if the API never runs short
|
|
"""
|
|
import asyncio
|
|
import os
|
|
import sqlite3
|
|
import sys
|
|
import tempfile
|
|
|
|
_TEST_DB = os.path.join(tempfile.mkdtemp(prefix="hardening-test-"), "members.db")
|
|
os.environ["DB_PATH"] = _TEST_DB # must exist BEFORE app.main is imported
|
|
|
|
for _cand in (os.environ.get("PORTAL_CODE_DIR"), "/app/code",
|
|
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))):
|
|
if _cand and os.path.isfile(os.path.join(_cand, "app", "main.py")):
|
|
sys.path.insert(0, _cand)
|
|
break
|
|
|
|
from app.main import ( # noqa: E402
|
|
cloudron_fetch_all_users, cloudron_fetch_all_users_sync, get_credit_balance,
|
|
get_db, grant_held_credits,
|
|
)
|
|
|
|
failures = 0
|
|
|
|
|
|
def check(name, got, want):
|
|
global failures
|
|
ok = got == want
|
|
if not ok:
|
|
failures += 1
|
|
print(f"[{'PASS' if ok else 'FAIL'}] {name}")
|
|
if not ok:
|
|
print(f" got {got!r}\n want {want!r}")
|
|
|
|
|
|
# ---------------------------------------------------------------- M5 ---------
|
|
conn = get_db()
|
|
conn.execute("INSERT INTO members (email, key_token, slug, active) "
|
|
"VALUES ('newmember@example.com', '', 'newmember', 1)")
|
|
conn.execute("INSERT INTO held_credits (slug, email, name, amount, oc_reference, status) "
|
|
"VALUES ('newmember', 'someone-else@example.com', 'W', 10.0, 'oc:newmember:or_1', 'held')")
|
|
conn.execute("INSERT INTO held_credits (slug, email, name, amount, oc_reference, status) "
|
|
"VALUES ('newmember', NULL, 'X', 5.0, 'oc:other:or_2', 'held')")
|
|
conn.commit()
|
|
|
|
# Mixed-case email on purpose: the grant must normalize it (the balance
|
|
# UPDATE/SELECT join on the member row and silently miss otherwise).
|
|
granted = grant_held_credits(conn, "NewMember@Example.com", "newmember")
|
|
check("matched holds sum (including the non-member slug hold)", granted, 15.0)
|
|
check("balance credited", get_credit_balance("newmember@example.com"), 15.0)
|
|
statuses = dict(conn.execute("SELECT id, status FROM held_credits").fetchall())
|
|
check("both holds flipped in the same pass", set(statuses.values()), {"granted"})
|
|
refs = [r[0] for r in conn.execute("SELECT reference FROM credit_ledger ORDER BY id").fetchall()]
|
|
check("one ledger row per hold, held:<id> shape", refs, ["held:1", "held:2"])
|
|
|
|
granted2 = grant_held_credits(conn, "newmember@example.com", "newmember")
|
|
check("re-run grants nothing (reference exists-check)", granted2, 0.0)
|
|
refs = [r[0] for r in conn.execute("SELECT reference FROM credit_ledger ORDER BY id").fetchall()]
|
|
check("re-run wrote no extra ledger rows", refs, ["held:1", "held:2"])
|
|
check("re-run left the balance alone", get_credit_balance("newmember@example.com"), 15.0)
|
|
|
|
# Crash-recovery shape: a hold flipped but its ledger row lost… inverse — a
|
|
# ledger row that exists while the hold reads 'held' (a crash between the two
|
|
# commits of the OLD code) must reconcile the status and move no money.
|
|
conn.execute("UPDATE held_credits SET status='held', resolved_at=NULL WHERE id = 2")
|
|
conn.commit()
|
|
granted3 = grant_held_credits(conn, "newmember@example.com", "newmember")
|
|
check("already-credited hold reconciles without re-crediting", granted3, 0.0)
|
|
check("its status is reconciled to granted",
|
|
dict(conn.execute("SELECT id, status FROM held_credits").fetchall()), {1: "granted", 2: "granted"})
|
|
check("balance still 15 after the reconciliation pass",
|
|
get_credit_balance("newmember@example.com"), 15.0)
|
|
|
|
_conn2 = get_db() # separate handle to prove committed state, not in-transaction state
|
|
n = _conn2.execute("SELECT COUNT(*) FROM credit_ledger").fetchone()[0]
|
|
check("committed state equals in-call state", n, 2)
|
|
_conn2.close()
|
|
conn.close()
|
|
|
|
# ---------------------------------------------------------------- M6 ---------
|
|
class FakeResponse:
|
|
def __init__(self, status_code, payload):
|
|
self.status_code = status_code
|
|
self._payload = payload
|
|
|
|
def json(self):
|
|
return self._payload
|
|
|
|
|
|
class FakeAsyncClient:
|
|
def __init__(self, pages):
|
|
self.pages = list(pages) # one entry per expected page
|
|
self.calls = []
|
|
|
|
async def get(self, url, params=None, headers=None):
|
|
self.calls.append(dict(params or {}))
|
|
if not self.pages:
|
|
return FakeResponse(200, {"users": []})
|
|
item = self.pages.pop(0)
|
|
if isinstance(item, int):
|
|
return FakeResponse(item, {})
|
|
return FakeResponse(200, {"users": item})
|
|
|
|
|
|
users_120 = [{"id": f"u{i}", "email": f"m{i}@example.com"} for i in range(120)]
|
|
page1, page2 = users_120[:100], users_120[100:]
|
|
|
|
fc = FakeAsyncClient([page1, page2])
|
|
got = asyncio.run(cloudron_fetch_all_users(fc))
|
|
check("110+ users: walked past the first page", len(got), 120)
|
|
check("per_page=100 on every page", {c.get("per_page") for c in fc.calls}, {100})
|
|
check("pages requested in order", [c.get("page") for c in fc.calls], [1, 2])
|
|
|
|
fc = FakeAsyncClient([500])
|
|
check("page-1 failure -> None (caller fails loudly)",
|
|
asyncio.run(cloudron_fetch_all_users(fc)), None)
|
|
|
|
fc = FakeAsyncClient([page1, 500, page2])
|
|
got = asyncio.run(cloudron_fetch_all_users(fc))
|
|
check("later-page failure keeps what was read", len(got), 100)
|
|
|
|
|
|
class EndlessFullPagesClient:
|
|
"""A fake API that never runs short — the hard cap must stop the walk."""
|
|
|
|
def __init__(self):
|
|
self.calls = 0
|
|
|
|
async def get(self, url, params=None, headers=None):
|
|
self.calls += 1
|
|
return FakeResponse(200, {"users": users_120[:100]})
|
|
|
|
|
|
fc = EndlessFullPagesClient()
|
|
got = asyncio.run(cloudron_fetch_all_users(fc))
|
|
check("hard cap: stops at 50 pages (5000 users)", (len(got), fc.calls), (5000, 50))
|
|
|
|
# --- sync walker (monkeypatch urllib at the call site's own module) ---------
|
|
import urllib.request
|
|
|
|
_real_urlopen = urllib.request.urlopen
|
|
_seen = {"n": 0}
|
|
|
|
|
|
def fake_urlopen(req, timeout=None):
|
|
_seen["n"] += 1
|
|
if _seen["n"] == 1:
|
|
raise RuntimeError("page 1 simulated failure")
|
|
class R:
|
|
def __enter__(self):
|
|
return self
|
|
def __exit__(self, *a):
|
|
return False
|
|
def read(self):
|
|
return b'{"users": []}'
|
|
return R()
|
|
|
|
|
|
urllib.request.urlopen = fake_urlopen
|
|
try:
|
|
try:
|
|
cloudron_fetch_all_users_sync()
|
|
check("sync walker: page-1 failure raises", False, True)
|
|
except RuntimeError:
|
|
check("sync walker: page-1 failure raises", True, True)
|
|
finally:
|
|
urllib.request.urlopen = _real_urlopen
|
|
|
|
_real_urlopen = urllib.request.urlopen
|
|
_state = {"pages": [users_120[:100], users_120[100:]], "urls": []}
|
|
|
|
|
|
def fake_urlopen2(req, timeout=None):
|
|
_state["urls"].append(req.full_url)
|
|
page = _state["pages"].pop(0) if _state["pages"] else []
|
|
import io
|
|
body = ('{"users": %s}' % __import__("json").dumps(page)).encode()
|
|
return io.BytesIO(body)
|
|
|
|
|
|
urllib.request.urlopen = fake_urlopen2
|
|
try:
|
|
got = cloudron_fetch_all_users_sync()
|
|
check("sync walker pages past the first page too", len(got), 120)
|
|
check("sync walker issued a page=2 request",
|
|
any("page=2" in u for u in _state["urls"]), True)
|
|
finally:
|
|
urllib.request.urlopen = _real_urlopen
|
|
|
|
# ------------------------------------------------------- M1: log redaction ---
|
|
# The webhook token and member emails must not survive into portal logs. The
|
|
# guard must FIRE: feed a record with both a token and an email and read back
|
|
# the formatted line.
|
|
import logging # noqa: E402
|
|
|
|
from app.main import _EMAIL_RE, _RedactWebhookPath # noqa: E402
|
|
|
|
flt = _RedactWebhookPath()
|
|
rec = logging.LogRecord("t", logging.INFO, "p", 1,
|
|
"Credit pack: +%s for %s (balance %s)", (20.0, "member@example.org", 30.0), None)
|
|
flt.filter(rec)
|
|
check("email in %-args redacted", rec.getMessage(), "Credit pack: +20.0 for <email> (balance 30.0)")
|
|
check("argument tuple preserved", isinstance(rec.args, tuple), True)
|
|
|
|
rec = logging.LogRecord("t", logging.INFO, "p", 1,
|
|
"Provisioned member hidden@example.org ok", (), None)
|
|
flt.filter(rec)
|
|
check("email embedded via f-string (msg) redacted", rec.getMessage(), "Provisioned member <email> ok")
|
|
|
|
rec = logging.LogRecord("t", logging.INFO, "p", 1, "Loomio invites: %s",
|
|
(["a@example.org", "b@example.org"],), None)
|
|
flt.filter(rec)
|
|
check("list args redacted, list type kept",
|
|
(rec.getMessage(), type(rec.args[0]).__name__), ("Loomio invites: ['<email>', '<email>']", "list"))
|
|
|
|
rec = logging.LogRecord("t", logging.INFO, "p", 1,
|
|
'POST /webhook/opencollective/sekret123?x=1 HTTP/1.1', (), None)
|
|
filt_result = flt.filter(rec)
|
|
check("webhook token redacted, filter returns True",
|
|
(rec.getMessage(), filt_result), ("POST /webhook/opencollective/<redacted>?x=1 HTTP/1.1", True))
|
|
rec = logging.LogRecord("t", logging.INFO, "p", 1,
|
|
"Credit pack for member@example.org via /webhook/opencollective/tok9", (), None)
|
|
flt.filter(rec)
|
|
check("token and email redacted in one line",
|
|
"<email>" in rec.getMessage() and "<redacted>" in rec.getMessage()
|
|
and "member@example.org" not in rec.getMessage() and "tok9" not in rec.getMessage(), True)
|
|
|
|
rec = logging.LogRecord("t", logging.INFO, "p", 1, "Matrix invite for @someuser:inference.coop", (), None)
|
|
flt.filter(rec)
|
|
check("MXIDs are not emails and stay readable",
|
|
rec.getMessage(), "Matrix invite for @someuser:inference.coop")
|
|
|
|
rec = logging.LogRecord("t", logging.INFO, "p", 1, "counter=%d", (7,), None)
|
|
flt.filter(rec)
|
|
check("non-string args untouched", rec.getMessage(), "counter=7")
|
|
|
|
rec = logging.LogRecord("t", logging.INFO, "p", 1, "boom", (), None)
|
|
rec.exc_text = "ValueError: No member account for lost@example.org"
|
|
flt.filter(rec)
|
|
check("already-formatted exception text redacted",
|
|
rec.exc_text, "ValueError: No member account for <email>")
|
|
|
|
check("regex matches a real-shaped address",
|
|
bool(_EMAIL_RE.search("contact info@a.b-stuff.coop today")), True)
|
|
|
|
print(f"\n{'FAILED' if failures else 'OK'} — {failures} failure(s)")
|
|
sys.exit(1 if failures else 0)
|