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
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { describe, expect, it } from 'bun:test';
import { join } from 'node:path';
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { handleSubmitPlan } from './submit-plan.ts';
import type { SessionToolContext } from '../context.ts';

function createCtx(
plansFolderPath: string,
opts: {
exists?: boolean;
readFile?: string;
onRead?: (path: string) => void;
onSubmitted?: (path: string) => void;
} = {}
): SessionToolContext {
return {
sessionId: 'session-123',
workspacePath: join('/tmp', 'workspace'),
get sourcesPath() {
return join(this.workspacePath, 'sources');
},
get skillsPath() {
return join(this.workspacePath, 'skills');
},
plansFolderPath,
callbacks: {
onPlanSubmitted: (path: string) => opts.onSubmitted?.(path),
onAuthRequest: () => {},
},
fs: {
exists: () => opts.exists ?? true,
readFile: (path: string) => {
opts.onRead?.(path);
return opts.readFile ?? '# Plan';
},
readFileBuffer: () => Buffer.from(opts.readFile ?? '# Plan'),
writeFile: () => {},
isDirectory: () => false,
readdir: () => [],
stat: () => ({ size: 0, isDirectory: () => false }),
},
loadSourceConfig: () => null,
};
}

describe('handleSubmitPlan', () => {
const plansFolderPath = join('/tmp', 'workspace', 'sessions', 'session-123', 'plans');

it('submits a plan inside the session plans directory', async () => {
const submitted: string[] = [];
const planPath = join(plansFolderPath, 'plan.md');
const ctx = createCtx(plansFolderPath, {
onSubmitted: (path) => submitted.push(path),
});

const result = await handleSubmitPlan(ctx, { planPath });

expect(result.isError).toBe(false);
expect(submitted).toEqual([planPath]);
});

it('rejects sibling paths that share the plans directory prefix', async () => {
const readAttempts: string[] = [];
const submitted: string[] = [];
const siblingPlanPath = join(`${plansFolderPath}-other`, 'plan.md');
const ctx = createCtx(plansFolderPath, {
onRead: (path) => readAttempts.push(path),
onSubmitted: (path) => submitted.push(path),
});

const result = await handleSubmitPlan(ctx, { planPath: siblingPlanPath });

expect(result.isError).toBe(true);
expect(result.content[0]?.text).toContain('session plans directory');
expect(readAttempts).toEqual([]);
expect(submitted).toEqual([]);
});

it('rejects paths that escape the plans directory through a symlink', async () => {
if (process.platform === 'win32') {
return;
}

const rootDir = mkdtempSync(join(tmpdir(), 'submit-plan-boundary-'));
try {
const realPlansDir = join(rootDir, 'plans');
const outsideDir = join(rootDir, 'outside');
mkdirSync(realPlansDir, { recursive: true });
mkdirSync(outsideDir, { recursive: true });
writeFileSync(join(outsideDir, 'plan.md'), '# outside');
symlinkSync(outsideDir, join(realPlansDir, 'escape-link'), 'dir');

const readAttempts: string[] = [];
const submitted: string[] = [];
const ctx = createCtx(realPlansDir, {
onRead: (path) => readAttempts.push(path),
onSubmitted: (path) => submitted.push(path),
});

const result = await handleSubmitPlan(ctx, {
planPath: join(realPlansDir, 'escape-link', 'plan.md'),
});

expect(result.isError).toBe(true);
expect(result.content[0]?.text).toContain('session plans directory');
expect(readAttempts).toEqual([]);
expect(submitted).toEqual([]);
} finally {
rmSync(rootDir, { recursive: true, force: true });
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { SessionToolContext } from '../context.ts';
import type { ToolResult } from '../types.ts';
import { successResponse, errorResponse } from '../response.ts';
import { isPathWithinDirectory } from '../runtime/path-security.ts';

export interface SubmitPlanArgs {
planPath: string;
Expand All @@ -27,6 +28,12 @@ export async function handleSubmitPlan(
): Promise<ToolResult> {
const { planPath } = args;

if (!isPathWithinDirectory(planPath, ctx.plansFolderPath)) {
return errorResponse(
`Plan file must be inside the session plans directory: ${ctx.plansFolderPath}`
);
}

// Verify the file exists
if (!ctx.fs.exists(planPath)) {
return errorResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it } from 'bun:test';
import { join } from 'node:path';
import {
getSessionPlansDir,
isPathInPlansDir,
} from '../session-scoped-tools.ts';

describe('session-scoped plan path helpers', () => {
const workspacePath = join('/tmp', 'workspace');
const sessionId = 'session-123';

it('allows the plans directory itself', () => {
const plansDir = getSessionPlansDir(workspacePath, sessionId);

expect(isPathInPlansDir(plansDir, workspacePath, sessionId)).toBe(true);
});

it('allows child paths inside the plans directory', () => {
const plansDir = getSessionPlansDir(workspacePath, sessionId);
const planPath = join(plansDir, 'plan.md');

expect(isPathInPlansDir(planPath, workspacePath, sessionId)).toBe(true);
});

it('rejects sibling paths that share the plans directory prefix', () => {
const plansDir = getSessionPlansDir(workspacePath, sessionId);
const siblingPlanPath = join(`${plansDir}-other`, 'plan.md');

expect(isPathInPlansDir(siblingPlanPath, workspacePath, sessionId)).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { getSessionPlansPath, getSessionPath } from '../sessions/storage.ts';
import { DOC_REFS } from '../docs/index.ts';
import { basename } from 'node:path';
import { basename, isAbsolute, relative, sep } from 'node:path';
import { createLocalMcpServer, localTool, type LocalTool } from '../mcp/local-tools.ts';

// Import from session-tools-core: registry + schemas + base descriptions
Expand Down Expand Up @@ -140,7 +140,13 @@ export function getSessionPlansDir(workspacePath: string, sessionId: string): st
*/
export function isPathInPlansDir(path: string, workspacePath: string, sessionId: string): boolean {
const plansDir = getSessionPlansDir(workspacePath, sessionId);
return path.startsWith(plansDir);
const relativePath = relative(plansDir, path);
return (
relativePath === '' ||
(!relativePath.startsWith(`..${sep}`) &&
relativePath !== '..' &&
!isAbsolute(relativePath))
);
}

// ============================================================
Expand Down
Loading