mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-24 00:44:46 +00:00
📱 fix: Open Sign-In and External Links From iOS Home-Screen Apps (#16077)
* 📱 fix: Open Sign-In and External Links From iOS Home-Screen Apps
Sign-in prompts, the MCP OAuth Continue buttons and the account menu links opened with window.open and a noopener,noreferrer features string. WebKit treats a features string as a popup-window request, which an iOS home-screen app cannot open, and the chat sign-in also awaited its CSRF bind before opening, outside the tap. Links now open through a target=_blank link with rel=noopener noreferrer, and the chat prompt binds when it appears so the tap opens the provider synchronously.
* fix: Keep sign-in closed until its CSRF bind lands, and surface builder OAuth in its dialog
A tap while the chat prompt's bind was still in flight could open the provider before its CSRF cookie existed, and an Action callback has no fallback without that cookie; the button now stays disabled until the bind lands. The MCP builder card opened the authorization URL only after its initialize request, which iOS home-screen apps refuse and which left the card with no way to continue; it now shows the MCP OAuth dialog instead.
* fix: Let the tapped prompt own the CSRF binding, and show only the card's live OAuth flow
Live sign-in prompts share one CSRF cookie per callback path, so the last prompt to bind owned it; a tap now re-binds its own prompt right after opening the provider. The builder card kept a local copy of the authorization URL that outlived its flow and could reopen it on a later initialization; it now shows the manager's shared flow URL, cleared when the flow ends, and only for a flow the card itself started.
This commit is contained in:
parent
c7665ab1ae
commit
038903abc0
11 changed files with 440 additions and 42 deletions
|
|
@ -0,0 +1,151 @@
|
|||
import React from 'react';
|
||||
import '@testing-library/jest-dom/extend-expect';
|
||||
import { render, screen, waitFor, fireEvent } from '@testing-library/react';
|
||||
import type { MCPServerStatusIconProps } from '~/components/MCP/MCPServerStatusIcon';
|
||||
import type { MCPServerDefinition } from '~/hooks';
|
||||
import MCPServerCard from './MCPServerCard';
|
||||
|
||||
const mockInitializeServer = jest.fn();
|
||||
const mockGetOAuthUrl = jest.fn();
|
||||
|
||||
jest.mock('~/hooks', () => ({
|
||||
useLocalize: () => (key: string) => key,
|
||||
useMCPServerManager: () => ({
|
||||
initializeServer: mockInitializeServer,
|
||||
revokeOAuthForServer: jest.fn(),
|
||||
getOAuthUrl: mockGetOAuthUrl,
|
||||
}),
|
||||
}));
|
||||
|
||||
jest.mock('@librechat/client', () => ({
|
||||
MCPIcon: () => <span />,
|
||||
}));
|
||||
|
||||
jest.mock('./MCPCardActions', () => ({
|
||||
__esModule: true,
|
||||
default: ({ onInitialize }: { onInitialize: () => void }) => (
|
||||
<button type="button" onClick={onInitialize}>
|
||||
{'com_nav_mcp_connect'}
|
||||
</button>
|
||||
),
|
||||
}));
|
||||
|
||||
jest.mock('./MCPServerDialog', () => ({ __esModule: true, default: () => null }));
|
||||
jest.mock('./MCPStatusBadge', () => ({ getStatusDotColor: () => '' }));
|
||||
jest.mock('~/components/ui/CustomIcon', () => ({ __esModule: true, default: () => null }));
|
||||
jest.mock('~/components/MCP/McpOAuthDialog', () => ({
|
||||
__esModule: true,
|
||||
default: ({ open, oauthUrl }: { open: boolean; oauthUrl: string }) =>
|
||||
open ? <div role="dialog">{oauthUrl}</div> : null,
|
||||
}));
|
||||
|
||||
const server = {
|
||||
serverName: 'clickhouse',
|
||||
config: {},
|
||||
effectivePermissions: 0,
|
||||
} as unknown as MCPServerDefinition;
|
||||
|
||||
/** Mirrors the manager's shared state: the flow URL exists only while initializing. */
|
||||
const renderCard = (isInitializing: boolean, sharedOAuthUrl: string | null = null) => {
|
||||
mockGetOAuthUrl.mockReturnValue(sharedOAuthUrl);
|
||||
const statusProps = {
|
||||
serverName: 'clickhouse',
|
||||
serverStatus: { connectionState: 'disconnected', requiresOAuth: true },
|
||||
onConfigClick: jest.fn(),
|
||||
isInitializing,
|
||||
canCancel: true,
|
||||
onCancel: jest.fn(),
|
||||
hasCustomUserVars: false,
|
||||
} as unknown as MCPServerStatusIconProps;
|
||||
return (
|
||||
<MCPServerCard
|
||||
server={server}
|
||||
getServerStatusIconProps={() => statusProps}
|
||||
canCreateEditMCPs={false}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
const firstUrl = 'https://auth.example/authorize?flow=1';
|
||||
|
||||
describe('MCPServerCard', () => {
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
test('surfaces the authorization URL in the OAuth dialog instead of opening a tab', async () => {
|
||||
const open = jest.spyOn(window, 'open').mockImplementation(() => null);
|
||||
mockInitializeServer.mockResolvedValue({
|
||||
success: true,
|
||||
oauthRequired: true,
|
||||
oauthUrl: firstUrl,
|
||||
});
|
||||
const { rerender } = render(renderCard(false));
|
||||
|
||||
fireEvent.click(screen.getByText('com_nav_mcp_connect'));
|
||||
await waitFor(() => expect(mockInitializeServer).toHaveBeenCalledWith('clickhouse', false));
|
||||
rerender(renderCard(true, firstUrl));
|
||||
|
||||
expect(await screen.findByRole('dialog')).toHaveTextContent(firstUrl);
|
||||
expect(open).not.toHaveBeenCalled();
|
||||
|
||||
rerender(renderCard(false));
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
||||
open.mockRestore();
|
||||
});
|
||||
|
||||
test('keeps the OAuth dialog closed when connecting needs no authorization', async () => {
|
||||
mockInitializeServer.mockResolvedValue({ success: true, oauthRequired: false });
|
||||
const { rerender } = render(renderCard(false));
|
||||
|
||||
fireEvent.click(screen.getByText('com_nav_mcp_connect'));
|
||||
await waitFor(() => expect(mockInitializeServer).toHaveBeenCalled());
|
||||
rerender(renderCard(true));
|
||||
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('stays closed for flows it did not start once its own flow ended', async () => {
|
||||
mockInitializeServer.mockResolvedValueOnce({
|
||||
success: true,
|
||||
oauthRequired: true,
|
||||
oauthUrl: firstUrl,
|
||||
});
|
||||
const { rerender } = render(renderCard(false));
|
||||
fireEvent.click(screen.getByText('com_nav_mcp_connect'));
|
||||
rerender(renderCard(true, firstUrl));
|
||||
expect(await screen.findByRole('dialog')).toBeInTheDocument();
|
||||
rerender(renderCard(false));
|
||||
|
||||
/** Another surface, such as the chat's MCP menu, initializes the same server. */
|
||||
rerender(renderCard(true, 'https://auth.example/authorize?flow=2'));
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
test('does not reopen a previous flow while the next connect is pending', async () => {
|
||||
mockInitializeServer.mockResolvedValueOnce({
|
||||
success: true,
|
||||
oauthRequired: true,
|
||||
oauthUrl: firstUrl,
|
||||
});
|
||||
const { rerender } = render(renderCard(false));
|
||||
fireEvent.click(screen.getByText('com_nav_mcp_connect'));
|
||||
rerender(renderCard(true, firstUrl));
|
||||
expect(await screen.findByRole('dialog')).toBeInTheDocument();
|
||||
rerender(renderCard(false));
|
||||
|
||||
let finishSecond: ((value: { success: boolean; oauthRequired: boolean }) => void) | undefined;
|
||||
mockInitializeServer.mockReturnValueOnce(
|
||||
new Promise((resolve) => {
|
||||
finishSecond = resolve;
|
||||
}),
|
||||
);
|
||||
fireEvent.click(screen.getByText('com_nav_mcp_connect'));
|
||||
rerender(renderCard(true));
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
||||
|
||||
finishSecond!({ success: true, oauthRequired: false });
|
||||
await waitFor(() => expect(mockInitializeServer).toHaveBeenCalledTimes(2));
|
||||
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
import { useState, useRef } from 'react';
|
||||
import { useState, useRef, useEffect } from 'react';
|
||||
import { MCPIcon } from '@librechat/client';
|
||||
import { PermissionBits, hasPermissions } from 'librechat-data-provider';
|
||||
import type { MCPServerStatusIconProps } from '~/components/MCP/MCPServerStatusIcon';
|
||||
import type { MCPServerDefinition } from '~/hooks';
|
||||
import McpOAuthDialog from '~/components/MCP/McpOAuthDialog';
|
||||
import { useMCPServerManager, useLocalize } from '~/hooks';
|
||||
import { getStatusDotColor } from './MCPStatusBadge';
|
||||
import CustomIcon from '~/components/ui/CustomIcon';
|
||||
|
|
@ -31,8 +32,9 @@ export default function MCPServerCard({
|
|||
}: MCPServerCardProps) {
|
||||
const localize = useLocalize();
|
||||
const triggerRef = useRef<HTMLDivElement>(null);
|
||||
const { initializeServer, revokeOAuthForServer } = useMCPServerManager();
|
||||
const { initializeServer, revokeOAuthForServer, getOAuthUrl } = useMCPServerManager();
|
||||
const [dialogOpen, setDialogOpen] = useState(false);
|
||||
const [awaitingOAuth, setAwaitingOAuth] = useState(false);
|
||||
|
||||
const statusIconProps = getServerStatusIconProps(server.serverName);
|
||||
const {
|
||||
|
|
@ -49,8 +51,20 @@ export default function MCPServerCard({
|
|||
const description = server.config?.description;
|
||||
const statusDotColor = getStatusDotColor(serverStatus, isInitializing);
|
||||
const canEdit = canCreateEditMCPs && canEditThisServer;
|
||||
/** The shared flow URL is cleared when its initialization ends, so it can never be stale. */
|
||||
const sharedOAuthUrl = getOAuthUrl(server.serverName);
|
||||
|
||||
const handleInitialize = () => {
|
||||
useEffect(() => {
|
||||
if (!isInitializing) {
|
||||
setAwaitingOAuth(false);
|
||||
}
|
||||
}, [isInitializing]);
|
||||
|
||||
/**
|
||||
* `autoOpenOAuth=false` surfaces the authorization URL in the OAuth dialog, whose Continue opens
|
||||
* it inside the tap. A tab opened after the initialize request is blocked on iOS home-screen apps.
|
||||
*/
|
||||
const handleInitialize = async () => {
|
||||
/** If server has custom user vars and is not already connected, show config dialog first
|
||||
* This ensures users can enter credentials before initialization attempts
|
||||
*/
|
||||
|
|
@ -58,7 +72,11 @@ export default function MCPServerCard({
|
|||
onConfigClick({ stopPropagation: () => {}, preventDefault: () => {} } as React.MouseEvent);
|
||||
return;
|
||||
}
|
||||
initializeServer(server.serverName);
|
||||
setAwaitingOAuth(false);
|
||||
const response = await initializeServer(server.serverName, false);
|
||||
if (response?.oauthRequired && response.oauthUrl) {
|
||||
setAwaitingOAuth(true);
|
||||
}
|
||||
};
|
||||
|
||||
const handleRevoke = () => {
|
||||
|
|
@ -156,6 +174,17 @@ export default function MCPServerCard({
|
|||
server={server}
|
||||
/>
|
||||
)}
|
||||
<McpOAuthDialog
|
||||
open={awaitingOAuth && isInitializing && sharedOAuthUrl != null}
|
||||
onOpenChange={(open) => {
|
||||
if (!open) {
|
||||
setAwaitingOAuth(false);
|
||||
}
|
||||
}}
|
||||
serverName={server.serverName}
|
||||
oauthUrl={sharedOAuthUrl ?? ''}
|
||||
iconUrl={server.config?.iconPath}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue