项目文件夹

文件
herdiyanitdev 6e56c028cf feat: support --allow-unrestricted-paths configuration (#2296)
## Summary

validatePath() in McpContext returned immediately, with no restriction
at all, whenever roots() returned undefined. roots() only returns
undefined when the connecting MCP client never negotiates the optional
roots capability during initialize, which any minimal client can trigger
simply by omitting it from its declared capabilities.

Since roots() already always appends the OS temp directory to whatever
explicit roots are configured, this change makes it return that same
default (temp directory only) instead of undefined when no roots have
been set. This removes the early return in validatePath() entirely, so
path validation now runs unconditionally rather than being conditional
on whether the connecting client happened to negotiate a capability it
was never required to declare per the MCP spec.


Any filePath-accepting tool (take_screenshot, saveFile, and the
performance/Lighthouse export tools that route through the same check)
had its only path-traversal guard silently disabled for the lifetime of
a connection whenever the client omitted the optional roots capability.
Since this server is designed to let an LLM drive a browser, and browsed
page content is not trusted input, this meant a client that simply
doesn't implement roots (a plausible, non-adversarial default for
lightweight or custom MCP clients) removed the only boundary preventing
the connected agent from writing to any path the process can reach.


Added a test that exercises the actual default state of roots (never
calling setRoots()) directly, since the existing tests always call
setRoots(), even with an empty array, before validating. Verified
locally with a minimal MCP client that declares no capabilities: before
this change, take_screenshot with a filePath outside any root wrote a
real file to an arbitrary path with no error; after this change, the
same call is rejected with the existing Access denied error. Also
verified that a client that does declare roots is unaffected, and that
writes to the OS temp directory continue to succeed with no roots
negotiated, matching prior behavior for that path.
2026-07-08 16:49:31 +00:00

157 行
4.8 KiB
TypeScript

/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import {describe, it} from 'node:test';
import {pathToFileURL} from 'node:url';
import {withMcpContext} from './utils.js';
describe('McpContext Roots', () => {
it('should allow access to os.tmpdir() even if roots are empty', async () => {
await withMcpContext(async (_response, context) => {
context.setRoots([]);
const tmpPath = path.join(os.tmpdir(), 'test-file.txt');
// This should not throw
await context.validatePath(tmpPath);
});
});
it('should deny paths outside the temp directory when the client never negotiates roots', async () => {
await withMcpContext(async (_response, context) => {
// setRoots() is intentionally never called here, matching a client
// that omits the optional MCP `roots` capability during initialize.
const outsidePath = path.resolve(
os.homedir(),
'a_very_unlikely_path_name_never_negotiated_roots',
);
await assert.rejects(context.validatePath(outsidePath), /Access denied/);
const tmpPath = path.join(os.tmpdir(), 'test-file.txt');
// The temp directory must remain reachable even with no negotiated
// roots, matching the existing "empty roots" behavior above.
await context.validatePath(tmpPath);
});
});
it('should allow access to os.tmpdir() when other roots are set', async () => {
await withMcpContext(async (_response, context) => {
const otherRoot = path.resolve(
os.tmpdir(),
'other_workspace_root_for_test',
);
await fs.mkdir(otherRoot, {recursive: true});
try {
context.setRoots([{uri: pathToFileURL(otherRoot).href, name: 'other'}]);
const tmpPath = path.join(os.tmpdir(), 'test-file.txt');
// This should not throw.
await context.validatePath(tmpPath);
// Other root should also be allowed.
await context.validatePath(path.join(otherRoot, 'file.txt'));
// Outside should still be denied. Use a path that is definitely not a root or temp dir.
const outsidePath = path.resolve(
os.homedir(),
'a_very_unlikely_path_name_12345',
);
await assert.rejects(
context.validatePath(outsidePath),
/Access denied/,
);
} finally {
await fs.rm(otherRoot, {recursive: true, force: true});
}
});
});
it('should enforce extensions and validate the output path', async () => {
await withMcpContext(async (_response, context) => {
const workspacePath = await fs.mkdtemp(
path.join(os.tmpdir(), 'workspace-root-'),
);
try {
context.setRoots([
{uri: pathToFileURL(workspacePath).href, name: 'workspace'},
]);
const testCases: Array<{
filePath: string;
extension: '.json' | '.txt' | '.png' | '.zip';
expected: string;
}> = [
{
filePath: 'result',
extension: '.json',
expected: 'result.json',
},
{
filePath: 'result.jpg',
extension: '.txt',
expected: 'result.txt',
},
{
filePath: 'nested/result.jpg',
extension: '.png',
expected: 'nested/result.png',
},
{
filePath: '.bashrc',
extension: '.txt',
expected: '.bashrc.txt',
},
{
filePath: 'file.tar.gz',
extension: '.zip',
expected: 'file.tar.zip',
},
];
for (const testCase of testCases) {
const resolvedPath = await context.ensureExtension(
path.join(workspacePath, testCase.filePath),
testCase.extension,
);
assert.strictEqual(
resolvedPath,
path.join(workspacePath, testCase.expected),
);
}
} finally {
await fs.rm(workspacePath, {recursive: true, force: true});
}
});
});
it('should deny extension-enforced paths outside roots', async () => {
await withMcpContext(async (_response, context) => {
const workspacePath = await fs.mkdtemp(
path.join(os.tmpdir(), 'workspace-root-'),
);
try {
context.setRoots([
{uri: pathToFileURL(workspacePath).href, name: 'workspace'},
]);
await assert.rejects(
context.ensureExtension(
path.join(os.homedir(), 'outside-root-result'),
'.json',
),
/Access denied/,
);
} finally {
await fs.rm(workspacePath, {recursive: true, force: true});
}
});
});
});