From a06ee14b7555ea3c9645ef6b5cb6e0342439e838 Mon Sep 17 00:00:00 2001 From: Marco Beretta <81851188+berry-13@users.noreply.github.com> Date: Mon, 12 Oct 2026 16:33:08 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=90=20fix:=20Encrypt=20User=20Plugin?= =?UTF-8?q?=20Credentials=20With=20Random-IV=20v3=20Format=20(#16996)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: Encrypt User Plugin Credentials With Random-IV v3 Format * test: Cover Credential Encryption Formats in Mock E2E Scenarios * test: Round-Trip Real Credential Helpers in PluginService Spec * 🙈 fix: Remove SharePoint Picker Payload Logging (#16997) * fix: Add credentials.writeFormat to Gate v3 Credential Writes During Rollouts * test: Expect 201 When the Legacy Write Format Override Is Created * 🔊 fix: Clear TTS Audio Cache On Session End and Hash Its Keys (#16998) * fix: Clear TTS Audio Cache On Session End and Hash Its Keys * fix: Read Manual TTS Replays From the Hashed Cache Key * fix: Purge Legacy TTS Cache Keys and Fence Writes Across Logout * fix: Keep TTS Cache Writes Off the Playback Path * test: Expect Hashed Keys in the StreamAudio Cache Lookup * test: Use a Single-Token Reply Label in the TTS Cache Scenario * fix: Settle TTS Cache Clear Before IdP Logout and Across Tabs * test: Cover the TTS Cache Fence When Another Tab Logs Out * test: Wait for the Second Tab Before Logging Out in the TTS Scenario * fix: Latch TTS Cache Writes Shut Until the Tab Authenticates Again * fix: Reopen TTS Cache Writes When Authentication Completes In Place * fix: Leave for the IdP Even When TTS Cache Cleanup Fails * fix: Retry a Failed Legacy TTS Purge and Fence Other Tabs on Manual Clear * fix: Rerun the Legacy TTS Purge After Each Clear and Sign-In * fix: Rerun the Legacy TTS Purge When Another Tab Clears * fix: Fence Pre-Auth TTS Writes, Retry Failed Clears, and Fall Back to Storage Events * fix: Version the Hashed TTS Cache Key Path * fix: Move TTS Audio to a New Cache and Keep It Closed Until a Failed Clear Succeeds * fix: Persist a Pending TTS Cache Delete Across Navigation * fix: Reopen TTS Caching Only on Session Start and Fence Cache Reads * fix: Keep the TTS Delete Fence in Cache Storage and Close Reads After Logout * refactor: Keep Read-Aloud Audio in Tab Memory Instead of Cache Storage * test: Return to the Conversation After Re-Login in the TTS Logout Scenario * test: Close the Phone Sidebar Before Replaying in the TTS Logout Scenario * fix: Bound the In-Memory TTS Cache by Size as Well as Count * fix: Scope Cached TTS Audio to the Signed-In User * fix: Keep the Settings Cache Delete Available While Legacy Audio Remains * fix: Recheck Legacy TTS Audio After a Settings Clear and on Unowned Handoff * fix: Fence TTS Writes Started Before an Account Signs In * test: Opt the Fresh TTS Account Into Enter-to-Send --- api/server/controllers/UserController.js | 8 +- api/server/controllers/UserController.spec.js | 3 + api/server/services/PluginService.js | 9 +- api/server/services/PluginService.spec.js | 53 ++- .../src/components/Chat/Input/StreamAudio.tsx | 30 +- .../Chat/Input/__tests__/StreamAudio.spec.tsx | 20 +- .../Nav/SettingsTabs/Data/DeleteCache.tsx | 12 +- .../Data/__tests__/DeleteCache.test.tsx | 59 +++ client/src/data-provider/Files/sharepoint.ts | 4 +- client/src/hooks/AuthContext.tsx | 6 + client/src/hooks/Files/useSharePointPicker.ts | 30 -- .../hooks/Input/useTextToSpeechExternal.ts | 31 +- .../src/hooks/__tests__/AuthContext.spec.tsx | 109 +++++- client/src/main.jsx | 4 + client/src/utils/index.ts | 1 + client/src/utils/ttscache.spec.ts | 174 +++++++++ client/src/utils/ttscache.ts | 124 ++++++ e2e/config/librechat.e2e.yaml | 16 + e2e/setup/fake-mcp-server.js | 5 + .../scenarios/credential-encryption.spec.ts | 369 ++++++++++++++++++ .../scenarios/sharepoint-picker-logs.spec.ts | 99 +++++ e2e/specs/mock/scenarios/tts-cache.spec.ts | 226 +++++++++++ librechat.example.yaml | 8 + packages/api/src/agents/auth.ts | 5 +- packages/api/src/crypto/credentials.spec.ts | 90 +++++ packages/api/src/crypto/credentials.ts | 33 ++ packages/api/src/crypto/index.ts | 1 + packages/data-provider/src/config.ts | 14 + packages/data-schemas/src/app/service.spec.ts | 14 + packages/data-schemas/src/app/service.ts | 1 + packages/data-schemas/src/types/app.ts | 3 + 31 files changed, 1476 insertions(+), 85 deletions(-) create mode 100644 client/src/components/Nav/SettingsTabs/Data/__tests__/DeleteCache.test.tsx create mode 100644 client/src/utils/ttscache.spec.ts create mode 100644 client/src/utils/ttscache.ts create mode 100644 e2e/specs/mock/scenarios/credential-encryption.spec.ts create mode 100644 e2e/specs/mock/scenarios/sharepoint-picker-logs.spec.ts create mode 100644 e2e/specs/mock/scenarios/tts-cache.spec.ts create mode 100644 packages/api/src/crypto/credentials.spec.ts create mode 100644 packages/api/src/crypto/credentials.ts diff --git a/api/server/controllers/UserController.js b/api/server/controllers/UserController.js index 3dc4e0a6a6..9c57a6d584 100644 --- a/api/server/controllers/UserController.js +++ b/api/server/controllers/UserController.js @@ -312,7 +312,13 @@ const updateUserPluginsController = async (req, res) => { authService = pluginKey === Tools.web_search && value === '' ? await deleteUserPluginAuth(user.id, field) - : await updateUserPluginAuth(user.id, field, pluginKey, value); + : await updateUserPluginAuth( + user.id, + field, + pluginKey, + value, + appConfig?.credentials?.writeFormat, + ); if (authService instanceof Error) { logger.error('[authService]', authService); ({ status, message } = normalizeHttpError(authService)); diff --git a/api/server/controllers/UserController.spec.js b/api/server/controllers/UserController.spec.js index 34d932263d..51a3654068 100644 --- a/api/server/controllers/UserController.spec.js +++ b/api/server/controllers/UserController.spec.js @@ -226,6 +226,7 @@ describe('updateUserPluginsController', () => { await updateUserPluginsController( { config: { + credentials: { writeFormat: 'legacy' }, webSearch: { keenableApiKey: '${KEENABLE_API_KEY}', keenableApiUrl: '${KEENABLE_API_URL}', @@ -253,6 +254,7 @@ describe('updateUserPluginsController', () => { 'KEENABLE_API_KEY', 'web_search', 'new-key', + 'legacy', ); for (const selectionField of Object.values(webSearchSelectionFields)) { expect(updateUserPluginAuth).not.toHaveBeenCalledWith( @@ -260,6 +262,7 @@ describe('updateUserPluginsController', () => { selectionField, 'web_search', expect.anything(), + expect.anything(), ); } }); diff --git a/api/server/services/PluginService.js b/api/server/services/PluginService.js index 71941b9fc5..4c52aa1acf 100644 --- a/api/server/services/PluginService.js +++ b/api/server/services/PluginService.js @@ -1,5 +1,5 @@ const { logger } = require('@librechat/data-schemas'); -const { encrypt, decrypt } = require('@librechat/api'); +const { encryptCredential, decryptCredential } = require('@librechat/api'); const { findOnePluginAuth, updatePluginAuth, deletePluginAuth } = require('~/models'); /** @@ -47,7 +47,7 @@ const getUserPluginAuthValue = async (userId, authField, throwError = true, plug throw error; } - const decryptedValue = await decrypt(pluginAuth.value); + const decryptedValue = await decryptCredential(pluginAuth.value); return decryptedValue; } catch (err) { if (!throwError) { @@ -92,12 +92,13 @@ const getUserPluginAuthValue = async (userId, authField, throwError = true, plug * @param {string} authField * @param {string} pluginKey * @param {string} value + * @param {'v3' | 'legacy'} [writeFormat] - `credentials.writeFormat` from the app config * @returns {Promise} * @throws {Error} */ -const updateUserPluginAuth = async (userId, authField, pluginKey, value) => { +const updateUserPluginAuth = async (userId, authField, pluginKey, value, writeFormat) => { try { - const encryptedValue = await encrypt(value); + const encryptedValue = await encryptCredential(value, writeFormat); return await updatePluginAuth({ userId, authField, diff --git a/api/server/services/PluginService.spec.js b/api/server/services/PluginService.spec.js index c4858e6885..c656ae0025 100644 --- a/api/server/services/PluginService.spec.js +++ b/api/server/services/PluginService.spec.js @@ -1,22 +1,24 @@ const mockLoggerError = jest.fn(); const mockFindOnePluginAuth = jest.fn(); +const mockUpdatePluginAuth = jest.fn(); jest.mock('@librechat/data-schemas', () => ({ + ...jest.requireActual('@librechat/data-schemas'), logger: { error: mockLoggerError }, })); -jest.mock('@librechat/api', () => ({ - encrypt: jest.fn(), - decrypt: jest.fn(), -})); +jest.mock('@librechat/api', () => { + const { encryptCredential, decryptCredential } = jest.requireActual('@librechat/api'); + return { encryptCredential, decryptCredential }; +}); jest.mock('~/models', () => ({ findOnePluginAuth: (...args) => mockFindOnePluginAuth(...args), - updatePluginAuth: jest.fn(), + updatePluginAuth: (...args) => mockUpdatePluginAuth(...args), deletePluginAuth: jest.fn(), })); -const { getUserPluginAuthValue } = require('./PluginService'); +const { getUserPluginAuthValue, updateUserPluginAuth } = require('./PluginService'); describe('getUserPluginAuthValue', () => { beforeEach(() => { @@ -44,3 +46,42 @@ describe('getUserPluginAuthValue', () => { expect(mockLoggerError).toHaveBeenCalledWith('[getUserPluginAuthValue]', error); }); }); + +describe('plugin auth encryption', () => { + beforeEach(() => { + jest.clearAllMocks(); + }); + + it('stores a random-IV v3 value that reads back as the plaintext', async () => { + mockUpdatePluginAuth.mockImplementation(async (doc) => doc); + + await updateUserPluginAuth('user-id', 'API_KEY', 'plugin', 'plain'); + await updateUserPluginAuth('user-id', 'API_KEY', 'plugin', 'plain'); + + const [first, second] = mockUpdatePluginAuth.mock.calls.map(([doc]) => doc.value); + expect(first).toMatch(/^v3:[0-9a-f]{32}:[0-9a-f]+$/); + expect(second).not.toBe(first); + + mockFindOnePluginAuth.mockResolvedValue({ value: first }); + await expect(getUserPluginAuthValue('user-id', 'API_KEY')).resolves.toBe('plain'); + }); + + it('stores the legacy fixed-IV format when the write format is legacy', async () => { + const { encrypt } = jest.requireActual('@librechat/data-schemas'); + mockUpdatePluginAuth.mockImplementation(async (doc) => doc); + + await updateUserPluginAuth('user-id', 'API_KEY', 'plugin', 'plain', 'legacy'); + + const [[{ value }]] = mockUpdatePluginAuth.mock.calls; + expect(value).toBe(await encrypt('plain')); + mockFindOnePluginAuth.mockResolvedValue({ value }); + await expect(getUserPluginAuthValue('user-id', 'API_KEY')).resolves.toBe('plain'); + }); + + it('reads a value stored in the legacy fixed-IV format', async () => { + const { encrypt } = jest.requireActual('@librechat/data-schemas'); + mockFindOnePluginAuth.mockResolvedValue({ value: await encrypt('legacy-plain') }); + + await expect(getUserPluginAuthValue('user-id', 'API_KEY')).resolves.toBe('legacy-plain'); + }); +}); diff --git a/client/src/components/Chat/Input/StreamAudio.tsx b/client/src/components/Chat/Input/StreamAudio.tsx index a35a839183..4288474dc9 100644 --- a/client/src/components/Chat/Input/StreamAudio.tsx +++ b/client/src/components/Chat/Input/StreamAudio.tsx @@ -10,9 +10,15 @@ import { MediaSourceAppender, usePauseGlobalAudio, } from '~/hooks/Audio'; +import { + logger, + getTTSCacheKey, + setCachedTTSAudio, + getCachedTTSAudio, + getTTSCacheGeneration, +} from '~/utils'; import { useAuthContext } from '~/hooks'; import { globalAudioId } from '~/common'; -import { logger } from '~/utils'; import store from '~/store'; function timeoutPromise(ms: number, message?: string) { @@ -25,7 +31,7 @@ const promiseTimeoutMessage = 'Reader promise timed out'; const maxPromiseTime = 15000; export default function StreamAudio({ index = 0 }) { - const { token } = useAuthContext(); + const { token, user } = useAuthContext(); const cacheTTS = useRecoilValue(store.cacheTTS); const playbackRate = useRecoilValue(store.playbackRate); @@ -57,6 +63,7 @@ export default function StreamAudio({ index = 0 }) { async function fetchAudio() { setIsFetching(true); + const generation = getTTSCacheGeneration(); let mediaSource: MediaSourceAppender | undefined; try { @@ -68,15 +75,15 @@ export default function StreamAudio({ index = 0 }) { /** Keyed by the spoken text, as manual playback is, so audio cached from an * unfiltered message (reasoning included) is never replayed. */ - let cacheKey = latestMessage ? getSpeechText(latestMessage) : ''; - const cache = await caches.open('tts-responses'); - const cachedResponse = await cache.match(cacheKey); + let cacheKey = latestMessage + ? await getTTSCacheKey(getSpeechText(latestMessage), voice, user?.id) + : null; + const cachedAudio = cacheKey ? getCachedTTSAudio(cacheKey) : undefined; setAudioRunId(activeRunId); - if (cachedResponse) { + if (cachedAudio) { logger.log('Audio found in cache'); - const audioBlob = await cachedResponse.blob(); - const blobUrl = URL.createObjectURL(audioBlob); + const blobUrl = URL.createObjectURL(cachedAudio); setGlobalAudioURL(blobUrl); setIsFetching(false); return; @@ -141,12 +148,14 @@ export default function StreamAudio({ index = 0 }) { const targetMessage = latestMessages.find( (msg) => msg.messageId === latestMessage?.messageId, ); - cacheKey = targetMessage ? getSpeechText(targetMessage) : ''; + cacheKey = targetMessage + ? await getTTSCacheKey(getSpeechText(targetMessage), voice, user?.id) + : null; if (!cacheKey) { logger.warn('Cache key not found, skipping audio cache'); } else { logger.log('Adding audio to cache'); - await cache.put(cacheKey, new Response(audioBlob)); + setCachedTTSAudio(cacheKey, audioBlob, generation); } } } @@ -181,6 +190,7 @@ export default function StreamAudio({ index = 0 }) { audioRef, voice, token, + user?.id, ]); useEffect(() => { diff --git a/client/src/components/Chat/Input/__tests__/StreamAudio.spec.tsx b/client/src/components/Chat/Input/__tests__/StreamAudio.spec.tsx index 3ddf54f75a..24b3a0dc92 100644 --- a/client/src/components/Chat/Input/__tests__/StreamAudio.spec.tsx +++ b/client/src/components/Chat/Input/__tests__/StreamAudio.spec.tsx @@ -1,10 +1,13 @@ import React from 'react'; +import { TextEncoder } from 'util'; +import { webcrypto } from 'crypto'; import { RecoilRoot } from 'recoil'; import { render, waitFor } from '@testing-library/react'; import { MemoryRouter, Routes, Route } from 'react-router-dom'; import { Constants, QueryKeys } from 'librechat-data-provider'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import type { TMessage, TConversation } from 'librechat-data-provider'; +import { getTTSCacheKey, setCachedTTSAudio, getTTSCacheGeneration } from '~/utils'; import StreamAudio from '../StreamAudio'; import store from '~/store'; @@ -12,15 +15,14 @@ jest.mock('~/hooks', () => ({ useAuthContext: () => ({ token: 'token' }) })); const conversationId = 'convo-1'; -const match = jest.fn(async (_key: string) => ({ blob: async () => new Blob() })); +const recording = new Blob(['spoken answer'], { type: 'audio/mpeg' }); +const fetchMock = jest.fn(() => Promise.reject(new Error('not cached'))); describe('StreamAudio cache', () => { beforeAll(() => { - Object.defineProperty(global, 'caches', { - writable: true, - configurable: true, - value: { open: async () => ({ match, put: jest.fn() }) }, - }); + Object.defineProperty(global, 'crypto', { value: webcrypto, configurable: true }); + Object.assign(global, { TextEncoder }); + Object.assign(global, { fetch: fetchMock }); URL.createObjectURL = jest.fn(() => 'blob:audio'); URL.revokeObjectURL = jest.fn(); }); @@ -28,6 +30,8 @@ describe('StreamAudio cache', () => { /** Audio cached before reasoning was filtered is keyed by the full text; looking up the * spoken answer instead means that recording is never replayed. */ it('looks up cached audio by the spoken answer, not the stored text', async () => { + const key = await getTTSCacheKey('The capital of Germany is Berlin.'); + setCachedTTSAudio(key as string, recording, getTTSCacheGeneration()); const message = { messageId: 'assistant-1', conversationId, @@ -58,7 +62,7 @@ describe('StreamAudio cache', () => { , ); - await waitFor(() => expect(match).toHaveBeenCalled()); - expect(match).toHaveBeenCalledWith('The capital of Germany is Berlin.'); + await waitFor(() => expect(URL.createObjectURL).toHaveBeenCalledWith(recording)); + expect(fetchMock).not.toHaveBeenCalled(); }); }); diff --git a/client/src/components/Nav/SettingsTabs/Data/DeleteCache.tsx b/client/src/components/Nav/SettingsTabs/Data/DeleteCache.tsx index 7e5170d7e6..3f78c20104 100644 --- a/client/src/components/Nav/SettingsTabs/Data/DeleteCache.tsx +++ b/client/src/components/Nav/SettingsTabs/Data/DeleteCache.tsx @@ -9,6 +9,7 @@ import { InfoHoverCard, useOnClickOutside, } from '@librechat/client'; +import { clearTTSCache, hasLegacyTTSCache, hasCachedTTSAudio } from '~/utils'; import { useLocalize } from '~/hooks'; import { ESide } from '~/common'; @@ -22,9 +23,7 @@ export const DeleteCache = ({ disabled = false }: { disabled?: boolean }) => { useOnClickOutside(contentRef, () => confirmClear && setConfirmClear(false), []); const checkCache = useCallback(async () => { - const cache = await caches.open('tts-responses'); - const keys = await cache.keys(); - setIsCacheEmpty(keys.length === 0); + setIsCacheEmpty(!hasCachedTTSAudio() && !(await hasLegacyTTSCache())); }, []); useEffect(() => { @@ -33,10 +32,11 @@ export const DeleteCache = ({ disabled = false }: { disabled?: boolean }) => { const revokeAllUserKeys = useCallback(async () => { setIsLoading(true); - const cache = await caches.open('tts-responses'); - await cache.keys().then((keys) => Promise.all(keys.map((key) => cache.delete(key)))); + await clearTTSCache(); + /** A legacy delete that failed leaves the action available for another try. */ + await checkCache(); setIsLoading(false); - }, []); + }, [checkCache]); return (
diff --git a/client/src/components/Nav/SettingsTabs/Data/__tests__/DeleteCache.test.tsx b/client/src/components/Nav/SettingsTabs/Data/__tests__/DeleteCache.test.tsx new file mode 100644 index 0000000000..d5c2f1dfc5 --- /dev/null +++ b/client/src/components/Nav/SettingsTabs/Data/__tests__/DeleteCache.test.tsx @@ -0,0 +1,59 @@ +import React from 'react'; +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; +import { clearTTSCache, setCachedTTSAudio, getTTSCacheGeneration } from '~/utils'; +import { DeleteCache } from '../DeleteCache'; + +jest.mock('~/hooks', () => ({ + useLocalize: () => (key: string) => key, +})); + +const deleteButton = () => screen.getByRole('button', { name: 'com_nav_delete_cache_storage' }); + +describe('DeleteCache', () => { + const has = jest.fn(async (_name: string) => false); + + beforeEach(() => { + has.mockResolvedValue(false); + Object.defineProperty(globalThis, 'caches', { + value: { has, delete: jest.fn(async () => true) }, + configurable: true, + }); + clearTTSCache(); + }); + + afterEach(() => { + Reflect.deleteProperty(globalThis, 'caches'); + }); + + it('is disabled when nothing is cached in memory or left on disk', async () => { + render(); + await waitFor(() => expect(has).toHaveBeenCalled()); + expect(deleteButton()).toBeDisabled(); + }); + + it('is enabled while audio is cached in this tab', async () => { + setCachedTTSAudio('key', new Blob(['audio']), getTTSCacheGeneration()); + render(); + await waitFor(() => expect(deleteButton()).toBeEnabled()); + }); + + it("stays available when an earlier build's cache could not be deleted at startup", async () => { + has.mockImplementation(async (name: string) => name === 'tts-responses'); + render(); + await waitFor(() => expect(deleteButton()).toBeEnabled()); + }); + + it('stays available after a delete whose legacy cache could not be removed', async () => { + setCachedTTSAudio('key', new Blob(['audio']), getTTSCacheGeneration()); + has.mockImplementation(async (name: string) => name === 'tts-responses'); + render(); + await waitFor(() => expect(deleteButton()).toBeEnabled()); + + has.mockClear(); + fireEvent.click(deleteButton()); + fireEvent.click(await screen.findByRole('button', { name: 'com_ui_delete' })); + /** The recheck after the delete finds the legacy cache still there. */ + await waitFor(() => expect(has).toHaveBeenCalled()); + expect(deleteButton()).toBeEnabled(); + }); +}); diff --git a/client/src/data-provider/Files/sharepoint.ts b/client/src/data-provider/Files/sharepoint.ts index 596448c1b8..9cc08938cb 100644 --- a/client/src/data-provider/Files/sharepoint.ts +++ b/client/src/data-provider/Files/sharepoint.ts @@ -160,7 +160,7 @@ export const useSharePointBatchDownload = (): UseMutationResult< return downloadedFile; } catch (error) { - console.error(`Failed to download ${file.name}:`, error); + console.error('SharePoint file download failed'); failed.push(file.name); completed++; onProgress?.({ @@ -183,7 +183,7 @@ export const useSharePointBatchDownload = (): UseMutationResult< } if (failed.length > 0) { - console.warn(`Failed to download ${failed.length} files:`, failed); + console.warn(`Failed to download ${failed.length} files`); } return downloadedFiles; diff --git a/client/src/hooks/AuthContext.tsx b/client/src/hooks/AuthContext.tsx index e4752f8055..640febf32d 100644 --- a/client/src/hooks/AuthContext.tsx +++ b/client/src/hooks/AuthContext.tsx @@ -25,7 +25,9 @@ import { import type * as t from 'librechat-data-provider'; import type { ReactNode } from 'react'; import { + clearTTSCache, isSafeRedirect, + setTTSCacheOwner, getPostLoginRedirect, clearPostLoginRedirect, persistRedirectToSession, @@ -66,6 +68,7 @@ const endSessionClientState = (): void => { clearRetainedFileDeletions(); clearComposerDraftStorage(); endPullRequestSession(); + void clearTTSCache(); }; /** * Only recognized codes override the HTTP status used by the login error translation. @@ -119,6 +122,8 @@ const AuthContextProvider = ({ setIsAuthReady(true); if (isAuthenticated) { setQueriesEnabled(true); + /** A different account signing in without a logout must not inherit cached audio. */ + setTTSCacheOwner(user?.id); /** * Any accepted full-auth response supersedes a staged enrollment, whichever path minted * it. The setup endpoints trust the stored bearer independently of the session, so a @@ -259,6 +264,7 @@ const AuthContextProvider = ({ setIsAuthReady(true); setQueriesEnabled(true); openFileDeletionRetention(); + setTTSCacheOwner(authenticatedUser.id); navigate(redirect, { replace: true }); }, [navigate, setQueriesEnabled, setUser], diff --git a/client/src/hooks/Files/useSharePointPicker.ts b/client/src/hooks/Files/useSharePointPicker.ts index 05b68e95a4..21cbd520de 100644 --- a/client/src/hooks/Files/useSharePointPicker.ts +++ b/client/src/hooks/Files/useSharePointPicker.ts @@ -67,7 +67,6 @@ export default function useSharePointPicker({ try { switch (message.data.type) { case 'notification': - console.log('SharePoint picker notification:', message.data); break; case 'command': { @@ -78,13 +77,9 @@ export default function useSharePointPicker({ }); const command = message.data.data; - console.log('SharePoint picker command:', command); switch (command.command) { case 'authenticate': - console.log('Authentication requested, providing token'); - console.log('Command details:', command); // Add this line - console.log('Token available:', !!token?.access_token); // Add this line if (token?.access_token) { port.postMessage({ type: 'result', @@ -111,7 +106,6 @@ export default function useSharePointPicker({ break; case 'close': - console.log('Close command received'); port.postMessage({ type: 'result', id: message.data.id, @@ -123,10 +117,7 @@ export default function useSharePointPicker({ break; case 'pick': { - console.log('Files picked from SharePoint:', command); - const items = command.items || command.files || []; - console.log('Extracted items:', items); if (items && items.length > 0) { const selectedFiles = items.map((item: any) => ({ @@ -144,8 +135,6 @@ export default function useSharePointPicker({ sharePointItem: item, })); - console.log('Processed SharePoint files:', selectedFiles); - if (onFilesSelected) { onFilesSelected(selectedFiles); } @@ -185,7 +174,6 @@ export default function useSharePointPicker({ } default: - console.log('Unknown message type:', message.data.type); break; } } catch (error) { @@ -198,18 +186,11 @@ export default function useSharePointPicker({ // Initialization message handler - establishes MessagePort communication const initMessageHandler = useCallback( (event: MessageEvent) => { - console.log('=== SharePoint picker init message received ==='); - console.log('Event source:', event.source); - console.log('Event data:', event.data); - console.log('Expected channelId:', channelIdRef.current); - // Check if this message is from our iframe if (event.source && event.source === iframeRef.current?.contentWindow) { const message = event.data; if (message.type === 'initialize' && message.channelId === channelIdRef.current) { - console.log('Establishing MessagePort communication'); - // Get the MessagePort from the event portRef.current = event.ports[0]; @@ -222,8 +203,6 @@ export default function useSharePointPicker({ portRef.current.postMessage({ type: 'activate', }); - - console.log('MessagePort established and activated'); } else { console.error('No MessagePort found in initialize event'); } @@ -251,15 +230,6 @@ export default function useSharePointPicker({ const channelId = generateChannelId(); channelIdRef.current = channelId; - console.log('=== SharePoint File Picker v8 (MessagePort) ==='); - console.log('Token available:', { - hasToken: !!token.access_token, - tokenType: token.token_type, - expiresIn: token.expires_in, - scopes: token.scope, - }); - console.log('Channel ID:', channelId); - const pickerOptions: SPPickerConfig = { sdk: '8.0', entry: { diff --git a/client/src/hooks/Input/useTextToSpeechExternal.ts b/client/src/hooks/Input/useTextToSpeechExternal.ts index 99c1a2344e..d2219d6f11 100644 --- a/client/src/hooks/Input/useTextToSpeechExternal.ts +++ b/client/src/hooks/Input/useTextToSpeechExternal.ts @@ -1,8 +1,14 @@ import { useState, useMemo, useRef, useCallback, useEffect } from 'react'; import { useRecoilValue } from 'recoil'; import { useToastContext } from '@librechat/client'; +import { + getTTSCacheKey, + setCachedTTSAudio, + getCachedTTSAudio, + getTTSCacheGeneration, +} from '~/utils'; import { useTextToSpeechMutation, useVoicesQuery } from '~/data-provider'; -import { useLocalize } from '~/hooks'; +import { useLocalize, useAuthContext } from '~/hooks'; import store from '~/store'; const createFormData = (text: string, voice: string) => { @@ -28,6 +34,7 @@ function useTextToSpeechExternal({ index = 0, }: TUseTTSExternal) { const localize = useLocalize(); + const { user } = useAuthContext(); const { showToast } = useToastContext(); const voice = useRecoilValue(store.voice); const cacheTTS = useRecoilValue(store.cacheTTS); @@ -36,6 +43,8 @@ function useTextToSpeechExternal({ const [downloadFile, setDownloadFile] = useState(false); const promiseAudioRef = useRef(null); + /** The cache generation each request started in, so audio from before a logout is not kept. */ + const requestGenerations = useRef(new WeakMap()); /* Global Audio Variables */ const globalIsFetching = useRecoilValue(store.globalAudioFetchingFamily(index)); @@ -105,11 +114,12 @@ function useTextToSpeechExternal({ const inputText = (variables.get('input') ?? '') as string; const audioBlob = new Blob([data], { type: 'audio/mpeg' }); - if (cacheTTS && inputText) { - const cache = await caches.open('tts-responses'); - const request = new Request(inputText); - const response = new Response(audioBlob); - cache.put(request, response); + const generation = requestGenerations.current.get(variables); + if (cacheTTS && inputText && generation != null) { + /** Not awaited: caching must never delay playback. */ + void getTTSCacheKey(inputText, (variables.get('voice') ?? '') as string, user?.id).then( + (cacheKey) => cacheKey && setCachedTTSAudio(cacheKey, audioBlob, generation), + ); } const blobUrl = URL.createObjectURL(audioBlob); @@ -134,6 +144,7 @@ function useTextToSpeechExternal({ const startMutation = (text: string, download: boolean) => { const formData = createFormData(text, voice ?? ''); + requestGenerations.current.set(formData, getTTSCacheGeneration()); setDownloadFile(download); processAudio(formData); }; @@ -147,12 +158,12 @@ function useTextToSpeechExternal({ }; const handleCachedResponse = async (text: string, download: boolean) => { - const cachedResponse = await caches.match(text); - if (!cachedResponse) { + const cacheKey = await getTTSCacheKey(text, voice ?? '', user?.id); + const cachedAudio = cacheKey ? getCachedTTSAudio(cacheKey) : undefined; + if (!cachedAudio) { return startMutation(text, download); } - const audioBlob = await cachedResponse.blob(); - const blobUrl = URL.createObjectURL(audioBlob); + const blobUrl = URL.createObjectURL(cachedAudio); if (download) { downloadAudio(blobUrl); } else { diff --git a/client/src/hooks/__tests__/AuthContext.spec.tsx b/client/src/hooks/__tests__/AuthContext.spec.tsx index f7a667bfc7..f2d35f7618 100644 --- a/client/src/hooks/__tests__/AuthContext.spec.tsx +++ b/client/src/hooks/__tests__/AuthContext.spec.tsx @@ -14,18 +14,21 @@ import { persistTwoFactorSetupToken, } from 'librechat-data-provider'; import type { TAuthConfig } from '~/common'; +import { + SESSION_KEY, + getCachedTTSAudio, + setCachedTTSAudio, + retainFileDeletion, + getTTSCacheGeneration, + clearRetainedFileDeletions, + takeRetainedFileDeletions, +} from '~/utils'; import { chatFilterStatusAtom, chatFilterTagsAtom, chatSortAtom, resetChatFilterSessionAtom, } from '~/components/Conversations/chatFilters'; -import { - SESSION_KEY, - retainFileDeletion, - clearRetainedFileDeletions, - takeRetainedFileDeletions, -} from '~/utils'; import { AuthContextProvider, useAuthContext } from '../AuthContext'; const mockNavigate = jest.fn(); @@ -1146,3 +1149,97 @@ describe('AuthContextProvider: enrollment hand-off that keeps the document', () expect(readTwoFactorSetupToken()).toBe('setup-token'); }); }); + +describe('AuthContextProvider: read-aloud cache at every session end', () => { + const mockUseGetUserQuery = jest.requireMock('~/data-provider').useGetUserQuery; + const cacheAudio = () => + setCachedTTSAudio('departing', new Blob(['audio']), getTTSCacheGeneration()); + const refreshOptions = () => + mockRefreshMutate.mock.calls[0][1] as { + onSuccess: (data: unknown) => void; + onError: (error: unknown) => void; + }; + + beforeEach(() => { + jest.clearAllMocks(); + jest.spyOn(window.location, 'replace').mockImplementation(() => {}); + cacheAudio(); + }); + + afterEach(() => { + jest.useRealTimers(); + window.history.replaceState({}, '', '/'); + }); + + it('empties on logout', () => { + renderProvider(); + act(() => mockCapturedLogoutOptions.onSuccess({ message: 'Logout successful' })); + expect(getCachedTTSAudio('departing')).toBeUndefined(); + }); + + it('empties on an IdP logout redirect', () => { + renderProvider(); + act(() => + mockCapturedLogoutOptions.onSuccess({ + message: 'Logout successful', + redirect: 'https://idp.example.com/logout', + }), + ); + expect(getCachedTTSAudio('departing')).toBeUndefined(); + }); + + it('empties when the logout request fails', () => { + renderProvider(); + act(() => mockCapturedLogoutOptions.onError(new Error('network'))); + expect(getCachedTTSAudio('departing')).toBeUndefined(); + }); + + it('empties when the refresh token is rejected', () => { + renderProviderLive(); + act(() => refreshOptions().onError(new Error('401'))); + expect(getCachedTTSAudio('departing')).toBeUndefined(); + }); + + it('empties when a refresh returns no session', () => { + renderProviderLive(); + act(() => refreshOptions().onSuccess({})); + expect(getCachedTTSAudio('departing')).toBeUndefined(); + }); + + it('empties when the user query fails', () => { + mockUseGetUserQuery.mockReturnValue({ + data: undefined, + isError: true, + error: new Error('401'), + }); + renderProvider(); + expect(getCachedTTSAudio('departing')).toBeUndefined(); + mockUseGetUserQuery.mockReturnValue({ data: undefined, isError: false, error: null }); + }); + + it('empties when a different account signs in without a logout', () => { + jest.useFakeTimers(); + renderProviderLive(); + act(() => { + refreshOptions().onSuccess({ user: { id: 'first', role: 'USER' }, token: 'first-token' }); + jest.advanceTimersByTime(100); + }); + cacheAudio(); + act(() => { + capturedAuthContext?.completeAuthentication('second-token', { id: 'second' } as never); + }); + expect(getCachedTTSAudio('departing')).toBeUndefined(); + }); + + it('keeps the cache when the same account signs in again', () => { + renderProvider(); + act(() => { + capturedAuthContext?.completeAuthentication('token', { id: 'same' } as never); + }); + cacheAudio(); + act(() => { + capturedAuthContext?.completeAuthentication('token-2', { id: 'same' } as never); + }); + expect(getCachedTTSAudio('departing')).toBeDefined(); + }); +}); diff --git a/client/src/main.jsx b/client/src/main.jsx index b719fde74c..de9aad20d6 100644 --- a/client/src/main.jsx +++ b/client/src/main.jsx @@ -7,9 +7,13 @@ import '@librechat/client/style.css'; import './style.css'; import './mobile.css'; import { ApiErrorBoundaryProvider } from './hooks/ApiErrorBoundaryContext'; +import { clearTTSCache } from './utils/ttscache'; import 'katex/dist/katex.min.css'; import 'katex/dist/contrib/copy-tex.js'; +/** Read-aloud audio lives in memory now; drop what earlier versions persisted to disk. */ +void clearTTSCache(); + const container = document.getElementById('root'); const root = createRoot(container); diff --git a/client/src/utils/index.ts b/client/src/utils/index.ts index f49e2a70c2..096965fe41 100644 --- a/client/src/utils/index.ts +++ b/client/src/utils/index.ts @@ -4,6 +4,7 @@ import type { TAskFunction } from '~/common'; import logger from './logger'; export * from './map'; +export * from './ttscache'; export * from './policies'; export * from './json'; export * from './icons'; diff --git a/client/src/utils/ttscache.spec.ts b/client/src/utils/ttscache.spec.ts new file mode 100644 index 0000000000..16215c1c48 --- /dev/null +++ b/client/src/utils/ttscache.spec.ts @@ -0,0 +1,174 @@ +import { TextEncoder } from 'util'; +import { webcrypto } from 'crypto'; +import { + clearTTSCache, + getTTSCacheKey, + hasCachedTTSAudio, + hasLegacyTTSCache, + setCachedTTSAudio, + setTTSCacheOwner, + getCachedTTSAudio, + getTTSCacheGeneration, +} from './ttscache'; + +const text = 'a private assistant reply'; +const audio = (label: string) => new Blob([label], { type: 'audio/mpeg' }); + +beforeEach(() => { + clearTTSCache(); +}); + +describe('getTTSCacheKey', () => { + beforeAll(() => { + Object.defineProperty(globalThis, 'crypto', { value: webcrypto, configurable: true }); + Object.assign(globalThis, { TextEncoder }); + }); + + it('is a SHA-256 hash that never contains the message text', async () => { + const key = await getTTSCacheKey(text, 'alloy'); + expect(key).toMatch(/^[0-9a-f]{64}$/); + expect(key).not.toContain('private'); + }); + + it('is stable for the same text and voice and differs by voice or text', async () => { + const key = await getTTSCacheKey(text, 'alloy'); + expect(await getTTSCacheKey(text, 'alloy')).toBe(key); + expect(await getTTSCacheKey(text, 'echo')).not.toBe(key); + expect(await getTTSCacheKey('other', 'alloy')).not.toBe(key); + }); + + it("differs by user, so one account never matches another account's audio", async () => { + expect(await getTTSCacheKey(text, 'alloy', 'user-a')).not.toBe( + await getTTSCacheKey(text, 'alloy', 'user-b'), + ); + }); + + it('returns null for empty text', async () => { + expect(await getTTSCacheKey('', 'alloy')).toBeNull(); + }); +}); + +describe('in-memory audio cache', () => { + it('returns stored audio and misses unknown keys', () => { + const recording = audio('one'); + setCachedTTSAudio('one', recording, getTTSCacheGeneration()); + expect(getCachedTTSAudio('one')).toBe(recording); + expect(getCachedTTSAudio('two')).toBeUndefined(); + expect(hasCachedTTSAudio()).toBe(true); + }); + + it('evicts the least recently used entry beyond its cap', () => { + const generation = getTTSCacheGeneration(); + for (let index = 0; index < 50; index += 1) { + setCachedTTSAudio(`key-${index}`, audio(`${index}`), generation); + } + /** Playing the oldest entry makes it the most recent, so the next one goes instead. */ + expect(getCachedTTSAudio('key-0')).toBeDefined(); + setCachedTTSAudio('key-50', audio('50'), generation); + + expect(getCachedTTSAudio('key-0')).toBeDefined(); + expect(getCachedTTSAudio('key-1')).toBeUndefined(); + expect(getCachedTTSAudio('key-50')).toBeDefined(); + }); + + it('evicts the oldest recordings once their total size exceeds the budget', () => { + const generation = getTTSCacheGeneration(); + const tenMegabytes = 10 * 1024 * 1024; + for (let index = 0; index < 4; index += 1) { + setCachedTTSAudio(`long-${index}`, new Blob([new Uint8Array(tenMegabytes)]), generation); + } + expect(getCachedTTSAudio('long-0')).toBeUndefined(); + expect(getCachedTTSAudio('long-3')).toBeDefined(); + }); + + it('does not keep a single recording larger than the budget', () => { + const huge = new Blob([new Uint8Array(33 * 1024 * 1024)]); + setCachedTTSAudio('huge', huge, getTTSCacheGeneration()); + expect(getCachedTTSAudio('huge')).toBeUndefined(); + }); + + it('empties on clear', () => { + setCachedTTSAudio('one', audio('one'), getTTSCacheGeneration()); + clearTTSCache(); + expect(getCachedTTSAudio('one')).toBeUndefined(); + expect(hasCachedTTSAudio()).toBe(false); + }); + + it('empties when a different account takes over the tab, not when the same one returns', () => { + setTTSCacheOwner('first'); + setCachedTTSAudio('mine', audio('mine'), getTTSCacheGeneration()); + setTTSCacheOwner('first'); + setTTSCacheOwner(undefined); + expect(getCachedTTSAudio('mine')).toBeDefined(); + + setTTSCacheOwner('second'); + expect(getCachedTTSAudio('mine')).toBeUndefined(); + }); + + it('does not hand audio recorded after a clear to the next account that signs in', () => { + setTTSCacheOwner('first'); + clearTTSCache(); + setCachedTTSAudio('unowned', audio('unowned'), getTTSCacheGeneration()); + setTTSCacheOwner('second'); + expect(getCachedTTSAudio('unowned')).toBeUndefined(); + }); + + it('drops audio from a request started before an account signed in', () => { + clearTTSCache(); + const generation = getTTSCacheGeneration(); + setTTSCacheOwner('arriving'); + setCachedTTSAudio('early', audio('early'), generation); + expect(getCachedTTSAudio('early')).toBeUndefined(); + }); + + it('drops audio from a request that was in flight across a clear', () => { + const generation = getTTSCacheGeneration(); + clearTTSCache(); + setCachedTTSAudio('late', audio('late'), generation); + expect(getCachedTTSAudio('late')).toBeUndefined(); + + setCachedTTSAudio('next', audio('next'), getTTSCacheGeneration()); + expect(getCachedTTSAudio('next')).toBeDefined(); + }); +}); + +describe('legacy Cache Storage cleanup', () => { + afterEach(() => { + Reflect.deleteProperty(globalThis, 'caches'); + }); + + it('deletes the cache earlier versions persisted', async () => { + const del = jest.fn().mockResolvedValue(true); + Object.defineProperty(globalThis, 'caches', { value: { delete: del }, configurable: true }); + clearTTSCache(); + await Promise.resolve(); + expect(del).toHaveBeenCalledWith('tts-responses'); + expect(del).toHaveBeenCalledWith('tts-responses-v1'); + expect(del).toHaveBeenCalledWith('tts-responses-v1:pending'); + }); + + it("reports an earlier build's cache that is still on disk", async () => { + const has = jest.fn(async (name: string) => name === 'tts-responses'); + Object.defineProperty(globalThis, 'caches', { value: { has }, configurable: true }); + await expect(hasLegacyTTSCache()).resolves.toBe(true); + + has.mockResolvedValue(false); + await expect(hasLegacyTTSCache()).resolves.toBe(false); + /** An unreadable state keeps the retry available rather than reporting it empty. */ + has.mockRejectedValue(new Error('blocked')); + await expect(hasLegacyTTSCache()).resolves.toBe(true); + Reflect.deleteProperty(globalThis, 'caches'); + await expect(hasLegacyTTSCache()).resolves.toBe(false); + }); + + it('tolerates a missing Cache API and a rejected delete', async () => { + Reflect.deleteProperty(globalThis, 'caches'); + expect(() => clearTTSCache()).not.toThrow(); + + const del = jest.fn().mockRejectedValue(new Error('blocked')); + Object.defineProperty(globalThis, 'caches', { value: { delete: del }, configurable: true }); + expect(() => clearTTSCache()).not.toThrow(); + await expect(clearTTSCache()).resolves.toBeUndefined(); + expect(del).toHaveBeenCalled(); + }); +}); diff --git a/client/src/utils/ttscache.ts b/client/src/utils/ttscache.ts new file mode 100644 index 0000000000..d6166f1fe3 --- /dev/null +++ b/client/src/utils/ttscache.ts @@ -0,0 +1,124 @@ +/** Cache Storage names earlier builds persisted read-aloud audio under (the first keyed by the + * message text). Audio is now held only in this tab's memory, so these are just deleted. */ +const LEGACY_TTS_CACHE_NAMES = ['tts-responses', 'tts-responses-v1', 'tts-responses-v1:pending']; +const MAX_ENTRIES = 50; +/** Long replies make large recordings, so the cache is bounded by size as well as count. */ +const MAX_BYTES = 32 * 1024 * 1024; + +/** Spoken audio by hashed key, oldest first; it never leaves this tab and dies with it. */ +const audioByKey = new Map(); +let totalBytes = 0; +/** Bumped on every clear, so a request that was in flight across a logout cannot write. */ +let generation = 0; +/** The account the cached audio belongs to. */ +let ownerId: string | undefined; + +const toHex = (buffer: ArrayBuffer): string => + Array.from(new Uint8Array(buffer), (byte) => byte.toString(16).padStart(2, '0')).join(''); + +/** Returns a SHA-256 hash of the user, voice and spoken text, so the cache holds no message text + * and one account can never match another's audio. Returns null when hashing is unavailable + * (no secure context); the caller skips the cache. */ +export async function getTTSCacheKey( + text: string, + voice?: string | null, + userId?: string | null, +): Promise { + if (!text || typeof crypto === 'undefined' || !crypto.subtle) { + return null; + } + try { + const data = new TextEncoder().encode(JSON.stringify([userId ?? '', voice ?? '', text])); + return toHex(await crypto.subtle.digest('SHA-256', data)); + } catch { + return null; + } +} + +export const getTTSCacheGeneration = (): number => generation; + +export const hasCachedTTSAudio = (): boolean => audioByKey.size > 0; + +/** Whether an earlier build's audio may still be on disk (its delete failed), so it can be + * retried. A check that fails reads as "may be", which keeps the retry available. */ +export async function hasLegacyTTSCache(): Promise { + if (typeof caches === 'undefined') { + return false; + } + try { + const found = await Promise.all(LEGACY_TTS_CACHE_NAMES.map((name) => caches.has(name))); + return found.some(Boolean); + } catch { + return true; + } +} + +export function getCachedTTSAudio(key: string): Blob | undefined { + const audio = audioByKey.get(key); + if (audio) { + /** Most recently played stays last, so eviction drops the least recently used. */ + audioByKey.delete(key); + audioByKey.set(key, audio); + } + return audio; +} + +const removeAudio = (key: string): void => { + const audio = audioByKey.get(key); + if (audio) { + totalBytes -= audio.size; + audioByKey.delete(key); + } +}; + +/** Stores audio for a request started at `requestGeneration`; skipped if a clear came since, or + * if the recording alone exceeds the size budget. */ +export function setCachedTTSAudio(key: string, audio: Blob, requestGeneration: number): void { + if (requestGeneration !== generation || audio.size > MAX_BYTES) { + return; + } + removeAudio(key); + audioByKey.set(key, audio); + totalBytes += audio.size; + while (audioByKey.size > MAX_ENTRIES || totalBytes > MAX_BYTES) { + const oldest = audioByKey.keys().next().value; + if (oldest === undefined) { + break; + } + removeAudio(oldest); + } +} + +/** Drops every cached recording and any audio earlier versions left in Cache Storage. */ +export function clearTTSCache(): Promise { + audioByKey.clear(); + totalBytes = 0; + generation += 1; + ownerId = undefined; + if (typeof caches === 'undefined') { + return Promise.resolve(); + } + /** Settles when the legacy deletes do; callers that need not know can ignore it. */ + return Promise.all( + LEGACY_TTS_CACHE_NAMES.map((name) => + Promise.resolve() + .then(() => caches.delete(name)) + .catch(() => false), + ), + ).then(() => undefined); +} + +/** Records the signed-in account; signing in as a different one in this tab empties the cache. */ +export function setTTSCacheOwner(userId?: string | null): void { + if (!userId) { + return; + } + /** Audio recorded while no owner was set (after a clear) is not handed to whoever signs in. */ + if (ownerId !== userId && (ownerId !== undefined || audioByKey.size > 0)) { + void clearTTSCache(); + } else if (ownerId !== userId) { + /** A request started before this account signed in cannot write into its cache. */ + generation += 1; + } + ownerId = userId; +} diff --git a/e2e/config/librechat.e2e.yaml b/e2e/config/librechat.e2e.yaml index 5e85f397d8..625d89d0a7 100644 --- a/e2e/config/librechat.e2e.yaml +++ b/e2e/config/librechat.e2e.yaml @@ -75,6 +75,22 @@ mcpServers: title: E2E Memory description: Local MCP fixture used by mock end-to-end tests. timeout: 30000 + e2e-user-vars: + type: stdio + command: node + args: + - e2e/setup/fake-mcp-server.js + startup: false + env: + E2E_MCP_USER_TOKEN: '{{E2E_MCP_USER_TOKEN}}' + E2E_MCP_EXPECTED_USER_TOKEN: e2e-user-token-secret + customUserVars: + E2E_MCP_USER_TOKEN: + title: E2E User Token + description: Per-user token the fixture refuses to start without. + title: E2E User Vars + description: Stdio MCP fixture that starts only with the expected per-user token. + timeout: 30000 e2e-http: type: streamable-http url: http://127.0.0.1:8765/mcp diff --git a/e2e/setup/fake-mcp-server.js b/e2e/setup/fake-mcp-server.js index 77454aa5f7..1763ee1a2b 100644 --- a/e2e/setup/fake-mcp-server.js +++ b/e2e/setup/fake-mcp-server.js @@ -114,6 +114,11 @@ if (process.env.E2E_MCP_LIST_CHANGED === 'true') { } async function main() { + const expectedUserToken = process.env.E2E_MCP_EXPECTED_USER_TOKEN; + if (expectedUserToken && process.env.E2E_MCP_USER_TOKEN !== expectedUserToken) { + console.error('[fake-mcp-server] E2E_MCP_USER_TOKEN does not match the expected value'); + process.exit(1); + } await server.connect(new StdioServerTransport()); } diff --git a/e2e/specs/mock/scenarios/credential-encryption.spec.ts b/e2e/specs/mock/scenarios/credential-encryption.spec.ts new file mode 100644 index 0000000000..f840aec074 --- /dev/null +++ b/e2e/specs/mock/scenarios/credential-encryption.spec.ts @@ -0,0 +1,369 @@ +import { ObjectId } from 'mongodb'; +import { randomUUID } from 'crypto'; +import { expect, test } from '@playwright/test'; +import type { APIRequestContext } from '@playwright/test'; +import type { User } from '../../../types'; +import cleanupUser from '../../../setup/cleanupUser'; +import { loginAdmin } from '../content-filters.helpers'; +import { withMongo } from '../db'; + +/** + * Stored user credentials (tool and web search keys, MCP custom user variables) are + * written in the random-IV `v3:` format and read by format, so records written in the + * legacy fixed-IV format keep working next to new ones. The `e2e-user-vars` MCP fixture + * starts only when the token it receives equals the expected plaintext, so a successful + * reinitialize proves the server decrypted the stored value exactly. `credentials.writeFormat: + * legacy` switches new writes back to the fixed-IV format for rolling upgrades. + */ + +const MCP_SERVER = 'e2e-user-vars'; +const MCP_PLUGIN_KEY = `mcp_${MCP_SERVER}`; +const MCP_VAR = 'E2E_MCP_USER_TOKEN'; +/** Must match `E2E_MCP_EXPECTED_USER_TOKEN` in e2e/config/librechat.e2e.yaml. */ +const MCP_TOKEN = 'e2e-user-token-secret'; +const WEB_SEARCH = 'web_search'; +const WEB_SEARCH_FIELDS = { + serperApiKey: 'SERPER_API_KEY', + firecrawlApiKey: 'FIRECRAWL_API_KEY', + jinaApiKey: 'JINA_API_KEY', +} as const; +const V3_FORMAT = /^v3:[0-9a-f]{32}:[0-9a-f]+$/; +const LEGACY_FORMAT = /^[0-9a-f]+$/; + +type StoredAuth = { authField: string; value: string; pluginKey?: string }; + +async function registerAndLogin( + request: APIRequestContext, + user: User, +): Promise<{ token: string; userId: string }> { + const registerResponse = await request.post('/api/auth/register', { + data: { + email: user.email, + name: user.name, + password: user.password, + confirm_password: user.password, + }, + }); + expect(registerResponse.ok()).toBeTruthy(); + + const loginResponse = await request.post('/api/auth/login', { + data: { email: user.email, password: user.password }, + }); + expect(loginResponse.ok()).toBeTruthy(); + const body = (await loginResponse.json()) as { token?: string; user?: { _id?: string } }; + if (!body.token || !body.user?._id) { + throw new Error('Expected login response to include a bearer token and user id'); + } + return { token: body.token, userId: body.user._id }; +} + +function newUser(label: string): User { + return { + email: `credential-${label}-${randomUUID()}@example.com`, + name: 'Credential Encryption', + password: 'securepassword123', + }; +} + +async function installPluginAuth( + request: APIRequestContext, + token: string, + pluginKey: string, + auth: Record, +): Promise { + const response = await request.post('/api/user/plugins', { + headers: { Authorization: `Bearer ${token}` }, + data: { pluginKey, action: 'install', auth }, + }); + expect(response.status(), await response.text()).toBe(200); +} + +async function readStoredAuths(userId: string, pluginKey: string): Promise { + return withMongo(async (db) => { + const docs = await db.collection('pluginauths').find({ userId, pluginKey }).toArray(); + return docs.map((doc) => ({ + authField: doc.authField as string, + value: doc.value as string, + pluginKey: doc.pluginKey as string | undefined, + })); + }); +} + +/** + * Produces a value in the legacy fixed-IV format by letting the server encrypt it on a + * path that still writes that format (user endpoint keys), then removes the key record. + */ +async function legacyCiphertext( + request: APIRequestContext, + token: string, + userId: string, + plaintext: string, +): Promise { + const name = `e2e-legacy-${randomUUID()}`; + const response = await request.put('/api/keys', { + headers: { Authorization: `Bearer ${token}` }, + data: { name, value: plaintext }, + }); + expect(response.status()).toBe(201); + return withMongo(async (db) => { + const doc = await db + .collection('keys') + .findOneAndDelete({ userId: new ObjectId(userId), name }); + const value = (doc as { value?: unknown } | null)?.value; + if (typeof value !== 'string') { + throw new Error('Expected the legacy key record to carry an encrypted value'); + } + expect(value).toMatch(LEGACY_FORMAT); + return value; + }); +} + +async function seedStoredAuth(userId: string, auth: StoredAuth): Promise { + await withMongo(async (db) => { + const now = new Date(); + await db + .collection('pluginauths') + .updateOne( + { userId, pluginKey: auth.pluginKey, authField: auth.authField }, + { $set: { value: auth.value, updatedAt: now }, $setOnInsert: { createdAt: now } }, + { upsert: true }, + ); + }); +} + +async function cleanupCredentials(user: User): Promise { + await withMongo(async (db) => { + const doc = await db.collection('users').findOne({ email: user.email }); + if (doc) { + await db.collection('pluginauths').deleteMany({ userId: doc._id.toString() }); + } + }); + await cleanupUser(user); +} + +async function webSearchAuthenticated(request: APIRequestContext, token: string): Promise { + const response = await request.get(`/api/agents/tools/${WEB_SEARCH}/auth`, { + headers: { Authorization: `Bearer ${token}` }, + }); + expect(response.status(), await response.text()).toBe(200); + const body = (await response.json()) as { authenticated?: boolean }; + return body.authenticated === true; +} + +async function mcpVarIsSet(request: APIRequestContext, token: string): Promise { + const response = await request.get(`/api/mcp/${MCP_SERVER}/auth-values`, { + headers: { Authorization: `Bearer ${token}` }, + }); + expect(response.status(), await response.text()).toBe(200); + const body = (await response.json()) as { authValueFlags?: Record }; + return body.authValueFlags?.[MCP_VAR] === true; +} + +async function reinitializeMcp(request: APIRequestContext, token: string): Promise { + const response = await request.post(`/api/mcp/${MCP_SERVER}/reinitialize`, { + headers: { Authorization: `Bearer ${token}` }, + }); + if (!response.ok()) { + return false; + } + const body = (await response.json()) as { success?: boolean }; + return body.success === true; +} + +test.describe('stored credential encryption', () => { + test('a saved web search key is stored with a random IV and still authenticates @scenario:plugin-credential-save-stores-v3', async ({ + request, + }) => { + const user = newUser('web-save'); + try { + const { token, userId } = await registerAndLogin(request, user); + const secret = `e2e-search-${randomUUID()}`; + const auth = Object.fromEntries(Object.keys(WEB_SEARCH_FIELDS).map((key) => [key, secret])); + + await installPluginAuth(request, token, WEB_SEARCH, auth); + const first = await readStoredAuths(userId, WEB_SEARCH); + const credentials = first.filter((doc) => + (Object.values(WEB_SEARCH_FIELDS) as string[]).includes(doc.authField), + ); + expect(credentials.map((doc) => doc.authField).sort()).toEqual( + Object.values(WEB_SEARCH_FIELDS).sort(), + ); + for (const doc of credentials) { + expect(doc.value).toMatch(V3_FORMAT); + expect(doc.value).not.toContain(secret); + } + /** Same plaintext in three fields: a fixed IV would give three identical values. */ + expect(new Set(credentials.map((doc) => doc.value)).size).toBe(credentials.length); + + expect(await webSearchAuthenticated(request, token)).toBe(true); + + /** Saving the same value again yields a new ciphertext. */ + await installPluginAuth(request, token, WEB_SEARCH, auth); + const second = await readStoredAuths(userId, WEB_SEARCH); + for (const doc of credentials) { + const resaved = second.find((entry) => entry.authField === doc.authField); + expect(resaved?.value).toMatch(V3_FORMAT); + expect(resaved?.value).not.toBe(doc.value); + } + expect(await webSearchAuthenticated(request, token)).toBe(true); + } finally { + await cleanupCredentials(user); + } + }); + + test('a web search key stored in the legacy format still authenticates and is not rewritten @scenario:plugin-credential-legacy-record-authenticates', async ({ + request, + }) => { + const user = newUser('web-legacy'); + try { + const { token, userId } = await registerAndLogin(request, user); + expect(await webSearchAuthenticated(request, token)).toBe(false); + + const seeded: StoredAuth[] = []; + for (const authField of Object.values(WEB_SEARCH_FIELDS)) { + const value = await legacyCiphertext(request, token, userId, `e2e-legacy-${authField}`); + const auth = { authField, value, pluginKey: WEB_SEARCH }; + await seedStoredAuth(userId, auth); + seeded.push(auth); + } + + expect(await webSearchAuthenticated(request, token)).toBe(true); + + const stored = await readStoredAuths(userId, WEB_SEARCH); + for (const auth of seeded) { + expect(stored.find((doc) => doc.authField === auth.authField)?.value).toBe(auth.value); + } + } finally { + await cleanupCredentials(user); + } + }); + + test('legacy and v3 web search keys for one user authenticate together @scenario:plugin-credential-mixed-records-authenticate', async ({ + request, + }) => { + const user = newUser('web-mixed'); + try { + const { token, userId } = await registerAndLogin(request, user); + const secret = `e2e-search-${randomUUID()}`; + await installPluginAuth(request, token, WEB_SEARCH, { + firecrawlApiKey: secret, + jinaApiKey: secret, + }); + const legacy = await legacyCiphertext(request, token, userId, secret); + await seedStoredAuth(userId, { + authField: WEB_SEARCH_FIELDS.serperApiKey, + value: legacy, + pluginKey: WEB_SEARCH, + }); + + const stored = await readStoredAuths(userId, WEB_SEARCH); + const formats = Object.values(WEB_SEARCH_FIELDS).map( + (field) => stored.find((doc) => doc.authField === field)?.value ?? '', + ); + expect(formats[0]).toMatch(LEGACY_FORMAT); + expect(formats[1]).toMatch(V3_FORMAT); + expect(formats[2]).toMatch(V3_FORMAT); + + expect(await webSearchAuthenticated(request, token)).toBe(true); + } finally { + await cleanupCredentials(user); + } + }); + + test('a saved MCP custom user variable is stored as v3 and reaches the server decrypted @scenario:mcp-user-var-save-stores-v3', async ({ + request, + }) => { + test.setTimeout(90000); + const user = newUser('mcp-save'); + try { + const { token, userId } = await registerAndLogin(request, user); + expect(await mcpVarIsSet(request, token)).toBe(false); + expect(await reinitializeMcp(request, token)).toBe(false); + + await installPluginAuth(request, token, MCP_PLUGIN_KEY, { [MCP_VAR]: MCP_TOKEN }); + const stored = await readStoredAuths(userId, MCP_PLUGIN_KEY); + expect(stored).toHaveLength(1); + expect(stored[0].authField).toBe(MCP_VAR); + expect(stored[0].value).toMatch(V3_FORMAT); + expect(stored[0].value).not.toContain(MCP_TOKEN); + + expect(await mcpVarIsSet(request, token)).toBe(true); + expect(await reinitializeMcp(request, token)).toBe(true); + } finally { + await cleanupCredentials(user); + } + }); + + test('an MCP custom user variable stored in the legacy format still connects and is not rewritten @scenario:mcp-user-var-legacy-record-connects', async ({ + request, + }) => { + test.setTimeout(90000); + const user = newUser('mcp-legacy'); + try { + const { token, userId } = await registerAndLogin(request, user); + const value = await legacyCiphertext(request, token, userId, MCP_TOKEN); + await seedStoredAuth(userId, { authField: MCP_VAR, value, pluginKey: MCP_PLUGIN_KEY }); + + expect(await mcpVarIsSet(request, token)).toBe(true); + expect(await reinitializeMcp(request, token)).toBe(true); + + const stored = await readStoredAuths(userId, MCP_PLUGIN_KEY); + expect(stored.map((doc) => doc.value)).toEqual([value]); + } finally { + await cleanupCredentials(user); + } + }); + + test('with the legacy write format configured, a saved credential is stored in the legacy format and still connects @scenario:credential-legacy-write-format-config', async ({ + request, + }) => { + test.setTimeout(90000); + const user = newUser('legacy-format'); + const adminToken = await loginAdmin(request); + const adminHeaders = { Authorization: `Bearer ${adminToken}` }; + let userId: string | undefined; + try { + const login = await registerAndLogin(request, user); + userId = login.userId; + const { token } = login; + + /** Per-user override, so only this fresh user's writes change format. */ + const put = await request.put(`/api/admin/config/user/${userId}`, { + headers: adminHeaders, + data: { overrides: { credentials: { writeFormat: 'legacy' } } }, + }); + /** A first override for a principal is created (201, `configVersion` 1). */ + expect(put.status(), await put.text()).toBe(201); + const created = (await put.json()) as { + config?: { overrides?: { credentials?: { writeFormat?: string } } }; + }; + expect(created.config?.overrides?.credentials?.writeFormat).toBe('legacy'); + + /** The override reaches the merged config after an async cache invalidation. */ + await expect + .poll( + async () => { + await installPluginAuth(request, token, MCP_PLUGIN_KEY, { [MCP_VAR]: MCP_TOKEN }); + const [stored] = await readStoredAuths(login.userId, MCP_PLUGIN_KEY); + return LEGACY_FORMAT.test(stored?.value ?? '') ? 'legacy' : stored?.value; + }, + { timeout: 30000, intervals: [500, 1000, 2000] }, + ) + .toBe('legacy'); + + const stored = await readStoredAuths(userId, MCP_PLUGIN_KEY); + expect(stored).toHaveLength(1); + expect(stored[0].value).not.toContain(MCP_TOKEN); + expect(await mcpVarIsSet(request, token)).toBe(true); + expect(await reinitializeMcp(request, token)).toBe(true); + } finally { + if (userId) { + const del = await request.delete(`/api/admin/config/user/${userId}`, { + headers: adminHeaders, + }); + expect([200, 204, 404]).toContain(del.status()); + } + await cleanupCredentials(user); + } + }); +}); diff --git a/e2e/specs/mock/scenarios/sharepoint-picker-logs.spec.ts b/e2e/specs/mock/scenarios/sharepoint-picker-logs.spec.ts new file mode 100644 index 0000000000..c4bacf917a --- /dev/null +++ b/e2e/specs/mock/scenarios/sharepoint-picker-logs.spec.ts @@ -0,0 +1,99 @@ +import { expect, test } from '@playwright/test'; +import type { Page } from '@playwright/test'; +import { NEW_CHAT_PATH } from '../helpers'; + +const SHAREPOINT_ORIGIN = 'https://contoso.sharepoint.example'; +const DOWNLOAD_URL = 'https://download.example.test/secret-contract.pdf'; +const SECRET_FILE_NAME = 'secret-contract-q3.pdf'; +const SECRET_TOKEN = 'graph-token-must-not-be-logged'; + +/** The picker page SharePoint serves: it hands the app a MessagePort, then plays the picker's side. */ +const PICKER_PAGE = ``; + +async function enableSharePoint(page: Page) { + await page.route('**/api/config', async (route) => { + const response = await route.fetch(); + const config = await response.json(); + await route.fulfill({ + response, + json: { + ...config, + sharePointFilePickerEnabled: true, + sharePointBaseUrl: SHAREPOINT_ORIGIN, + sharePointPickerGraphScope: 'Files.Read.All', + sharePointPickerSharePointScope: `${SHAREPOINT_ORIGIN}/.default`, + }, + }); + }); + await page.route('**/api/user', async (route) => { + const response = await route.fetch(); + const user = await response.json(); + await route.fulfill({ response, json: { ...user, provider: 'openid' } }); + }); + await page.route('**/api/auth/graph-token*', (route) => + route.fulfill({ + json: { access_token: SECRET_TOKEN, token_type: 'Bearer', expires_in: 3600, scope: 'x' }, + }), + ); + await page.route(`${SHAREPOINT_ORIGIN}/**`, (route) => + route.fulfill({ contentType: 'text/html', body: PICKER_PAGE }), + ); + await page.route(DOWNLOAD_URL, (route) => route.fulfill({ status: 500, body: 'unavailable' })); +} + +test.use({ serviceWorkers: 'block', viewport: { width: 1280, height: 800 } }); + +test('picking SharePoint files keeps file names and tokens out of the console @scenario:sharepoint-picker-keeps-payloads-out-of-console', async ({ + page, +}) => { + test.setTimeout(60000); + const pending: Promise[] = []; + page.on('console', (message) => { + const args = message.args().map((arg) => arg.jsonValue().catch(() => '[unserializable]')); + pending.push( + Promise.all(args).then((values) => + [message.text(), ...values.map((value) => JSON.stringify(value))].join(' '), + ), + ); + }); + await enableSharePoint(page); + + await page.goto(NEW_CHAT_PATH); + await page.getByRole('button', { name: 'Attach and tools' }).click(); + await page.getByRole('button', { name: 'More upload options' }).click(); + await page + .getByRole('button', { name: /From SharePoint/ }) + .first() + .click(); + + await expect + .poll(async () => (await Promise.all(pending)).some((text) => text.includes('download failed'))) + .toBe(true); + + const logged = await Promise.all(pending); + const leaked = logged.filter((text) => + [SECRET_FILE_NAME, SECRET_TOKEN, DOWNLOAD_URL].some((secret) => text.includes(secret)), + ); + expect(leaked).toEqual([]); +}); diff --git a/e2e/specs/mock/scenarios/tts-cache.spec.ts b/e2e/specs/mock/scenarios/tts-cache.spec.ts new file mode 100644 index 0000000000..5d56229c05 --- /dev/null +++ b/e2e/specs/mock/scenarios/tts-cache.spec.ts @@ -0,0 +1,226 @@ +import { randomUUID } from 'node:crypto'; +import { expect, test } from '@playwright/test'; +import type { Browser, BrowserContext, Page } from '@playwright/test'; +import type { User } from '../../../types'; +import { + MOCK_ENDPOINTS, + NEW_CHAT_PATH, + replyPrompt, + replyText, + selectMockEndpoint, + sendMessageAndWaitForCompletion, +} from '../helpers'; +import { deleteConversations } from '../db'; +import cleanupUser from '../../../setup/cleanupUser'; + +/** + * Read-aloud audio is cached only in the tab's memory: a replay plays without a second TTS + * request, nothing is written to Cache Storage, logout empties the cache, and the cache that + * earlier versions persisted to disk is deleted when the app starts. + */ + +test.describe.configure({ timeout: 120_000 }); + +/** The Cache Storage name earlier versions persisted audio under, keyed by message text. */ +const LEGACY_TTS_CACHE = 'tts-responses'; +const createdConversationIds: string[] = []; + +test.afterAll(async () => { + await deleteConversations(createdConversationIds); +}); + +/** Stands in for a configured external TTS provider: the e2e config has none. */ +async function routeExternalSpeech( + target: Page | BrowserContext, +): Promise<{ requests: () => number }> { + let ttsRequests = 0; + await target.route('**/api/files/speech/config/get', (route) => + route.fulfill({ + json: { textToSpeech: true, engineTTS: 'external', cacheTTS: true, ttsExternal: true }, + }), + ); + await target.route('**/api/files/speech/tts/voices', (route) => + route.fulfill({ json: ['alloy'] }), + ); + await target.route('**/api/files/speech/tts/manual', (route) => { + ttsRequests += 1; + return route.fulfill({ + body: Buffer.from([0xff, 0xf3, 0x00, 0x00]), + contentType: 'audio/mpeg', + }); + }); + return { requests: () => ttsRequests }; +} + +const useExternalEngine = (target: Page | BrowserContext) => + target.addInitScript(() => { + localStorage.setItem('engineTTS', JSON.stringify('external')); + localStorage.setItem('voice', JSON.stringify('alloy')); + }); + +/** Sends a message to the mock model and returns the read-aloud button of its reply. */ +async function replyToReadAloud(page: Page, prefix: string) { + await selectMockEndpoint(page, MOCK_ENDPOINTS[0]); + const label = `${prefix}-${randomUUID().slice(0, 8)}`; + await sendMessageAndWaitForCompletion(page, replyPrompt(label)); + await expect(page.getByText(replyText(label)).first()).toBeVisible({ timeout: 20000 }); + const conversationId = new URL(page.url()).pathname.split('/c/')[1]; + if (conversationId) createdConversationIds.push(conversationId); + return page.getByRole('button', { name: 'Read aloud' }).last(); +} + +const cacheStorageNames = (page: Page) => page.evaluate(() => caches.keys()); + +test.describe('read-aloud audio cache', () => { + test('replaying a message plays from memory without a second TTS request @scenario:tts-replay-served-from-memory', async ({ + page, + }) => { + await useExternalEngine(page); + const speech = await routeExternalSpeech(page); + await page.goto(NEW_CHAT_PATH, { timeout: 10000 }); + + const readAloud = await replyToReadAloud(page, 'tts'); + await readAloud.click(); + await expect.poll(() => speech.requests()).toBe(1); + + await expect(readAloud).toHaveAccessibleName('Read aloud'); + await readAloud.click(); + /** Give a second request time to start, if one were going to. */ + await page.waitForTimeout(1500); + expect(speech.requests()).toBe(1); + }); + + test('read-aloud audio is never written to Cache Storage @scenario:tts-audio-never-persisted', async ({ + page, + }) => { + await useExternalEngine(page); + const speech = await routeExternalSpeech(page); + await page.goto(NEW_CHAT_PATH, { timeout: 10000 }); + + const readAloud = await replyToReadAloud(page, 'disk'); + await readAloud.click(); + await expect.poll(() => speech.requests()).toBe(1); + await page.waitForTimeout(1000); + + expect(await cacheStorageNames(page)).not.toContain(LEGACY_TTS_CACHE); + expect((await cacheStorageNames(page)).filter((name) => name.startsWith('tts'))).toEqual([]); + }); + + test('logging out empties the read-aloud cache @scenario:tts-memory-cleared-on-logout', async ({ + browser, + baseURL, + }) => { + const { context, page, user } = await signInFreshUser(browser, baseURL); + try { + await useExternalEngine(context); + const speech = await routeExternalSpeech(context); + await page.goto(NEW_CHAT_PATH, { timeout: 10000 }); + + const readAloud = await replyToReadAloud(page, 'logout'); + const conversationPath = new URL(page.url()).pathname; + await readAloud.click(); + await expect.poll(() => speech.requests()).toBe(1); + + /** Same tab, no reload: only the logout can have emptied the in-memory cache. The marker + * would be gone if the document had been replaced. */ + await page.evaluate(() => Object.assign(window, { ttsSameDocument: true })); + await logOut(page); + await logIn(page, user, /\/c\//); + if (new URL(page.url()).pathname !== conversationPath) { + await page.evaluate((path) => { + window.history.pushState({}, '', path); + window.dispatchEvent(new PopStateEvent('popstate')); + }, conversationPath); + } + await expect(page).toHaveURL(new RegExp(conversationPath)); + expect(await page.evaluate(() => 'ttsSameDocument' in window)).toBe(true); + /** On a phone the sidebar drawer reopens over the conversation after signing in. */ + if ((page.viewportSize()?.width ?? 0) < 768) { + const closeSidebar = page.getByTestId('close-sidebar-button'); + if (await closeSidebar.isVisible()) { + await closeSidebar.click(); + } + } + await page.getByRole('button', { name: 'Read aloud' }).last().click(); + await expect.poll(() => speech.requests()).toBe(2); + } finally { + await context.close().catch(() => undefined); + await cleanupUser(user); + } + }); + + test('audio earlier versions saved to Cache Storage is deleted when the app starts @scenario:tts-legacy-disk-cache-purged-at-startup', async ({ + page, + }) => { + await page.goto(NEW_CHAT_PATH, { timeout: 10000 }); + await page.evaluate(async (name) => { + const cache = await caches.open(name); + await cache.put( + '/E2E%20legacy%20reply%20text', + new Response(new Blob([new Uint8Array([1])])), + ); + }, LEGACY_TTS_CACHE); + + await page.reload(); + await expect + .poll(() => page.evaluate((name) => caches.has(name), LEGACY_TTS_CACHE)) + .toBe(false); + }); +}); + +/** A dedicated account, so logging out does not end the session other specs share. */ +async function signInFreshUser( + browser: Browser, + baseURL: string | undefined, +): Promise<{ context: BrowserContext; page: Page; user: User }> { + if (typeof baseURL !== 'string') { + throw new Error('baseURL must be configured for mock scenarios'); + } + const user: User = { + name: 'TTS Cache', + email: `tts-cache-${randomUUID().slice(0, 8)}@example.com`, + password: `Pw-${randomUUID()}`, + }; + await cleanupUser(user); + const context = await browser.newContext({ storageState: undefined, baseURL }); + await context.addInitScript(() => { + localStorage.setItem('navVisible', 'true'); + /** The shared storage state opts into sending with Enter; a fresh account has to as well. */ + localStorage.setItem('enterToSend', 'true'); + }); + const registered = await context.request.post('/api/auth/register', { + data: { + email: user.email, + name: user.name, + password: user.password, + confirm_password: user.password, + }, + }); + expect(registered.ok()).toBeTruthy(); + + const page = await context.newPage(); + await page.goto('/login', { timeout: 10000 }); + await logIn(page, user); + return { context, page, user }; +} + +async function logIn(page: Page, user: User, landing = /\/c\/new/): Promise { + await page.getByLabel('Email').fill(user.email); + await page.getByLabel('Password').fill(user.password); + await page.getByTestId('login-button').click(); + await page.waitForURL(landing, { timeout: 10000 }); +} + +async function logOut(page: Page): Promise { + /** The sidebar toggle is only meaningful once the app has rendered its chrome. */ + await expect(page.getByRole('textbox', { name: 'Message input' })).toBeVisible({ + timeout: 20000, + }); + const navUser = page.getByTestId('nav-user'); + if (!(await navUser.isVisible())) { + await page.getByTestId('header-open-sidebar-button').click(); + } + await navUser.click(); + await page.getByRole('menuitem', { name: 'Log out' }).click(); + await page.waitForURL(/\/login/, { timeout: 10000 }); +} diff --git a/librechat.example.yaml b/librechat.example.yaml index 04389625ba..c38a3b80e3 100644 --- a/librechat.example.yaml +++ b/librechat.example.yaml @@ -29,6 +29,14 @@ projects: # outputChars: 512 # characters of each output kept, half from the start and half from the end # argsChars: 512 # characters of serialized arguments kept; JSON stays valid JSON +# Stored user credentials (tool and web search keys, MCP custom user variables) are +# written with a random IV per value ("v3", the default). Both formats are always read. +# During a rolling upgrade of a multi-replica deployment, set "legacy" until every +# replica runs a release that reads v3, then remove it. A rollback to a release that +# predates v3 cannot read credentials saved in the v3 format. +# credentials: +# writeFormat: v3 + # Langfuse connections are managed through Settings > Langfuse when available. # That flow verifies the credentials and stores the secret key encrypted; do not # place a plaintext langfuse.secretKey in this file. Environment-managed central diff --git a/packages/api/src/agents/auth.ts b/packages/api/src/agents/auth.ts index 5783de7b98..90a0d0b184 100644 --- a/packages/api/src/agents/auth.ts +++ b/packages/api/src/agents/auth.ts @@ -1,5 +1,6 @@ -import { logger, decrypt } from '@librechat/data-schemas'; +import { logger } from '@librechat/data-schemas'; import type { IPluginAuth, PluginAuthMethods } from '@librechat/data-schemas'; +import { decryptCredential } from '~/crypto/credentials'; export interface GetPluginAuthMapParams { userId: string; @@ -53,7 +54,7 @@ export async function getPluginAuthMap({ decryptionPromises.push( (async () => { try { - const decryptedValue = await decrypt(auth.value); + const decryptedValue = await decryptCredential(auth.value); authMap[pluginKey][auth.authField] = decryptedValue; } catch (error) { const message = error instanceof Error ? error.message : 'Unknown error'; diff --git a/packages/api/src/crypto/credentials.spec.ts b/packages/api/src/crypto/credentials.spec.ts new file mode 100644 index 0000000000..40a9d9c3ad --- /dev/null +++ b/packages/api/src/crypto/credentials.spec.ts @@ -0,0 +1,90 @@ +import '~/mcp/registry/__tests__/helpers/setupCredsEnv'; +import mongoose from 'mongoose'; +import { MongoMemoryServer } from 'mongodb-memory-server'; +import { encrypt, decrypt, createModels, createMethods } from '@librechat/data-schemas'; +import type { PluginAuthMethods } from '@librechat/data-schemas'; +import { encryptCredential, decryptCredential } from './credentials'; +import { getPluginAuthMap } from '~/agents/auth'; + +describe('credential encryption', () => { + it('writes v3 values with a fresh IV each time and reads them back', async () => { + const first = await encryptCredential('sk-secret'); + const second = await encryptCredential('sk-secret'); + + expect(first).toMatch(/^v3:[0-9a-f]{32}:[0-9a-f]+$/); + expect(second).not.toBe(first); + await expect(decryptCredential(first)).resolves.toBe('sk-secret'); + await expect(decryptCredential(second)).resolves.toBe('sk-secret'); + }); + + it('writes v3 when the format is v3 explicitly', async () => { + const value = await encryptCredential('sk-secret', 'v3'); + + expect(value).toMatch(/^v3:[0-9a-f]{32}:[0-9a-f]+$/); + await expect(decryptCredential(value)).resolves.toBe('sk-secret'); + }); + + it('writes the legacy fixed-IV format when configured, readable by legacy decrypt', async () => { + const value = await encryptCredential('sk-secret', 'legacy'); + + expect(value).toMatch(/^[0-9a-f]+$/); + expect(value).toBe(await encrypt('sk-secret')); + await expect(decrypt(value)).resolves.toBe('sk-secret'); + await expect(decryptCredential(value)).resolves.toBe('sk-secret'); + }); + + it('still reads legacy fixed-IV values', async () => { + const legacy = await encrypt('legacy-secret'); + + expect(legacy.startsWith('v3:')).toBe(false); + await expect(decryptCredential(legacy)).resolves.toBe('legacy-secret'); + }); + + it('round-trips an empty string', async () => { + await expect(decryptCredential(await encryptCredential(''))).resolves.toBe(''); + }); +}); + +describe('getPluginAuthMap with mixed-version records', () => { + let mongoServer: MongoMemoryServer; + let methods: PluginAuthMethods; + + beforeAll(async () => { + mongoServer = await MongoMemoryServer.create(); + await mongoose.connect(mongoServer.getUri()); + createModels(mongoose); + methods = createMethods(mongoose); + }); + + afterAll(async () => { + await mongoose.disconnect(); + await mongoServer.stop(); + }); + + it('decrypts legacy and v3 records stored for the same plugin', async () => { + const userId = new mongoose.Types.ObjectId().toString(); + const pluginKey = 'mcp_server'; + await methods.updatePluginAuth({ + userId, + pluginKey, + authField: 'LEGACY_KEY', + value: await encrypt('legacy-value'), + }); + await methods.updatePluginAuth({ + userId, + pluginKey, + authField: 'NEW_KEY', + value: await encryptCredential('new-value'), + }); + + const authMap = await getPluginAuthMap({ + userId, + pluginKeys: [pluginKey], + findPluginAuthsByKeys: methods.findPluginAuthsByKeys, + }); + + expect(authMap).toEqual({ + [pluginKey]: { LEGACY_KEY: 'legacy-value', NEW_KEY: 'new-value' }, + }); + }); +}); diff --git a/packages/api/src/crypto/credentials.ts b/packages/api/src/crypto/credentials.ts new file mode 100644 index 0000000000..a5b0102968 --- /dev/null +++ b/packages/api/src/crypto/credentials.ts @@ -0,0 +1,33 @@ +import { encryptV3, decryptV3, encrypt, decrypt } from '@librechat/data-schemas'; +import type { TCredentialsConfig } from 'librechat-data-provider'; + +const V3_PREFIX = 'v3:'; + +export type CredentialWriteFormat = TCredentialsConfig['writeFormat']; + +/** + * Encrypts a stored user credential (plugin, tool, or MCP custom user variable). + * `v3` (the default) uses a random IV per value and carries the `v3:` prefix; + * `legacy` writes the fixed-IV format that releases predating `v3` can read. + */ +export async function encryptCredential( + value: string, + format: CredentialWriteFormat = 'v3', +): Promise { + if (format === 'legacy') { + return encrypt(value); + } + return encryptV3(value); +} + +/** + * Decrypts a stored user credential by format: `v3:` values use `decryptV3`, + * anything else is a legacy fixed-IV value (hex only, so it can never start + * with `v3:`) and uses the legacy `decrypt`. + */ +export async function decryptCredential(value: string): Promise { + if (value.startsWith(V3_PREFIX)) { + return decryptV3(value); + } + return decrypt(value); +} diff --git a/packages/api/src/crypto/index.ts b/packages/api/src/crypto/index.ts index ac78a47b29..3b265a71f0 100644 --- a/packages/api/src/crypto/index.ts +++ b/packages/api/src/crypto/index.ts @@ -9,3 +9,4 @@ export { getRandomValues, } from '@librechat/data-schemas'; export * from './jwt'; +export * from './credentials'; diff --git a/packages/data-provider/src/config.ts b/packages/data-provider/src/config.ts index a01a4c6932..d7575d9ca6 100644 --- a/packages/data-provider/src/config.ts +++ b/packages/data-provider/src/config.ts @@ -3882,6 +3882,19 @@ export const langfuseTraceConfigSchema = z.object({ export type LangfuseTraceConfig = z.infer; +/** + * Format new stored user credentials (tool, web search and MCP custom user variable values) + * are written in. Both formats are always readable; `legacy` keeps writes readable by + * replicas on releases that predate `v3` during a rolling upgrade. + */ +export const credentialsConfigSchema = z + .object({ + writeFormat: z.enum(['v3', 'legacy']).default('v3'), + }) + .strict(); + +export type TCredentialsConfig = z.infer; + export const langfuseConfigSchema = z.object({ enabled: z.boolean().optional(), publicKey: z.string().optional(), @@ -4012,6 +4025,7 @@ export const configSchema = z.object({ .strict() .optional(), langfuse: langfuseConfigSchema.optional(), + credentials: credentialsConfigSchema.optional(), memory: memorySchema.optional(), summarization: summarizationConfigSchema.optional(), skillSync: skillSyncConfigSchema, diff --git a/packages/data-schemas/src/app/service.spec.ts b/packages/data-schemas/src/app/service.spec.ts index 7bb0320086..92d59a5aa7 100644 --- a/packages/data-schemas/src/app/service.spec.ts +++ b/packages/data-schemas/src/app/service.spec.ts @@ -309,6 +309,20 @@ describe('AppService MCP App sandbox configuration', () => { }); }); +describe('AppService credential write format', () => { + it('defaults stored credential writes to v3', async () => { + const result = await AppService({ config: {} as DeepPartial }); + expect(result.credentials).toEqual({ writeFormat: 'v3' }); + }); + + it('carries a configured legacy write format onto the app config', async () => { + const result = await AppService({ + config: { credentials: { writeFormat: 'legacy' } } as DeepPartial, + }); + expect(result.credentials).toEqual({ writeFormat: 'legacy' }); + }); +}); + describe('AppService conversation list limits', () => { it('carries the configured filter limits onto the app config', async () => { const result = await AppService({ diff --git a/packages/data-schemas/src/app/service.ts b/packages/data-schemas/src/app/service.ts index 725fd4f644..d34a6e8063 100644 --- a/packages/data-schemas/src/app/service.ts +++ b/packages/data-schemas/src/app/service.ts @@ -234,6 +234,7 @@ export const AppService = async (params?: { fileStrategies: config.fileStrategies, cloudfront: config.cloudfront as AppConfig['cloudfront'], secureImageLinks: config.secureImageLinks !== false, + credentials: { writeFormat: config.credentials?.writeFormat ?? 'v3' }, conversationList: loadConversationListConfig(config), toolCallPreviews: loadToolCallPreviewsConfig(config), }; diff --git a/packages/data-schemas/src/types/app.ts b/packages/data-schemas/src/types/app.ts index 92ab7c996e..4394d1fbe5 100644 --- a/packages/data-schemas/src/types/app.ts +++ b/packages/data-schemas/src/types/app.ts @@ -4,6 +4,7 @@ import type { TFileConfig, TAzureConfig, TCustomConfig, + TCredentialsConfig, TChatProjectsConfig, TMemoryConfig, EModelEndpoint, @@ -118,6 +119,8 @@ export interface AppConfig { fileConfig?: TFileConfig; /** Secure image links configuration, enabled unless explicitly disabled */ secureImageLinks?: TCustomConfig['secureImageLinks']; + /** Stored user credential write format, `v3` unless configured */ + credentials?: TCredentialsConfig; /** Validation limits for the conversation list's filter facets */ conversationList?: TConversationListConfig; /** Bounds for the tool-call previews sent on conversation loads */