diff --git a/src/main/browser/browser-session-partition-policies.ts b/src/main/browser/browser-session-partition-policies.ts index 1d55e607b60..d55dbc30d78 100644 --- a/src/main/browser/browser-session-partition-policies.ts +++ b/src/main/browser/browser-session-partition-policies.ts @@ -96,7 +96,11 @@ function resolvePermissionNoticeUrl( export type BrowserPartitionDownloadPolicy = 'route' | 'deny' export type BrowserPartitionPermissionPolicy = 'browser' | 'deny' -export function installBrowserSessionPartitionPolicies( +// Why async despite no await: the user agent policy is configured before the first suspension, and +// getBrowserProcessUserAgentIdentity throws when the process identity was never initialized. Callers +// report failure through the promise (`void install(...).catch(...)`), so a synchronous throw would +// escape every one of them and gate browser-session startup on bookkeeping that is allowed to fail. +export async function installBrowserSessionPartitionPolicies( profile: BrowserSessionProfile, options: { downloads?: BrowserPartitionDownloadPolicy diff --git a/src/main/browser/browser-session-partition-proxy-install.test.ts b/src/main/browser/browser-session-partition-proxy-install.test.ts index c882b457be8..35f7b923732 100644 --- a/src/main/browser/browser-session-partition-proxy-install.test.ts +++ b/src/main/browser/browser-session-partition-proxy-install.test.ts @@ -25,6 +25,8 @@ const { sessionsByPartition, fromPartitionMock } = vi.hoisted(() => { return { sessionsByPartition, fromPartitionMock } }) +const identityState = vi.hoisted(() => ({ unavailable: false })) + vi.mock('electron', () => ({ session: { defaultSession: { resolveProxy: vi.fn(async () => 'DIRECT'), setProxy: vi.fn(async () => {}) }, @@ -47,10 +49,16 @@ vi.mock('./browser-session-ua', () => ({ installBrowserSessionUserAgentPolicy: vi.fn(() => vi.fn()) })) vi.mock('./browser-process-user-agent', () => ({ - getBrowserProcessUserAgentIdentity: () => ({ - mode: 'clean', - userAgent: 'Mozilla/5.0 Chrome/150.0.0.0 Safari/537.36' - }) + getBrowserProcessUserAgentIdentity: () => { + // The real one throws when the process identity was never initialized. + if (identityState.unavailable) { + throw new Error('Browser process user agent is not initialized') + } + return { + mode: 'clean', + userAgent: 'Mozilla/5.0 Chrome/150.0.0.0 Safari/537.36' + } + } })) vi.mock('./browser-webauthn-access', () => ({ allowsBrowserWebAuthnPermission: vi.fn(() => false), @@ -100,6 +108,23 @@ describe('installBrowserSessionPartitionPolicies proxy wiring', () => { afterEach(() => { vi.unstubAllEnvs() + identityState.unavailable = false + }) + + // The installer returns Promise, so every caller reports failure through the promise — + // `void install(...).catch(...)` at browser-session-registry.ts:136 and :336, and a bare + // `void install(...)` at browser-session-route-policies.ts:16. The user agent policy is + // configured synchronously before the first await, so a throw from there escapes all of them + // and takes down browser-session startup instead of being reported. + it('reports an unavailable process identity through the promise, not a synchronous throw', async () => { + const profile = nextProfile() + identityState.unavailable = true + + let installation: Promise | undefined + expect(() => { + installation = installBrowserSessionPartitionPolicies(profile) + }).not.toThrow() + await expect(installation).rejects.toThrow('Browser process user agent is not initialized') }) // Why (STA-4779): the installer is the single funnel every browser partition passes through.