|
| 1 | +#!/usr/bin/env tsx |
| 2 | + |
| 3 | +import { strict as assert } from "node:assert"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 6 | +import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; |
| 7 | +import { LoggingMessageNotificationSchema } from "@modelcontextprotocol/sdk/types.js"; |
| 8 | +import { |
| 9 | + createTestResults, |
| 10 | + printTestSummary, |
| 11 | + testFunction, |
| 12 | +} from "../helpers/test-utils.js"; |
| 13 | + |
| 14 | +const results = createTestResults(); |
| 15 | + |
| 16 | +async function connectCli( |
| 17 | + searxngUrl: string, |
| 18 | + extraEnv: Record<string, string> = {}, |
| 19 | +) { |
| 20 | + const logs: unknown[] = []; |
| 21 | + let stderr = ""; |
| 22 | + const transport = new StdioClientTransport({ |
| 23 | + command: process.execPath, |
| 24 | + args: ["--import", "tsx", "src/cli.ts"], |
| 25 | + cwd: process.cwd(), |
| 26 | + env: { |
| 27 | + ...process.env, |
| 28 | + SEARXNG_URL: searxngUrl, |
| 29 | + ...extraEnv, |
| 30 | + } as Record<string, string>, |
| 31 | + stderr: "pipe", |
| 32 | + }); |
| 33 | + transport.stderr?.on("data", (chunk) => { |
| 34 | + stderr += String(chunk); |
| 35 | + }); |
| 36 | + const client = new Client( |
| 37 | + { name: "diagnostic-security-test", version: "1.0.0" }, |
| 38 | + { capabilities: {} }, |
| 39 | + ); |
| 40 | + client.setNotificationHandler(LoggingMessageNotificationSchema, (notification) => { |
| 41 | + logs.push(notification); |
| 42 | + }); |
| 43 | + await client.connect(transport); |
| 44 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 45 | + return { client, logs, getStderr: () => stderr }; |
| 46 | +} |
| 47 | + |
| 48 | +async function runTests() { |
| 49 | + console.log("Integration Testing: credential-safe diagnostics\n"); |
| 50 | + |
| 51 | + await testFunction("real CLI startup logging removes URL Basic Auth userinfo", async () => { |
| 52 | + const markerUrl = "https://cli-user:cli-secret@search.example.com/path"; |
| 53 | + const { client, logs, getStderr } = await connectCli(markerUrl); |
| 54 | + await client.close(); |
| 55 | + |
| 56 | + const output = `${JSON.stringify(logs)}\n${getStderr()}`; |
| 57 | + assert.ok(!output.includes("cli-user"), output); |
| 58 | + assert.ok(!output.includes("cli-secret"), output); |
| 59 | + assert.ok(output.includes("https://search.example.com/path"), output); |
| 60 | + }, results); |
| 61 | + |
| 62 | + await testFunction("real CLI JSON-RPC errors remove invalid URL credentials", async () => { |
| 63 | + const markerUrl = "ftp://rpc-user:rpc-secret@search.example.com/path"; |
| 64 | + const { client, logs, getStderr } = await connectCli(markerUrl); |
| 65 | + let errorText = ""; |
| 66 | + try { |
| 67 | + await client.callTool({ |
| 68 | + name: "searxng_web_search", |
| 69 | + arguments: { query: "test" }, |
| 70 | + }); |
| 71 | + assert.fail("Expected invalid protocol error"); |
| 72 | + } catch (error) { |
| 73 | + errorText = error instanceof Error ? `${error.message}\n${error.stack}` : String(error); |
| 74 | + } |
| 75 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 76 | + await client.close(); |
| 77 | + |
| 78 | + const output = `${errorText}\n${JSON.stringify(logs)}\n${getStderr()}`; |
| 79 | + assert.ok(!output.includes("rpc-user"), output); |
| 80 | + assert.ok(!output.includes("rpc-secret"), output); |
| 81 | + assert.ok(output.includes("ftp:"), output); |
| 82 | + assert.ok(output.includes("search.example.com"), output); |
| 83 | + }, results); |
| 84 | + |
| 85 | + await testFunction("outbound network failures never echo Basic Auth material", async () => { |
| 86 | + const markerUrl = "http://network-user:network-secret@127.0.0.1:1"; |
| 87 | + const { client, logs, getStderr } = await connectCli(markerUrl, { |
| 88 | + FETCH_TIMEOUT_MS: "250", |
| 89 | + }); |
| 90 | + let errorText = ""; |
| 91 | + try { |
| 92 | + await client.callTool({ |
| 93 | + name: "searxng_web_search", |
| 94 | + arguments: { query: "test" }, |
| 95 | + }); |
| 96 | + assert.fail("Expected network failure"); |
| 97 | + } catch (error) { |
| 98 | + errorText = error instanceof Error ? `${error.message}\n${error.stack}` : String(error); |
| 99 | + } |
| 100 | + await new Promise((resolve) => setTimeout(resolve, 20)); |
| 101 | + await client.close(); |
| 102 | + |
| 103 | + const output = `${errorText}\n${JSON.stringify(logs)}\n${getStderr()}`; |
| 104 | + assert.ok(!output.includes("network-user"), output); |
| 105 | + assert.ok(!output.includes("network-secret"), output); |
| 106 | + assert.ok( |
| 107 | + output.includes("Connection") || output.includes("Network"), |
| 108 | + output, |
| 109 | + ); |
| 110 | + }, results); |
| 111 | + |
| 112 | + printTestSummary(results, "Credential-Safe Diagnostics"); |
| 113 | + return results; |
| 114 | +} |
| 115 | + |
| 116 | +if ( |
| 117 | + process.argv[1] !== undefined |
| 118 | + && fileURLToPath(import.meta.url) === process.argv[1] |
| 119 | +) { |
| 120 | + runTests().then((testResults) => { |
| 121 | + process.exit(testResults.failed > 0 ? 1 : 0); |
| 122 | + }).catch(console.error); |
| 123 | +} |
| 124 | + |
| 125 | +export { runTests }; |
0 commit comments