mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-09-21 23:55:23 +00:00
* feat(web-search): route outbound search and scrape requests through the SSRF-safe agent Build the SSRF-safe agents at the web-search tool-assembly site and pass them into the search tool config so outbound search and scrape connections are validated at connect time against their resolved IP, on every hop including redirects, consistent with the other outbound clients. Add allowedAddresses to webSearchSchema, reusing allowedAddressesSchema, so self-hosters can permit a deliberately-private search or scrape endpoint (for example a private SearXNG instance). The field is resolved directly from the webSearch config at the createSearchTool call site, not through loadWebSearchAuth, because it is config and not an auth credential. webSearchSchema is flat (providers are chosen by enums, not by counting keys), so the field is inert with respect to provider selection. Document the field and its operator warning in librechat.example.yaml, and assert the wiring in handleTools.test.js: the SSRF-safe agents are threaded into the search tool config, allowedAddresses is passed through when set, and omitting it still threads the agents with no exemptions. TODO awaits @librechat/agents release with the httpAgent hook: this consumes optional httpAgent/httpsAgent fields on the search-tool config that are not yet in a published @librechat/agents. package.json is intentionally left at the current version; bump it to the release that ships the hook before this lands. Validated locally against a revendored @librechat/agents build, not a published release. * 🛡️ fix: Apply allowedAddresses to the Web Search SSRF Preflight The connect-time SSRF agent already honors webSearch.allowedAddresses, but loadWebSearchAuth ran the isSSRFUrl preflight without it, so an admin-permitted private search or scrape URL was stripped before the agent could ever use it. Thread allowedAddresses and the URL's effective port through isSSRFTarget and resolveHostnameSSRF so the exemption is consistent across both SSRF layers. * 🛡️ fix: Validate Web Search Destinations and Defer to Configured Proxies Handing agents to createSearchTool covered only the connect-time DNS lookup, which Node skips for IP-literal hosts, and a configured proxy connects on our behalf without running that check. A literal private target such as http://169.254.169.254 could therefore reach the network. Route every resolved web-search destination through the existing applySSRFSafeAgentIfDirect contract so a blocked literal target throws before any request is made, and withhold the agents when a proxy owns egress, since one agent pair is shared by every provider and a direct-connect agent on a proxied connection would break the request while asserting protection the proxy's network context cannot provide. * 🛡️ fix: Keep Web Search SSRF Agents Under a Proxy and Restore Pooling Withholding the agents whenever a proxy was configured removed protection from every direct and NO_PROXY destination in exchange for preventing a failure that cannot occur: for an https target Axios substitutes its own CONNECT tunnel, so the injected agent is never used for the proxy connection. Only a plaintext http target keeps our agent and repoints it at the proxy, and only a proxy whose hostname resolves private then trips the connect-time check. Always pass the agents and exempt the proxy endpoint instead, deriving host:port from the same PROXY, HTTP_PROXY, and HTTPS_PROXY resolution the rest of LibreChat uses so the proxy hop stays reachable while destinations remain guarded. Axios already applies NO_PROXY per request, so bypassed routes keep enforcement with no extra logic. Drop the load-time destination validation. It duplicated the isSSRFTarget preflight for user-provided URLs, rejected admin values that were previously legal, and threw from inside loadTools, where both loader wrappers swallow the error and drop every tool for the turn rather than degrading web search alone. Build the agents with keepAlive and cache them per exemption list. A bare http.Agent does not pool, so the previous code replaced the pooled global agents for every search, scrape, and rerank call and allocated a fresh pair per turn. * 🛡️ fix: Reject IP-Literal Private Targets on Web Search Connections Node resolves nothing for a literal host, so the connect-time lookup never saw one: a destination or a redirect target given as http://169.254.169.254 reached the network. Redirect hops pass through the same createConnection, so checking the literal there covers both cases and removes the need for a maxRedirects control that createSearchTool cannot accept. Gate it behind blockLiteralHosts so only web search opts in. A caller that reaches a proxy or a deliberate private service by literal address must exempt it first, and the merged consumers of createSSRFSafeAgents have no such exemption, so enabling this everywhere would break configurations that work today. * 🛡️ fix: Keep IPv6 Brackets on Derived Proxy Exemptions The exemption parser accepts an IPv6 entry only as [ipv6]:port, so stripping the brackets produced fd00::1:3128, which carries three colons and is dropped as malformed. An IPv6 proxy therefore stayed unexempted and the connect-time check rejected it, failing every web-search request routed through it. Use the URL hostname as parsed, which already carries the brackets. * 🛡️ fix: Exempt Proxies Configured Through ALL_PROXY Axios resolves a proxy through proxy-from-env, which falls back to all_proxy in either case after <protocol>_proxy, so ALL_PROXY on its own is enough to route a request through a proxy. Exemptions were derived from PROXY, HTTP_PROXY, and HTTPS_PROXY only, leaving such a proxy unexempted and rejected with ESSRF. Derive the exemptions from the full set of variables that can put a proxy in front of these requests instead. The installed proxy-from-env 2.1.0 reads no npm_config variables, so those are deliberately not included. * 🛡️ fix: Drop the Unearned PROXY Exemption and Harden the Web Search Guard Nothing on this path consumes PROXY: Axios resolves proxies through proxy-from-env, which reads only <protocol>_proxy and all_proxy, and web search never calls applyAxiosProxyConfig. Exempting it therefore granted a bypass rather than preserving a working route, and a user-settable search URL that redirects to that address reached it and returned the body. Remove PROXY and proxy, and skip a socks endpoint for the same reason, since Axios cannot proxy through one. Tolerate a non-array allowedAddresses instead of spreading it, which threw out of loadTools and dropped every tool for the turn. The YAML path is schema-validated but the admin override path merges without parsing, so the value is reachable. Separate cache keys with NUL rather than a newline, so an entry containing a newline cannot collide with two separate entries, and bound the cache. Give the agents the idle timeout the global agents carry, which keepAlive alone did not restore. Reject a unix socket, which carries no host to validate. Also treat fec0::/10 site-local as private, matching the fe80::/10 handling beside it. Exercise the real resolver in handleTools.test.js rather than mocking it, so the wiring test now fails if the agents it threads do not actually block a private target. * 🛡️ fix: Derive Proxy Exemptions Through Axios's Own Resolver Unioning every populated proxy variable exempted addresses that never carry a request. proxy-from-env picks a protocol-specific variable before all_proxy and lowercase before uppercase, so an ignored value became a trusted host:port that a redirect onto a direct route could reach. It also normalizes a scheme-less value such as proxy.internal:3128 to an http URL, where parsing the raw string yielded an empty hostname and no exemption at all, breaking the proxy hop. Resolve through getProxyForUrl, the entry point Axios itself calls, so precedence, scheme normalization, and NO_PROXY match exactly and cannot drift. NO_PROXY covering everything now yields no exemption, since nothing is proxied. Declared locally rather than adding a types package, alongside the existing declaration in the same directory. Also revert the fec0::/10 site-local change. domain.spec asserts that boundary deliberately to prove the fe80::/10 mask does not over-reach, and the shared address schema still classifies fec0 as public, so a runtime block there would leave operators unable to configure the exemption. It belongs with those two together, not in this PR. * 🛡️ fix: Resolve Proxy Exemptions Against the Real Destinations Resolving against placeholder probe hosts applied destination-specific NO_PROXY rules to a host nobody dials. With NO_PROXY matching the probe domain but not a real provider, no exemption was derived even though Axios still proxied the actual request, so the agent rejected the private proxy hop with ESSRF. Resolve per configured destination instead, passing the values loadWebSearchAuth already resolved. Only plaintext http destinations are considered, since for an https destination Axios substitutes its own CONNECT tunnel and never uses the injected agent for the proxy connection, which is also why provider defaults need no exemption: every one of them is https. * 🛡️ fix: Accept Embedded-IPv4 IPv6 Forms in the Address Exemption Schema The runtime guard blocks 6to4, NAT64, and Teredo addresses whose embedded IPv4 is private, but the schema's local copy recognized only ULA, link-local, and the dotted IPv4-mapped form, so an entry such as [64:ff9b::a00:1]:8080 was dropped as a public literal. An operator reaching a private endpoint that way could not configure the exemption at all. Mirror hasPrivateEmbeddedIPv4 in the schema helper, which the surrounding comment already asks to keep in sync. Public embedded addresses stay rejected, since an exemption there has no defensive purpose.
884 lines
31 KiB
JavaScript
884 lines
31 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const { MongoMemoryServer } = require('mongodb-memory-server');
|
|
|
|
const mockPluginService = {
|
|
updateUserPluginAuth: jest.fn(),
|
|
deleteUserPluginAuth: jest.fn(),
|
|
getUserPluginAuthValue: jest.fn(),
|
|
};
|
|
const mockGetMCPServerTools = jest.fn();
|
|
const mockCreateMCPTool = jest.fn();
|
|
const mockCreateMCPTools = jest.fn();
|
|
const mockGetServerConfig = jest.fn();
|
|
const mockGetAccessibleMcpServerNames = jest.fn(async () => []);
|
|
|
|
const mockCreateSearchTool = jest.fn(() => ({ name: 'web_search' }));
|
|
const mockLoadWebSearchAuth = jest.fn(async () => ({
|
|
authResult: { searchProvider: 'serper', searxngInstanceUrl: 'http://searxng.internal:8080' },
|
|
}));
|
|
|
|
jest.mock('@librechat/agents', () => ({
|
|
...jest.requireActual('@librechat/agents'),
|
|
createSearchTool: (...args) => mockCreateSearchTool(...args),
|
|
}));
|
|
|
|
jest.mock('@librechat/api', () => ({
|
|
...jest.requireActual('@librechat/api'),
|
|
loadWebSearchAuth: (...args) => mockLoadWebSearchAuth(...args),
|
|
}));
|
|
|
|
jest.mock('~/server/services/PluginService', () => mockPluginService);
|
|
|
|
jest.mock('~/server/services/Config', () => ({
|
|
getAppConfig: jest.fn().mockResolvedValue({
|
|
// Default app config for tool tests
|
|
paths: { uploads: '/tmp' },
|
|
fileStrategy: 'local',
|
|
filteredTools: [],
|
|
includedTools: [],
|
|
}),
|
|
getCachedTools: jest.fn().mockResolvedValue({
|
|
// Default cached tools for tests
|
|
dalle: {
|
|
type: 'function',
|
|
function: {
|
|
name: 'dalle',
|
|
description: 'DALL-E image generation',
|
|
parameters: {},
|
|
},
|
|
},
|
|
}),
|
|
getMCPServerTools: (...args) => mockGetMCPServerTools(...args),
|
|
}));
|
|
|
|
jest.mock('~/server/services/MCP', () => ({
|
|
createMCPTool: (...args) => mockCreateMCPTool(...args),
|
|
createMCPTools: (...args) => mockCreateMCPTools(...args),
|
|
createMCPPermissionContext: jest.fn(() => ({
|
|
canUseServers: jest.fn().mockResolvedValue(true),
|
|
})),
|
|
resolveConfigServers: jest.fn().mockResolvedValue({}),
|
|
resolveMcpServerContext: jest.fn(async () => ({ configServers: {}, serverNames: [] })),
|
|
/** Mirrors the real resolver: threaded set wins, then the accessible fetch
|
|
* (union with raw so operator-only fixtures keep working), incomplete on
|
|
* failure. The pure sensitivity predicate is the REAL @librechat/api one. */
|
|
resolveCollisionAuditNames: jest.fn(async ({ rawServerNames, accessibleServerNames }) => {
|
|
if (accessibleServerNames?.length) {
|
|
return { names: accessibleServerNames, complete: true };
|
|
}
|
|
try {
|
|
const fetched = await mockGetAccessibleMcpServerNames();
|
|
return {
|
|
names: fetched?.length ? fetched : rawServerNames,
|
|
complete: true,
|
|
};
|
|
} catch {
|
|
return { names: rawServerNames, complete: false };
|
|
}
|
|
}),
|
|
}));
|
|
|
|
jest.mock('~/config', () => ({
|
|
getMCPServersRegistry: jest.fn(() => ({
|
|
getServerConfig: (...args) => mockGetServerConfig(...args),
|
|
})),
|
|
}));
|
|
|
|
const { Calculator } = require('@librechat/agents');
|
|
const { Tools, Constants } = require('librechat-data-provider');
|
|
const { ASK_USER_QUESTION_TOOL_NAME } = require('@librechat/api');
|
|
|
|
const { User } = require('~/db/models');
|
|
const PluginService = require('~/server/services/PluginService');
|
|
const { validateTools, loadTools, loadToolWithAuth } = require('./handleTools');
|
|
const { StructuredSD, availableTools, DALLE3 } = require('../');
|
|
|
|
describe('Tool Handlers', () => {
|
|
let mongoServer;
|
|
let fakeUser;
|
|
const pluginKey = 'dalle';
|
|
const pluginKey2 = 'wolfram';
|
|
const ToolClass = DALLE3;
|
|
const initialTools = [pluginKey, pluginKey2];
|
|
const mockCredential = 'mock-credential';
|
|
const mainPlugin = availableTools.find((tool) => tool.pluginKey === pluginKey);
|
|
const authConfigs = mainPlugin.authConfig;
|
|
|
|
beforeAll(async () => {
|
|
mongoServer = await MongoMemoryServer.create();
|
|
const mongoUri = mongoServer.getUri();
|
|
await mongoose.connect(mongoUri);
|
|
|
|
const userAuthValues = {};
|
|
mockPluginService.getUserPluginAuthValue.mockImplementation((userId, authField) => {
|
|
return userAuthValues[`${userId}-${authField}`];
|
|
});
|
|
mockPluginService.updateUserPluginAuth.mockImplementation(
|
|
(userId, authField, _pluginKey, credential) => {
|
|
const fields = authField.split('||');
|
|
fields.forEach((field) => {
|
|
userAuthValues[`${userId}-${field}`] = credential;
|
|
});
|
|
},
|
|
);
|
|
|
|
fakeUser = new User({
|
|
name: 'Fake User',
|
|
username: 'fakeuser',
|
|
email: 'fakeuser@example.com',
|
|
emailVerified: false,
|
|
// file deepcode ignore NoHardcodedPasswords/test: fake value
|
|
password: 'fakepassword123',
|
|
avatar: '',
|
|
provider: 'local',
|
|
role: 'USER',
|
|
googleId: null,
|
|
plugins: [],
|
|
refreshToken: [],
|
|
});
|
|
await fakeUser.save();
|
|
for (const authConfig of authConfigs) {
|
|
await PluginService.updateUserPluginAuth(
|
|
fakeUser._id,
|
|
authConfig.authField,
|
|
pluginKey,
|
|
mockCredential,
|
|
);
|
|
}
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await mongoose.disconnect();
|
|
await mongoServer.stop();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
// Clear mocks but not the database since we need the user to persist
|
|
jest.clearAllMocks();
|
|
|
|
// Reset the mock implementations
|
|
const userAuthValues = {};
|
|
mockPluginService.getUserPluginAuthValue.mockImplementation((userId, authField) => {
|
|
return userAuthValues[`${userId}-${authField}`];
|
|
});
|
|
mockPluginService.updateUserPluginAuth.mockImplementation(
|
|
(userId, authField, _pluginKey, credential) => {
|
|
const fields = authField.split('||');
|
|
fields.forEach((field) => {
|
|
userAuthValues[`${userId}-${field}`] = credential;
|
|
});
|
|
},
|
|
);
|
|
|
|
// Re-add the auth configs for the user
|
|
for (const authConfig of authConfigs) {
|
|
await PluginService.updateUserPluginAuth(
|
|
fakeUser._id,
|
|
authConfig.authField,
|
|
pluginKey,
|
|
mockCredential,
|
|
);
|
|
}
|
|
});
|
|
|
|
describe('validateTools', () => {
|
|
it('returns valid tools given input tools and user authentication', async () => {
|
|
const validTools = await validateTools(fakeUser._id, initialTools);
|
|
expect(validTools).toBeDefined();
|
|
expect(validTools.some((tool) => tool === pluginKey)).toBeTruthy();
|
|
expect(validTools.length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('removes tools without valid credentials from the validTools array', async () => {
|
|
const validTools = await validateTools(fakeUser._id, initialTools);
|
|
expect(validTools.some((tool) => tool.pluginKey === pluginKey2)).toBeFalsy();
|
|
});
|
|
|
|
it('returns an empty array when no authenticated tools are provided', async () => {
|
|
const validTools = await validateTools(fakeUser._id, []);
|
|
expect(validTools).toEqual([]);
|
|
});
|
|
|
|
it('should validate a tool from an Environment Variable', async () => {
|
|
const plugin = availableTools.find((tool) => tool.pluginKey === pluginKey2);
|
|
const authConfigs = plugin.authConfig;
|
|
for (const authConfig of authConfigs) {
|
|
process.env[authConfig.authField] = mockCredential;
|
|
}
|
|
const validTools = await validateTools(fakeUser._id, [pluginKey2]);
|
|
expect(validTools.length).toEqual(1);
|
|
for (const authConfig of authConfigs) {
|
|
delete process.env[authConfig.authField];
|
|
}
|
|
});
|
|
});
|
|
|
|
describe('loadTools', () => {
|
|
let toolFunctions;
|
|
let loadTool1;
|
|
let loadTool2;
|
|
let loadTool3;
|
|
const sampleTools = [...initialTools, 'calculator'];
|
|
let ToolClass2 = Calculator;
|
|
let remainingTools = availableTools.filter(
|
|
(tool) => sampleTools.indexOf(tool.pluginKey) === -1,
|
|
);
|
|
|
|
beforeAll(async () => {
|
|
const toolMap = await loadTools({
|
|
user: fakeUser._id,
|
|
tools: sampleTools,
|
|
returnMap: true,
|
|
useSpecs: true,
|
|
});
|
|
toolFunctions = toolMap;
|
|
loadTool1 = toolFunctions[sampleTools[0]];
|
|
loadTool2 = toolFunctions[sampleTools[1]];
|
|
loadTool3 = toolFunctions[sampleTools[2]];
|
|
});
|
|
|
|
let originalEnv;
|
|
|
|
beforeEach(() => {
|
|
originalEnv = process.env;
|
|
process.env = { ...originalEnv };
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env = originalEnv;
|
|
});
|
|
|
|
it('returns the expected load functions for requested tools', async () => {
|
|
expect(loadTool1).toBeDefined();
|
|
expect(loadTool2).toBeDefined();
|
|
expect(loadTool3).toBeDefined();
|
|
|
|
for (const tool of remainingTools) {
|
|
expect(toolFunctions[tool.pluginKey]).toBeUndefined();
|
|
}
|
|
});
|
|
|
|
it('should initialize an authenticated tool or one without authentication', async () => {
|
|
const authTool = await loadTool1();
|
|
const tool = await loadTool3();
|
|
expect(authTool).toBeInstanceOf(ToolClass);
|
|
expect(tool).toBeInstanceOf(ToolClass2);
|
|
});
|
|
|
|
it('should initialize an authenticated tool with primary auth field', async () => {
|
|
process.env.DALLE3_API_KEY = 'mocked_api_key';
|
|
const initToolFunction = loadToolWithAuth(
|
|
'userId',
|
|
['DALLE3_API_KEY||DALLE_API_KEY'],
|
|
ToolClass,
|
|
);
|
|
const authTool = await initToolFunction();
|
|
|
|
expect(authTool).toBeInstanceOf(ToolClass);
|
|
expect(mockPluginService.getUserPluginAuthValue).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should initialize an authenticated tool with alternate auth field when primary is missing', async () => {
|
|
delete process.env.DALLE3_API_KEY; // Ensure the primary key is not set
|
|
process.env.DALLE_API_KEY = 'mocked_alternate_api_key';
|
|
const initToolFunction = loadToolWithAuth(
|
|
'userId',
|
|
['DALLE3_API_KEY||DALLE_API_KEY'],
|
|
ToolClass,
|
|
);
|
|
const authTool = await initToolFunction();
|
|
|
|
expect(authTool).toBeInstanceOf(ToolClass);
|
|
expect(mockPluginService.getUserPluginAuthValue).toHaveBeenCalledTimes(1);
|
|
expect(mockPluginService.getUserPluginAuthValue).toHaveBeenCalledWith(
|
|
'userId',
|
|
'DALLE3_API_KEY',
|
|
true,
|
|
);
|
|
});
|
|
|
|
it('should fallback to getUserPluginAuthValue when env vars are missing', async () => {
|
|
mockPluginService.updateUserPluginAuth('userId', 'DALLE_API_KEY', 'dalle', 'mocked_api_key');
|
|
const initToolFunction = loadToolWithAuth(
|
|
'userId',
|
|
['DALLE3_API_KEY||DALLE_API_KEY'],
|
|
ToolClass,
|
|
);
|
|
const authTool = await initToolFunction();
|
|
|
|
expect(authTool).toBeInstanceOf(ToolClass);
|
|
expect(mockPluginService.getUserPluginAuthValue).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it('should throw an error for an unauthenticated tool', async () => {
|
|
try {
|
|
await loadTool2();
|
|
} catch (error) {
|
|
expect(error).toBeDefined();
|
|
}
|
|
});
|
|
it('returns an empty object when no tools are requested', async () => {
|
|
toolFunctions = await loadTools({
|
|
user: fakeUser._id,
|
|
returnMap: true,
|
|
useSpecs: true,
|
|
});
|
|
expect(toolFunctions).toEqual({});
|
|
});
|
|
it('should return the StructuredTool version when using functions', async () => {
|
|
process.env.SD_WEBUI_URL = mockCredential;
|
|
toolFunctions = await loadTools({
|
|
user: fakeUser._id,
|
|
tools: ['stable-diffusion'],
|
|
functions: true,
|
|
returnMap: true,
|
|
useSpecs: true,
|
|
});
|
|
const structuredTool = await toolFunctions['stable-diffusion']();
|
|
expect(structuredTool).toBeInstanceOf(StructuredSD);
|
|
delete process.env.SD_WEBUI_URL;
|
|
});
|
|
|
|
it('loads the ask_user_question tool when not returning a map', async () => {
|
|
const { loadedTools } = await loadTools({
|
|
user: fakeUser._id,
|
|
tools: [ASK_USER_QUESTION_TOOL_NAME],
|
|
useSpecs: true,
|
|
});
|
|
expect(loadedTools).toHaveLength(1);
|
|
expect(loadedTools[0].name).toBe(ASK_USER_QUESTION_TOOL_NAME);
|
|
});
|
|
|
|
it('passes request body to chat MCP tool creation and skips stale cache for BODY-scoped servers', async () => {
|
|
const serverName = 'body-scoped';
|
|
const toolKey = `search${Constants.mcp_delimiter}${serverName}`;
|
|
const requestBody = { conversationId: 'conv-123', messageId: 'msg-123' };
|
|
const jobCreatedAt = 1234;
|
|
const serverConfig = {
|
|
type: 'streamable-http',
|
|
url: 'https://api.example.com/messages/{{LIBRECHAT_BODY_MESSAGEID}}/mcp',
|
|
source: 'yaml',
|
|
};
|
|
|
|
mockGetServerConfig.mockResolvedValue(serverConfig);
|
|
mockCreateMCPTool.mockResolvedValue({ name: 'loaded-mcp-tool' });
|
|
|
|
const result = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [toolKey],
|
|
options: {
|
|
req: {
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
body: requestBody,
|
|
},
|
|
jobCreatedAt,
|
|
},
|
|
});
|
|
|
|
expect(result.loadedTools).toEqual([{ name: 'loaded-mcp-tool' }]);
|
|
expect(mockGetMCPServerTools).toHaveBeenCalledWith(
|
|
fakeUser._id.toString(),
|
|
serverName,
|
|
serverConfig,
|
|
);
|
|
expect(mockCreateMCPTool).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
requestBody,
|
|
jobCreatedAt,
|
|
toolKey,
|
|
config: serverConfig,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('resolves normalized tool keys back to the raw server for config lookups', async () => {
|
|
/** Model-facing keys embed `normalizeServerName(server)`, while the
|
|
* registry/config/cache are keyed by the raw config name — a
|
|
* special-character server must still resolve its config and receive
|
|
* the normalized key as the toolKey. */
|
|
const rawServerName = 'Connector: Company';
|
|
const normalizedKey = `search${Constants.mcp_delimiter}Connector__Company`;
|
|
const serverConfig = {
|
|
type: 'streamable-http',
|
|
url: 'https://api.example.com/mcp',
|
|
source: 'yaml',
|
|
};
|
|
|
|
const { resolveMcpServerContext } = require('~/server/services/MCP');
|
|
resolveMcpServerContext.mockResolvedValueOnce({
|
|
configServers: { [rawServerName]: serverConfig },
|
|
serverNames: ['Connector__Company'],
|
|
rawServerNames: [rawServerName],
|
|
});
|
|
/** Direct-first: the parsed (normalized) name is tried as-is and only
|
|
* the raw alias resolves — mirroring a registry keyed by raw names. */
|
|
mockGetServerConfig.mockImplementation(async (name) =>
|
|
name === rawServerName ? serverConfig : null,
|
|
);
|
|
mockCreateMCPTool.mockResolvedValue({ name: normalizedKey });
|
|
|
|
const result = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [normalizedKey],
|
|
options: {
|
|
req: {
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
body: {},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.loadedTools).toEqual([{ name: normalizedKey }]);
|
|
expect(mockGetServerConfig).toHaveBeenCalledWith(
|
|
rawServerName,
|
|
expect.anything(),
|
|
expect.anything(),
|
|
);
|
|
expect(mockCreateMCPTool).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
toolKey: normalizedKey,
|
|
serverName: rawServerName,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('skips tools of a shadowed server (colliding normalized names) at execution', async () => {
|
|
/** Instances of a shadowed server get the SAME normalized names as the
|
|
* winner's, so in-run dispatch could execute either — legacy raw keys
|
|
* and mcp_all tokens bypass catalog filtering, so execution must also
|
|
* fail closed. */
|
|
const serverConfig = {
|
|
type: 'streamable-http',
|
|
url: 'https://x.example/mcp',
|
|
source: 'yaml',
|
|
};
|
|
const { resolveMcpServerContext } = require('~/server/services/MCP');
|
|
resolveMcpServerContext.mockResolvedValueOnce({
|
|
configServers: {},
|
|
serverNames: ['Sales_Force', 'Sales_Force'],
|
|
rawServerNames: ['Sales Force', 'Sales:Force'],
|
|
});
|
|
mockGetServerConfig.mockResolvedValue(serverConfig);
|
|
mockCreateMCPTool.mockResolvedValue({ name: 'never' });
|
|
|
|
const result = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [`search${Constants.mcp_delimiter}Sales:Force`],
|
|
options: {
|
|
req: {
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
body: {},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.loadedTools).toEqual([]);
|
|
expect(mockCreateMCPTool).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('detects CROSS-TIER collisions via the accessible-server set at execution', async () => {
|
|
/** A user-DB server `foo` shadowing operator `foo!` is invisible to the
|
|
* operator-config names — the guard must consult the full accessible
|
|
* set so the operator server's legacy raw key fails closed instead of
|
|
* joining the run under the same normalized name as the DB server. */
|
|
const serverConfig = {
|
|
type: 'streamable-http',
|
|
url: 'https://x.example/mcp',
|
|
source: 'yaml',
|
|
};
|
|
const { resolveMcpServerContext } = require('~/server/services/MCP');
|
|
resolveMcpServerContext.mockResolvedValueOnce({
|
|
configServers: {},
|
|
serverNames: ['foo'],
|
|
rawServerNames: ['foo!'],
|
|
});
|
|
mockGetAccessibleMcpServerNames.mockResolvedValueOnce(['foo', 'foo!']);
|
|
mockGetServerConfig.mockResolvedValue(serverConfig);
|
|
mockCreateMCPTool.mockResolvedValue({ name: 'never' });
|
|
|
|
const result = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [`search${Constants.mcp_delimiter}foo!`],
|
|
options: {
|
|
req: {
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
body: {},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.loadedTools).toEqual([]);
|
|
expect(mockCreateMCPTool).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('reuses the initialization audit snapshot threaded as bare execution options', async () => {
|
|
/** Deferred execution threads initialization's COMPLETE audit as
|
|
* `options.accessibleMcpServerNames` (no server context is resolved
|
|
* there) — a transient registry failure at execution must not
|
|
* fail-closed a tool the same turn already advertised. */
|
|
const rawServerName = 'Connector: Company';
|
|
const normalizedKey = `search${Constants.mcp_delimiter}Connector__Company`;
|
|
const serverConfig = {
|
|
type: 'streamable-http',
|
|
url: 'https://api.example.com/mcp',
|
|
source: 'yaml',
|
|
};
|
|
const { resolveMcpServerContext } = require('~/server/services/MCP');
|
|
resolveMcpServerContext.mockResolvedValueOnce({
|
|
configServers: { [rawServerName]: serverConfig },
|
|
serverNames: ['Connector__Company'],
|
|
rawServerNames: [rawServerName],
|
|
});
|
|
mockGetAccessibleMcpServerNames.mockImplementation(async () => {
|
|
throw new Error('registry down');
|
|
});
|
|
mockGetServerConfig.mockImplementation(async (name) =>
|
|
name === rawServerName ? serverConfig : null,
|
|
);
|
|
mockCreateMCPTool.mockResolvedValue({ name: normalizedKey });
|
|
|
|
try {
|
|
const result = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [normalizedKey],
|
|
options: {
|
|
accessibleMcpServerNames: [rawServerName],
|
|
req: {
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
body: {},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.loadedTools).toEqual([{ name: normalizedKey }]);
|
|
expect(mockGetAccessibleMcpServerNames).not.toHaveBeenCalled();
|
|
} finally {
|
|
mockGetAccessibleMcpServerNames.mockImplementation(async () => []);
|
|
}
|
|
});
|
|
|
|
it('detects cross-tier collisions from the execution-threaded audit snapshot', async () => {
|
|
const serverConfig = {
|
|
type: 'streamable-http',
|
|
url: 'https://x.example/mcp',
|
|
source: 'yaml',
|
|
};
|
|
const { resolveMcpServerContext } = require('~/server/services/MCP');
|
|
resolveMcpServerContext.mockResolvedValueOnce({
|
|
configServers: {},
|
|
serverNames: ['foo'],
|
|
rawServerNames: ['foo!'],
|
|
});
|
|
mockGetServerConfig.mockResolvedValue(serverConfig);
|
|
mockCreateMCPTool.mockResolvedValue({ name: 'never' });
|
|
|
|
const result = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [`search${Constants.mcp_delimiter}foo!`],
|
|
options: {
|
|
accessibleMcpServerNames: ['foo', 'foo!'],
|
|
req: {
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
body: {},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.loadedTools).toEqual([]);
|
|
expect(mockCreateMCPTool).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('keeps a server resolving under the parsed name as-is (direct identity wins)', async () => {
|
|
/** A user-DB server named exactly like an operator server's normalized
|
|
* form must keep its own identity instead of being rerouted. */
|
|
const dbServerName = 'Connector__Company';
|
|
const toolKey = `search${Constants.mcp_delimiter}${dbServerName}`;
|
|
const serverConfig = {
|
|
type: 'streamable-http',
|
|
url: 'https://db.example.com/mcp',
|
|
source: 'user',
|
|
};
|
|
|
|
const { resolveMcpServerContext } = require('~/server/services/MCP');
|
|
resolveMcpServerContext.mockResolvedValueOnce({
|
|
configServers: {},
|
|
serverNames: ['Connector__Company'],
|
|
rawServerNames: ['Connector: Company'],
|
|
});
|
|
mockGetServerConfig.mockImplementation(async (name) =>
|
|
name === dbServerName ? serverConfig : null,
|
|
);
|
|
mockCreateMCPTool.mockResolvedValue({ name: toolKey });
|
|
|
|
const result = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [toolKey],
|
|
options: {
|
|
req: {
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
body: {},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.loadedTools).toEqual([{ name: toolKey }]);
|
|
expect(mockCreateMCPTool).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
toolKey,
|
|
serverName: dbServerName,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('still resolves legacy raw-keyed tools for a special-character server', async () => {
|
|
const rawServerName = 'Connector: Company';
|
|
const legacyKey = `search${Constants.mcp_delimiter}${rawServerName}`;
|
|
const serverConfig = {
|
|
type: 'streamable-http',
|
|
url: 'https://api.example.com/mcp',
|
|
source: 'yaml',
|
|
};
|
|
|
|
const { resolveMcpServerContext } = require('~/server/services/MCP');
|
|
resolveMcpServerContext.mockResolvedValueOnce({
|
|
configServers: { [rawServerName]: serverConfig },
|
|
serverNames: ['Connector__Company'],
|
|
rawServerNames: [rawServerName],
|
|
});
|
|
mockGetServerConfig.mockResolvedValue(serverConfig);
|
|
mockCreateMCPTool.mockResolvedValue({ name: 'loaded-mcp-tool' });
|
|
|
|
const result = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [legacyKey],
|
|
options: {
|
|
req: {
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
body: {},
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.loadedTools).toEqual([{ name: 'loaded-mcp-tool' }]);
|
|
expect(mockGetServerConfig).toHaveBeenCalledWith(
|
|
rawServerName,
|
|
expect.anything(),
|
|
expect.anything(),
|
|
);
|
|
expect(mockCreateMCPTool).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
toolKey: legacyKey,
|
|
serverName: rawServerName,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('resolves an MCP tool whose raw name itself contains the delimiter substring', async () => {
|
|
// Regression test for https://github.com/danny-avila/LibreChat/issues/14440:
|
|
// gateways that prefix aggregated tool names by server (e.g. LiteLLM's
|
|
// MCP proxy) can produce a raw tool name that already contains "_mcp_"
|
|
// (e.g. GitLab's own "get_mcp_server_version" tool becomes
|
|
// "gitlab-get_mcp_server_version" once gateway-prefixed). Once
|
|
// LibreChat appends its own server suffix, the combined key has the
|
|
// delimiter twice - a naive split used to silently derive the wrong
|
|
// server name ("server_version" instead of "gitlab") and drop the tool.
|
|
const serverName = 'gitlab';
|
|
const rawToolName = 'gitlab-get_mcp_server_version';
|
|
const toolKey = `${rawToolName}${Constants.mcp_delimiter}${serverName}`;
|
|
const serverConfig = {
|
|
type: 'streamable-http',
|
|
url: 'https://litellm.example.com/gitlab/mcp',
|
|
source: 'yaml',
|
|
};
|
|
|
|
mockGetServerConfig.mockResolvedValue(serverConfig);
|
|
mockCreateMCPTool.mockResolvedValue({ name: 'loaded-mcp-tool' });
|
|
|
|
const result = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [toolKey],
|
|
options: {
|
|
req: {
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.loadedTools).toEqual([{ name: 'loaded-mcp-tool' }]);
|
|
expect(mockGetServerConfig).toHaveBeenCalledWith(
|
|
serverName,
|
|
expect.anything(),
|
|
expect.anything(),
|
|
);
|
|
expect(mockCreateMCPTool).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
toolKey,
|
|
config: serverConfig,
|
|
/** The resolved server rides along, so `createMCPTool` uses it for auth,
|
|
* reconnection and invocation instead of re-parsing the ambiguous key. */
|
|
serverName,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('uses run-scoped MCP tool definitions before cache lookup', async () => {
|
|
const serverName = 'body-scoped';
|
|
const toolKey = `search${Constants.mcp_delimiter}${serverName}`;
|
|
const requestBody = { conversationId: 'conv-123', messageId: 'msg-123' };
|
|
const serverConfig = {
|
|
type: 'streamable-http',
|
|
url: 'https://api.example.com/messages/{{LIBRECHAT_BODY_MESSAGEID}}/mcp',
|
|
source: 'yaml',
|
|
};
|
|
const runScopedTools = {
|
|
[toolKey]: {
|
|
function: {
|
|
name: toolKey,
|
|
description: 'Run-scoped search',
|
|
parameters: { type: 'object', properties: {} },
|
|
},
|
|
},
|
|
};
|
|
|
|
mockGetServerConfig.mockResolvedValue(serverConfig);
|
|
mockCreateMCPTool.mockResolvedValue({ name: 'loaded-mcp-tool' });
|
|
|
|
const result = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [toolKey],
|
|
options: {
|
|
mcpAvailableTools: {
|
|
[serverName]: runScopedTools,
|
|
},
|
|
req: {
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
body: requestBody,
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.loadedTools).toEqual([{ name: 'loaded-mcp-tool' }]);
|
|
expect(mockGetMCPServerTools).not.toHaveBeenCalled();
|
|
expect(mockCreateMCPTool).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
availableTools: runScopedTools,
|
|
requestBody,
|
|
toolKey,
|
|
config: serverConfig,
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('reuses discovered request-scoped MCP tool definitions within a server loop', async () => {
|
|
const serverName = 'body-scoped';
|
|
const firstToolKey = `search${Constants.mcp_delimiter}${serverName}`;
|
|
const secondToolKey = `lookup${Constants.mcp_delimiter}${serverName}`;
|
|
const requestBody = { conversationId: 'conv-123', messageId: 'msg-123' };
|
|
const serverConfig = {
|
|
type: 'streamable-http',
|
|
url: 'https://api.example.com/messages/{{LIBRECHAT_BODY_MESSAGEID}}/mcp',
|
|
source: 'yaml',
|
|
};
|
|
const discoveredTools = {
|
|
[firstToolKey]: {
|
|
function: {
|
|
description: 'Search',
|
|
parameters: { type: 'object', properties: {} },
|
|
},
|
|
},
|
|
[secondToolKey]: {
|
|
function: {
|
|
description: 'Lookup',
|
|
parameters: { type: 'object', properties: {} },
|
|
},
|
|
},
|
|
};
|
|
|
|
mockGetServerConfig.mockResolvedValue(serverConfig);
|
|
mockCreateMCPTool
|
|
.mockImplementationOnce(async ({ onAvailableTools }) => {
|
|
onAvailableTools(discoveredTools);
|
|
return { name: 'search-tool' };
|
|
})
|
|
.mockImplementationOnce(async ({ availableTools }) => {
|
|
expect(availableTools).toBe(discoveredTools);
|
|
return { name: 'lookup-tool' };
|
|
});
|
|
|
|
const result = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [firstToolKey, secondToolKey],
|
|
options: {
|
|
req: {
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
body: requestBody,
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.loadedTools).toEqual([{ name: 'search-tool' }, { name: 'lookup-tool' }]);
|
|
expect(mockGetMCPServerTools).toHaveBeenCalledTimes(1);
|
|
expect(mockCreateMCPTool).toHaveBeenCalledTimes(2);
|
|
expect(mockCreateMCPTool).toHaveBeenNthCalledWith(
|
|
2,
|
|
expect.objectContaining({
|
|
availableTools: discoveredTools,
|
|
requestBody,
|
|
toolKey: secondToolKey,
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('web_search SSRF-safe agent wiring', () => {
|
|
const buildReq = () => ({
|
|
user: { id: fakeUser._id.toString(), role: 'USER' },
|
|
body: {},
|
|
});
|
|
|
|
/** Uses the real resolver, so this fails if the wiring delivers agents that do not guard. */
|
|
async function loadWebSearchConfig(webSearch) {
|
|
const toolMap = await loadTools({
|
|
user: fakeUser._id.toString(),
|
|
tools: [Tools.web_search],
|
|
returnMap: true,
|
|
webSearch,
|
|
options: { req: buildReq() },
|
|
});
|
|
await toolMap[Tools.web_search]();
|
|
return mockCreateSearchTool.mock.calls.at(-1)[0];
|
|
}
|
|
|
|
it('threads pooled SSRF-safe agents into the search tool config', async () => {
|
|
const config = await loadWebSearchConfig({ allowedAddresses: ['localhost:8888'] });
|
|
|
|
expect(typeof config.httpAgent.createConnection).toBe('function');
|
|
expect(typeof config.httpsAgent.createConnection).toBe('function');
|
|
expect(config.httpAgent.options.keepAlive).toBe(true);
|
|
});
|
|
|
|
it('threads agents that actually reject a private target', async () => {
|
|
const config = await loadWebSearchConfig({});
|
|
|
|
expect(() =>
|
|
config.httpAgent.createConnection({ host: '169.254.169.254', port: 80 }),
|
|
).toThrow(expect.objectContaining({ code: 'ESSRF' }));
|
|
});
|
|
|
|
it('honors allowedAddresses end to end, exempting the configured host:port only', async () => {
|
|
const config = await loadWebSearchConfig({ allowedAddresses: ['127.0.0.1:8080'] });
|
|
|
|
const socket = config.httpAgent.createConnection({ host: '127.0.0.1', port: 8080 });
|
|
socket?.destroy?.();
|
|
expect(() => config.httpAgent.createConnection({ host: '127.0.0.1', port: 9 })).toThrow(
|
|
expect.objectContaining({ code: 'ESSRF' }),
|
|
);
|
|
});
|
|
|
|
it('does not throw out of loadTools when allowedAddresses is not an array', async () => {
|
|
await expect(
|
|
loadWebSearchConfig({ allowedAddresses: { '10.0.0.5:11434': true } }),
|
|
).resolves.toBeDefined();
|
|
});
|
|
});
|
|
});
|