fix(agentbbs): a .. in a room label can no longer move the room log outside basePath (#4518)

Co-authored-by: r <r@x>
This commit is contained in:
rUvandr authored and GitHub committed 2026-10-26 14:06:02 -04:00
1 parent fc323f5108
commit 3671915b8d
3 files changed
+26 -4

No files matched your search

@@ -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 ..');
}
});
});
@@ -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;
}
/**
@@ -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 {