Skip to content

fix: stripe expand depth + WorkOS invite for static admin key - #4207

Merged
bokelley merged 2 commits into
mainfrom
bokelley/fix-stripe-expand-and-workos-invite
May 8, 2026
Merged

bokelley merged 2 commits into
mainfrom
bokelley/fix-stripe-expand-and-workos-invite

Conversation

@bokelley

@bokelley bokelley commented May 7, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes two production errors surfaced by Addie's system-error channel.

1. Stripe — getAllOpenInvoices exceeds 4-level expand cap

server/src/billing/stripe-client.ts passed expand: ['data.customer', 'data.lines.data.price.product'] on invoices.list. data.lines.data.price.product is 5 levels and Stripe caps expand at 4, so Stripe returned a 400 and the function silently swallowed it and returned []. Effect: every admin "all open invoices" call was empty.

Fix: drop the deep expand and resolve product names via stripe.products.retrieve with a per-call Map cache. Mirrors the existing pattern in getRevenueEvents (same file).

2. WorkOS — sendInvitation rejects synthetic admin_api_key user

POST /api/organizations/:orgId/members/by-email (Path 1: WorkOS user does not exist yet) passed inviterUserId: user.id. When the caller authenticates via the static ADMIN_API_KEY, user.id === 'admin_api_key' (synthetic — see server/src/middleware/auth.ts:745), and WorkOS responds User not found: 'admin_api_key'. The endpoint already special-cases the static-admin path for the membership lookup; it now also omits inviterUserId for that path. Audit attribution remains via the existing inviter_email field on the audit log entry. Unblocks the Triton incident-script flow (scripts/incidents/2026-04-triton-promote-hayem.ts).

Test plan

  • npx vitest run tests/billing/stripe-client.test.ts — added two regression tests:
    • Stripe stub that 400s on any expand path > 4 levels; asserts no path the function sends exceeds 4 levels.
    • End-to-end stub that yields invoices with string product ids; asserts product_name is populated via separate products.retrieve and that the cache prevents N+1.
  • DATABASE_URL=... npx vitest run server/tests/integration/member-by-email-policy.test.ts — added two regression tests:
    • Normal authenticated admin: inviterUserId is set to the caller's id.
    • Static admin API key path (req.user.id = 'admin_api_key', req.isStaticAdminApiKey = true): inviterUserId is undefined, all other fields unchanged.
  • tsc --project server/tsconfig.json --noEmit
  • Precommit (test:unit, test:test-dynamic-imports, test:callapi-state-change, typecheck)

🤖 Generated with Claude Code

bokelley and others added 2 commits May 7, 2026 17:57
- getAllOpenInvoices passed a 5-level expand path; Stripe caps it at 4,
  so every "all open invoices" call failed and returned []. Drop the
  deep expand and resolve product names via stripe.products.retrieve
  with a per-call cache, mirroring getRevenueEvents.
- POST /members/by-email Path 1 passed `inviterUserId: 'admin_api_key'`
  (the synthetic ADMIN_API_KEY user id) when the caller authed via the
  static API key, which WorkOS rejected with "User not found:
  'admin_api_key'". Omit inviterUserId for that auth path; audit log
  still records inviter_email.

Adds regression tests for both: a Stripe stub that 400s on >4-level
expand, and a by-email integration test that asserts inviterUserId is
omitted when isStaticAdminApiKey is set.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
getAllOpenInvoices wrapped its work in try/catch that returned [] on any
error, which is exactly what hid the original 4-level-expand bug as "No
pending invoices found" for admins. Drop the swallow; the sole caller
(list_pending_invoices admin handler) already has its own try/catch that
returns a structured `{ success: false, error }` response.

Pin tests harder against the original bugs:
- Stripe: split into two tests — one that asserts every expand path the
  function sends is <=4 levels, and a second that confirms a Stripe-side
  error reaches the caller via `rejects.toThrow`.
- by-email: assert `'inviterUserId' in args === false` rather than
  `=== undefined`, so a regression that re-introduces an explicit
  `inviterUserId: undefined` (or 'admin_api_key') fails.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley

bokelley commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

Issue #4208 proposes audit-log enrichment for every static-admin-gated mutation (req.ip, SHA-256 key fingerprint, X-Admin-Operator header) — same surface as this PR (auth.ts, isStaticAdminApiKey routes, scripts/incidents/*). Consider folding before merge or confirm as a follow-up. Cross-linked: #4208.


Generated by Claude Code

@bokelley
bokelley merged commit 9a38d2d into main May 8, 2026
13 checks passed
@bokelley
bokelley deleted the bokelley/fix-stripe-expand-and-workos-invite branch May 8, 2026 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant