From 62b84fab26b4601ca7ad1587c8aa1213a9de1195 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Wed, 14 Oct 2026 12:53:27 -0400 Subject: [PATCH 1/2] test(billing): token-session capabilities resolve for the Comfy user, not the Firebase uid The billing SDK on the token session scopes itself by the Firebase uid, but /billing/capabilities answers resolved_for.user_id with the Comfy user id the Cloud JWT names. For a linked identity the two differ and the capabilities reader drops the 200 as out of scope. FE-3440 --- .../sdk/billingSdkStore.tokenSession.test.ts | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/platform/workspace/billing/sdk/billingSdkStore.tokenSession.test.ts diff --git a/src/platform/workspace/billing/sdk/billingSdkStore.tokenSession.test.ts b/src/platform/workspace/billing/sdk/billingSdkStore.tokenSession.test.ts new file mode 100644 index 00000000000..c8937c01832 --- /dev/null +++ b/src/platform/workspace/billing/sdk/billingSdkStore.tokenSession.test.ts @@ -0,0 +1,104 @@ +import type { + AccountCredential, + SessionSnapshot +} from '@comfyorg/account-core/session' +import { fromPartial } from '@total-typescript/shoehorn' +import type { User } from 'firebase/auth' +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { useBillingContext } from '@/composables/billing/useBillingContext' +import { useWorkspaceAuthStore } from '@/platform/workspace/stores/workspaceAuthStore' +import { stubAccountIdentityPort } from '@/utils/__tests__/stubAccountIdentityPort' + +import { useBillingSdkStore } from './billingSdkStore' + +vi.mock(import('@/composables/billing/useBillingContext')) + +vi.mock(import('@/platform/workspace/composables/useBillingCapabilities')) + +vi.mock(import('@/platform/settings/composables/useSettingsDialog')) + +vi.mock(import('@/platform/telemetry')) + +vi.mock(import('@/composables/useFeatureFlags')) + +vi.mock(import('@/platform/distribution/types'), () => ({ isCloud: true })) + +vi.mock(import('firebase/auth')) + +const FIREBASE_UID = 'firebase-uid' +const COMFY_USER_ID = 'comfy-user-id' + +function cloudJwtFor(subject: string): string { + const encode = (value: object) => + btoa(JSON.stringify(value)).replace(/=+$/, '') + return `${encode({ alg: 'RS256' })}.${encode({ sub: subject })}.signature` +} + +const CREDENTIAL: AccountCredential = { + token: cloudJwtFor(COMFY_USER_ID), + expiresAt: Date.now() + 60 * 60 * 1000, + uid: FIREBASE_UID, + workspace: { id: 'ws-1', name: 'Personal', type: 'personal' }, + role: 'owner', + permissions: [] +} + +const SNAPSHOT: SessionSnapshot = { + phase: 'authenticated', + user: fromPartial({ uid: FIREBASE_UID }), + session: CREDENTIAL +} + +const CAPABILITIES = { + capabilities: { + can_cancel: false, + can_change_seats: false, + can_downgrade_to_personal: false, + can_invite_members: false, + can_manage_members: false, + can_reactivate: false, + can_revert_scheduled_change: false, + can_subscribe_self_serve: true, + can_top_up: true + }, + expires_at: new Date(Date.now() + 10 * 60 * 1000).toISOString(), + resolved_for: { user_id: COMFY_USER_ID, workspace_id: 'ws-1' }, + revision: 1, + rollout_defaults_applied: { + can_downgrade_to_personal: false, + can_subscribe_self_serve: false, + can_top_up: false + } +} + +describe('useBillingSdkStore on the token session', () => { + beforeEach(() => { + stubAccountIdentityPort() + vi.mocked(useBillingContext).mockReturnValue(useBillingContext()) + vi.mocked(fetch).mockImplementation( + async () => + new Response(JSON.stringify(CAPABILITIES), { + status: 200 + }) + ) + const session = useWorkspaceAuthStore().getUnifiedSessionClient() + vi.spyOn(session, 'getSnapshot').mockReturnValue(SNAPSHOT) + vi.spyOn(session, 'ensureFresh').mockResolvedValue({ + status: 'ok', + session: CREDENTIAL + }) + }) + + it('accepts capabilities resolved for the Comfy user its token names when that is not the Firebase uid', async () => { + const result = await useBillingSdkStore().readCapabilities({}) + + expect(result).toMatchObject({ + status: 'ok', + value: { + capabilities: { can_subscribe_self_serve: true }, + resolved_for: { user_id: COMFY_USER_ID, workspace_id: 'ws-1' } + } + }) + }) +}) From 1117dcc171cc1591ad5e9981569874f1200d46b6 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Wed, 14 Oct 2026 12:55:02 -0400 Subject: [PATCH 2/2] fix(billing): scope the token-session billing SDK by the user its Cloud JWT names On the token session the billing SDK took its scope user from the Firebase uid. /billing/capabilities answers resolved_for.user_id with the Comfy user id the Cloud JWT was issued for, and since identity mappings a Firebase subject can link to an existing Comfy user, so the two differ for linked accounts. The capabilities reader then rejected every 200 as out of scope: no Upgrade plan button, Change to Pro inert, capability_read.failed on every load. The web session already scopes by the server's user id. The token-session scope now reads the user from the minted Cloud JWT's sub, the identity the server resolves the request to. FE-3440 --- .../sdk/billingSdkStore.tokenSession.test.ts | 7 +----- .../workspace/billing/sdk/billingSdkStore.ts | 24 ++++++++++++++++++- .../sdk/billingSdkTelemetryOwnership.test.ts | 4 +++- .../billing/sdk/billingSdkTestUtils.ts | 7 ++++++ 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/platform/workspace/billing/sdk/billingSdkStore.tokenSession.test.ts b/src/platform/workspace/billing/sdk/billingSdkStore.tokenSession.test.ts index c8937c01832..1cc19af5d8e 100644 --- a/src/platform/workspace/billing/sdk/billingSdkStore.tokenSession.test.ts +++ b/src/platform/workspace/billing/sdk/billingSdkStore.tokenSession.test.ts @@ -11,6 +11,7 @@ import { useWorkspaceAuthStore } from '@/platform/workspace/stores/workspaceAuth import { stubAccountIdentityPort } from '@/utils/__tests__/stubAccountIdentityPort' import { useBillingSdkStore } from './billingSdkStore' +import { cloudJwtFor } from './billingSdkTestUtils' vi.mock(import('@/composables/billing/useBillingContext')) @@ -29,12 +30,6 @@ vi.mock(import('firebase/auth')) const FIREBASE_UID = 'firebase-uid' const COMFY_USER_ID = 'comfy-user-id' -function cloudJwtFor(subject: string): string { - const encode = (value: object) => - btoa(JSON.stringify(value)).replace(/=+$/, '') - return `${encode({ alg: 'RS256' })}.${encode({ sub: subject })}.signature` -} - const CREDENTIAL: AccountCredential = { token: cloudJwtFor(COMFY_USER_ID), expiresAt: Date.now() + 60 * 60 * 1000, diff --git a/src/platform/workspace/billing/sdk/billingSdkStore.ts b/src/platform/workspace/billing/sdk/billingSdkStore.ts index ccb28b95cd5..6a12c3e35ba 100644 --- a/src/platform/workspace/billing/sdk/billingSdkStore.ts +++ b/src/platform/workspace/billing/sdk/billingSdkStore.ts @@ -11,6 +11,8 @@ import type { BillingEventsReadOptions, BillingOperationTelemetryEvent, BillingResult, + BillingScopeSource, + BillingSession, CapabilitiesReadOptions, EmbeddedChallengePort, PendingBillingOperation, @@ -32,6 +34,7 @@ import { useToast } from '@/components/ui/toast/toastStore' import { useBillingContext } from '@/composables/billing/useBillingContext' import { useFeatureFlags } from '@/composables/useFeatureFlags' import { t } from '@/i18n' +import { readAccessTokenIdentity } from '@/platform/auth/localWeb/accessTokenIdentity' import { webSessionRequests } from '@/platform/auth/session/webSessionFetch' import { isCloud } from '@/platform/distribution/types' import { useSettingsDialog } from '@/platform/settings/composables/useSettingsDialog' @@ -120,6 +123,23 @@ async function loadChallengePort(): Promise { } } +function tokenSessionScopeSource(session: BillingSession): BillingScopeSource { + return { + getScope: () => { + const credential = session.getSnapshot().session + const identity = credential && readAccessTokenIdentity(credential.token) + return ( + identity && { + userId: identity.userId, + workspaceId: credential.workspace.id, + role: credential.role + } + ) + }, + subscribe: (listener) => session.subscribe(listener) + } +} + export const useBillingSdkStore = defineStore('billingSdk', () => { const workspaceAuthStore = useWorkspaceAuthStore() const workspaceStore = useTeamWorkspaceStore() @@ -148,8 +168,10 @@ export const useBillingSdkStore = defineStore('billingSdk', () => { workspaceId: requests.workspaceId } } + const session = workspaceAuthStore.getUnifiedSessionClient() return { - session: workspaceAuthStore.getUnifiedSessionClient(), + session, + scopeSource: tokenSessionScopeSource(session), workspaceId: () => workspaceAuthStore.getUnifiedMintWorkspaceId() } } diff --git a/src/platform/workspace/billing/sdk/billingSdkTelemetryOwnership.test.ts b/src/platform/workspace/billing/sdk/billingSdkTelemetryOwnership.test.ts index c0b0c06bc4b..868a3a36ea1 100644 --- a/src/platform/workspace/billing/sdk/billingSdkTelemetryOwnership.test.ts +++ b/src/platform/workspace/billing/sdk/billingSdkTelemetryOwnership.test.ts @@ -44,6 +44,8 @@ import { stubFirebaseAuthHarness } from '@/utils/__tests__/stubAccountIdentityPort' +import { cloudJwtFor } from './billingSdkTestUtils' + vi.mock(import('@/platform/telemetry')) vi.mock(import('@/composables/useFeatureFlags')) vi.mock(import('@/composables/billing/useBillingContext')) @@ -63,7 +65,7 @@ vi.mock( }) ) const CREDENTIAL: AccountCredential = { - token: 'workspace-jwt', + token: cloudJwtFor('uid-1'), expiresAt: Date.now() + 60 * 60 * 1000, uid: 'uid-1', workspace: { id: 'ws-1', name: 'Personal', type: 'personal' }, diff --git a/src/platform/workspace/billing/sdk/billingSdkTestUtils.ts b/src/platform/workspace/billing/sdk/billingSdkTestUtils.ts index 9418f85b41f..989ebef9572 100644 --- a/src/platform/workspace/billing/sdk/billingSdkTestUtils.ts +++ b/src/platform/workspace/billing/sdk/billingSdkTestUtils.ts @@ -14,6 +14,13 @@ import { vi } from 'vitest' import type { BillingSdk } from './createBillingSdk' +/** An unsigned token shaped like a Cloud JWT issued for `subject`. */ +export function cloudJwtFor(subject: string): string { + const encode = (value: object) => + btoa(JSON.stringify(value)).replace(/=+$/, '') + return `${encode({ alg: 'RS256' })}.${encode({ sub: subject })}.signature` +} + /** A server code minted the one sanctioned way, so a test cannot invent one. */ export function serverCode(code: string): BillingServerCode | undefined { return readBillingErrorCode({ code, message: 'server text' })