privacy: redact member emails from portal logs (audit M1)

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.
This commit is contained in:
inference-bot committed 2026-11-05 16:17:21 +00:00
1 parent 2b501b75be
commit d63f702ec3
2 files changed
+97 -6

No files matched your search

+41 -6
View File
@@ -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("<email>", value)
return value
def _redact_log_value(value):
if isinstance(value, str) and "/webhook/opencollective/" in value:
return _WEBHOOK_TOKEN_RE.sub(r"\1<redacted>", value)
value = _WEBHOOK_TOKEN_RE.sub(r"\1<redacted>", 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 <redacted>."""
"""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