Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ function waitForUrl(popup: Window, redirectUri: string): Promise<OAuthResponseDa
popup?.close();
}
} catch (error) {
console.error(error);
// Ignore DOMException: Blocked a frame with origin from accessing a cross-origin frame.
// A hack to get around same-origin security policy errors in IE.
if (!(error instanceof DOMException && error.name === 'SecurityError')) {
console.error(error);
}
}
}, 500);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,55 @@ describe('microsoftLogin', () => {
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 });
Expand Down
Loading