diff --git a/v3/@claude-flow/cli/__tests__/agentbbs-tools.test.ts b/v3/@claude-flow/cli/__tests__/agentbbs-tools.test.ts index bb563e1309..9ce551112a 100644 --- a/v3/@claude-flow/cli/__tests__/agentbbs-tools.test.ts +++ b/v3/@claude-flow/cli/__tests__/agentbbs-tools.test.ts @@ -277,3 +277,14 @@ describe('agentbbs — dedup + seq optimizations (ADR-164, PeerHello spam fix)', expect(lastPeerHelloMs(p)).toBe(Date.parse(t2)); }); }); + +describe('room labels cannot climb out of the BBS directory (#4416 follow-up)', () => { + it('refuses a label containing ..', async () => { + const { agentbbsTools } = await import('../src/mcp-tools/agentbbs-tools.js'); + const register = agentbbsTools.find((t) => /register/.test(t.name))!; + for (const roomLabel of ['..', '../x', '#a/../../b']) { + const result = await register.handler({ roomLabel } as never).catch((e: Error) => ({ error: e.message })); + expect(JSON.stringify(result), roomLabel).toContain('must not contain ..'); + } + }); +}); diff --git a/v3/@claude-flow/cli/src/mcp-tools/agentbbs-federation.ts b/v3/@claude-flow/cli/src/mcp-tools/agentbbs-federation.ts index a591d76ed3..74e92b84a9 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/agentbbs-federation.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/agentbbs-federation.ts @@ -52,7 +52,7 @@ */ import { existsSync, mkdirSync, readFileSync, writeFileSync, appendFileSync, chmodSync } from 'node:fs'; -import { join } from 'node:path'; +import { isAbsolute, join, relative, resolve } from 'node:path'; import { createHash, randomBytes, timingSafeEqual } from 'node:crypto'; import { createRequire } from 'node:module'; import type { Server, IncomingMessage, ServerResponse } from 'node:http'; @@ -170,7 +170,12 @@ function peersPath(basePath: string): string { } function roomLogPath(basePath: string, roomId: string): string { - return join(basePath, `room-${roomId}.jsonl`); + // The room id reaches a file name: validate it, then make sure the result stays under basePath. + validateRoomId(roomId); + const file = join(basePath, `room-${roomId}.jsonl`); + const rel = relative(resolve(basePath), resolve(file)); + if (rel.startsWith('..') || isAbsolute(rel)) throw new Error('Invalid room id'); + return file; } /** diff --git a/v3/@claude-flow/cli/src/mcp-tools/agentbbs-tools.ts b/v3/@claude-flow/cli/src/mcp-tools/agentbbs-tools.ts index 7f37157853..3608c97d33 100644 --- a/v3/@claude-flow/cli/src/mcp-tools/agentbbs-tools.ts +++ b/v3/@claude-flow/cli/src/mcp-tools/agentbbs-tools.ts @@ -36,7 +36,7 @@ */ import { existsSync, mkdirSync, readFileSync, writeFileSync, appendFileSync, statSync, openSync, readSync, closeSync } from 'node:fs'; -import { resolve, isAbsolute, join } from 'node:path'; +import { resolve, isAbsolute, join, relative } from 'node:path'; import { randomBytes, createHash } from 'node:crypto'; import { execFileSync } from 'node:child_process'; import type { MCPTool } from './types.js'; @@ -102,6 +102,8 @@ function validateRoomLabel(label: string): string { if (!/^[A-Za-z0-9_.\-:/@#]+$/.test(label)) { throw new Error('roomLabel may only contain [A-Za-z0-9_.\\-:/@#]'); } + // The label becomes part of the room id, and the room id part of a file name. + if (label.includes('..')) throw new Error('roomLabel must not contain ..'); return label; } @@ -135,7 +137,11 @@ function roomIdFromLabel(label: string): string { } function roomLogPath(basePath: string, roomId: string): string { - return join(basePath, `room-${roomId}.jsonl`); + validateRoomId(roomId); + const file = join(basePath, `room-${roomId}.jsonl`); + const rel = relative(resolve(basePath), resolve(file)); + if (rel.startsWith('..') || isAbsolute(rel)) throw new Error('roomId escapes the BBS directory'); + return file; } function roomsRegistryPath(basePath: string): string {