diff --git a/browser_tests/tests/agent/agentRunModePopover.spec.ts b/browser_tests/tests/agent/agentRunModePopover.spec.ts index bcf1476abf9..8d9ecf73259 100644 --- a/browser_tests/tests/agent/agentRunModePopover.spec.ts +++ b/browser_tests/tests/agent/agentRunModePopover.spec.ts @@ -118,9 +118,9 @@ test.describe('Agent run permissions popover', { tag: '@cloud' }, () => { }) }) - // A rejected write keeps the menu open to retry from, but the options go - // aria-disabled mid-write: that retry survives only if focus does. - test('A failed save can be retried from the keyboard', async ({ + // Regression coverage for https://github.com/Comfy-Org/ComfyUI_frontend/pull/20177: + // auth diagnostics must leave the rejected write retryable at the UI. + test('An auth-rejected save can be retried from the keyboard', async ({ agentPanel, comfyPage }) => { @@ -145,7 +145,17 @@ test.describe('Agent run permissions popover', { tag: '@cloud' }, () => { if (rejectNext) { rejectNext = false await held - return route.fulfill({ status: 500, body: 'nope' }) + return route.fulfill({ + ...jsonRoute({ + accepted: ['bearer_jwt', 'x_api_key'], + error: { + type: 'auth_type_not_allowed', + message: + 'Authentication method not allowed for this endpoint. Accepted: bearer_jwt, x_api_key' + } + }), + status: 403 + }) } return route.fulfill(jsonRoute(saved)) }) diff --git a/src/platform/auth/authCredential.test.ts b/src/platform/auth/authCredential.test.ts new file mode 100644 index 00000000000..688d861468c --- /dev/null +++ b/src/platform/auth/authCredential.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, it, vi } from 'vitest' + +import type { AuthHeader } from '@/types/authTypes' + +import { authCredentialOf, notifyAuthCredential } from './authCredential' + +describe('authCredentialOf', () => { + it.for([ + [null, 'none'], + [{ Authorization: 'Bearer t' }, 'bearer'], + [{ 'X-API-KEY': 'k' }, 'api-key'], + [{}, 'none'], + [{ authorization: 'Bearer t' }, 'none'] + ] as const)('classifies %j as %s', ([header, credential]) => { + expect(authCredentialOf(header as AuthHeader | null)).toBe(credential) + }) +}) + +describe('notifyAuthCredential', () => { + it('delivers the credential kind', () => { + const callback = vi.fn() + + notifyAuthCredential(callback, 'bearer') + + expect(callback).toHaveBeenCalledExactlyOnceWith('bearer') + }) + + it('does nothing without a callback', () => { + expect(() => notifyAuthCredential(undefined, 'none')).not.toThrow() + }) + + it('swallows a throwing callback', () => { + vi.spyOn(console, 'warn').mockImplementation(() => {}) + + expect(() => + notifyAuthCredential(() => { + throw new Error('boom') + }, 'none') + ).not.toThrow() + }) +}) diff --git a/src/platform/auth/authCredential.ts b/src/platform/auth/authCredential.ts new file mode 100644 index 00000000000..c453855a840 --- /dev/null +++ b/src/platform/auth/authCredential.ts @@ -0,0 +1,38 @@ +import type { + ApiKeyAuthHeader, + AuthCredential, + AuthHeader, + LoggedInAuthHeader +} from '@/types/authTypes' + +const HEADER_CREDENTIAL = { + Authorization: 'bearer', + 'X-API-KEY': 'api-key' +} as const satisfies Record< + keyof LoggedInAuthHeader | keyof ApiKeyAuthHeader, + AuthCredential +> + +/** The credential kind an auth header carries; `none` when it carries neither. */ +export function authCredentialOf(header: AuthHeader | null): AuthCredential { + if (header === null) return 'none' + const headerKey = Object.keys(HEADER_CREDENTIAL).find((key) => key in header) + return headerKey === undefined + ? 'none' + : HEADER_CREDENTIAL[headerKey as keyof typeof HEADER_CREDENTIAL] +} + +/** + * Delivers a credential kind to a diagnostics callback without letting the + * callback affect the request: telemetry must fail open. + */ +export function notifyAuthCredential( + callback: ((credential: AuthCredential) => void) | undefined, + credential: AuthCredential +): void { + try { + callback?.(credential) + } catch (error) { + console.warn('onAuthCredential callback failed:', error) + } +} diff --git a/src/platform/auth/authRejection.test.ts b/src/platform/auth/authRejection.test.ts new file mode 100644 index 00000000000..3e7a0768835 --- /dev/null +++ b/src/platform/auth/authRejection.test.ts @@ -0,0 +1,120 @@ +import { describe, expect, it } from 'vitest' + +import { + MAX_ACCEPTED_METHODS, + MAX_REASON_TEXT_LENGTH, + authRejectionTags, + isAuthRejectionStatus +} from './authRejection' + +const NOT_ALLOWED_MESSAGE = + 'Authentication method not allowed for this endpoint. Accepted: bearer_jwt, x_api_key' + +describe('isAuthRejectionStatus', () => { + it.for([ + [401, true], + [403, true], + [400, false], + [404, false], + [500, false] + ] as const)('%i -> %s', ([status, expected]) => { + expect(isAuthRejectionStatus(status)).toBe(expected) + }) +}) + +describe('authRejectionTags', () => { + it('describes an auth-type refusal from its structure', () => { + expect( + authRejectionTags( + { + accepted: ['bearer_jwt', 'x_api_key'], + error: { type: 'auth_type_not_allowed', message: NOT_ALLOWED_MESSAGE } + }, + NOT_ALLOWED_MESSAGE + ) + ).toEqual({ + backendReason: 'auth_method_not_allowed', + backendErrorType: 'auth_type_not_allowed', + acceptedMethods: 'bearer_jwt,x_api_key' + }) + }) + + it('classifies the plain { error: string } shape from the message', () => { + expect( + authRejectionTags({ error: NOT_ALLOWED_MESSAGE }, NOT_ALLOWED_MESSAGE) + ).toEqual({ backendReason: 'auth_method_not_allowed' }) + }) + + it('reads the ingest { code, message } shape, preferring its message', () => { + expect( + authRejectionTags({ code: 'UNAUTHORIZED', message: 'no credential' }, '') + ).toEqual({ backendReason: 'other', backendErrorCode: 'UNAUTHORIZED' }) + }) + + it.for([ + 'user 3f2b8c1e-9a4d-4e57-8b1a-0c6d2f7e5a91 not authorized for workspace 7d1e4b2a-5c3f-4a68-9e0b-1f2a3b4c5d6e', + 'invalid key 9f86d081884c7d659a2feaa0c55ad015', + 'token eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.abc rejected', + 'denied for someone@example.com' + ])('reports no part of free-form text: %s', (text) => { + const tags = authRejectionTags({ error: text }, text) + + expect(tags).toEqual({ backendReason: 'other' }) + }) + + it('never copies an unknown code or method into a tag', () => { + const tags = authRejectionTags( + { + code: 'ws_7d1e4b2a5c3f4a689e0b1f2a3b4c5d6e', + message: 'nope' + }, + '' + ) + const methods = authRejectionTags( + { + accepted: ['bearer_jwt', 'custom-7d1e4b2a', 'x_api_key', 'again'], + error: { type: 'auth_type_not_allowed', message: 'nope' } + }, + 'nope' + ) + + expect(tags.backendErrorCode).toBe('other') + expect(methods.acceptedMethods).toBe('bearer_jwt,other,x_api_key') + }) + + it('reads only the first MAX_ACCEPTED_METHODS accepted methods', () => { + const accepted = [ + ...Array.from({ length: MAX_ACCEPTED_METHODS }, () => 'unlisted'), + 'bearer_jwt' + ] + + const tags = authRejectionTags( + { accepted, error: { type: 'auth_type_not_allowed', message: 'x' } }, + 'x' + ) + + expect(tags.acceptedMethods).toBe('other') + }) + + it('reads only a bounded prefix of the message', () => { + const text = `${' '.repeat(MAX_REASON_TEXT_LENGTH)}authentication method not allowed` + + expect(authRejectionTags(undefined, text)).toEqual({}) + }) + + it.for([ + undefined, + null, + 0, + 'text', + [], + [1, 2], + {}, + { error: null }, + { error: { type: 5 } }, + { accepted: 'bearer_jwt' }, + { code: 403, message: 7 } + ])('is total for the hostile body %j', (body) => { + expect(() => authRejectionTags(body, '')).not.toThrow() + }) +}) diff --git a/src/platform/auth/authRejection.ts b/src/platform/auth/authRejection.ts new file mode 100644 index 00000000000..ed05f36ee5c --- /dev/null +++ b/src/platform/auth/authRejection.ts @@ -0,0 +1,113 @@ +import type { AuthTypeNotAllowedError } from '@comfyorg/ingest-types' +import { + zAuthTypeNotAllowedError, + zErrorResponse +} from '@comfyorg/ingest-types/zod' + +export const AUTH_REJECTION_STATUSES = [401, 403] as const +export type AuthRejectionStatus = (typeof AUTH_REJECTION_STATUSES)[number] + +export function isAuthRejectionStatus( + status: number +): status is AuthRejectionStatus { + return AUTH_REJECTION_STATUSES.some((rejection) => rejection === status) +} + +const OTHER = 'other' +type Other = typeof OTHER + +/** + * Backend values are reported only when they are on these lists, and anything + * else is `other`: the backend text is uncontrolled and can carry user, + * workspace or key identifiers, and an open value space would make the tags + * unbounded in cardinality. The generated ingest types describe these codes in + * prose only, so this is the one place they are listed. + */ +export const KNOWN_AUTH_REFUSAL_CODES = [ + 'UNAUTHORIZED', + 'FORBIDDEN', + 'csrf_invalid', + 'workspace_access_denied', + 'workspace_id_invalid', + 'origin_not_allowed', + 'cross_site_request' +] as const +export const KNOWN_ACCEPTED_AUTH_METHODS = ['bearer_jwt', 'x_api_key'] as const + +/** Message phrases for the plain `{ error: string }` shape, which has no code. */ +const REASON_PHRASES = [ + { + phrase: 'authentication method not allowed', + reason: 'auth_method_not_allowed' + } +] as const + +export const MAX_REASON_TEXT_LENGTH = 500 +export const MAX_ACCEPTED_METHODS = 8 + +type AuthRefusalCode = (typeof KNOWN_AUTH_REFUSAL_CODES)[number] | Other +type AcceptedAuthMethod = (typeof KNOWN_ACCEPTED_AUTH_METHODS)[number] | Other +type AuthRejectionReason = (typeof REASON_PHRASES)[number]['reason'] | Other + +interface BackendFields { + reason: AuthRejectionReason + errorType: AuthTypeNotAllowedError['error']['type'] + errorCode: AuthRefusalCode +} + +/** Tags describing the backend's refusal: `backendReason`, `backendErrorType`, ... */ +export type AuthRejectionTags = { + [K in keyof BackendFields as `backend${Capitalize}`]?: BackendFields[K] +} & { acceptedMethods?: string } + +function isOneOf( + known: readonly T[], + value: string +): value is T { + return known.some((entry) => entry === value) +} + +function reasonOf(text: string): AuthRejectionReason | undefined { + const bounded = text.slice(0, MAX_REASON_TEXT_LENGTH).toLowerCase() + if (bounded.trim().length === 0) return undefined + return ( + REASON_PHRASES.find(({ phrase }) => bounded.includes(phrase))?.reason ?? + OTHER + ) +} + +function codeOf(code: string): AuthRefusalCode { + return isOneOf(KNOWN_AUTH_REFUSAL_CODES, code) ? code : OTHER +} + +function acceptedMethodsOf(accepted: readonly string[]): string | undefined { + const methods = accepted + .slice(0, MAX_ACCEPTED_METHODS) + .map( + (method): AcceptedAuthMethod => + isOneOf(KNOWN_ACCEPTED_AUTH_METHODS, method) ? method : OTHER + ) + return methods.length > 0 ? [...new Set(methods)].join(',') : undefined +} + +/** + * The tags that tell an auth refusal's causes apart, taken from the ingest + * error shapes (`AuthTypeNotAllowedError`, `ErrorResponse`) or, for a body in + * neither, from the already-extracted message. Pure and total: any input + * yields a (possibly empty) tag set, and no value is copied from the response. + */ +export function authRejectionTags( + body: unknown, + message: string +): AuthRejectionTags { + const notAllowed = zAuthTypeNotAllowedError.safeParse(body).data + const refusal = zErrorResponse.safeParse(body).data + const reason = reasonOf(refusal?.message ?? message) + const acceptedMethods = notAllowed && acceptedMethodsOf(notAllowed.accepted) + return { + ...(reason && { backendReason: reason }), + ...(notAllowed && { backendErrorType: notAllowed.error.type }), + ...(refusal && { backendErrorCode: codeOf(refusal.code) }), + ...(acceptedMethods && { acceptedMethods }) + } +} diff --git a/src/scripts/api.fetchApi.test.ts b/src/scripts/api.fetchApi.test.ts index bc7138b892e..7e419b10f08 100644 --- a/src/scripts/api.fetchApi.test.ts +++ b/src/scripts/api.fetchApi.test.ts @@ -274,6 +274,68 @@ describe('api.fetchApi', () => { expect(onAuthScheme).toHaveBeenCalledExactlyOnceWith('none') }) + it('is not broken by a throwing onAuthCredential callback', async () => { + signInOnCloud() + vi.spyOn(console, 'warn').mockImplementation(() => {}) + vi.mocked(useAuthStore().getAuthHeader).mockResolvedValue({ + Authorization: 'Bearer tokenA' + }) + vi.mocked(global.fetch).mockResolvedValue(new Response('ok')) + + const response = await api.fetchApi('/test', { + onAuthCredential: () => { + throw new Error('callback bug') + } + }) + + expect(await response.text()).toBe('ok') + expect(vi.mocked(global.fetch).mock.calls[0][1]?.headers).toMatchObject({ + Authorization: 'Bearer tokenA' + }) + }) + + it('reports the credential kind each auth path sent', async () => { + vi.mocked(global.fetch).mockResolvedValue(new Response()) + const offCloud = vi.fn() + await api.fetchApi('/test', { onAuthCredential: offCloud }) + + signInOnCloud() + const getAuthHeader = vi.mocked(useAuthStore().getAuthHeader) + const bearer = vi.fn() + getAuthHeader.mockResolvedValueOnce({ Authorization: 'Bearer tokenA' }) + await api.fetchApi('/test', { onAuthCredential: bearer }) + const apiKey = vi.fn() + getAuthHeader.mockResolvedValueOnce({ 'X-API-KEY': 'key' }) + await api.fetchApi('/test', { onAuthCredential: apiKey }) + const missing = vi.fn() + getAuthHeader.mockResolvedValueOnce(null) + await api.fetchApi('/test', { onAuthCredential: missing }) + const session = vi.fn() + const release = provideWebSessionRequests( + fromPartial({ + scope: async () => ({ session: fromPartial({}), epoch: 1 }), + send: vi.fn().mockResolvedValue(new Response()) + }) + ) + try { + await api.fetchApi('/test', { onAuthCredential: session }) + } finally { + release() + } + + expect( + [offCloud, bearer, apiKey, missing, session].map( + (callback) => callback.mock.calls + ) + ).toEqual([ + [['none']], + [['bearer']], + [['api-key']], + [['none']], + [['session-cookie']] + ]) + }) + it('is not forwarded to fetch as a request option', async () => { vi.mocked(global.fetch).mockResolvedValue(new Response()) diff --git a/src/scripts/api.ts b/src/scripts/api.ts index ff6def8a6ae..9946c51b24d 100644 --- a/src/scripts/api.ts +++ b/src/scripts/api.ts @@ -17,6 +17,10 @@ import { trimEnd } from 'es-toolkit' import { ref } from 'vue' import defaultClientFeatureFlags from '@/config/clientFeatureFlags.json' with { type: 'json' } +import { + authCredentialOf, + notifyAuthCredential +} from '@/platform/auth/authCredential' import { scopeMediaRoute } from '@/platform/auth/session/sessionMediaUrl' import { webSessionRequests } from '@/platform/auth/session/webSessionFetch' import { @@ -78,7 +82,7 @@ import type { } from '@/platform/remote/comfyui/jobs/jobTypes' import type { ComfyNodeDef } from '@/schemas/nodeDefSchema' import type { useAuthStore } from '@/stores/authStore' -import type { AuthHeader } from '@/types/authTypes' +import type { AuthCredential, AuthHeader, AuthScheme } from '@/types/authTypes' import type { NodeExecutionId } from '@/types/nodeIdentification' import { fetchHistory, @@ -145,22 +149,13 @@ interface QueuePromptRequestBody { const FETCH_RESPONSE_HEADERS_TIMEOUT_MS = 60_000 -/** - * Which cloud auth path a request actually took, for error diagnostics (PM-1802). - * - * `none` means no auth scheme was used at all, which covers both a non-cloud - * distribution and a cloud request whose auth header was unavailable. Those are - * the same statement about the request - nothing authenticated it - and the - * deploy surface already distinguishes them, so this stays three values rather - * than growing a fourth that only restates `isCloud`. - */ -export type AuthScheme = 'web-session' | 'cloud-auth-header' | 'none' - interface FetchApiOptions extends RequestInit { timeoutMs?: number | null onAuthHeader?: (attached: boolean) => void /** Reports which auth path was taken, independent of onAuthHeader's attached/not boolean. */ onAuthScheme?: (scheme: AuthScheme) => void + /** Reports the kind of credential sent, to tell a missing token from a wrong one. */ + onAuthCredential?: (credential: AuthCredential) => void } const FETCH_ROUTE_GROUPS = new Set([ @@ -609,7 +604,11 @@ export class ComfyApi extends EventTarget { private async addCloudAuthHeader( headers: HeadersInit, onAuthHeader: FetchApiOptions['onAuthHeader'] - ): Promise<{ scheme: AuthScheme; unifiedRetryOn401: boolean }> { + ): Promise<{ + scheme: AuthScheme + credential: AuthCredential + unifiedRetryOn401: boolean + }> { // Get Firebase JWT token if user is logged in const getAuthHeaderIfAvailable = async (): Promise => { try { @@ -623,13 +622,16 @@ export class ComfyApi extends EventTarget { const authHeader = await getAuthHeaderIfAvailable() onAuthHeader?.(authHeader !== null) - if (!authHeader) return { scheme: 'none', unifiedRetryOn401: false } + if (!authHeader) { + return { scheme: 'none', credential: 'none', unifiedRetryOn401: false } + } for (const [key, value] of Object.entries(authHeader)) { addHeaderEntry(headers, key, value) } return { scheme: 'cloud-auth-header', + credential: authCredentialOf(authHeader), unifiedRetryOn401: await shouldRemintCloudRequest() } } @@ -662,6 +664,7 @@ export class ComfyApi extends EventTarget { timeoutMs = FETCH_RESPONSE_HEADERS_TIMEOUT_MS, onAuthHeader, onAuthScheme, + onAuthCredential, ...requestOptions } = options ?? {} const headers: HeadersInit = requestOptions.headers ?? {} @@ -674,14 +677,17 @@ export class ComfyApi extends EventTarget { if (sendOnWebSession) { onAuthHeader?.(true) onAuthScheme?.('web-session') + notifyAuthCredential(onAuthCredential, 'session-cookie') } else { const cloudAuth = await this.addCloudAuthHeader(headers, onAuthHeader) unifiedRetryOn401 = cloudAuth.unifiedRetryOn401 onAuthScheme?.(cloudAuth.scheme) + notifyAuthCredential(onAuthCredential, cloudAuth.credential) } } else { onAuthHeader?.(false) onAuthScheme?.('none') + notifyAuthCredential(onAuthCredential, 'none') } addHeaderEntry(headers, 'Comfy-User', this.user) diff --git a/src/types/authTypes.ts b/src/types/authTypes.ts index 549cd01b86a..fab11c90fc6 100644 --- a/src/types/authTypes.ts +++ b/src/types/authTypes.ts @@ -1,4 +1,4 @@ -type LoggedInAuthHeader = { +export type LoggedInAuthHeader = { Authorization: `Bearer ${string}` } @@ -8,6 +8,20 @@ export type ApiKeyAuthHeader = { export type AuthHeader = LoggedInAuthHeader | ApiKeyAuthHeader +/** + * Which cloud auth path a request actually took, for error diagnostics. + * + * `none` means no auth scheme was used at all, which covers both a non-cloud + * distribution and a cloud request whose auth header was unavailable. Those are + * the same statement about the request - nothing authenticated it - and the + * deploy surface already distinguishes them, so this stays three values rather + * than growing a fourth that only restates `isCloud`. + */ +export type AuthScheme = 'web-session' | 'cloud-auth-header' | 'none' + +/** What credential a request carried, for telemetry; never the credential itself. */ +export type AuthCredential = 'session-cookie' | 'bearer' | 'api-key' | 'none' + /** * Identifier for an authenticated user. * diff --git a/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts b/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts index 8a2fd806781..ed5c3ea3e78 100644 --- a/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts @@ -4,7 +4,8 @@ import { markErrorReported, reportError } from '@/platform/telemetry/reportError' -import type { AuthScheme } from '@/scripts/api' +import type { AuthCredential, AuthScheme } from '@/types/authTypes' +import * as authRejection from '@/platform/auth/authRejection' import { api } from '@/scripts/api' import type { CloudWorkflowEntry } from '../../schemas/agentApiSchema' @@ -34,9 +35,14 @@ function respond(response: Response) { vi.mocked(api.fetchApi).mockResolvedValueOnce(response) } -function respondWithAuthScheme(response: Response, scheme: AuthScheme) { +function respondWithAuthScheme( + response: Response, + scheme: AuthScheme, + credential?: AuthCredential +) { vi.mocked(api.fetchApi).mockImplementationOnce(async (_route, init) => { init?.onAuthScheme?.(scheme) + if (credential) init?.onAuthCredential?.(credential) return response }) } @@ -623,7 +629,9 @@ describe('error mapping', () => { tags: { operation: 'get_thread_messages', status: 401, - authScheme: 'web-session' + authScheme: 'web-session', + credential: 'unreported', + backendReason: 'auth_method_not_allowed' }, level: 'warning' }) @@ -642,7 +650,9 @@ describe('error mapping', () => { expect(reportedTags()).toEqual({ operation: 'get_thread_messages', status: 403, - authScheme: 'cloud-auth-header' + authScheme: 'cloud-auth-header', + credential: 'unreported', + backendReason: 'other' }) }) @@ -656,7 +666,9 @@ describe('error mapping', () => { expect(reportedTags()).toEqual({ operation: 'get_thread_messages', status: 401, - authScheme: 'none' + authScheme: 'none', + credential: 'unreported', + backendReason: 'other' }) }) @@ -670,7 +682,175 @@ describe('error mapping', () => { expect(reportedTags()).toEqual({ operation: 'get_thread_messages', status: 401, - authScheme: 'unreported' + authScheme: 'unreported', + credential: 'unreported', + backendReason: 'other' + }) + }) + + it('reports the credential kind and the backend refusal details on an auth-type rejection', async () => { + respondWithAuthScheme( + jsonResponse(403, { + accepted: ['bearer_jwt', 'x_api_key'], + error: { + type: 'auth_type_not_allowed', + message: + 'Authentication method not allowed for this endpoint. Accepted: bearer_jwt, x_api_key' + } + }), + 'web-session', + 'session-cookie' + ) + + const error = await makeClient() + .getRunMode() + .catch((e: unknown) => e) + + expect(reportError).toHaveBeenCalledExactlyOnceWith(expect.any(Error), { + surface: 'agent', + errorType: 'agent_api_auth_rejected', + tags: { + operation: 'get_run_mode', + status: 403, + authScheme: 'web-session', + credential: 'session-cookie', + backendReason: 'auth_method_not_allowed', + backendErrorType: 'auth_type_not_allowed', + acceptedMethods: 'bearer_jwt,x_api_key' + }, + level: 'warning' + }) + expect(reportedError().message).toBe( + 'Agent API request rejected by authentication' + ) + expect(error).toBeInstanceOf(AgentApiError) + }) + + it.for([ + ['session-cookie', 'web-session'], + ['bearer', 'cloud-auth-header'], + ['api-key', 'cloud-auth-header'], + ['none', 'none'] + ] as const)( + 'tags the %s credential so cookie-only, missing and wrong tokens stay distinguishable', + async ([credential, scheme]) => { + respondWithAuthScheme( + jsonResponse(401, { code: 'UNAUTHORIZED', message: 'no credential' }), + scheme, + credential + ) + + await makeClient() + .getRunMode() + .catch((e: unknown) => e) + + expect(reportedTags()).toMatchObject({ + authScheme: scheme, + credential, + backendReason: 'other', + backendErrorCode: 'UNAUTHORIZED' + }) + } + ) + + it('classifies the ingest { code, message } shape from its message field', async () => { + respondWithAuthScheme( + jsonResponse(401, { + code: 'UNAUTHORIZED', + message: 'Authentication method not allowed for this endpoint' + }), + 'web-session', + 'session-cookie' + ) + + await makeClient() + .getRunMode() + .catch((e: unknown) => e) + + expect(reportedTags()).toEqual({ + operation: 'get_run_mode', + status: 401, + authScheme: 'web-session', + credential: 'session-cookie', + backendReason: 'auth_method_not_allowed', + backendErrorCode: 'UNAUTHORIZED' + }) + }) + + it.for([ + 'user 3f2b8c1e-9a4d-4e57-8b1a-0c6d2f7e5a91 not authorized for workspace 7d1e4b2a-5c3f-4a68-9e0b-1f2a3b4c5d6e', + 'invalid key 9f86d081884c7d659a2feaa0c55ad015', + 'token eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.abc rejected', + 'denied for someone@example.com', + 'x '.repeat(5000) + ])('reports no part of free-form backend text: %s', async (text) => { + respondWithAuthScheme( + jsonResponse(403, { error: text }), + 'cloud-auth-header', + 'bearer' + ) + + await makeClient() + .getRunMode() + .catch((e: unknown) => e) + + expect(reportedTags()).toEqual({ + operation: 'get_run_mode', + status: 403, + authScheme: 'cloud-auth-header', + credential: 'bearer', + backendReason: 'other' + }) + expect(vi.mocked(reportError).mock.calls.at(-1)?.[1].context).toBe( + undefined + ) + expect(JSON.stringify(vi.mocked(reportError).mock.calls)).not.toMatch( + /[0-9a-f]{12,}|eyJ|example\.com/ + ) + }) + + it('still throws the original error unchanged when the reporter throws', async () => { + vi.mocked(reportError).mockImplementationOnce(() => { + throw new Error('sink down') + }) + respondWithAuthScheme( + jsonResponse(403, { error: 'access denied' }), + 'cloud-auth-header', + 'bearer' + ) + + const error = await makeClient() + .getRunMode() + .catch((e: unknown) => e) + + expect(error).toBeInstanceOf(AgentApiError) + expect(error).toMatchObject({ status: 403, message: 'access denied' }) + expect(markErrorReported).not.toHaveBeenCalled() + }) + + it('still reports the base tags when classifying the backend body throws', async () => { + const classify = vi + .spyOn(authRejection, 'authRejectionTags') + .mockImplementationOnce(() => { + throw new Error('classifier bug') + }) + respondWithAuthScheme( + jsonResponse(403, { error: 'access denied' }), + 'cloud-auth-header', + 'bearer' + ) + + const error = await makeClient() + .getRunMode() + .catch((e: unknown) => e) + classify.mockRestore() + + expect(error).toBeInstanceOf(AgentApiError) + expect(reportedTags()).toEqual({ + operation: 'get_run_mode', + status: 403, + authScheme: 'cloud-auth-header', + credential: 'bearer' }) }) @@ -687,7 +867,9 @@ describe('error mapping', () => { expect(reportedTags()).toEqual({ operation: 'answer_thread_ask', status: 403, - authScheme: 'cloud-auth-header' + authScheme: 'cloud-auth-header', + credential: 'unreported', + backendReason: 'other' }) const serialized = JSON.stringify([ reportedError().message, @@ -711,7 +893,9 @@ describe('error mapping', () => { expect(reportedTags()).toEqual({ operation: 'cancel_thread_message', status: 403, - authScheme: 'cloud-auth-header' + authScheme: 'cloud-auth-header', + credential: 'unreported', + backendReason: 'other' }) const serialized = JSON.stringify([ reportedError().message, @@ -752,7 +936,9 @@ describe('error mapping', () => { expect(reportedTags()).toEqual({ operation: 'list_cloud_workflows', status: 401, - authScheme: 'cloud-auth-header' + authScheme: 'cloud-auth-header', + credential: 'unreported', + backendReason: 'other' }) }) diff --git a/src/workbench/extensions/agent/services/agent/agentRestClient.ts b/src/workbench/extensions/agent/services/agent/agentRestClient.ts index 0dca8495289..eae56c0159e 100644 --- a/src/workbench/extensions/agent/services/agent/agentRestClient.ts +++ b/src/workbench/extensions/agent/services/agent/agentRestClient.ts @@ -9,12 +9,20 @@ import { } from '@comfyorg/ingest-types/zod' import type { z } from 'zod' +import type { + AuthRejectionStatus, + AuthRejectionTags +} from '@/platform/auth/authRejection' +import { + authRejectionTags, + isAuthRejectionStatus +} from '@/platform/auth/authRejection' import { markErrorReported, reportError } from '@/platform/telemetry/reportError' -import type { AuthScheme } from '@/scripts/api' import { api } from '@/scripts/api' +import type { AuthCredential, AuthScheme } from '@/types/authTypes' import { zAgentAnswerReceipt, @@ -77,17 +85,61 @@ type AgentApiOperation = | 'upload_image' type ReportedAuthScheme = AuthScheme | 'unreported' +type ReportedAuthCredential = AuthCredential | 'unreported' /** * The reported message for an auth rejection, constant by construction. * * The backend's own text is what `AgentApiError` carries to the caller, but it - * is not what gets reported: it is uncontrolled, is not needed to diagnose - * PM-1802, and varies enough to fragment issue grouping across what is one - * failure mode. Status, operation and auth scheme ride as tags instead. + * is not the Sentry message: it is uncontrolled and varies enough to fragment + * issue grouping across what is one failure mode. Status, operation, auth + * scheme, credential kind and an allowlisted classification of the backend's + * refusal ride as tags. */ const AUTH_REJECTED_MESSAGE = 'Agent API request rejected by authentication' +interface AuthRejection { + status: AuthRejectionStatus + operation: AgentApiOperation + authScheme: ReportedAuthScheme + credential: ReportedAuthCredential + body: unknown + message: string +} + +/** + * Reports an auth rejection and says whether it was delivered. Diagnostics + * fail open: nothing in here may change the error the caller receives, so + * tag-building and the reporter are each allowed to fail on their own. + */ +function reportAuthRejection({ + status, + operation, + authScheme, + credential, + body, + message +}: AuthRejection): boolean { + let backendTags: AuthRejectionTags = {} + try { + backendTags = authRejectionTags(body, message) + } catch (error) { + console.warn('Could not classify the agent auth rejection:', error) + } + try { + reportError(new Error(AUTH_REJECTED_MESSAGE), { + surface: 'agent', + errorType: 'agent_api_auth_rejected', + tags: { operation, status, authScheme, credential, ...backendTags }, + level: 'warning' + }) + return true + } catch (error) { + console.warn('Could not report the agent auth rejection:', error) + return false + } +} + export class AgentApiError extends Error { readonly status: number readonly body: unknown @@ -432,7 +484,8 @@ export function createAgentRestClient() { async function toApiError( response: Response, operation: AgentApiOperation, - authScheme: ReportedAuthScheme + authScheme: ReportedAuthScheme, + credential: ReportedAuthCredential ): Promise { const body = parseErrorBody(await response.text()) const message = getErrorMessage(body, response.statusText) @@ -449,13 +502,17 @@ export function createAgentRestClient() { body, retryAfterSeconds ) - if (response.status === 401 || response.status === 403) { - reportError(new Error(AUTH_REJECTED_MESSAGE), { - surface: 'agent', - errorType: 'agent_api_auth_rejected', - tags: { operation, status: response.status, authScheme }, - level: 'warning' + if ( + isAuthRejectionStatus(response.status) && + reportAuthRejection({ + status: response.status, + operation, + authScheme, + credential, + body, + message }) + ) { // Callers still receive the backend text for the UI, but their generic // catch boundaries must not emit it as a second, separately-grouped // report after the complete bounded diagnostic above. @@ -471,13 +528,19 @@ export function createAgentRestClient() { schema: z.ZodType ): Promise { let authScheme: ReportedAuthScheme = 'unreported' + let credential: ReportedAuthCredential = 'unreported' const response = await api.fetchApi(route, { ...init, onAuthScheme: (scheme) => { authScheme = scheme + }, + onAuthCredential: (kind) => { + credential = kind } }) - if (!response.ok) throw await toApiError(response, operation, authScheme) + if (!response.ok) { + throw await toApiError(response, operation, authScheme, credential) + } let payload: unknown try { payload = await response.json()