|
| 1 | +import { spawn } from 'node:child_process'; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import process from 'node:process'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 8 | +import type { ServerDefinition } from '../src/config.js'; |
| 9 | +import { loadVaultEntry } from '../src/oauth-vault.js'; |
| 10 | + |
| 11 | +const CLI_ENTRY = fileURLToPath(new URL('../dist/cli.js', import.meta.url)); |
| 12 | +const SERVER_URL = 'https://example.test/mcp'; |
| 13 | +const definition: ServerDefinition = { |
| 14 | + name: 'demo', |
| 15 | + command: { kind: 'http', url: new URL(SERVER_URL) }, |
| 16 | + auth: 'oauth', |
| 17 | +}; |
| 18 | + |
| 19 | +interface CliResult { |
| 20 | + exitCode: number | null; |
| 21 | + stdout: string; |
| 22 | + stderr: string; |
| 23 | +} |
| 24 | + |
| 25 | +describe('built vault CLI input boundary', () => { |
| 26 | + const originalDataHome = process.env.XDG_DATA_HOME; |
| 27 | + let configPath: string; |
| 28 | + let dataHome: string; |
| 29 | + let tempDir: string; |
| 30 | + |
| 31 | + beforeEach(async () => { |
| 32 | + tempDir = await fs.mkdtemp(path.join(os.tmpdir(), 'mcporter-vault-cli-')); |
| 33 | + dataHome = path.join(tempDir, 'data'); |
| 34 | + configPath = path.join(tempDir, 'mcporter.json'); |
| 35 | + process.env.XDG_DATA_HOME = dataHome; |
| 36 | + await fs.writeFile( |
| 37 | + configPath, |
| 38 | + JSON.stringify({ imports: [], mcpServers: { demo: { baseUrl: SERVER_URL, auth: 'oauth' } } }), |
| 39 | + 'utf8' |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(async () => { |
| 44 | + if (originalDataHome === undefined) delete process.env.XDG_DATA_HOME; |
| 45 | + else process.env.XDG_DATA_HOME = originalDataHome; |
| 46 | + await fs.rm(tempDir, { recursive: true, force: true }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('does not echo malformed credential input through parser diagnostics', async () => { |
| 50 | + const marker = 'VAULT_SECRET_MARKER_7H3K9'; |
| 51 | + const result = await runVault(['--stdin'], `${marker} malformed`); |
| 52 | + const output = `${result.stdout}\n${result.stderr}`; |
| 53 | + |
| 54 | + expect(result.exitCode).not.toBe(0); |
| 55 | + expect(output).toContain('Vault payload from stdin is not valid JSON.'); |
| 56 | + expect(output).not.toContain(marker); |
| 57 | + expect(output).not.toContain('VAULT_SECR'); |
| 58 | + expect(output).not.toContain('SyntaxError'); |
| 59 | + expect(output).not.toContain('Unexpected token'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('names a malformed credential file without echoing its contents', async () => { |
| 63 | + const marker = 'VAULT_FILE_SECRET_MARKER_2Q8M4'; |
| 64 | + const payloadPath = path.join(tempDir, 'tokens.json'); |
| 65 | + await fs.writeFile(payloadPath, `${marker} malformed`, 'utf8'); |
| 66 | + const result = await runVault(['--tokens-file', payloadPath]); |
| 67 | + const output = `${result.stdout}\n${result.stderr}`; |
| 68 | + |
| 69 | + expect(result.exitCode).not.toBe(0); |
| 70 | + expect(output).toContain(`Vault payload file '${payloadPath}' is not valid JSON.`); |
| 71 | + expect(output).not.toContain(marker); |
| 72 | + expect(output).not.toContain('VAULT_FILE_SECR'); |
| 73 | + expect(output).not.toContain('SyntaxError'); |
| 74 | + }); |
| 75 | + |
| 76 | + it.each([ |
| 77 | + ['expires_at string', JSON.stringify(payload({ expires_at: 'soon' })), 'tokens.expires_at'], |
| 78 | + ['expiresAt string', JSON.stringify(payload({ expiresAt: 'soon' })), 'tokens.expiresAt'], |
| 79 | + ['expires_at positive infinity', rawPayload('expires_at', '1e999'), 'tokens.expires_at'], |
| 80 | + ['expiresAt negative infinity', rawPayload('expiresAt', '-1e999'), 'tokens.expiresAt'], |
| 81 | + ])('rejects invalid %s', async (_name, input, field) => { |
| 82 | + const result = await runVault(['--stdin'], input); |
| 83 | + expect(result.exitCode).not.toBe(0); |
| 84 | + expect(`${result.stdout}\n${result.stderr}`).toContain(`${field} must be a finite number`); |
| 85 | + }); |
| 86 | + |
| 87 | + it('rejects a JSON NaN literal at the sanitized parse boundary', async () => { |
| 88 | + const result = await runVault(['--stdin'], rawPayload('expires_at', 'NaN')); |
| 89 | + expect(result.exitCode).not.toBe(0); |
| 90 | + expect(`${result.stdout}\n${result.stderr}`).toContain('Vault payload from stdin is not valid JSON.'); |
| 91 | + }); |
| 92 | + |
| 93 | + it.each([ |
| 94 | + ['expires_at', 0], |
| 95 | + ['expiresAt', 1_754_600_000.5], |
| 96 | + ] as const)('persists finite %s values and unrelated valid credential fields', async (field, value) => { |
| 97 | + const input = { |
| 98 | + tokens: { |
| 99 | + access_token: 'fake-access-token', |
| 100 | + token_type: 'Bearer', |
| 101 | + refresh_token: 'fake-refresh-token', |
| 102 | + scope: 'read write', |
| 103 | + issuer: 'https://issuer.example', |
| 104 | + expires_in: 3600.25, |
| 105 | + [field]: value, |
| 106 | + id_token: 'fake-id-token', |
| 107 | + }, |
| 108 | + clientInfo: { |
| 109 | + client_id: 'fake-client', |
| 110 | + redirect_uris: ['https://example.test/callback'], |
| 111 | + grant_types: ['authorization_code', 'refresh_token'], |
| 112 | + response_types: ['code'], |
| 113 | + client_name: null, |
| 114 | + provider_metadata: { tenant: 'demo' }, |
| 115 | + }, |
| 116 | + }; |
| 117 | + |
| 118 | + const result = await runVault(['--stdin'], JSON.stringify(input)); |
| 119 | + expect(result.exitCode, result.stderr).toBe(0); |
| 120 | + await expect(loadVaultEntry(definition)).resolves.toMatchObject(input); |
| 121 | + }); |
| 122 | + |
| 123 | + async function runVault(sourceArgs: string[], input?: string): Promise<CliResult> { |
| 124 | + const home = path.join(tempDir, 'home'); |
| 125 | + await fs.mkdir(home, { recursive: true }); |
| 126 | + return new Promise<CliResult>((resolve, reject) => { |
| 127 | + const child = spawn( |
| 128 | + process.execPath, |
| 129 | + [CLI_ENTRY, '--config', configPath, 'vault', 'set', 'demo', ...sourceArgs], |
| 130 | + { |
| 131 | + cwd: tempDir, |
| 132 | + env: { |
| 133 | + ...process.env, |
| 134 | + HOME: home, |
| 135 | + XDG_CONFIG_HOME: path.join(home, 'config'), |
| 136 | + XDG_DATA_HOME: dataHome, |
| 137 | + XDG_CACHE_HOME: path.join(home, 'cache'), |
| 138 | + XDG_STATE_HOME: path.join(home, 'state'), |
| 139 | + MCPORTER_NO_FORCE_EXIT: '1', |
| 140 | + }, |
| 141 | + stdio: ['pipe', 'pipe', 'pipe'], |
| 142 | + } |
| 143 | + ); |
| 144 | + let stdout = ''; |
| 145 | + let stderr = ''; |
| 146 | + child.stdout.setEncoding('utf8'); |
| 147 | + child.stderr.setEncoding('utf8'); |
| 148 | + child.stdout.on('data', (chunk: string) => (stdout += chunk)); |
| 149 | + child.stderr.on('data', (chunk: string) => (stderr += chunk)); |
| 150 | + child.once('error', reject); |
| 151 | + child.once('exit', (exitCode) => resolve({ exitCode, stdout, stderr })); |
| 152 | + child.stdin.end(input); |
| 153 | + }); |
| 154 | + } |
| 155 | +}); |
| 156 | + |
| 157 | +function payload(expiry: Record<string, unknown>): Record<string, unknown> { |
| 158 | + return { tokens: { access_token: 'fake-access-token', token_type: 'Bearer', ...expiry } }; |
| 159 | +} |
| 160 | + |
| 161 | +function rawPayload(field: string, value: string): string { |
| 162 | + return `{"tokens":{"access_token":"fake-access-token","token_type":"Bearer","${field}":${value}}}`; |
| 163 | +} |
0 commit comments