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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 57 additions & 2 deletions src/adapters/tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,22 +104,77 @@ describe('validatePath realpath containment', () => {
}
});

it('allows warehouse reads while keeping writes outside the project root denied', async () => {
it('validatePath itself accepts warehouse only when allowWarehouseRead is true', async () => {
vi.stubEnv('OPENSWARM_WAREHOUSE_ROOT', WAREHOUSE_DIR);
const file = path.join(WAREHOUSE_DIR, 'direct-validate.env');
await fs.writeFile(file, 'KEY_NAME=redacted\n');
try {
const allowed = validatePath(file, TMP_DIR, { allowWarehouseRead: true });
expect(allowed).toBe(await fs.realpath(file));
expect(() => validatePath(file, TMP_DIR)).toThrow(/outside the project root/);
expect(() => validatePath(file, TMP_DIR, { allowWarehouseRead: false })).toThrow(/outside the project root/);
} finally {
vi.unstubAllEnvs();
}
});

it('allows warehouse reads when OPENSWARM_WAREHOUSE_ROOT is set', async () => {
vi.stubEnv('OPENSWARM_WAREHOUSE_ROOT', WAREHOUSE_DIR);
const file = path.join(WAREHOUSE_DIR, 'vega-agent.env');
await fs.writeFile(file, 'KEY_NAME=redacted\n');
try {
const read = await executeTool(makeCall('read_file', { path: file }), TMP_DIR);
const write = await executeTool(makeCall('write_file', { path: file, content: 'changed' }), TMP_DIR);
expect(read).toMatchObject({ is_error: false });
expect(read.content).toContain('KEY_NAME=redacted');
} finally {
vi.unstubAllEnvs();
}
});

it('refuses warehouse writes even when OPENSWARM_WAREHOUSE_ROOT is set', async () => {
vi.stubEnv('OPENSWARM_WAREHOUSE_ROOT', WAREHOUSE_DIR);
const file = path.join(WAREHOUSE_DIR, 'vega-agent-write.env');
await fs.writeFile(file, 'KEY_NAME=redacted\n');
try {
const write = await executeTool(makeCall('write_file', { path: file, content: 'changed' }), TMP_DIR);
expect(write).toMatchObject({ is_error: true });
expect(write.content).toContain('outside the project root');
await expect(fs.readFile(file, 'utf8')).resolves.toBe('KEY_NAME=redacted\n');
} finally {
vi.unstubAllEnvs();
}
});

it('still refuses unrelated outside paths when warehouse is configured', async () => {
vi.stubEnv('OPENSWARM_WAREHOUSE_ROOT', WAREHOUSE_DIR);
try {
const result = await executeTool(
makeCall('read_file', { path: '/etc/passwd' }),
TMP_DIR,
);
expect(result).toMatchObject({ is_error: true });
expect(result.content).toContain('outside the project root');
} finally {
vi.unstubAllEnvs();
}
});

it('still allows /tmp reads when warehouse is configured', async () => {
vi.stubEnv('OPENSWARM_WAREHOUSE_ROOT', WAREHOUSE_DIR);
const filePath = path.join(TMP_DIR, 'warehouse-tmp-unchanged.txt');
await fs.writeFile(filePath, 'tmp-ok', 'utf-8');
try {
const result = await executeTool(
makeCall('read_file', { path: filePath }),
path.join(TMP_DIR, 'absent-project-cwd'),
);
expect(result).toMatchObject({ is_error: false });
expect(result.content).toContain('tmp-ok');
} finally {
vi.unstubAllEnvs();
}
});

it('allows a read-only file tool to follow a worktree symlink into the warehouse', async () => {
vi.stubEnv('OPENSWARM_WAREHOUSE_ROOT', WAREHOUSE_DIR);
const target = path.join(WAREHOUSE_DIR, 'INDEX.md');
Expand Down
9 changes: 8 additions & 1 deletion src/support/warehouseRoutes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { IncomingMessage, ServerResponse } from 'node:http';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
import fs from 'node:fs/promises';
import path from 'node:path';
import { tryHandleWarehouseRoutes } from './warehouseRoutes.js';
import { resolveWarehouseReadPath, tryHandleWarehouseRoutes } from './warehouseRoutes.js';

const ROOT = await fs.mkdtemp('/var/tmp/openswarm-warehouse-routes-');
const OUTSIDE = await fs.mkdtemp('/var/tmp/openswarm-warehouse-outside-');
Expand Down Expand Up @@ -59,6 +59,13 @@ afterAll(async () => {
});

describe('warehouse HTTP routes', () => {
it('returns one canonical path used for both authorization and I/O', async () => {
const expected = await fs.realpath(path.join(ROOT, 'INDEX.md'));
const authorized = resolveWarehouseReadPath('INDEX.md');
expect(authorized).toBe(expected);
await expect(fs.readFile(authorized, 'utf8')).resolves.toBe('# Warehouse\n');
});

it('lists dotfiles with size and mtime for browser exploration', async () => {
const response = await call('GET', '/api/warehouse/tree?path=vega-agent/env');
expect(response).toMatchObject({ handled: true, status: 200 });
Expand Down
Loading