MiniMax M3 · 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
9
Comments matching a bug
4
Comments not matching
5
Cost of this PR
$0.142

Published comments (9)

highbugpackages/app-store/_utils/oauth/parseRefreshTokenResponse.ts:5-11

parseRefreshTokenResponse schema uses computed keys instead of catchall — Computed keys become literal `'ZodString'` rather than a catch-all, so real provider responses with `refresh_token`/`expiry_date` fail validation and `parseRefreshTokenResponse` throws. Use `z.object({ access_token: z.string() }).catchall(z.number())` so arbitrary keys are accepted.

highbugpackages/app-store/_utils/oauth/refreshOAuthTokens.ts:3-20

refreshOAuthTokens returns inconsistent shapes across branches — Sync returns a `Response` while no-sync returns `refreshFunction`'s result, and `CalendarService.ts` consumers read `.data`/`.expiresIn` unconditionally, so toggling `APP_CREDENTIAL_SHARING_ENABLED` crashes refresh for those four integrations. Normalize both branches to the same shape before returning.

highbugapps/web/pages/api/webhook/app-credential.ts:62-91

Race condition in credential findFirst/update/create webhook handler — Two concurrent webhooks for the same `(userId, appSlug)` can both miss the lookup and race to `create`, producing duplicate or failed rows. Replace `findFirst`/`update`/`create` with a single `prisma.credential.upsert` keyed on the unique `(userId, appId)` constraint.

highbugapps/web/pages/api/webhook/app-credential.ts:25-26

Webhook secret check misses headers due to missing toLowerCase on key — Node lowercases incoming header keys, so a non-lowercase `CALCOM_WEBHOOK_HEADER_NAME` silently breaks verification and effectively disables the webhook. Normalize the lookup key via `.toLowerCase()` before reading `req.headers`.

highbugpackages/app-store/zoho-bigin/lib/CalendarService.ts:85-94

Pass user id, not credential id, to refreshOAuthTokens — Passing `credentialId` instead of `credential.userId` causes the admin sync endpoint to receive the credential row's id as `calcomUserId` and resolve the wrong principal (or reject). Pass `credential.userId` to the third argument of `refreshOAuthTokens`.

highbugpackages/app-store/salesforce/lib/CalendarService.ts:75-103

Salesforce refresh path returns Connection with stale access_token/instance_url — The returned `jsforce.Connection` still reads `accessToken`/`instanceUrl` from the stale `credentialKey`, so `this.conn` resolves to an expired connection while the DB holds fresh tokens. Pass `accessToken: accessTokenParsed.data.access_token` and `instanceUrl: accessTokenParsed.data.instance_url` when constructing the `Connection`.

highbugpackages/app-store/_utils/oauth/parseRefreshTokenResponse.ts:25-27

Refresh token fallback writes literal "refresh_token" instead of previous token — Set the fallback to `response.refresh_token` or omit the override.

mediumbugapps/web/pages/api/webhook/app-credential.ts:31-59

Webhook handler must not 500 on invalid JSON body or decryption failure — `safeParse` and `JSON.parse`/`symmetricDecrypt` throw on bad input and bubble up as Next.js 500s instead of structured errors. Switch to `safeParse` returning `res.status(400).json(...)`, and wrap `symmetricDecrypt`/`JSON.parse` to return the same 400 on failure.

mediumsecurityapps/web/pages/api/webhook/app-credential.ts:24-29

Webhook secret compared with !== is vulnerable to timing attacks — `!==` short-circuits on length and the first differing byte, letting attackers recover the secret character-by-character via request latency. Compare equal-length `Buffer`s with `crypto.timingSafeEqual`.

Gold bugs found (4)

mediumThe webhook parses the body with zod .parse and decrypts/JSON-parses the keys without error handling, so invalid payloads produce 500 errors instead of 400 responses.
highparseRefreshTokenResponse writes the hardcoded string 'refresh_token' when the refresh response omits a refresh token, so the stored credential gets an invalid refresh token and later refreshes fail.
mediumminimumTokenResponseSchema uses computed keys from z.string().toString(), which produce a literal key rather than a wildcard, so the sync-mode schema does not validate or keep expiry and other token fields as intended.
highAfter refreshing the Salesforce token, jsforce.Connection is created with the stale access_token and instance_url instead of the newly refreshed values.

Gold bugs missed (5)

highGoogle Calendar persists the safeParse result object instead of its data as the credential key, corrupting the stored credential so later reads fail.
mediumSalesforce getClient now refreshes the OAuth token and writes the credential on every service construction, adding a network round trip and DB write even when the access token is valid.
highThe new Salesforce token refresh uses prisma without importing it, so the code fails type checking and throws a ReferenceError at runtime.
highSalesforce checks response.statusText !== 'OK' instead of response.ok when refreshing the token, which can misclassify successful responses, and then ignores the refreshed token.
highThe app-credential webhook looks up appStoreMetadata by app.slug, but the map is keyed by dirName, so apps whose slug differs from their directory (e.g. google-calendar) always get a 404 and their credentials cannot be synced.