webhook: a first-time contributor's order must not vanish in silence
ci/woodpecker/push/woodpecker Pipeline was canceled

On 2026-11-13 21:59:43 Open Collective delivered order.processed for a live
$5/month membership (Conor W, conor.canaday@gmail.com, order
or_eU8gRkrPCvFsaYHrgiCfz). The portal answered 200 and did nothing.

The membership branch resolves the payer by SLUG against the collective's
members list:

    members = await fetch_members()
    member = next((m for m in members if m["slug"] == slug), None)

and a first-time contributor's account is not in that list yet — OC reports the
payer as guest-…/incognito-…. So `member` was None, the branch fell through, and
because the delivery had SUCCEEDED there was no error to record either: no
account, no welcome email, no credit, no log line. The sweep's unmapped-backers
check found it hours later, which is the wrong place to find it.

Now an unknown payer is resolved from the order's account slug
(`Individual.email`, which OC exposes for an ordinary guest checkout) and handed
to the same provisioning path a known member gets. When even that yields no
email — an incognito profile, hidden on purpose — the webhook logs at ERROR that
nobody was provisioned or credited, instead of shrugging.

The defect worth naming is the SHAPE: an `if` with no `else` on a money path,
where doing nothing leaves no trace. tests/test_resolve_payer.py pins the
resolution cases (guest with email, incognito, unknown account, read failure, no
token) and the fact that the branch can no longer fall through quietly.
This commit is contained in:
inference-admin committed 2026-11-13 22:12:02 +00:00
1 parent 27929987b2
commit 5a7380686c
2 files changed
+171

No files matched your search

+60
View File
@@ -4191,6 +4191,30 @@ async def _process_opencollective_webhook(request: Request, token: str):
# calendar (all members landing on Oct 1 otherwise). # calendar (all members landing on Oct 1 otherwise).
members = await fetch_members() members = await fetch_members()
member = next((m for m in members if m["slug"] == slug), None) member = next((m for m in members if m["slug"] == slug), None)
if not member and slug:
# A FIRST-TIME contributor's OC profile is usually not in the
# collective's members list yet when their first order lands (OC
# reports the payer as guest-…/incognito-…). The slug lookup then
# finds nothing, and this branch used to fall straight through —
# SILENTLY. The webhook answered 200, nobody was provisioned,
# nothing was credited and nothing was logged. On 2026-11-13 that
# cost conor.canaday@gmail.com his account and his welcome email
# on a live $5/month membership; only the sweep's
# unmapped-backers check noticed, hours later.
payer = await fetch_account_by_slug(slug)
email = (payer or {}).get("email")
if email:
member = {"email": email,
"name": (payer or {}).get("name") or email.split("@")[0],
"slug": slug}
logger.warning(
"Webhook: membership order from %s is not in the OC members "
"list yet; resolved it to %s by account email", slug, email)
else:
logger.error(
"Webhook: membership order from %s could not be resolved to an "
"email (incognito profile); NOBODY was provisioned or credited — "
"the sweep's unmapped-backers check must report this", slug)
if member: if member:
# A contribution may arrive under a DIFFERENT email than the # A contribution may arrive under a DIFFERENT email than the
# portal account (a invited friend who joined under another OC # portal account (a invited friend who joined under another OC
@@ -7369,6 +7393,42 @@ def payment_reminder_email(name: str, contribute_url: str) -> tuple[str, str, st
return subject, text, html return subject, text, html
async def fetch_account_by_slug(slug: str) -> dict | None:
"""The Open Collective account behind a payer slug, with its email if OC gives it.
Orders report their payer as `fromAccount { slug }`. For a FIRST-TIME
contributor that account is often not in the collective's members list yet
(OC reports the payer as `guest-…`/`incognito-…`), which is exactly the case
this exists for: it is the only way to learn who paid, and therefore the only
way to provision them.
OC exposes `email` on the Individual fragment — visible for an ordinary guest
checkout, null for an incognito profile (the contributor hid it on purpose).
Returns None when the account cannot be read; NEVER raises, because its
caller must be able to say "could not resolve" rather than die mid-webhook.
"""
slug = (slug or "").strip()
if not slug or not OC_PERSONAL_TOKEN:
return None
query = ("query($slug: String!) { account(slug: $slug) { __typename id name slug "
"... on Individual { email isIncognito } } }")
try:
async with httpx.AsyncClient(timeout=30.0) as client:
data = await _oc_graphql_post(client, {"query": query, "variables": {"slug": slug}})
except Exception as e: # read failure: report, never raise
logger.warning("OC account lookup failed for slug %s: %s", slug, e)
return None
acct = ((data or {}).get("data") or {}).get("account") or None
if not acct:
return None
if acct.get("isIncognito"):
# Hidden on purpose: there is no email to provision from, and inventing
# one from the slug would be worse than saying so.
return {**acct, "email": None}
acct["email"] = (acct.get("email") or "").strip().lower() or None
return acct
async def fetch_member_index() -> dict[str, dict]: async def fetch_member_index() -> dict[str, dict]:
"""email -> {slug, joined, role, tier} from the live OC member list. """email -> {slug, joined, role, tier} from the live OC member list.
+111
View File
@@ -0,0 +1,111 @@
#!/usr/bin/env python3
"""Regression test: a first-time contributor's order must not be dropped in silence.
WHY THIS EXISTS
On 2026-11-13 21:59:43 OC delivered `order.processed` for a live $5/month
membership (Conor W, conor.canaday@gmail.com, order or_eU8gRkrPCvFsaYHrgiCfz).
The portal answered 200 and did nothing: the membership branch resolves the
payer with
members = await fetch_members()
member = next((m for m in members if m["slug"] == slug), None)
and for a FIRST-TIME contributor the payer's OC account is not in the
collective's members list yet — OC reports them as guest-…/incognito-…. So
`member` was None, the whole branch fell through, and because the payload had
arrived successfully there was no error to record either. No account, no
welcome email, no credit, no log line. The sweep's unmapped-backers check
found it hours later, which is the wrong place to find it.
The fix resolves the payer from the order's account slug (`Individual.email`,
which OC exposes for an ordinary guest checkout), and when even that yields no
email it says so at ERROR level. A payment we cannot attribute is news, never
a shrug.
The "silent no-op" shape is the actual defect here: an `if` with no `else` on a
money path, where the absence of an action leaves no trace.
"""
import asyncio
import os
import pathlib
import sys
import tempfile
ROOT = pathlib.Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "payer-fixture.db")
import app.main as m # noqa: E402
m.DB_PATH = os.environ["DB_PATH"]
m.OC_PERSONAL_TOKEN = "test-token"
FAILS = []
def check(cond, msg):
print((" ok " if cond else " FAIL ") + msg)
if not cond:
FAILS.append(msg)
REAL_POST = m._oc_graphql_post
def fake_post(payload):
"""Stand in for OC's GraphQL endpoint."""
async def _post(client, json_body, headers=None, attempts=2):
return payload
return _post
print("1. an ordinary guest checkout resolves to the payer's email")
m._oc_graphql_post = fake_post({"data": {"account": {
"__typename": "Individual", "id": "acc_x", "name": "Conor W",
"slug": "guest-71e9d110", "email": "Conor.Canaday@Gmail.com", "isIncognito": False}}})
acct = asyncio.run(m.fetch_account_by_slug("guest-71e9d110"))
check(acct and acct["email"] == "conor.canaday@gmail.com",
f"the email is returned and normalised (got {acct and acct.get('email')!r})")
check(acct and acct["name"] == "Conor W", "the name comes back too, for the welcome email")
print("2. an incognito profile yields NO email — and says so rather than guessing")
m._oc_graphql_post = fake_post({"data": {"account": {
"__typename": "Individual", "id": "acc_y", "name": "Incognito",
"slug": "incognito-5ad60182", "email": None, "isIncognito": True}}})
acct = asyncio.run(m.fetch_account_by_slug("incognito-5ad60182"))
check(acct is not None and acct.get("email") is None,
"incognito: the account is returned, the email is None (a slug is not an address)")
print("3. failures and edge cases never raise — the caller must be able to report")
m._oc_graphql_post = fake_post({"data": {"account": None}})
check(asyncio.run(m.fetch_account_by_slug("guest-nope")) is None,
"an account OC does not know returns None")
m._oc_graphql_post = fake_post({})
check(asyncio.run(m.fetch_account_by_slug("guest-nope")) is None,
"an empty payload returns None")
def boom(client, json_body, headers=None, attempts=2):
raise RuntimeError("OC read timed out")
m._oc_graphql_post = boom
check(asyncio.run(m.fetch_account_by_slug("guest-x")) is None,
"a failing lookup returns None instead of dying mid-webhook")
check(asyncio.run(m.fetch_account_by_slug("")) is None, "an empty slug is not looked up")
m.OC_PERSONAL_TOKEN = ""
check(asyncio.run(m.fetch_account_by_slug("guest-x")) is None,
"with no OC token configured there is no lookup to make")
m.OC_PERSONAL_TOKEN = "test-token"
print("4. the webhook branch: the fix is present, and its silent-fallthrough shape is gone")
src = (ROOT / "app" / "main.py").read_text()
branch = src[src.index('if slug and "membership" in tier_name.lower()'):]
branch = branch[:branch.index("elif slug and first_payment")]
check("fetch_account_by_slug(slug)" in branch,
"the branch resolves an unknown payer by account email")
check("logger.error(" in branch and "NOBODY was provisioned or credited" in branch,
"an unresolvable payer is logged at ERROR — not swallowed")
check(branch.count("if member:") == 1 and "logger.warning(" in branch,
"a resolved-by-email payer is announced in the log")
m._oc_graphql_post = REAL_POST
print()
if FAILS:
print(f"resolve payer: {len(FAILS)} FAILURE(S)")
sys.exit(1)
print("resolve payer: OK — first-time payers resolve, unresolvable ones are loud")