Repository navigation
Supersedes #22836: fix(payment): guard stripe webhook against null metadata and missing item price lookups - #24628
Conversation
…item price lookups
|
Verdict: the production change is correct, minimal, and behavior-preserving — formal sign-off stays with a human maintainer because this is the Stripe payment webhook. Credit to @Speedyzyzz for the original diagnosis and fix in #22836; both commits carry the original authorship, and the payment hunks here are verified line-for-line identical to the original, now carried onto current main. backend/routers/payment.py — all five hunks are sound null-safety rewrites. backend/tests/unit/test_stripe_webhook_null_metadata_crash.py — four deterministic repro tests (checkout.session.completed, customer.subscription.created, subscription_schedule.completed, each with Security: strictly reduces crash surface on /v1/payments/stripe/webhook — no change to signature verification, no new external calls, no billing-logic change. Blast radius: none for well-formed payloads; null-metadata events that previously 500'd (with Stripe retries and stale subscription state) now process or return the intended 400. USER IMPACT: paying users whose Stripe events carry null metadata get them processed instead of a server error; everyone else sees no difference. Non-blocking nice-to-have: the diff also guards Payment-path changes keep formal approval with the human maintainers — leaving this ready for maintainer sign-off before merge. by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
|
Verdict: the production change is correct, tightly scoped, and well-tested — nothing blocking from my side; final merge sign-off stays with a human maintainer because this is the Stripe billing webhook path. Verified on head de9bc73:
Security: no new data flows, webhook signature verification untouched; this strictly reduces crash/retry-loop surface on an authenticated Stripe callback. CodeQL, the backend unit suite, and both hermetic backend gates are green on this head. Supersession is clean — #22836 is closed and @Speedyzyzz's authorship is preserved on both commits. Performance: pure lookup-shape changes on an already-loaded dict — no added queries or per-call cost. USER IMPACT: payments arriving with null metadata no longer fail webhook processing — subscription state updates land instead of silently going stale while Stripe retries into 500s. Still wanted (non-blocking): a direct test for the new missing- Because this touches entitlement-affecting billing code, the final go should come from a human maintainer despite the clean review. by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
What this does, in plain terms
Omi's payment webhook (the endpoint Stripe calls whenever a checkout, subscription or schedule changes) reads event fields like
session['metadata']['app_id']to decide what to do with the payment. When Stripe sends"metadata": null— which its API does in realistic payloads — the code'ssession.get('metadata', {})idiom returnsNoneinstead of the intended empty dict (the key exists, with a null value), so the next.get(...)crashes withAttributeError: 'NoneType' object has no attribute 'get'. The webhook answers HTTP 500 and Stripe retries the delivery; meanwhile the event is never processed, so subscription state in Omi can go stale. This PR makes every such read null-safe ((x.get('metadata') or {})), guards the subscriptionitems.dataprice-id lookup, and adds four deterministic reproduction tests. For well-formed payloads nothing changes — this strictly reduces crash risk on the payment path.What this carries
Supersedes #22836 — "fix(payment): guard stripe webhook against null metadata and missing item price lookups" by @Speedyzyzz (Balavignesh). Both commits are cherry-picked with original authorship intact (he is the git author on both); the revive run added no commits of its own. The original sat reviewed-but-unmerged for 14 days while main moved underneath it (held-recovery work reshaped
backend/routers/payment.py); this branch carries it onto current main so it can run today's CI.The production change was already verified on the original PR by its maintainer review: "the core diagnosis is right … the
(x.get('metadata') or {})form is the correct fix and matches the pattern this same file already uses", all five hunks behavior-preserving,sub_itemsisinstance guard "only feeds the 'unknown price ID' warning log, not any billing decision". From its side the review found the PR ready; final merge sign-off stays with the human maintainer, as on the original.Credit: all of the code in this PR is @Speedyzyzz's (git author on both commits).
Product invariants affected
none
How it was verified (this branch, current main @ b9ef1e3)
origin/main+ the author's new test file):python -m pytest tests/unit/test_stripe_webhook_null_metadata_crash.py→ 4 failed in 1.67s — every test hits the unguarded chained lookup, exactly the crash the PR describes.test_stripe_webhook_null_metadata_crash.py,test_stripe_webhook_none_guard.py(desktop-backend: Stripe webhook 500s and stale URL 503s #7282, a different failure mode: FirestoreNotFound/builder-None, so no overlap),test_stripe_webhook_behavioral.py,test_stripe_event_ledger.py,test_stripe_subscription_terminal_status.py,test_billing_webhook_delivery.py,test_subscription_ingestion_characterization.py,test_delete_account_stripe_cancel.py,test_finops_stripe_book.py,test_finops_stripe_fees.py→ junittests=407 failures=0 errors=0 skipped=0(3.26s).backend/scripts/typecheck.sh(pyright) on this branch →3355 errors, 91503 warnings; plainorigin/maincontrol → identical3355 errors, and the path-normalized per-error diff is empty. Zero branch delta.bash scripts/backend-python-format --check backend/routers/payment.py backend/tests/unit/test_stripe_webhook_null_metadata_crash.py→ clean (2 files would be left unchanged).Tests
Four regression tests in
backend/tests/unit/test_stripe_webhook_null_metadata_crash.py(authored in #22836, carried unchanged):test_checkout_session_completed_handles_null_metadata_gracefully—metadata: Nonecheckout must not raise and returns{'status': 'success'}.test_customer_subscription_created_handles_null_metadata_gracefully— null-metadata subscription event returns the handler's realNo customer ID or UIDmessage.test_subscription_schedule_completed_handles_null_metadata_gracefully— null-metadata schedule completion proceeds without AttributeError.test_checkout_session_completed_with_client_reference_id_and_null_metadata— pins the fallback ordering:client_reference_idresolvesuidwhen metadata is null.UI evidence
UI-Evidence: none -- backend-only change
Failure class
Failure-Class: none
Line-Count-Exception: backend/routers/payment.py | 1980 -> 1982 | carried #22836: five null-metadata guard hunks for the Stripe webhook (net +2 lines), cherry-picked unchanged from the reviewed original
Supersedes #22836