|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import assert from 'node:assert'; |
| 8 | +import os from 'node:os'; |
| 9 | +import path from 'node:path'; |
| 10 | +import {describe, it} from 'node:test'; |
| 11 | +import {pathToFileURL} from 'node:url'; |
| 12 | + |
| 13 | +import {withMcpContext} from './utils.js'; |
| 14 | + |
| 15 | +describe('McpContext Roots', () => { |
| 16 | + it('should allow access to os.tmpdir() even if roots are empty', async () => { |
| 17 | + await withMcpContext(async (_response, context) => { |
| 18 | + context.setRoots([]); |
| 19 | + const tmpPath = path.join(os.tmpdir(), 'test-file.txt'); |
| 20 | + // This should not throw |
| 21 | + context.validatePath(tmpPath); |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should allow access to os.tmpdir() when other roots are set', async () => { |
| 26 | + await withMcpContext(async (_response, context) => { |
| 27 | + const otherRoot = path.resolve( |
| 28 | + os.tmpdir(), |
| 29 | + '..', |
| 30 | + 'other_workspace_root_for_test', |
| 31 | + ); |
| 32 | + context.setRoots([{uri: pathToFileURL(otherRoot).href, name: 'other'}]); |
| 33 | + |
| 34 | + const tmpPath = path.join(os.tmpdir(), 'test-file.txt'); |
| 35 | + // This should not throw |
| 36 | + context.validatePath(tmpPath); |
| 37 | + |
| 38 | + // Other root should also be allowed |
| 39 | + context.validatePath(path.join(otherRoot, 'file.txt')); |
| 40 | + |
| 41 | + // Outside should still be denied. Use a path that is definitely not a root or temp dir. |
| 42 | + // We use a sibling directory to the temp dir or home dir. |
| 43 | + const outsidePath = path.resolve( |
| 44 | + os.homedir(), |
| 45 | + 'a_very_unlikely_path_name_12345', |
| 46 | + ); |
| 47 | + assert.throws(() => context.validatePath(outsidePath), /Access denied/); |
| 48 | + }); |
| 49 | + }); |
| 50 | +}); |
0 commit comments