credits: make the credit-pack webhook grant idempotent (audit H1)
- reference built per-order from the webhook's order.idV2 (the id the amount lookup resolves) instead of the absent data.id that produced the legacy empty-suffix 'oc:<slug>:' format; legacy ledger rows are neither re-keyed nor rejected (a new-format reference can never match one) - credit_reference_exists() gates add_credits; a re-delivered order returns 'already_credited' with the standing balance - hold path: one hold per purchase via a pre-check plus a UNIQUE index on held_credits.oc_reference (index creation logs CRITICAL and continues if the live table ever carried duplicates — audited: none) - get_order_amount sends the webhook-supplied order id as a GraphQL variable instead of interpolating it into the query (audit L9) - regression test: replay credits exactly once; legacy rows never block a first grant; unique-index semantics incl. NULLs
This commit is contained in:
1 parent
48128ab97b
commit
e6ab177a05
2 files changed
+249
-5
No files matched your search
+95
-5
@@ -232,19 +232,24 @@ async def get_order_amount(order_id_v2: str | None) -> int | None:
|
||||
The webhook's `totalAmount` includes the buyer's optional OC tip, which
|
||||
the co-op never receives — credits must be granted on the tip-free
|
||||
amount. Returns cents, or None if unresolvable.
|
||||
|
||||
The order id arrives from the OC webhook (untrusted input), so it is sent
|
||||
as a GraphQL VARIABLE, never interpolated into the query string (audit L9,
|
||||
2026-11-05 — an interpolated id was a GraphQL injection into the
|
||||
unauthenticated OC API; the variables form is parameterized by the server).
|
||||
"""
|
||||
if not order_id_v2:
|
||||
return None
|
||||
if order_id_v2 in _order_amount_cache:
|
||||
return _order_amount_cache[order_id_v2]
|
||||
try:
|
||||
query = ('{ order(order: {id: "%s"}) { amount { valueInCents } '
|
||||
'platformTipAmount { valueInCents } } }' % order_id_v2)
|
||||
query = ("query OrderAmount($id: String!) { order(order: {id: $id}) "
|
||||
"{ amount { valueInCents } platformTipAmount { valueInCents } } }")
|
||||
async with httpx.AsyncClient(timeout=15.0) as client:
|
||||
r = await client.post(OC_GRAPHQL_URL,
|
||||
headers={"Content-Type": "application/json",
|
||||
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64)"},
|
||||
json={"query": query})
|
||||
json={"query": query, "variables": {"id": order_id_v2}})
|
||||
o = r.json().get("data", {}).get("order") or {}
|
||||
cents = (o.get("amount") or {}).get("valueInCents")
|
||||
if cents is not None:
|
||||
@@ -255,6 +260,33 @@ async def get_order_amount(order_id_v2: str | None) -> int | None:
|
||||
return None
|
||||
|
||||
|
||||
def credit_pack_reference(slug: str, order: dict | None) -> str:
|
||||
"""Stable idempotence key for a credit-pack purchase (audit H1, 2026-11-05).
|
||||
|
||||
Keyed on the OC order id from the webhook payload (`order.idV2` — the id
|
||||
`get_order_amount` resolves, so a VERIFIED purchase always carries one).
|
||||
The webhook's top-level `data.id` is absent from real deliveries; building
|
||||
the reference from it produced the legacy `oc:<slug>:` empty-suffix
|
||||
format, which cannot distinguish two purchases by the same member and so
|
||||
cannot dedupe a re-delivered event. (The shortfall path keys on the OC
|
||||
transaction publicId — equally unique; the webhook payload does not carry
|
||||
it, and per this module's contract the collective order list is the
|
||||
complete source for that shape, so no extra OC query is added here.)
|
||||
|
||||
Legacy ledger rows in the empty-suffix format are neither re-keyed nor
|
||||
rejected: a new-format reference (non-empty id suffix) can never match
|
||||
one, so the exists-check never blocks a first real grant, and the
|
||||
historical rows stay exactly as recorded.
|
||||
"""
|
||||
ref_id = str((order or {}).get("idV2") or "").strip()
|
||||
if not ref_id:
|
||||
raise ValueError(
|
||||
"credit pack: webhook payload carries no order idV2; refusing to "
|
||||
"build an ambiguous credit reference"
|
||||
)
|
||||
return f"oc:{slug}:{ref_id}"
|
||||
|
||||
|
||||
|
||||
# Personal token for the "Inference Co-op Bot" account, which is an admin of
|
||||
# the collective. Authenticated as an admin, the GraphQL API exposes member
|
||||
@@ -833,6 +865,20 @@ def get_db() -> sqlite3.Connection:
|
||||
"resolved_at TEXT"
|
||||
")"
|
||||
)
|
||||
# Unique oc_reference (audit H1, 2026-11-05): an OC re-delivery must never
|
||||
# create a second hold (or, via the webhook grant path's exists-check, a
|
||||
# second credit) for the same purchase. Holds recorded before the fix used
|
||||
# the legacy empty-suffix format and are distinct as audited, so the index
|
||||
# applies cleanly to the live table; multiple NULLs stay legal (SQLite).
|
||||
# If creation fails there are REAL duplicates — log CRITICAL and keep
|
||||
# serving unguarded rather than boot-looping the portal.
|
||||
try:
|
||||
conn.execute(
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS idx_held_credits_oc_reference "
|
||||
"ON held_credits(oc_reference)"
|
||||
)
|
||||
except sqlite3.OperationalError as e:
|
||||
logger.error("held_credits.oc_reference unique index NOT created: %s", e)
|
||||
|
||||
# Member-managed API keys: add a fingerprint (last-4 of the key, shown in
|
||||
# lists) and purge the stored plaintext. Member API keys are revealed ONCE
|
||||
@@ -2544,9 +2590,27 @@ async def opencollective_webhook(request: Request, token: str):
|
||||
"Credit pack: charged $%.2f including tip; granting tip-free $%.2f",
|
||||
charged, amount,
|
||||
)
|
||||
oc_ref = f"oc:{slug}:{data.get('id', '')}"
|
||||
oc_ref = credit_pack_reference(slug, order)
|
||||
|
||||
if email:
|
||||
# Idempotence (audit H1, 2026-11-05): OC re-delivers events and
|
||||
# this handler is slow (OC member fetch + email + Loomio +
|
||||
# Matrix inside the request), which invites re-delivery. This
|
||||
# purchase was already recorded once — return the standing
|
||||
# balance WITHOUT crediting again. The reference is per-order,
|
||||
# so this never blocks a new purchase, and legacy empty-suffix
|
||||
# ledger rows can never match a new-format reference.
|
||||
if credit_reference_exists(oc_ref):
|
||||
logger.info(
|
||||
"Credit pack: %s already in the ledger (OC re-delivery) "
|
||||
"— not crediting again", oc_ref,
|
||||
)
|
||||
return JSONResponse({
|
||||
"status": "already_credited", "email": email,
|
||||
"credits_added": 0.0,
|
||||
"credit_balance": get_credit_balance(email),
|
||||
"reference": oc_ref,
|
||||
})
|
||||
# Member purchase → credits land immediately.
|
||||
new_balance = add_credits(
|
||||
email, amount,
|
||||
@@ -2580,12 +2644,38 @@ async def opencollective_webhook(request: Request, token: str):
|
||||
logger.warning("Credit-pack hold: payer lookup failed for %s: %s", slug, e)
|
||||
|
||||
conn = get_db()
|
||||
# Idempotence (audit H1, 2026-11-05): one hold per purchase. The
|
||||
# unique index on oc_reference is the hard stop; this pre-check
|
||||
# returns a calm response on re-delivery instead of a 500 (which
|
||||
# would make OC re-deliver again).
|
||||
existing = conn.execute(
|
||||
"SELECT id FROM held_credits WHERE oc_reference = ?", (oc_ref,)
|
||||
).fetchone()
|
||||
if existing:
|
||||
conn.close()
|
||||
logger.info(
|
||||
"Credit pack HOLD: %s already recorded (hold #%s); not duplicating",
|
||||
oc_ref, existing[0],
|
||||
)
|
||||
return JSONResponse({
|
||||
"status": "hold_already_recorded", "slug": slug,
|
||||
"hold_id": existing[0],
|
||||
})
|
||||
conn.execute(
|
||||
"INSERT INTO held_credits (slug, email, name, amount, oc_reference) "
|
||||
"VALUES (?, ?, ?, ?, ?)",
|
||||
(slug, buyer_email or None, buyer_name, amount, oc_ref),
|
||||
)
|
||||
conn.commit()
|
||||
try:
|
||||
conn.commit()
|
||||
except sqlite3.IntegrityError:
|
||||
# Lost a race with a concurrent delivery of the same order;
|
||||
# the unique index kept us from double-holding.
|
||||
conn.close()
|
||||
logger.warning(
|
||||
"Credit pack HOLD: %s raced an existing hold; not duplicating", oc_ref,
|
||||
)
|
||||
return JSONResponse({"status": "hold_already_recorded", "slug": slug})
|
||||
conn.close()
|
||||
|
||||
logger.warning(
|
||||
|
||||
Reference in new issue
Block a user