From 7410ae7fe9ffdb5ea9aa9c59252a4c3d17ab7b56 Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 05:41:42 +0000 Subject: [PATCH 1/2] fix(config): retain base URLs across refresh failures Follow-up to https://github.com/Comfy-Org/ComfyUI_frontend/pull/15658#discussion_r3857363581 --- .../remoteConfig/refreshRemoteConfig.test.ts | 37 +++++++++++++++++-- .../remoteConfig/refreshRemoteConfig.ts | 15 +++++--- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/platform/remoteConfig/refreshRemoteConfig.test.ts b/src/platform/remoteConfig/refreshRemoteConfig.test.ts index 456c91b09f0..cc0cd4dd603 100644 --- a/src/platform/remoteConfig/refreshRemoteConfig.test.ts +++ b/src/platform/remoteConfig/refreshRemoteConfig.test.ts @@ -2,7 +2,10 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { api } from '@/scripts/api' -import { refreshRemoteConfig } from './refreshRemoteConfig' +import { + invalidateRemoteConfig, + refreshRemoteConfig +} from './refreshRemoteConfig' import { cachedLegacyBillingMigrationEnabled, remoteConfig, @@ -47,6 +50,26 @@ describe('refreshRemoteConfig', () => { window.__CONFIG__ = {} }) + it('retains base URLs while invalidating identity-specific config', () => { + remoteConfig.value = { + subscription_required: true, + comfy_api_base_url: 'https://api.example.com', + comfy_cloud_base_url: 'https://cloud.example.com', + comfy_platform_base_url: 'https://platform.example.com' + } + window.__CONFIG__ = remoteConfig.value + + invalidateRemoteConfig() + + expect(remoteConfig.value).toEqual({ + comfy_api_base_url: 'https://api.example.com', + comfy_cloud_base_url: 'https://cloud.example.com', + comfy_platform_base_url: 'https://platform.example.com' + }) + expect(window.__CONFIG__).toEqual(remoteConfig.value) + expect(remoteConfigState.value).toBe('unloaded') + }) + describe('with auth (default)', () => { it('uses api.fetchApi when useAuth is true', async () => { vi.mocked(api.fetchApi).mockResolvedValue(mockSuccessResponse()) @@ -214,14 +237,20 @@ describe('refreshRemoteConfig', () => { expect(window.__CONFIG__).toEqual({}) }) - it('clears config on fetch error', async () => { + it('preserves config on fetch error', async () => { + const existingConfig = { + subscription_required: true, + comfy_cloud_base_url: 'https://cloud.example.com' + } cachedLegacyBillingMigrationEnabled.value = true + remoteConfig.value = existingConfig + window.__CONFIG__ = existingConfig vi.mocked(api.fetchApi).mockRejectedValue(new Error('Network error')) await refreshRemoteConfig() - expect(remoteConfig.value).toEqual({}) - expect(window.__CONFIG__).toEqual({}) + expect(remoteConfig.value).toEqual(existingConfig) + expect(window.__CONFIG__).toEqual(existingConfig) expect(cachedLegacyBillingMigrationEnabled.value).toBeUndefined() }) diff --git a/src/platform/remoteConfig/refreshRemoteConfig.ts b/src/platform/remoteConfig/refreshRemoteConfig.ts index 9cfeece9b3b..c77d8364f3e 100644 --- a/src/platform/remoteConfig/refreshRemoteConfig.ts +++ b/src/platform/remoteConfig/refreshRemoteConfig.ts @@ -9,7 +9,7 @@ import { // Cap the bootstrap fetch so a wedged /features endpoint can never block app.mount indefinitely. // A same-origin GET against the local comfyui server should resolve in well under a second; -// on timeout the catch below clears remoteConfig and consumers fall back to build-time defaults. +// on timeout consumers retain the last known config or use build-time defaults. const FEATURES_FETCH_TIMEOUT_MS = 5_000 interface RefreshRemoteConfigOptions { @@ -28,8 +28,15 @@ export function invalidateRemoteConfig(): void { refreshGeneration++ for (const controller of activeRefreshControllers) controller.abort() activeRefreshControllers.clear() - window.__CONFIG__ = {} - remoteConfig.value = {} + const { comfy_api_base_url, comfy_cloud_base_url, comfy_platform_base_url } = + remoteConfig.value + const retainedConfig = { + ...(comfy_api_base_url && { comfy_api_base_url }), + ...(comfy_cloud_base_url && { comfy_cloud_base_url }), + ...(comfy_platform_base_url && { comfy_platform_base_url }) + } + window.__CONFIG__ = retainedConfig + remoteConfig.value = retainedConfig remoteConfigErrorStatus.value = null remoteConfigState.value = 'unloaded' cachedLegacyBillingMigrationEnabled.value = undefined @@ -110,8 +117,6 @@ export async function refreshRemoteConfig( if (generation !== refreshGeneration) return if (signal?.aborted) return console.error('Failed to fetch remote config:', error) - window.__CONFIG__ = {} - remoteConfig.value = {} remoteConfigErrorStatus.value = null if (useAuth) cachedLegacyBillingMigrationEnabled.value = undefined remoteConfigState.value = 'error' From bfcfe99cd74f454c768b4e402a0105c19970c53d Mon Sep 17 00:00:00 2001 From: bymyself Date: Wed, 2 Sep 2026 05:43:39 +0000 Subject: [PATCH 2/2] fix(config): validate remote base URLs Follow-up to https://github.com/Comfy-Org/ComfyUI_frontend/pull/15658#discussion_r3857363592 and https://github.com/Comfy-Org/ComfyUI_frontend/pull/15658#discussion_r3857363598 --- src/config/comfyApi.test.ts | 147 ++++++++++++++++-------------------- src/config/comfyApi.ts | 36 +++++---- 2 files changed, 86 insertions(+), 97 deletions(-) diff --git a/src/config/comfyApi.test.ts b/src/config/comfyApi.test.ts index 787ea1d31a0..6ea673778b6 100644 --- a/src/config/comfyApi.test.ts +++ b/src/config/comfyApi.test.ts @@ -16,89 +16,72 @@ vi.mock('@/scripts/api', () => ({ } })) -describe('getComfyApiBaseUrl', () => { - const originalConfig = remoteConfig.value - - beforeEach(() => { - remoteConfig.value = {} - }) - - afterEach(() => { - remoteConfig.value = originalConfig - }) - - it('honors the server-provided override', () => { - remoteConfig.value = { comfy_api_base_url: 'https://my-ephem.example.com' } - expect(getComfyApiBaseUrl()).toBe('https://my-ephem.example.com') - }) - - it('falls back to the build-time default when the key is absent', () => { - expect(getComfyApiBaseUrl()).toBe('https://stagingapi.comfy.org') - }) - - it('falls back to the build-time default when the value is empty', () => { - remoteConfig.value = { comfy_api_base_url: '' } - expect(getComfyApiBaseUrl()).toBe('https://stagingapi.comfy.org') - }) -}) - -describe('getComfyCloudBaseUrl', () => { - const originalConfig = remoteConfig.value - - beforeEach(() => { - remoteConfig.value = {} - }) - - afterEach(() => { - remoteConfig.value = originalConfig - }) - - it('honors the server-provided override', () => { - remoteConfig.value = { - comfy_cloud_base_url: 'https://my-ephem-cloud.example.com' - } - expect(getComfyCloudBaseUrl()).toBe('https://my-ephem-cloud.example.com') - }) - - it('falls back to the build-time default when the key is absent', () => { - expect(getComfyCloudBaseUrl()).toBe('https://testcloud.comfy.org') - }) - - it('falls back to the build-time default when the value is empty', () => { - remoteConfig.value = { comfy_cloud_base_url: '' } - expect(getComfyCloudBaseUrl()).toBe('https://testcloud.comfy.org') - }) -}) - -describe('getComfyPlatformBaseUrl', () => { - const originalConfig = remoteConfig.value - - beforeEach(() => { - remoteConfig.value = {} - }) - - afterEach(() => { - remoteConfig.value = originalConfig - }) - - it('honors the server-provided override', () => { - remoteConfig.value = { - comfy_platform_base_url: 'https://my-ephem-platform.example.com' - } - expect(getComfyPlatformBaseUrl()).toBe( - 'https://my-ephem-platform.example.com' +interface BaseUrlCase { + label: string + getBaseUrl: () => string + setOverride: (value: string) => void + defaultUrl: string +} + +const baseUrlCases: BaseUrlCase[] = [ + { + label: 'API', + getBaseUrl: getComfyApiBaseUrl, + setOverride: (value) => { + remoteConfig.value = { comfy_api_base_url: value } + }, + defaultUrl: 'https://stagingapi.comfy.org' + }, + { + label: 'Cloud', + getBaseUrl: getComfyCloudBaseUrl, + setOverride: (value) => { + remoteConfig.value = { comfy_cloud_base_url: value } + }, + defaultUrl: 'https://testcloud.comfy.org' + }, + { + label: 'Platform', + getBaseUrl: getComfyPlatformBaseUrl, + setOverride: (value) => { + remoteConfig.value = { comfy_platform_base_url: value } + }, + defaultUrl: 'https://stagingplatform.comfy.org' + } +] + +describe.for(baseUrlCases)( + '$label base URL', + ({ getBaseUrl, setOverride, defaultUrl }) => { + const originalConfig = remoteConfig.value + + beforeEach(() => { + remoteConfig.value = {} + }) + + afterEach(() => { + remoteConfig.value = originalConfig + }) + + it('honors a server-provided HTTPS override', () => { + setOverride('https://custom.example.com') + expect(getBaseUrl()).toBe('https://custom.example.com') + }) + + it('removes a trailing slash from the override', () => { + setOverride('https://custom.example.com/') + expect(getBaseUrl()).toBe('https://custom.example.com') + }) + + it.for([undefined, '', 'not-a-url', 'http://custom.example.com'])( + 'falls back to the build-time default for %s', + (override) => { + if (override !== undefined) setOverride(override) + expect(getBaseUrl()).toBe(defaultUrl) + } ) - }) - - it('falls back to the build-time default when the key is absent', () => { - expect(getComfyPlatformBaseUrl()).toBe('https://stagingplatform.comfy.org') - }) - - it('falls back to the build-time default when the value is empty', () => { - remoteConfig.value = { comfy_platform_base_url: '' } - expect(getComfyPlatformBaseUrl()).toBe('https://stagingplatform.comfy.org') - }) -}) + } +) describe('compatibility with comfyui servers that predate the override keys', () => { const originalConfig = remoteConfig.value diff --git a/src/config/comfyApi.ts b/src/config/comfyApi.ts index dca7a491f30..4daeca75cac 100644 --- a/src/config/comfyApi.ts +++ b/src/config/comfyApi.ts @@ -25,26 +25,32 @@ const BUILD_TIME_PLATFORM_BASE_URL = __USE_PROD_CONFIG__ : (import.meta.env.VITE_STAGING_PLATFORM_BASE_URL ?? STAGING_PLATFORM_BASE_URL) +function resolveBaseUrl( + key: + | 'comfy_api_base_url' + | 'comfy_cloud_base_url' + | 'comfy_platform_base_url', + defaultValue: string +): string { + const value = configValueOrDefault(remoteConfig.value, key, defaultValue) + + try { + const url = new URL(value) + if (url.protocol !== 'https:' || !url.hostname) return defaultValue + return url.href.replace(/\/$/, '') + } catch { + return defaultValue + } +} + export function getComfyApiBaseUrl(): string { - return configValueOrDefault( - remoteConfig.value, - 'comfy_api_base_url', - BUILD_TIME_API_BASE_URL - ) + return resolveBaseUrl('comfy_api_base_url', BUILD_TIME_API_BASE_URL) } export function getComfyCloudBaseUrl(): string { - return configValueOrDefault( - remoteConfig.value, - 'comfy_cloud_base_url', - BUILD_TIME_CLOUD_BASE_URL - ) + return resolveBaseUrl('comfy_cloud_base_url', BUILD_TIME_CLOUD_BASE_URL) } export function getComfyPlatformBaseUrl(): string { - return configValueOrDefault( - remoteConfig.value, - 'comfy_platform_base_url', - BUILD_TIME_PLATFORM_BASE_URL - ) + return resolveBaseUrl('comfy_platform_base_url', BUILD_TIME_PLATFORM_BASE_URL) }