From ebacf7c882b16ef62cc91f606f30a50f40c48ce2 Mon Sep 17 00:00:00 2001 From: Neil Date: Thu, 12 Nov 2026 22:47:05 -0800 Subject: [PATCH] fix(cloud): premerge lint: type guards instead of assertions, MFA member fields in their own file, runtime catalog, link-muted button --- .../hosted-sandbox-config.test.ts | 2 +- .../hosted-sandbox/hosted-sandbox-config.ts | 9 +++-- .../orca-profile-org-members-handlers.ts | 17 +++++----- .../cloud-mfa/profile-cloud-mfa-service.ts | 10 ++++-- .../profile-cloud-team-mfa-service.ts | 33 +++++++++++-------- .../profile-cloud-org-members-client.ts | 2 +- .../team-mfa/TeamMfaBlockedScreen.tsx | 8 +---- .../src/components/team-mfa/team-mfa.test.tsx | 4 +-- .../src/i18n/en-runtime-required.json | 12 +++++++ src/shared/orca-account-mfa.ts | 14 ++++++++ src/shared/orca-profiles.ts | 14 ++------ 11 files changed, 75 insertions(+), 50 deletions(-) diff --git a/src/main/hosted-sandbox/hosted-sandbox-config.test.ts b/src/main/hosted-sandbox/hosted-sandbox-config.test.ts index 143960b1ab1..c22aa14be3d 100644 --- a/src/main/hosted-sandbox/hosted-sandbox-config.test.ts +++ b/src/main/hosted-sandbox/hosted-sandbox-config.test.ts @@ -126,7 +126,7 @@ describe('isMfaRequiredRefusal', () => { { status: 403 } ) expect(await isMfaRequiredRefusal(response)).toBe(true) - expect(((await response.json()) as { orgId: string }).orgId).toBe('org_1') + expect(await response.json()).toMatchObject({ orgId: 'org_1' }) }) it('is false for other refusals and for bodies that are not JSON', async () => { diff --git a/src/main/hosted-sandbox/hosted-sandbox-config.ts b/src/main/hosted-sandbox/hosted-sandbox-config.ts index 973ab6596e5..6558709f8af 100644 --- a/src/main/hosted-sandbox/hosted-sandbox-config.ts +++ b/src/main/hosted-sandbox/hosted-sandbox-config.ts @@ -28,8 +28,13 @@ export const MFA_REQUIRED_REASON = 'mfa_required' /** Reads a copy of the body, so the caller can still read the response. */ export async function isMfaRequiredRefusal(response: Response): Promise { try { - const body = (await response.clone().json()) as { error?: unknown } | null - return body?.error === MFA_REQUIRED_REASON + const body: unknown = await response.clone().json() + return ( + typeof body === 'object' && + body !== null && + 'error' in body && + body.error === MFA_REQUIRED_REASON + ) } catch { return false } diff --git a/src/main/ipc/profiles/orca-profile-org-members-handlers.ts b/src/main/ipc/profiles/orca-profile-org-members-handlers.ts index 455bdc47600..889b38d0d94 100644 --- a/src/main/ipc/profiles/orca-profile-org-members-handlers.ts +++ b/src/main/ipc/profiles/orca-profile-org-members-handlers.ts @@ -160,9 +160,8 @@ export function registerOrcaProfileOrgMemberHandlers(): void { createOrcaProfileOrg(getProfileUserDataPath(), { name: orgCreateNameFromUnknown(rawArgs) }) ) - ipcMain.handle( - 'orcaProfiles:accountMfa', - async (): Promise => getOrcaProfileAccountMfa(getProfileUserDataPath()) + ipcMain.handle('orcaProfiles:accountMfa', async (): Promise => + getOrcaProfileAccountMfa(getProfileUserDataPath()) ) ipcMain.handle( @@ -175,15 +174,15 @@ export function registerOrcaProfileOrgMemberHandlers(): void { 'orcaProfiles:teamMfaPolicySet', async (_event, rawArgs: unknown): Promise => { const { orgId, record } = orgMembersScopedArgs(rawArgs) - const enforce = record.enforce // Only the fixed choices cross to the server; anything else is refused here. - if (enforce !== 'off' && !ORCA_TEAM_MFA_GRACE_CHOICES.some((choice) => choice === enforce)) { + const enforce: OrcaTeamMfaGrace | 'off' | undefined = + record.enforce === 'off' + ? 'off' + : ORCA_TEAM_MFA_GRACE_CHOICES.find((choice) => choice === record.enforce) + if (enforce === undefined) { return { status: 'failed', error: 'invalid_enforce' } } - return setOrcaTeamMfaPolicy(getProfileUserDataPath(), { - orgId, - enforce: enforce as OrcaTeamMfaGrace | 'off' - }) + return setOrcaTeamMfaPolicy(getProfileUserDataPath(), { orgId, enforce }) } ) diff --git a/src/main/orca-profiles/cloud-mfa/profile-cloud-mfa-service.ts b/src/main/orca-profiles/cloud-mfa/profile-cloud-mfa-service.ts index a901526a3cb..f4349260791 100644 --- a/src/main/orca-profiles/cloud-mfa/profile-cloud-mfa-service.ts +++ b/src/main/orca-profiles/cloud-mfa/profile-cloud-mfa-service.ts @@ -15,7 +15,8 @@ import { ensureActiveOrcaProfile } from '../profile-index-store' * a compromised or misconfigured answer cannot send the person to another site to "set up MFA". */ export function normalizeAccountMfa(value: unknown, apiBaseUrl: string): OrcaAccountMfa { - const record = value && typeof value === 'object' ? (value as Record) : {} + const record: Record = + typeof value === 'object' && value !== null ? { ...value } : {} const mfa = record.mfa === 'on' || record.mfa === 'off' ? record.mfa : null let setupUrl: URL | null = null try { @@ -46,10 +47,13 @@ async function fetchAccountMfa( if (!response.ok) { throw new OrcaCloudRequestError(response.status, await extractErrorCode(response)) } - return normalizeAccountMfa((await response.json()) as unknown, config.apiBaseUrl) + const body: unknown = await response.json() + return normalizeAccountMfa(body, config.apiBaseUrl) } -export async function getOrcaProfileAccountMfa(userDataPath: string): Promise { +export async function getOrcaProfileAccountMfa( + userDataPath: string +): Promise { const active = ensureActiveOrcaProfile(userDataPath) // Local dev auth has no server and no factor to enrol. if (isOrcaCloudDevAuthEnabled()) { diff --git a/src/main/orca-profiles/cloud-mfa/profile-cloud-team-mfa-service.ts b/src/main/orca-profiles/cloud-mfa/profile-cloud-team-mfa-service.ts index ae7f8101976..88cb059de74 100644 --- a/src/main/orca-profiles/cloud-mfa/profile-cloud-team-mfa-service.ts +++ b/src/main/orca-profiles/cloud-mfa/profile-cloud-team-mfa-service.ts @@ -25,6 +25,10 @@ import { } from '../cloud-org/profile-cloud-org-members-service' import { ensureActiveOrcaProfile } from '../profile-index-store' +function isRecord(value: unknown): value is Record { + return typeof value === 'object' && value !== null +} + const SELF_STATES: readonly OrcaTeamMfaSelfState[] = ['on', 'off', 'pending', 'blocked', 'unknown'] function finiteNumber(value: unknown): number | null { @@ -47,27 +51,25 @@ export function sameOriginUrl(value: unknown, apiBaseUrl: string): string | null } function normalizeDeadline(value: unknown): { enforceAt: number; setAt: number } | null { - const record = value && typeof value === 'object' ? (value as Record) : null + const record = isRecord(value) ? value : null const enforceAt = finiteNumber(record?.enforceAt) const setAt = finiteNumber(record?.setAt) return enforceAt !== null && setAt !== null ? { enforceAt, setAt } : null } export function normalizeTeamMfaPolicy(value: unknown, apiBaseUrl: string): OrcaTeamMfaPolicy { - const record = value && typeof value === 'object' ? (value as Record) : null + const record = isRecord(value) ? value : null const setupUrl = sameOriginUrl(record?.setupUrl, apiBaseUrl) if (!record || !setupUrl) { throw new Error('invalid_orca_team_mfa_policy') } - const selfRecord = - record.self && typeof record.self === 'object' ? (record.self as Record) : {} + const selfRecord = isRecord(record.self) ? record.self : {} // An unrecognized state is "unknown", never "blocked": only the server's word blocks anyone. const self = SELF_STATES.find((state) => state === selfRecord.mfa) ?? 'unknown' const pendingMembers = finiteNumber(record.pendingMembers) - const inactive = - record.inactivePolicy && typeof record.inactivePolicy === 'object' - ? finiteNumber((record.inactivePolicy as Record).enforceAt) - : null + const inactive = isRecord(record.inactivePolicy) + ? finiteNumber(record.inactivePolicy.enforceAt) + : null return { policy: normalizeDeadline(record.policy), self, @@ -86,10 +88,10 @@ export function normalizeTeamMfaPolicy(value: unknown, apiBaseUrl: string): Orca } export function normalizePendingMembers(value: unknown): OrcaTeamMfaPendingMember[] { - const record = value && typeof value === 'object' ? (value as Record) : null + const record = isRecord(value) ? value : null const members = Array.isArray(record?.members) ? record.members : [] return members.flatMap((entry): OrcaTeamMfaPendingMember[] => { - const member = entry && typeof entry === 'object' ? (entry as Record) : null + const member = isRecord(entry) ? entry : null const email = typeof member?.email === 'string' ? member.email.trim() : '' if (!member || !email) { return [] @@ -111,7 +113,8 @@ async function request(url: string, init: RequestInit): Promise { if (!response.ok) { throw new OrcaCloudRequestError(response.status, await extractErrorCode(response)) } - return (await response.json()) as unknown + const body: unknown = await response.json() + return body } type Unavailable = Extract< @@ -203,7 +206,8 @@ export async function setOrcaTeamMfaPolicy( args.enforce === 'off' ? { ...requestInit('GET', token), method: 'DELETE' } : { ...requestInit('POST', token, { enforce: args.enforce }), method: 'PUT' } - const body = (await request(url('/mfa-policy'), init)) as Record | null + const answer = await request(url('/mfa-policy'), init) + const body = isRecord(answer) ? answer : null return { policy: normalizeDeadline(body?.policy), pendingMembers: finiteNumber(body?.pendingMembers) ?? 0 @@ -254,10 +258,11 @@ export async function remindOrcaTeamMfa( const result = await call( userDataPath, async (_apiBaseUrl, token, url) => { - const body = (await request( + const answer = await request( url('/mfa-policy/remind'), requestInit('POST', token, args.userId ? { userId: args.userId } : {}) - )) as Record | null + ) + const body = isRecord(answer) ? answer : null const delivery: 'log' | 'test-inbox' | 'unknown' = body?.delivery === 'log' || body?.delivery === 'test-inbox' ? body.delivery : 'unknown' return { recipients: finiteNumber(body?.recipients) ?? 0, delivery } diff --git a/src/main/orca-profiles/cloud-org/profile-cloud-org-members-client.ts b/src/main/orca-profiles/cloud-org/profile-cloud-org-members-client.ts index 26ad8e230d3..90e14c3e393 100644 --- a/src/main/orca-profiles/cloud-org/profile-cloud-org-members-client.ts +++ b/src/main/orca-profiles/cloud-org/profile-cloud-org-members-client.ts @@ -1,6 +1,6 @@ +import type { OrcaOrgMemberMfa } from '../../../shared/orca-account-mfa' import type { OrcaOrgMember, - OrcaOrgMemberMfa, OrcaOrgMembersRoster, OrcaOrgPendingInvite, OrcaOrgRole diff --git a/src/renderer/src/components/team-mfa/TeamMfaBlockedScreen.tsx b/src/renderer/src/components/team-mfa/TeamMfaBlockedScreen.tsx index bb18047e2ec..f89665070ec 100644 --- a/src/renderer/src/components/team-mfa/TeamMfaBlockedScreen.tsx +++ b/src/renderer/src/components/team-mfa/TeamMfaBlockedScreen.tsx @@ -84,13 +84,7 @@ export function TeamMfaBlockedScreen({ : null}

{onUsePersonal ? ( - ) : null} diff --git a/src/renderer/src/components/team-mfa/team-mfa.test.tsx b/src/renderer/src/components/team-mfa/team-mfa.test.tsx index 18999d7b3b2..34bc8f81f65 100644 --- a/src/renderer/src/components/team-mfa/team-mfa.test.tsx +++ b/src/renderer/src/components/team-mfa/team-mfa.test.tsx @@ -212,8 +212,8 @@ describe('Require MFA row', () => { describe('the server refusal in cloud workspace errors', () => { it('says to set up MFA, never to sign in again', () => { - for (const describe of [describeError, describeInlineError]) { - const message = describe('unauthorized', 'mfa_required', translate) + for (const explain of [describeError, describeInlineError]) { + const message = explain('unauthorized', 'mfa_required', translate) expect(message).toContain('requires two-factor authentication') expect(message).not.toMatch(/sign in/i) } diff --git a/src/renderer/src/i18n/en-runtime-required.json b/src/renderer/src/i18n/en-runtime-required.json index a987535364d..83d3ecc7cc7 100644 --- a/src/renderer/src/i18n/en-runtime-required.json +++ b/src/renderer/src/i18n/en-runtime-required.json @@ -1374,6 +1374,18 @@ "unavailableCount_one": "{{count}} unavailable", "unavailableCount_other": "{{count}} unavailable" }, + "teamMfa": { + "daysLeft_one": "{{count}} day left", + "daysLeft_other": "{{count}} days left", + "nowWarning_one": "{{count}} member doesn’t have it yet and will lose access to this team right away.", + "nowWarning_other": "{{count}} members don’t have it yet and will lose access to this team right away.", + "pendingCount_one": "{{count}} member still needs it", + "pendingCount_other": "{{count}} members still need it", + "reminded_one": "Reminder sent to {{count}} member.", + "reminded_other": "Reminder sent to {{count}} members.", + "soonTitle_one": "{{value0}} requires MFA in {{count}} day", + "soonTitle_other": "{{value0}} requires MFA in {{count}} days" + }, "teammates": { "daySandboxHours_one": "{{hours}} sandbox hour", "daySandboxHours_other": "{{hours}} sandbox hours" diff --git a/src/shared/orca-account-mfa.ts b/src/shared/orca-account-mfa.ts index 12596bc0cda..2274d9784eb 100644 --- a/src/shared/orca-account-mfa.ts +++ b/src/shared/orca-account-mfa.ts @@ -11,3 +11,17 @@ export type OrcaAccountMfaResult = /** `unsupported`: this Orca Cloud deployment (or local dev auth) has no MFA status. */ | { status: 'unconfigured' | 'reconnect-required' | 'unsupported' } | { status: 'failed'; error: string } + +/** + * A team member's two-factor state as owners and admins see it. Absent for everyone else, and + * for a member the provider said nothing about. 'pending': a team policy requires it by the + * member's `mfaDeadline` and they have not set it up. + */ +export type OrcaOrgMemberMfa = 'on' | 'off' | 'pending' + +/** The two-factor fields of a team member (OrcaOrgMember in orca-profiles.ts). */ +export type OrcaOrgMemberMfaFields = { + mfa?: OrcaOrgMemberMfa + /** Epoch ms the member must have MFA by; only with `mfa: 'pending'`. */ + mfaDeadline?: number +} diff --git a/src/shared/orca-profiles.ts b/src/shared/orca-profiles.ts index 4ce8ec0bdc8..1f8d3daf904 100644 --- a/src/shared/orca-profiles.ts +++ b/src/shared/orca-profiles.ts @@ -1,3 +1,4 @@ +import type { OrcaOrgMemberMfaFields } from './orca-account-mfa' import './constants' import { ORCA_BROWSER_PARTITION } from './browser/browser-session-partition' import type { ExecutionHostId } from './execution-host' @@ -250,7 +251,8 @@ export type RefreshCurrentOrcaProfileAuthResult = // Mirror the server’s fixed role enum so UI options cannot drift from accepted API values. export type OrcaOrgRole = 'owner' | 'admin' | 'member' -export type OrcaOrgMember = { +// Two-factor state (owners and admins only) lives in OrcaOrgMemberMfaFields. +export type OrcaOrgMember = OrcaOrgMemberMfaFields & { // Provisioned teammates without sign-in have no userId; disable mutations requiring that key. userId: string | null email: string @@ -258,18 +260,8 @@ export type OrcaOrgMember = { /** Profile picture from the identity provider (https only); avatars fall back to initials. */ pictureUrl?: string role: OrcaOrgRole - /** - * Whether the member has two-factor authentication. The server sends it to owners and admins - * only, so it is absent for everyone else, and for a member the provider said nothing about. - * 'pending': a team policy requires it by `mfaDeadline` and the member has not set it up. - */ - mfa?: OrcaOrgMemberMfa - /** Epoch ms the member must have MFA by; only with `mfa: 'pending'`. */ - mfaDeadline?: number } -export type OrcaOrgMemberMfa = 'on' | 'off' | 'pending' - export type OrcaOrgPendingInvite = { email: string role: OrcaOrgRole