@CryogenicPlanet2d ago
Reviewed. The diagnosis is correct and the original two protections are legit:
- Schema refine (
*/:rejection) — load-bearing, not cosmetic: permission rows flow verbatim into override keys (connected_account_tool_permissions.tsresolve), and the gate honors*:{ca}as an account-wide override (toolRouterV2/features/permissions/keys.ts,resolve.ts). Since the refine sits on the shared input schema, the consumer + dashboard upsert routes are covered too. validateEnhancedLinkPermissions— closes what the refine can't see on the only token-authenticated writer: well-formed slugs outside the session toolkit, outside the auth-config tool policy, or nonexistent.
Pushed 1f2fc7d on top to fix the ordering hazard. The original reorder (submit → persist) traded a documented-benign failure (orphan rows on a never-active account) for a worse one: if the upsert failed after a live submit, the connection committed ACTIVE with zero rows — and the resolver's legacy fallback treats a no-rows account as account-wide always_allow, silently disabling the gate. The commit makes submit + persist atomic instead:
- Threads an optional
Prisma.TransactionClientthroughsubmitLinkSessionInput→ all three branch status writes (updateNonRedirectable/updateRedirectable/executeS2STokenExchangeForExisting) →DB_CONNECTED_ACCOUNT_UPDATE.statusAndData, and intoDB_CONNECTED_ACCOUNT_TOOL_PERMISSIONS_UPSERT.manyForConnectedAccountNanoId(skips its own nested $transaction/serializable-retry when handed a caller-owned tx). All params optional — every existing call site unchanged. - Route wraps both in one
prisma.$transaction(submitWithPermissionsAtomically); rollback via a typed sentinel converted back to aResult. - S2S
persistFailedStatedeliberately stays on the global client so FAILED markers survive rollback. - Tx timeout 30s / maxWait 5s: the submit can hold external calls (token exchange, optional credential validation, ~10s fetch timeouts each) inside the tx window. Acceptable for this low-volume route, but it's the real cost of the design.