Skip to content

Commit 2cb6d3c

Browse files
committed
fix(sso): keep a signed-in visitor going to the app when the launch address is rate limited
1 parent ab848f6 commit 2cb6d3c

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

apps/sim/app/(auth)/sso/launch/[providerId]/route.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ describe('GET /sso/launch/[providerId]', () => {
7575
expect(mockSignInSSO).not.toHaveBeenCalled()
7676
})
7777

78+
it('keeps sending a signed-in visitor to the app when the address is rate limited', async () => {
79+
mockGetSession.mockResolvedValue({ user: { id: 'user-1' } })
80+
mockEnforceIpRateLimit.mockResolvedValue(new Response(null, { status: 429 }))
81+
82+
const response = await open()
83+
84+
expect(response.headers.get('location')).toBe('https://test.sim.ai/home')
85+
expect(mockEnforceIpRateLimit).not.toHaveBeenCalled()
86+
})
87+
7888
it.each([
7989
['no issuer', '', () => undefined],
8090
[
@@ -105,8 +115,8 @@ describe('GET /sso/launch/[providerId]', () => {
105115
const response = await open()
106116

107117
expect(response.headers.get('location')).toBe(SIGN_IN_LINK)
108-
expect(mockGetSession).not.toHaveBeenCalled()
109118
expect(mockIsAllowed).not.toHaveBeenCalled()
119+
expect(mockSignInSSO).not.toHaveBeenCalled()
110120
})
111121

112122
it('leaves SSO off when the deployment has not enabled it', async () => {

apps/sim/app/(auth)/sso/launch/[providerId]/route.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ export const GET = withRouteHandler(async (request: NextRequest, context: RouteC
3030
).toString()
3131
if (!isSsoEnabled) return NextResponse.redirect(new URL('/login', getBaseUrl()).toString())
3232

33+
const session = await getSession()
34+
if (session?.user) {
35+
return NextResponse.redirect(new URL(DEFAULT_POST_AUTH_ROUTE, getBaseUrl()).toString())
36+
}
37+
38+
/** Admitted per address, after the session, so a busy shared address never strands a signed-in visitor. */
3339
const rateLimited = await enforceIpRateLimit('sso-launch', request, {
3440
maxTokens: 30,
3541
refillRate: 30,
3642
refillIntervalMs: 60_000,
3743
})
3844
if (rateLimited) return NextResponse.redirect(signInLink)
3945

40-
const session = await getSession()
41-
if (session?.user) {
42-
return NextResponse.redirect(new URL(DEFAULT_POST_AUTH_ROUTE, getBaseUrl()).toString())
43-
}
44-
4546
const issuer = request.nextUrl.searchParams.get('iss')
4647
if (!issuer || !(await isIdpInitiatedLoginAllowed(providerId, issuer))) {
4748
return NextResponse.redirect(signInLink)

0 commit comments

Comments
 (0)