|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. |
| 3 | + * All Rights Reserved. SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { expect } from 'chai' |
| 7 | +import * as fs from 'fs' |
| 8 | +import * as os from 'os' |
| 9 | +import * as path from 'path' |
| 10 | +import { fingerprintServerConfig, fingerprintWorkspace, hasApproval, recordApproval } from './mcpConsentStore' |
| 11 | +import type { MCPServerConfig } from './mcpTypes' |
| 12 | + |
| 13 | +describe('mcpConsentStore', () => { |
| 14 | + let tmpHome: string |
| 15 | + let workspace: any |
| 16 | + let logger: any |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'mcpConsentTest-')) |
| 20 | + workspace = { |
| 21 | + fs: { |
| 22 | + exists: (p: string) => Promise.resolve(fs.existsSync(p)), |
| 23 | + readFile: (p: string) => Promise.resolve(Buffer.from(fs.readFileSync(p))), |
| 24 | + writeFile: (p: string, d: string) => Promise.resolve(fs.writeFileSync(p, d)), |
| 25 | + mkdir: (p: string, _opts: any) => Promise.resolve(fs.mkdirSync(p, { recursive: true })), |
| 26 | + getUserHomeDir: () => tmpHome, |
| 27 | + }, |
| 28 | + } |
| 29 | + logger = { warn: () => {}, info: () => {}, error: () => {} } |
| 30 | + }) |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + fs.rmSync(tmpHome, { recursive: true, force: true }) |
| 34 | + }) |
| 35 | + |
| 36 | + describe('fingerprintServerConfig', () => { |
| 37 | + it('is deterministic for identical config', () => { |
| 38 | + const cfg: MCPServerConfig = { command: 'sh', args: ['-c', 'echo hi'] } |
| 39 | + expect(fingerprintServerConfig(cfg)).to.equal(fingerprintServerConfig({ ...cfg })) |
| 40 | + }) |
| 41 | + |
| 42 | + it('differs when command changes', () => { |
| 43 | + const a: MCPServerConfig = { command: 'sh', args: ['-c', 'echo hi'] } |
| 44 | + const b: MCPServerConfig = { command: 'bash', args: ['-c', 'echo hi'] } |
| 45 | + expect(fingerprintServerConfig(a)).to.not.equal(fingerprintServerConfig(b)) |
| 46 | + }) |
| 47 | + |
| 48 | + it('differs when args change', () => { |
| 49 | + const a: MCPServerConfig = { command: 'sh', args: ['-c', 'echo hi'] } |
| 50 | + const b: MCPServerConfig = { command: 'sh', args: ['-c', 'echo bye'] } |
| 51 | + expect(fingerprintServerConfig(a)).to.not.equal(fingerprintServerConfig(b)) |
| 52 | + }) |
| 53 | + |
| 54 | + it('differs when env changes', () => { |
| 55 | + const a: MCPServerConfig = { command: 'sh', args: [], env: { FOO: '1' } } |
| 56 | + const b: MCPServerConfig = { command: 'sh', args: [], env: { FOO: '2' } } |
| 57 | + expect(fingerprintServerConfig(a)).to.not.equal(fingerprintServerConfig(b)) |
| 58 | + }) |
| 59 | + |
| 60 | + it('is stable regardless of env key order', () => { |
| 61 | + const a: MCPServerConfig = { command: 'sh', args: [], env: { A: '1', B: '2' } } |
| 62 | + const b: MCPServerConfig = { command: 'sh', args: [], env: { B: '2', A: '1' } } |
| 63 | + expect(fingerprintServerConfig(a)).to.equal(fingerprintServerConfig(b)) |
| 64 | + }) |
| 65 | + |
| 66 | + it('differs when url changes', () => { |
| 67 | + const a: MCPServerConfig = { url: 'https://a.example' } |
| 68 | + const b: MCPServerConfig = { url: 'https://b.example' } |
| 69 | + expect(fingerprintServerConfig(a)).to.not.equal(fingerprintServerConfig(b)) |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + describe('fingerprintWorkspace', () => { |
| 74 | + it('is keyed on the directory of the config, not the filename', () => { |
| 75 | + const a = fingerprintWorkspace('/foo/bar/.amazonq/mcp.json') |
| 76 | + const b = fingerprintWorkspace('/foo/bar/.amazonq/agents/default.json') |
| 77 | + // both live under /foo/bar/.amazonq's parent-dir once; path.dirname differs though |
| 78 | + expect(a).to.not.equal(b) |
| 79 | + }) |
| 80 | + |
| 81 | + it('is deterministic for the same path', () => { |
| 82 | + const p = '/foo/bar/.amazonq/mcp.json' |
| 83 | + expect(fingerprintWorkspace(p)).to.equal(fingerprintWorkspace(p)) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + describe('hasApproval / recordApproval', () => { |
| 88 | + const cfg: MCPServerConfig = { command: 'sh', args: ['-c', 'echo ok'] } |
| 89 | + const configPath = '/tmp/ws-a/.amazonq/mcp.json' |
| 90 | + |
| 91 | + it('returns false when store is empty', async () => { |
| 92 | + expect(await hasApproval(workspace, logger, 'poc', cfg, configPath)).to.be.false |
| 93 | + }) |
| 94 | + |
| 95 | + it('records and finds an approval for same (name, config, workspace)', async () => { |
| 96 | + await recordApproval(workspace, logger, 'poc', cfg, configPath) |
| 97 | + expect(await hasApproval(workspace, logger, 'poc', cfg, configPath)).to.be.true |
| 98 | + }) |
| 99 | + |
| 100 | + it('does not match when workspace path differs', async () => { |
| 101 | + await recordApproval(workspace, logger, 'poc', cfg, '/tmp/ws-a/.amazonq/mcp.json') |
| 102 | + expect(await hasApproval(workspace, logger, 'poc', cfg, '/tmp/ws-b/.amazonq/mcp.json')).to.be.false |
| 103 | + }) |
| 104 | + |
| 105 | + it('does not match when command changes (fingerprint invalidates)', async () => { |
| 106 | + await recordApproval(workspace, logger, 'poc', cfg, configPath) |
| 107 | + const mutated: MCPServerConfig = { command: 'sh', args: ['-c', 'curl evil'] } |
| 108 | + expect(await hasApproval(workspace, logger, 'poc', mutated, configPath)).to.be.false |
| 109 | + }) |
| 110 | + |
| 111 | + it('does not match when server name differs', async () => { |
| 112 | + await recordApproval(workspace, logger, 'poc', cfg, configPath) |
| 113 | + expect(await hasApproval(workspace, logger, 'other', cfg, configPath)).to.be.false |
| 114 | + }) |
| 115 | + |
| 116 | + it('dedupes repeated approvals for the same key', async () => { |
| 117 | + await recordApproval(workspace, logger, 'poc', cfg, configPath) |
| 118 | + await recordApproval(workspace, logger, 'poc', cfg, configPath) |
| 119 | + const stored = JSON.parse( |
| 120 | + fs.readFileSync(path.join(tmpHome, '.aws', 'amazonq', 'mcp-approvals.json')).toString() |
| 121 | + ) |
| 122 | + expect(stored.approvals).to.have.lengthOf(1) |
| 123 | + }) |
| 124 | + |
| 125 | + it('evicts stale entry when config changes for same server and workspace', async () => { |
| 126 | + await recordApproval(workspace, logger, 'poc', cfg, configPath) |
| 127 | + const mutated: MCPServerConfig = { command: 'sh', args: ['-c', 'echo changed'] } |
| 128 | + await recordApproval(workspace, logger, 'poc', mutated, configPath) |
| 129 | + const stored = JSON.parse( |
| 130 | + fs.readFileSync(path.join(tmpHome, '.aws', 'amazonq', 'mcp-approvals.json')).toString() |
| 131 | + ) |
| 132 | + // Should have exactly 1 entry — the old fingerprint was evicted |
| 133 | + expect(stored.approvals).to.have.lengthOf(1) |
| 134 | + expect(stored.approvals[0].fingerprint).to.equal(fingerprintServerConfig(mutated)) |
| 135 | + }) |
| 136 | + |
| 137 | + it('ignores a store with unrecognized version', async () => { |
| 138 | + const storeDir = path.join(tmpHome, '.aws', 'amazonq') |
| 139 | + fs.mkdirSync(storeDir, { recursive: true }) |
| 140 | + fs.writeFileSync(path.join(storeDir, 'mcp-approvals.json'), JSON.stringify({ version: 999, approvals: [] })) |
| 141 | + // record should still work (overwrites with v1) |
| 142 | + await recordApproval(workspace, logger, 'poc', cfg, configPath) |
| 143 | + expect(await hasApproval(workspace, logger, 'poc', cfg, configPath)).to.be.true |
| 144 | + }) |
| 145 | + |
| 146 | + it('treats a malformed store as empty', async () => { |
| 147 | + const storeDir = path.join(tmpHome, '.aws', 'amazonq') |
| 148 | + fs.mkdirSync(storeDir, { recursive: true }) |
| 149 | + fs.writeFileSync(path.join(storeDir, 'mcp-approvals.json'), 'not json') |
| 150 | + expect(await hasApproval(workspace, logger, 'poc', cfg, configPath)).to.be.false |
| 151 | + }) |
| 152 | + }) |
| 153 | +}) |
0 commit comments