项目文件夹

文件
Trung Huynh Chi a9228141ae fix: validate extension-enforced output paths (#2269)
Fixes output path validation so tools cannot validate one path and then
write to a different canonical target after extension enforcement.

Changes:
- Resolve dangling symlinks to their target path during
canonicalization.
- Validate the final extension-enforced output path before writing.
- Apply the same final-path validation to heap snapshots and
screencasts.
- Add regression coverage for dangling symlinks that point outside
configured roots.

Validation:
- npm run format
- npm run check-format
- npm run test tests/utils/files.test.ts
- npm run test tests/roots.test.ts
- npm run test tests/tools/memory.test.ts tests/tools/screencast.test.ts

Note: I also ran the full npm test suite locally. The targeted tests
above passed, but the full suite hit local WSL daemon/e2e startup
timeouts while waiting for daemon.pid / server_start, which appear
unrelated to this path-validation change.

---------

Co-authored-by: huynhtrungcsc <huynhtrungcsc@users.noreply.github.com>
2026-07-03 09:18:59 +00:00

140 行
4.0 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 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});
}
});
});
});