|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 10 | +import { tmpdir } from 'node:os'; |
| 11 | +import { join } from 'node:path'; |
| 12 | +import { LocalWorkspaceHost, createRootRestrictedHost } from './host'; |
| 13 | + |
| 14 | +describe('createRootRestrictedHost', () => { |
| 15 | + let root1: string; |
| 16 | + let root2: string; |
| 17 | + let outsideDir: string; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + root1 = mkdtempSync(join(tmpdir(), 'angular-cli-mcp-root1-')); |
| 21 | + root2 = mkdtempSync(join(tmpdir(), 'angular-cli-mcp-root2-')); |
| 22 | + outsideDir = mkdtempSync(join(tmpdir(), 'angular-cli-mcp-outside-')); |
| 23 | + |
| 24 | + writeFileSync(join(root1, 'file1.txt'), 'root 1 content'); |
| 25 | + writeFileSync(join(root2, 'file2.txt'), 'root 2 content'); |
| 26 | + writeFileSync(join(outsideDir, 'outside.txt'), 'outside content'); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + rmSync(root1, { recursive: true, force: true }); |
| 31 | + rmSync(root2, { recursive: true, force: true }); |
| 32 | + rmSync(outsideDir, { recursive: true, force: true }); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should allow file access inside any of the configured initial roots', () => { |
| 36 | + const host = createRootRestrictedHost(LocalWorkspaceHost, [root1, root2]); |
| 37 | + |
| 38 | + expect(host.existsSync(join(root1, 'file1.txt'))).toBeTrue(); |
| 39 | + expect(host.existsSync(join(root2, 'file2.txt'))).toBeTrue(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should reject file access outside of the configured roots', () => { |
| 43 | + const host = createRootRestrictedHost(LocalWorkspaceHost, [root1, root2]); |
| 44 | + |
| 45 | + expect(() => host.existsSync(join(outsideDir, 'outside.txt'))).toThrowError( |
| 46 | + new RegExp( |
| 47 | + `Access denied: path '${join(outsideDir, 'outside.txt')}' is outside allowed roots.`, |
| 48 | + ), |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should fall back to initial roots when setRoots is called with an empty array', () => { |
| 53 | + const host = createRootRestrictedHost(LocalWorkspaceHost, [root1, root2]); |
| 54 | + |
| 55 | + host.setRoots([]); |
| 56 | + |
| 57 | + expect(host.existsSync(join(root1, 'file1.txt'))).toBeTrue(); |
| 58 | + expect(host.existsSync(join(root2, 'file2.txt'))).toBeTrue(); |
| 59 | + }); |
| 60 | +}); |
0 commit comments