GLM-5.3 · Balanced
oauth-credential-sync-and-app-integration-enhancements-cal-com
cal.com · TypeScript · M · 100–299 lines · 6 files
The normal review, with one generalist agent. Judge: Claude Haiku 5.5.
- Gold bugs found
- 4 of 9
- Comments published
- 11
- Comments matching a bug
- 4
- Comments not matching
- 7
- Cost of this PR
- $1.277
Published comments (11)
refreshAccessToken persists zod SafeParseReturnType wrapper instead of parsed tokens — `googleCredentialSchema.parse` throws on the next `GoogleCalendarService` construction because the persisted key is the `{success, data}` wrapper, not the tokens — permanently disabling Google Calendar after each user's first token refresh. Unwrap with `.data` before persisting, e.g. `parseRefreshTokenResponse(...).data`.
Credential-sync path returns raw fetch Response, breaking token-parsing callers — With `CALCOM_CREDENTIAL_SYNC_ENDPOINT` set, `refreshOAuthTokens` returns the raw `Response`: googlecalendar reads `res?.data` and silently keeps the expired token, hubspot persists the Response as the credential key, and zoho callers crash. Parse the endpoint's JSON into a normalized token object, or pass it through each app's parser.
Updated credentials never reset the invalid flag, leaving app disabled — Fresh keys never revive a credential marked `invalid` — `getCalendarsEvents.ts` filters those out, so a user whose app was disabled stays broken despite the sync, defeating the app store as source of truth. Add `invalid: false` to the `prisma.credential.update` data payload.
Token refresh checks unreliable statusText instead of response.ok — `statusText` is empty for HTTP/2 fetch responses and varies across 2xx codes, so a valid refresh can throw `HttpError` 400 with an empty message, breaking every Salesforce operation. Check `response.ok` instead.
msteams refresh via sync endpoint marks token expired, causing refresh loop — The sync endpoint's response has no `expires_in`, so `expiry_date` becomes `Date.now()` and `getToken()` re-runs the sync POST plus a credential write on every Teams call, gradually overwriting the key's `refresh_token`. Map the endpoint's expiry into the `expires_in` offset the caller expects.
Sync-config-driven schema strips instance_url from Salesforce token response — On sync-enabled instances the saved key loses `instance_url`, so the next `getClient` builds `jsforce.Connection` with `instanceUrl: undefined` and every Salesforce call hits the wrong host. `parseRefreshTokenResponse` picks `minimumTokenResponseSchema` from the global sync config; since this response comes straight from Salesforce, use `salesforceTokenSchema` here.
Computed schema keys collapse to one literal key, stripping token response fields — Both computed keys collapse to the single literal `"[object Object]"` and `z.object` strips unknown keys, so callers persist only `access_token` plus the placeholder `refresh_token: "refresh_token"` — office365calendar overwrites users' real refresh tokens, zoomvideo stores a NaN expiry. Use `z.object({ access_token: z.string() }).passthrough()` and stop forcing the placeholder.
Unconditional Salesforce token refresh + DB write on every getClient call — Services are constructed per operation via `getCalendar()`, so eager `getClient` adds a blocking `login.salesforce.com` round trip and DB write to each one — and the new token goes unused since `Connection` still gets stale `credentialKey.access_token`. Gate refresh on token expiry, or let jsforce's built-in lazy refresh handle it.
Webhook secret compared with non-constant-time !==, leaking timing info — Plain `!==` leaks match-length timing on this Internet-reachable endpoint, where the secret is the only authorization for overwriting any user's credentials. Compare SHA-256 digests of both values with `crypto.timingSafeEqual`.
refreshOAuthTokens called with credential row id instead of user id — Passing `credentialId` (the credential row id) as `refreshOAuthTokens`'s `userId` parameter makes the sync endpoint resolve the wrong user — Bigin refresh fails or returns another user's tokens; every other caller passes `credential.userId`. Do the same here.
Race condition in webhook creates duplicate credentials without unique constraint — Two concurrent deliveries both see `findFirst` return null and both `create`, since `schema.prisma` has only `@@index` — no unique constraint — on `Credential(userId, appId)`; later syncs update just one row, leaving a stale duplicate. Add `@@unique([userId, appId])` or upsert semantics.