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 ? (
-