diff --git a/app/main.py b/app/main.py index a68e8bb..b84b7c5 100644 --- a/app/main.py +++ b/app/main.py @@ -114,24 +114,59 @@ logger = logging.getLogger("member-portal") # sit on the access logger and on the root handler so both uvicorn's line and any # string the app itself logs get scrubbed. _WEBHOOK_TOKEN_RE = re.compile(r"(/webhook/opencollective/)[^\s\"'?&]+") +# M1 (audit 2026-11-05): member emails persist in portal logs — send_email +# recipients, listmonk failures, Loomio invite lists, provisioning and credit +# lines (17 sites). Container logs are readable via `cloudron logs`, persist +# on the host and ride the backups, so an email is redacted from every record +# the same way the webhook token is. Logs stay useful: counts, slugs, MXIDs +# (matrix @user:server has no address shape) and amounts all survive; only +# the address is dropped. The daily digest's own sanitisation is unchanged. +_EMAIL_RE = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+") -def _redact_webhook_path(value): +def _redact_emails(value): + if isinstance(value, str) and "@" in value: + return _EMAIL_RE.sub("", value) + return value + + +def _redact_log_value(value): if isinstance(value, str) and "/webhook/opencollective/" in value: - return _WEBHOOK_TOKEN_RE.sub(r"\1", value) + value = _WEBHOOK_TOKEN_RE.sub(r"\1", value) + return _redact_emails(value) + + +def _redact_deep(value): + """Redact every string in a log argument, preserving the argument's type.""" + if isinstance(value, str): + return _redact_log_value(value) + if isinstance(value, tuple): + return tuple(_redact_deep(v) for v in value) + if isinstance(value, list): + return [_redact_deep(v) for v in value] + if isinstance(value, dict): + return {k: _redact_deep(v) for k, v in value.items()} return value class _RedactWebhookPath(logging.Filter): - """Replace the webhook token in any logged request path with .""" + """Scrub secrets and member emails out of any log record. + + Replaces the webhook token in a logged request path (ops/security#2) and + redacts email-shaped strings from messages, %-format args (recursing into + tuple/list/dict args) and any exception text already attached to the + record. A logging filter must never break logging. + """ def filter(self, record: logging.LogRecord) -> bool: try: - record.msg = _redact_webhook_path(record.msg) + record.msg = _redact_log_value(record.msg) if isinstance(record.args, tuple): - record.args = tuple(_redact_webhook_path(a) for a in record.args) + record.args = tuple(_redact_deep(a) for a in record.args) elif isinstance(record.args, dict): - record.args = {k: _redact_webhook_path(v) for k, v in record.args.items()} + record.args = {k: _redact_deep(v) for k, v in record.args.items()} + if isinstance(record.exc_text, str): + record.exc_text = _redact_log_value(record.exc_text) except Exception: pass # a logging filter must never break logging return True diff --git a/tests/test_portal_hardening.py b/tests/test_portal_hardening.py index 5a638b7..5ca2db7 100644 --- a/tests/test_portal_hardening.py +++ b/tests/test_portal_hardening.py @@ -205,5 +205,61 @@ try: 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 (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 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: ['', '']", "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/?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", + "" in rec.getMessage() and "" 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 ") + +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)