diff --git a/src/Exceptionless.Web/ClientApp/src/lib/features/auth/index.svelte.ts b/src/Exceptionless.Web/ClientApp/src/lib/features/auth/index.svelte.ts index 2ff0f366ad..ee01bc4e54 100644 --- a/src/Exceptionless.Web/ClientApp/src/lib/features/auth/index.svelte.ts +++ b/src/Exceptionless.Web/ClientApp/src/lib/features/auth/index.svelte.ts @@ -280,9 +280,9 @@ function waitForUrl(popup: Window, redirectUri: string): Promise { afterEach(() => { vi.useRealTimers(); vi.unstubAllGlobals(); + vi.restoreAllMocks(); }); + it('waits for the callback without logging expected cross-origin access errors', async () => { + // Arrange + vi.stubGlobal('crypto', { randomUUID: () => 'synthetic-state' }); + const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}); + const blockedRead = vi.spyOn(popup.location, 'href', 'get').mockImplementation(() => { + throw new DOMException('Synthetic cross-origin popup read', 'SecurityError'); + }); + + // Act + const login = microsoftLogin(); + await vi.advanceTimersByTimeAsync(1000); + const exchangesBeforeCallback = postJSON.mock.calls.length; + popup.location = new URL('http://localhost:7131/?code=synthetic-code&state=synthetic-state'); + await vi.advanceTimersByTimeAsync(500); + await login; + + // Assert + expect(blockedRead).toHaveBeenCalledTimes(2); + expect(consoleError).not.toHaveBeenCalled(); + expect(exchangesBeforeCallback).toBe(0); + expect(postJSON).toHaveBeenCalledOnce(); + expect(popup.close).toHaveBeenCalledOnce(); + }); + + it.each([new DOMException('Synthetic unrelated DOM error', 'InvalidStateError'), Object.assign(new Error('Synthetic error'), { name: 'SecurityError' })])( + 'logs an unexpected popup error (%s)', + async (error) => { + // Arrange + vi.stubGlobal('crypto', { randomUUID: () => 'synthetic-state' }); + const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}); + vi.spyOn(popup.location, 'href', 'get').mockImplementationOnce(() => { + throw error; + }); + + // Act + const login = microsoftLogin(); + await vi.advanceTimersByTimeAsync(500); + popup.location = new URL('http://localhost:7131/?code=synthetic-code&state=synthetic-state'); + await vi.advanceTimersByTimeAsync(500); + await login; + + // Assert + expect(consoleError).toHaveBeenCalledExactlyOnceWith(error); + } + ); + it.each([true, false])('validates state and preserves invitation and redirect with randomUUID available: %s', async (hasRandomUUID) => { const getRandomValues = vi.fn((bytes: Uint8Array) => bytes.fill(10)); vi.stubGlobal('crypto', { getRandomValues, randomUUID: hasRandomUUID ? () => 'oauth-state' : undefined });