From ae49c3eafcdf162a2f5aa04abf9906627890d149 Mon Sep 17 00:00:00 2001 From: Dante Date: Thu, 8 Oct 2026 18:18:49 +0000 Subject: [PATCH] FE-3298 test(cloud): pin the full-page SSO return to the served consent path (#20533) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary This PR picks up three follow-ups from Christian's review of #20468 (FE-3298, the SSO return to OAuth consent). It adds the browser coverage he asked for and fixes both of his notes on `ssoReturnPath.ts`. **Why:** the `/cloud/oauth/consent` redirect route now carries every SSO return to consent. Until now only an in-memory unit test guarded it. The bug in #20468 lived at the full-page-load boundary, and no test exercised that boundary. **How:** - **Browser case** in `cloudSsoSignIn.spec.ts`, built the way Christian outlined: - `/api/auth/sso/start` 302s to its `return_to`. - A document request for `/oauth/consent` gets the server's JSON 404. - The consent challenge is mocked. - Starting from `/cloud/login?oauth_request_id=…` with Continue with SSO, the test asserts the consent heading and the same `oauth_request_id`. No real IdP is involved. - **Trailing slash:** `toSsoReturnPath` now also maps `/oauth/consent/`, which also 404s on both hosts and which vue-router would match to the consent view. - **Coupling note:** the doc comment now names BE-4146, so this helper and the redirect route get removed together. No user-facing change, so there are no AS IS/TO BE screenshots. ## Verification - `ssoReturnPath.test.ts` covers five cases. The trailing-slash case fails on `main` and passes here. - SSO unit suites: 25 tests pass. - `typecheck:app` and `typecheck:browser` are clean. - CI runs the browser case (`@cloud`). Its target behaviour is already on `main`, so it is a regression guard and is not expected to go red on `main`. --- browser_tests/tests/cloudSsoSignIn.spec.ts | 78 +++++++++++++++++++++ src/platform/auth/sso/ssoReturnPath.test.ts | 35 +++++++++ src/platform/auth/sso/ssoReturnPath.ts | 7 +- 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 src/platform/auth/sso/ssoReturnPath.test.ts diff --git a/browser_tests/tests/cloudSsoSignIn.spec.ts b/browser_tests/tests/cloudSsoSignIn.spec.ts index 411e17be93c..b9ce16faad2 100644 --- a/browser_tests/tests/cloudSsoSignIn.spec.ts +++ b/browser_tests/tests/cloudSsoSignIn.spec.ts @@ -4,6 +4,7 @@ import type { Page, Request } from '@playwright/test' import type { ErrorResponse, SsoDiscoverResponse } from '@comfyorg/ingest-types' import enMessages from '@/locales/en/main.json' with { type: 'json' } +import type { OAuthConsentChallenge } from '@/platform/cloud/oauth/oauthApi' import type { operations } from '@/types/comfyRegistryTypes' import { @@ -28,6 +29,24 @@ const APP_ROOT = new RegExp( `^${APP_URL.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/?(\\?.*)?$` ) const SSO_COPY = enMessages.auth.sso +const OAUTH_REQUEST_ID = '550e8400-e29b-41d4-a716-446655440000' +const CONSENT_CHALLENGE: OAuthConsentChallenge = { + oauth_request_id: OAUTH_REQUEST_ID, + csrf_token: 'csrf-token', + client_display_name: 'Comfy Desktop', + resource_display_name: 'Comfy Cloud', + redirect_uri: 'http://127.0.0.1:50632/callback', + client_application_type: 'native', + scopes: ['comfy-cloud:user:read'], + workspaces: [ + { + id: 'personal-workspace', + name: 'Personal', + type: 'personal', + role: 'owner' + } + ] +} type CreateCustomerResponse = operations['createCustomer']['responses']['201']['content']['application/json'] @@ -177,6 +196,65 @@ test.describe('Cloud login SSO entry', { tag: ['@cloud', '@ui'] }, () => { expect(ssoStarts).toHaveLength(1) }) + test('an SSO sign-in for a pending OAuth request returns to a consent page the server serves', async ({ + page + }) => { + await answerDiscover(page, 200, SSO_DISCOVERED) + await page.route('**/api/auth/sso/start**', (route) => { + const returnTo = new URL(route.request().url()).searchParams.get( + 'return_to' + ) + // Playwright does not route requests that follow a redirect, so the + // IdP round trip ends in a fresh navigation to return_to instead. + const target = new URL(returnTo ?? '/', APP_URL).toString() + return route.fulfill({ + status: 200, + contentType: 'text/html', + body: `` + }) + }) + // The server serves the SPA as a page only at /cloud/oauth/consent and + // answers a document load of /oauth/consent with a JSON 404. + await page.route('**/oauth/consent**', async (route) => { + const request = route.request() + if (request.resourceType() !== 'document') return route.fallback() + const path = new URL(request.url()).pathname.replace(/\/+$/, '') + if (path === '/cloud/oauth/consent') { + const app = await route.fetch({ url: `${APP_URL}/` }) + return route.fulfill({ response: app }) + } + return route.fulfill({ + status: 404, + json: { code: 'NOT_FOUND', message: 'Not Found' } + }) + }) + await page.route('**/oauth/authorize?**', (route) => + route.request().resourceType() === 'document' + ? route.fallback() + : route.fulfill({ status: 200, json: CONSENT_CHALLENGE }) + ) + + await page.goto(`${APP_URL}/?oauth_request_id=${OAUTH_REQUEST_ID}`) + await expect(page).toHaveURL(/\/cloud\/login/) + await page.getByRole('button', { name: SSO_COPY.continueWithSso }).click() + await page.locator('#cloud-sso-email').fill(SSO_EMAIL) + await page + .getByRole('button', { name: SSO_COPY.submit, exact: true }) + .click() + + await expect( + page.getByRole('heading', { + name: enMessages.oauth.consent.title.replace( + '{client}', + CONSENT_CHALLENGE.client_display_name + ) + }) + ).toBeVisible() + expect(new URL(page.url()).searchParams.get('oauth_request_id')).toBe( + OAUTH_REQUEST_ID + ) + }) + test('an email that does not use SSO stays on the login page with the not-SSO message', async ({ page, ssoStarts diff --git a/src/platform/auth/sso/ssoReturnPath.test.ts b/src/platform/auth/sso/ssoReturnPath.test.ts new file mode 100644 index 00000000000..8b2df57e957 --- /dev/null +++ b/src/platform/auth/sso/ssoReturnPath.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from 'vitest' + +import { toSsoReturnPath } from '@/platform/auth/sso/ssoReturnPath' + +describe('toSsoReturnPath', () => { + it.for([ + { + name: 'the SPA-only consent path', + input: '/oauth/consent?oauth_request_id=req-1', + expected: '/cloud/oauth/consent?oauth_request_id=req-1' + }, + { + name: 'the consent path with a trailing slash', + input: '/oauth/consent/?oauth_request_id=req-1', + expected: '/cloud/oauth/consent?oauth_request_id=req-1' + }, + { + name: 'the served consent path', + input: '/cloud/oauth/consent?oauth_request_id=req-1', + expected: '/cloud/oauth/consent?oauth_request_id=req-1' + }, + { + name: 'any other app path', + input: '/workflows?id=7#top', + expected: '/workflows?id=7#top' + }, + { + name: 'a path that only starts like the consent path', + input: '/oauth/consent-help', + expected: '/oauth/consent-help' + } + ])('returns $expected for $name', ({ input, expected }) => { + expect(toSsoReturnPath(input)).toBe(expected) + }) +}) diff --git a/src/platform/auth/sso/ssoReturnPath.ts b/src/platform/auth/sso/ssoReturnPath.ts index e7435af334a..ba53e19d7ec 100644 --- a/src/platform/auth/sso/ssoReturnPath.ts +++ b/src/platform/auth/sso/ssoReturnPath.ts @@ -4,10 +4,13 @@ const SERVED_CONSENT_PATH = '/cloud/oauth/consent' /** * The path SSO's callback can land on with a full page load. The server * serves the consent page only at `/cloud/oauth/consent`, which the SPA then - * redirects; a direct `/oauth/consent` load gets a 404. + * redirects; a direct `/oauth/consent` load gets a 404. Remove together with + * that redirect route once BE-4146 serves `/oauth/consent`. */ export function toSsoReturnPath(fullPath: string): string { const url = new URL(fullPath, 'http://sso.invalid') - if (url.pathname !== SPA_ONLY_CONSENT_PATH) return fullPath + if (url.pathname.replace(/\/+$/, '') !== SPA_ONLY_CONSENT_PATH) { + return fullPath + } return `${SERVED_CONSENT_PATH}${url.search}${url.hash}` }