fix(integrations): prevent duplicate workspace creation on oauth resubmit#786
Merged
Merged
Conversation
…bmit The shared OAuth callback (Messenger/Instagram/TikTok/Zalo/...) created a new workspace whenever `state` carried no workspaceId, with no protection against the same OAuth attempt hitting the callback twice (slow provider round-trip + an impatient re-submit). Each hit created its own "New Workspace" and consumed a "workspaces" quota unit; only the first ever got a channel attached, leaving the rest as empty, quota-consuming orphans (loudly, via a "duplicate" error for TikTok/Zalo; silently, via cookie overwrite, for Messenger/Instagram). Add createWorkspaceForNewOauthState (apps/builder/src/lib/oauth-workspace.ts): locks and caches the created workspace by a hash of `state` + `userId` for 600s, so a repeat hit from the same user reuses the workspace instead of creating another one. The lock and the cached value use separate Redis keys, since sharing one key lets a cache write clobber the lock's own release bookkeeping. Falls back to creating fresh if a cached id points at a workspace deleted in the interim. Also fix workspace-switcher.tsx using `workspace.name` as a list key instead of `workspace.id` — duplicate "New Workspace" names (the default name for every fresh OAuth-created workspace) triggered React's duplicate-key warning.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
statecarries noworkspaceId, with no protection against the same OAuth attempt hitting the callback twice (slow provider round-trip + an impatient re-submit). Each hit created its own "New Workspace" and consumed a "workspaces" quota unit — only the first ever got a channel attached, leaving the rest as empty, quota-consuming orphans (visibly for TikTok/Zalo via a "duplicate" error, silently for Messenger/Instagram via pending-auth cookie overwrite).createWorkspaceForNewOauthState(apps/builder/src/lib/oauth-workspace.ts): locks and caches the created workspace by a hash ofstate+userIdfor 600s, so a repeat hit from the same user reuses the workspace instead of creating another one. The lock and the cached value use separate Redis keys — sharing one key would let the cache write clobber the lock's own release bookkeeping. Falls back to creating fresh if a cached id points at a workspace deleted in the interim.workspace-switcher.tsxusingworkspace.name(notworkspace.id) as a list key — duplicate "New Workspace" names (the default name for every fresh OAuth-created workspace) were triggering React's duplicate-key warning.Changes
apps/builder/src/app/integrations/[...integration]/callback.ts— delegate new-workspace creation on the shared OAuth callback to the deduping helper.apps/builder/src/lib/oauth-workspace.ts(new) — lock + cache the created workspace perstate+userId, with a graceful fallback if the cached workspace was deleted.apps/builder/src/components/workspace-switcher.tsx— key list items byworkspace.idinstead ofworkspace.name.Test plan
pnpm lintpnpm --filter builder check-types