|
| 1 | +import { describe, it } from 'node:test'; |
| 2 | +import assert from 'node:assert'; |
| 3 | +import { isToolAllowed } from '../src/mcpl/tool-policy.js'; |
| 4 | + |
| 5 | +describe('isToolAllowed', () => { |
| 6 | + it('allows everything when no policy is set', () => { |
| 7 | + assert.strictEqual(isToolAllowed('upload_file', undefined), true); |
| 8 | + assert.strictEqual(isToolAllowed('upload_file', {}), true); |
| 9 | + }); |
| 10 | + |
| 11 | + it('allow-list: only listed tools pass', () => { |
| 12 | + const policy = { enabledTools: ['list_files', 'read_file'] }; |
| 13 | + assert.strictEqual(isToolAllowed('list_files', policy), true); |
| 14 | + assert.strictEqual(isToolAllowed('read_file', policy), true); |
| 15 | + assert.strictEqual(isToolAllowed('upload_file', policy), false); |
| 16 | + }); |
| 17 | + |
| 18 | + it('deny-list: listed tools blocked, rest allowed', () => { |
| 19 | + const policy = { disabledTools: ['delete_file', 'rename'] }; |
| 20 | + assert.strictEqual(isToolAllowed('delete_file', policy), false); |
| 21 | + assert.strictEqual(isToolAllowed('rename', policy), false); |
| 22 | + assert.strictEqual(isToolAllowed('read_file', policy), true); |
| 23 | + }); |
| 24 | + |
| 25 | + it('deny wins over allow on overlap', () => { |
| 26 | + const policy = { enabledTools: ['*'], disabledTools: ['upload_*'] }; |
| 27 | + assert.strictEqual(isToolAllowed('upload_file', policy), false); |
| 28 | + assert.strictEqual(isToolAllowed('read_file', policy), true); |
| 29 | + }); |
| 30 | + |
| 31 | + it('wildcards: prefix, suffix, and bare *', () => { |
| 32 | + assert.strictEqual(isToolAllowed('read_file', { enabledTools: ['read_*'] }), true); |
| 33 | + assert.strictEqual(isToolAllowed('write_file', { enabledTools: ['read_*'] }), false); |
| 34 | + assert.strictEqual(isToolAllowed('read_file', { enabledTools: ['*_file'] }), true); |
| 35 | + assert.strictEqual(isToolAllowed('read_dir', { enabledTools: ['*_file'] }), false); |
| 36 | + assert.strictEqual(isToolAllowed('anything_at_all', { enabledTools: ['*'] }), true); |
| 37 | + }); |
| 38 | + |
| 39 | + it('literal patterns require exact match (not substring)', () => { |
| 40 | + const policy = { enabledTools: ['read'] }; |
| 41 | + assert.strictEqual(isToolAllowed('read', policy), true); |
| 42 | + assert.strictEqual(isToolAllowed('read_file', policy), false); |
| 43 | + }); |
| 44 | + |
| 45 | + it('regex metacharacters in patterns are treated literally', () => { |
| 46 | + const policy = { enabledTools: ['get.file', 'foo+bar', 'baz(qux)'] }; |
| 47 | + assert.strictEqual(isToolAllowed('get.file', policy), true); |
| 48 | + assert.strictEqual(isToolAllowed('getXfile', policy), false, '. is not a regex wildcard'); |
| 49 | + assert.strictEqual(isToolAllowed('foo+bar', policy), true); |
| 50 | + assert.strictEqual(isToolAllowed('foobar', policy), false, '+ is not a regex quantifier'); |
| 51 | + assert.strictEqual(isToolAllowed('baz(qux)', policy), true); |
| 52 | + }); |
| 53 | + |
| 54 | + it('empty arrays behave like absent fields', () => { |
| 55 | + assert.strictEqual(isToolAllowed('anything', { enabledTools: [] }), true); |
| 56 | + assert.strictEqual(isToolAllowed('anything', { disabledTools: [] }), true); |
| 57 | + }); |
| 58 | +}); |
0 commit comments