Repository navigation
fix: offer to switch account when a link is for another organization, keep JSON for API clients - #9827
Closed
Kitenite wants to merge 1 commit into
Conversation
…ep JSON for API clients requireOrgMember, the Slack link route and the document file routes now answer a browser navigation with a page: sign in when there is no usable session, and /switch-account when the signed-in user is not in the link's organization. Requests without text/html in Accept, or with Authorization, x-api-key or x-trpc-source, get the JSON 401/403 they got before #9819. The sign-in page also says when a session expired for links that are not a connector. /switch-account stays on the legacy host during the domain move because it acts on that host's session cookie.
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
Contributor
🚀 Preview Deployment🔗 Preview Links
Preview updates automatically with new commits |
Member
Author
|
Superseded by #9823, which now covers every case here (no session, expired session, wrong account, API clients, file links, the Slack link) and is merged. |
This branch was previously deployed
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.
Follow-up to SUPER-3480. Stacked on #9823, which is stacked on #9819. This PR targets #9823's branch, so the diff shows only this change.
Why
#9819 and #9823 send a signed-out browser from a connect link to sign in. Three gaps were left:
{"error":"User is not a member of this organization"}403. This applies to all 8 connect routes that userequireOrgMember. The Slack link route had the same dead end, as plain text.requireOrgMemberredirect every request without a session, so a JSON client got a 302 to an HTML page instead of a 401./api/{projects,tasks}/…/files/…) showed a bareUnauthorized. The sign-in page explained an expired session only for connector links.What
apps/api/src/lib/browserAuth.tsgeneralizes fix(api): send a signed-out browser to sign in from a connect link #9819'ssignInUrl.signedOut(request, apiResponse)andwrongAccount(request, apiResponse)redirect a browser navigation and returnapiResponseto anything else. A browser navigation hastext/htmlin Accept (the same parser as the domain move, now exported asacceptsHtml) and noAuthorization,x-api-keyorx-trpc-sourceheader. The web page is still picked on the request's own root domain, andreason=session_expiredbehaves as before.requireOrgMember,/api/integrations/slack/linkand the two file routes use these helpers./switch-accountpage (web). It says "You are signed in as {email}. This account is not a member of the organization this link is for." It names the connector when the link is a connector connect link, reusing feat: guide signed-out, expired and wrong-account browsers from any API link #9823'sconnectSignInContext. Sign in with another account signs out and goes to/sign-in?redirect=<link>, so the flow continues after sign-in. A signed-out visitor goes straight to sign-in. The page does not show the organization's name, because the visitor is not a member.resolveCallbackUrl(web origin or the API and its own-domain twins), so it cannot be used for an open redirect.proxy-routes, and it is retained on the legacy host indomain-move. It must sign out the session on the root domain the API saw, so a move to paradev.com would act on the wrong cookie.reason=session_expirednow shows "Your Paradev session in this browser has expired. Sign in again to continue." instead of "Sign in to continue to Paradev".resolveCallbackUrlandconnectSignInContextmoved toapps/web/src/app/utils/, because the sign-in page and/switch-accountboth use them now. The files did not change.Audit: browser-navigable routes
"API client" means no
text/htmlAccept, or anAuthorization/x-api-key/x-trpc-sourceheader. "Before" is main + #9819 + #9823.GET /api/connectors/<slug>/connect/switch-accountGET /api/integrations/{linear,google,slack,notion,sentry,microsoft-teams}/connect,GET /api/github/install/switch-accountGET /api/integrations/slack/link/switch-accountGET /api/{projects,tasks}/<id>/files/<file>/api/connectors/callback/*,/api/integrations/*/callback,/api/github/callback)?error=…?error=unauthorizedon the web pageGET /api/github/user/callbackGET /api/auth/desktop/connectGET /auth/integration-connect(web, legacy)/switch-account/oauth/consent,/cli/auth/code, web pages (/connect/<slug>,/integrations/*,/plugins/*)/accept-invitation/<id>GET /api(agent discovery)WWW-Authenticatehint (left as is, for agents)Not changed: a missing
organizationIdstill returns 400 JSON. That is a malformed link, not a session problem.Verification
requireOrgMember.test.ts: new tests for wrong account (browser →/switch-account), member passes, and API clients (no Accept, JSON Accept,Authorization,x-trpc-source) getting 401/403 JSON for both no session and wrong account. 5 of them fail on feat: guide signed-out, expired and wrong-account browsers from any API link #9823's code (checked by restoring the old file) and pass now.apps/api: all pass except 3 Slack event tests (webidl.util.markAsUncloneable). These fail the same way on feat: guide signed-out, expired and wrong-account browsers from any API link #9823's branch without this change.apps/web127 pass,packages/shareddomain-move tests pass.bun run typecheckexit 0,bun run lint:fixclean, andbun run check:i18npasses.{"error":"User is not a member of this organization"}. After: "Switch account to connect Ahrefs. You are signed in as outsider-…@example.com…"app.ahrefs.com/web/oauth/authorizewith our client and signed state. The flow continues.curlchecks: a JSON client gets401 {"error":"Unauthorized"}. A browser Accept plusAuthorizationalso gets 401. Before this change, a JSON client got a 302.Summary by cubic
Connect links in a browser now lead to sign-in or, when the signed-in account isn't in the link's organization, a new "switch account" page; API clients keep their 401/403 JSON responses.
{"error":"User is not a member of this organization"}403.text/htmlin Accept, or withAuthorization,x-api-key, orx-trpc-source) instead of a 302 redirect.requireOrgMemberconnect routes, the Slack link route, and the project/task document file links, which were showing bareUnauthorizedtext./switch-accountstays on the legacy host during the domain move because it signs out that host's session cookie, and it is added as a public proxy route withredirectvalidated byresolveCallbackUrl.resolveCallbackUrlandconnectSignInContextmoved from the sign-in route into sharedapps/web/src/app/utils/.Written for commit 68e51cc. Summary will update on new commits.