mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-18 05:52:10 +00:00
🛻 feat: Route Bash Through Attached Workspaces (#15546)
* feat(code): Route bash through attached workspaces * fix(code): harden attached bash schema adapter
This commit is contained in:
parent
2656f44c03
commit
fddf7e31f2
9 changed files with 485 additions and 18 deletions
95
packages/api/src/code/command.spec.ts
Normal file
95
packages/api/src/code/command.spec.ts
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
import type { CodeBridgeFetch } from './bridge';
|
||||
import { createAttachedWorkspaceBashTool } from './command';
|
||||
|
||||
function commandResponse(overrides: Record<string, unknown> = {}): Response {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
protocolVersion: 1,
|
||||
operation: 'execute_command',
|
||||
workspaceId: 'primary',
|
||||
exitCode: 0,
|
||||
stdout: 'ready\n',
|
||||
stderr: '',
|
||||
truncated: false,
|
||||
timedOut: false,
|
||||
...overrides,
|
||||
}),
|
||||
{ status: 200, headers: { 'Content-Type': 'application/json' } },
|
||||
);
|
||||
}
|
||||
|
||||
describe('createAttachedWorkspaceBashTool', () => {
|
||||
test('executes in the attached default workspace with per-user bridge authentication', async () => {
|
||||
const fetchImpl: CodeBridgeFetch = jest.fn(async () => commandResponse());
|
||||
const authHeaders = jest.fn().mockResolvedValue({
|
||||
Authorization: 'Bearer jwt',
|
||||
'X-LibreChat-Code-Worker-ID': 'user-worker',
|
||||
});
|
||||
const bashTool = createAttachedWorkspaceBashTool({
|
||||
baseUrl: 'https://code.example.com/v1/',
|
||||
authHeaders,
|
||||
fetchImpl,
|
||||
});
|
||||
|
||||
await expect(bashTool.func({ command: 'pwd' }, undefined, {})).resolves.toEqual([
|
||||
'stdout:\nready\n\n[exit code: 0]',
|
||||
{},
|
||||
]);
|
||||
|
||||
expect(authHeaders).toHaveBeenCalledTimes(1);
|
||||
expect(fetchImpl).toHaveBeenCalledWith(
|
||||
'https://code.example.com/v1/workspace-tools/execute',
|
||||
expect.objectContaining({
|
||||
headers: expect.objectContaining({
|
||||
Authorization: 'Bearer jwt',
|
||||
'X-LibreChat-Code-Worker-ID': 'user-worker',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
const request = JSON.parse(String((fetchImpl as jest.Mock).mock.calls[0][1]?.body));
|
||||
expect(request).toEqual({
|
||||
protocolVersion: 1,
|
||||
operation: 'execute_command',
|
||||
workspaceId: 'primary',
|
||||
command: 'pwd',
|
||||
maxOutputBytes: 256 * 1024,
|
||||
});
|
||||
});
|
||||
|
||||
test('preserves legacy positional args without interpolating shell metacharacters', async () => {
|
||||
const fetchImpl: CodeBridgeFetch = jest.fn(async () => commandResponse());
|
||||
const bashTool = createAttachedWorkspaceBashTool({
|
||||
baseUrl: 'https://code.example.com/v1',
|
||||
authHeaders: () => ({}),
|
||||
fetchImpl,
|
||||
});
|
||||
|
||||
await bashTool.func({ command: 'printf "%s" "$1"', args: ["a'b; echo unsafe"] }, undefined, {});
|
||||
|
||||
const request = JSON.parse(String((fetchImpl as jest.Mock).mock.calls[0][1]?.body));
|
||||
expect(request.command).toBe(`bash -c 'printf "%s" "$1"' -- 'a'"'"'b; echo unsafe'`);
|
||||
});
|
||||
|
||||
test('reports termination, timeouts, and truncation without hiding stderr', async () => {
|
||||
const fetchImpl: CodeBridgeFetch = jest.fn(async () =>
|
||||
commandResponse({
|
||||
exitCode: null,
|
||||
signal: 'SIGKILL',
|
||||
stdout: '',
|
||||
stderr: 'deadline reached',
|
||||
truncated: true,
|
||||
timedOut: true,
|
||||
}),
|
||||
);
|
||||
const bashTool = createAttachedWorkspaceBashTool({
|
||||
baseUrl: 'https://code.example.com/v1',
|
||||
authHeaders: () => ({}),
|
||||
fetchImpl,
|
||||
});
|
||||
|
||||
await expect(bashTool.func({ command: 'sleep 60' }, undefined, {})).resolves.toEqual([
|
||||
'stderr:\ndeadline reached\n[terminated by SIGKILL][timed out][output truncated]',
|
||||
{},
|
||||
]);
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue