fix(auth): keep login redirects on current deployment (#39013)

Co-authored-by: CodingOnStar <hanxujiang@dify.com>
This commit is contained in:
Coding On Star
2026-07-15 09:48:58 +00:00
committed by GitHub
co-authored by CodingOnStar
parent 6a511da325
commit e5b7281eb9
13 changed files with 81 additions and 60 deletions
+8 -10
View File
@@ -244,41 +244,39 @@ describe('auth refresh route', () => {
expect(response.headers.get('location')).toBe('/signin?redirect_url=%2F')
})
it('should use the Cloud home as the fallback after a successful refresh', async () => {
it('should keep a Cloud staging fallback on the current deployment after refresh', async () => {
mocks.isCloudEdition = true
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(null, { status: 200 })))
const { GET } = await import('../route')
const response = await GET(
createRequest(
'https://cloud.dify.ai/auth/refresh?redirect_url=https%3A%2F%2Fevil.example',
'https://saas.dify.dev/auth/refresh?redirect_url=https%3A%2F%2Fevil.example',
'refresh_token=old-refresh',
),
)
expect(response.status).toBe(303)
expect(response.headers.get('location')).toBe('https://cloud.dify.ai/')
expect(response.headers.get('location')).toBe('/')
})
it('should carry the Cloud fallback through signin when refresh fails', async () => {
it('should carry the current Cloud deployment fallback through signin when refresh fails', async () => {
mocks.isCloudEdition = true
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(null, { status: 401 })))
const { GET } = await import('../route')
const response = await GET(
createRequest(
'https://cloud.dify.ai/auth/refresh?redirect_url=https%3A%2F%2Fevil.example',
'https://saas.dify.dev/auth/refresh?redirect_url=https%3A%2F%2Fevil.example',
'refresh_token=expired',
),
)
expect(response.status).toBe(303)
expect(response.headers.get('location')).toBe(
'/signin?redirect_url=https%3A%2F%2Fcloud.dify.ai%2F',
)
expect(response.headers.get('location')).toBe('/signin?redirect_url=%2F')
})
it('should use the Cloud home when a trusted absolute target loops back to auth refresh', async () => {
it('should use the current deployment home when a trusted target loops back to auth refresh', async () => {
mocks.isCloudEdition = true
vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(null, { status: 200 })))
const { GET } = await import('../route')
@@ -291,7 +289,7 @@ describe('auth refresh route', () => {
)
expect(response.status).toBe(303)
expect(response.headers.get('location')).toBe('https://cloud.dify.ai/')
expect(response.headers.get('location')).toBe('/')
})
it.each(['/auth/refresh/', '/auth/%72efresh'])(
+1 -2
View File
@@ -1,5 +1,4 @@
import type { LoginRedirectTarget } from '@/utils/login-redirect'
import { IS_CLOUD_EDITION } from '@/config'
import { resolveServerConsoleApiUrl } from '@/service/server'
import { getServerLoginFallback, resolveLoginRedirectTarget } from '@/utils/login-redirect'
import { basePath } from '@/utils/var'
@@ -38,7 +37,7 @@ const addBasePathToInternalTarget = (target: LoginRedirectTarget): LoginRedirect
const resolveSafeRedirectTarget = (request: Request): LoginRedirectTarget => {
const requestUrl = new URL(request.url)
const redirectUrl = requestUrl.searchParams.get('redirect_url')
const fallback = getServerLoginFallback(IS_CLOUD_EDITION, basePath)
const fallback = getServerLoginFallback(basePath)
if (!redirectUrl) return fallback