Repository navigation
fix(auth): Firebase token fallback for remote origins #20122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Ashish-Soni08
wants to merge
3
commits into
Comfy-Org:main
Choose a base branch
from
Ashish-Soni08:pr-agent/issue-17271
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
fc3dfe7
test(auth): wait on the retry button and retrying assertion in the fo…
Ashish-Soni08 beef745
fix(auth): scope the workspace init cooldown to token requests and gu…
Ashish-Soni08 c530d00
fix(auth): only fall back to the Firebase token when workspace discov…
Ashish-Soni08 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
159 changes: 159 additions & 0 deletions
159
src/stores/__tests__/authTokenRemoteOriginFallback.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import { useApiKeyAuthStore } from '@/stores/apiKeyAuthStore' | ||
| import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' | ||
| import { useWorkspaceAuthStore } from '@/platform/workspace/stores/workspaceAuthStore' | ||
| import { fromPartial } from '@total-typescript/shoehorn' | ||
| import type { User } from 'firebase/auth' | ||
| import * as firebaseAuth from 'firebase/auth' | ||
| import type { Mock } from 'vitest' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { useAuthStore } from '@/stores/authStore' | ||
| import { stubFirebaseAuthHarness } from '@/utils/__tests__/stubAccountIdentityPort' | ||
|
|
||
| const { mockDistributionTypes } = vi.hoisted(() => ({ | ||
| mockDistributionTypes: { | ||
| isCloud: true, | ||
| isDesktop: true | ||
| } | ||
| })) | ||
|
|
||
| vi.mock(import('@/composables/useFeatureFlags')) | ||
|
|
||
| vi.mock(import('firebase/auth')) | ||
|
|
||
| vi.mock(import('@/platform/telemetry')) | ||
|
|
||
| vi.mock(import('@/services/dialogService')) | ||
|
|
||
| vi.mock(import('@/platform/distribution/types'), () => mockDistributionTypes) | ||
|
|
||
| type MockUser = Omit<User, 'getIdToken'> & { | ||
| getIdToken: Mock<User['getIdToken']> | ||
| } | ||
|
|
||
| /** | ||
| * Regression coverage for #17271: on a remote non-Cloud origin (e.g. a | ||
| * RunPod HTTPS proxy) the browser CORS-blocks GET cloud.comfy.org/api/workspaces, | ||
| * so teamWorkspaceStore.initialize() rejects and getWorkspaceAuthToken must | ||
| * still hand queue execution a usable credential — the Firebase ID token — | ||
| * instead of failing closed and 401ing the backend. | ||
| */ | ||
| describe('getWorkspaceAuthToken remote-origin workspace init failure', () => { | ||
| let store: ReturnType<typeof useAuthStore> | ||
| let authStateObservers: Array<(user: User | null) => void> | ||
|
|
||
| const mockUser = fromPartial<MockUser>({ | ||
| uid: 'test-user-id', | ||
| email: 'test@example.com', | ||
| getIdToken: vi.fn().mockResolvedValue('firebase-token') | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| mockDistributionTypes.isCloud = false | ||
| stubFirebaseAuthHarness() | ||
| authStateObservers = [] | ||
| vi.mocked(firebaseAuth.onAuthStateChanged).mockImplementation( | ||
| (_, callback) => { | ||
| const observer = callback as (user: User | null) => void | ||
| authStateObservers.push(observer) | ||
| observer(mockUser) | ||
| return vi.fn() | ||
| } | ||
| ) | ||
| store = useAuthStore() | ||
| useWorkspaceAuthStore().destroy() | ||
| Object.assign(useTeamWorkspaceStore(), { activeWorkspaceId: null }) | ||
| useTeamWorkspaceStore().initState = 'uninitialized' | ||
| vi.mocked(useTeamWorkspaceStore().initFailedAtDiscovery).mockReturnValue( | ||
| true | ||
| ) | ||
| vi.mocked(useWorkspaceAuthStore().ensureWorkspaceToken).mockResolvedValue( | ||
| null | ||
| ) | ||
| vi.mocked( | ||
| useTeamWorkspaceStore().resetForIdentityChange | ||
| ).mockImplementation(() => {}) | ||
| vi.mocked(useApiKeyAuthStore().getAuthHeader).mockReturnValue(null) | ||
| Object.assign(useApiKeyAuthStore(), { isAuthenticated: false }) | ||
| mockUser.getIdToken.mockClear() | ||
| mockUser.getIdToken.mockResolvedValue('firebase-token') | ||
| }) | ||
|
|
||
| it('falls back to the Firebase token when workspace init fails with a network error', async () => { | ||
| vi.mocked(useTeamWorkspaceStore().initialize).mockRejectedValue( | ||
| new Error('Network Error') | ||
| ) | ||
|
|
||
| await expect(store.getWorkspaceAuthToken()).resolves.toBe('firebase-token') | ||
| }) | ||
|
|
||
| it('asks the store to honor its failure cooldown', async () => { | ||
| vi.mocked(useTeamWorkspaceStore().initialize).mockRejectedValue( | ||
| new Error('Network Error') | ||
| ) | ||
|
|
||
| await store.getWorkspaceAuthToken() | ||
|
|
||
| expect(useTeamWorkspaceStore().initialize).toHaveBeenCalledWith({ | ||
| honorFailureCooldown: true | ||
| }) | ||
| }) | ||
|
|
||
| it('returns no credential when the account changes during workspace init', async () => { | ||
| const otherUser = fromPartial<MockUser>({ | ||
| uid: 'other-user-id', | ||
| email: 'other@example.com', | ||
| getIdToken: vi.fn().mockResolvedValue('other-firebase-token') | ||
| }) | ||
| vi.mocked(useTeamWorkspaceStore().initialize).mockImplementation( | ||
| async () => { | ||
| for (const observer of authStateObservers) observer(otherUser) | ||
| throw new Error('Network Error') | ||
| } | ||
| ) | ||
|
|
||
| await expect(store.getWorkspaceAuthToken()).resolves.toBeUndefined() | ||
| expect(otherUser.getIdToken).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('sends no credential when init fails after workspace discovery', async () => { | ||
| vi.mocked(useTeamWorkspaceStore().initialize).mockRejectedValue( | ||
| new Error('Token exchange failed') | ||
| ) | ||
| vi.mocked(useTeamWorkspaceStore().initFailedAtDiscovery).mockReturnValue( | ||
| false | ||
| ) | ||
|
|
||
| await expect(store.getWorkspaceAuthToken()).resolves.toBeUndefined() | ||
| expect(mockUser.getIdToken).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('returns no credential when the account changes during a successful init', async () => { | ||
| const otherUser = fromPartial<MockUser>({ | ||
| uid: 'other-user-id', | ||
| email: 'other@example.com', | ||
| getIdToken: vi.fn().mockResolvedValue('other-firebase-token') | ||
| }) | ||
| vi.mocked(useTeamWorkspaceStore().initialize).mockImplementation( | ||
| async () => { | ||
| for (const observer of authStateObservers) observer(otherUser) | ||
| Object.assign(useTeamWorkspaceStore(), { | ||
| activeWorkspaceId: 'other-workspace' | ||
| }) | ||
| } | ||
| ) | ||
|
|
||
| await expect(store.getWorkspaceAuthToken()).resolves.toBeUndefined() | ||
| expect(useWorkspaceAuthStore().ensureWorkspaceToken).not.toHaveBeenCalled() | ||
| expect(otherUser.getIdToken).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('still falls back when a previous boot left the store in the error state', async () => { | ||
| useTeamWorkspaceStore().initState = 'error' | ||
| vi.mocked(useTeamWorkspaceStore().initialize).mockRejectedValue( | ||
| new Error('Network Error') | ||
| ) | ||
|
|
||
| await expect(store.getWorkspaceAuthToken()).resolves.toBe('firebase-token') | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: The cooldown adds a second failure-state machine (
lastInitFailureAt,lastInitFailedAtDiscovery, an opt-in flag oninitialize) to a store that already tracksinitState,identityGenerationand in-flight promises. It's only needed because the fallback re-runs discovery on every submission. If reachability is fixed on the server side (see the review body), would we still need this? If we do keep a client stopgap, I'd keep it out ofteamWorkspaceStoreand let the token getter remember its own failure.