|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + isSensitiveUrlQueryParamName, |
| 4 | + isSensitiveUrlConfigPath, |
| 5 | + SENSITIVE_URL_HINT_TAG, |
| 6 | + hasSensitiveUrlHintTag, |
| 7 | + redactSensitiveUrl, |
| 8 | + redactSensitiveUrlLikeString, |
| 9 | +} from "./redact-sensitive-url.js"; |
| 10 | + |
| 11 | +describe("redactSensitiveUrl", () => { |
| 12 | + it("redacts userinfo and sensitive query params from valid URLs", () => { |
| 13 | + expect(redactSensitiveUrl("https://user:pass@example.com/mcp?token=secret&safe=value")).toBe( |
| 14 | + "https://***:***@example.com/mcp?token=***&safe=value", |
| 15 | + ); |
| 16 | + }); |
| 17 | + |
| 18 | + it("treats query param names case-insensitively", () => { |
| 19 | + expect(redactSensitiveUrl("https://example.com/mcp?Access_Token=secret")).toBe( |
| 20 | + "https://example.com/mcp?Access_Token=***", |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it("keeps non-sensitive URLs unchanged", () => { |
| 25 | + expect(redactSensitiveUrl("https://example.com/mcp?safe=value")).toBe( |
| 26 | + "https://example.com/mcp?safe=value", |
| 27 | + ); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("redactSensitiveUrlLikeString", () => { |
| 32 | + it("redacts invalid URL-like strings", () => { |
| 33 | + expect(redactSensitiveUrlLikeString("//user:pass@example.com/mcp?client_secret=secret")).toBe( |
| 34 | + "//***:***@example.com/mcp?client_secret=***", |
| 35 | + ); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("isSensitiveUrlQueryParamName", () => { |
| 40 | + it("matches the auth-oriented query params used by MCP SSE config redaction", () => { |
| 41 | + expect(isSensitiveUrlQueryParamName("token")).toBe(true); |
| 42 | + expect(isSensitiveUrlQueryParamName("refresh_token")).toBe(true); |
| 43 | + expect(isSensitiveUrlQueryParamName("safe")).toBe(false); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe("sensitive URL config metadata", () => { |
| 48 | + it("recognizes config paths that may embed URL secrets", () => { |
| 49 | + expect(isSensitiveUrlConfigPath("models.providers.*.baseUrl")).toBe(true); |
| 50 | + expect(isSensitiveUrlConfigPath("mcp.servers.remote.url")).toBe(true); |
| 51 | + expect(isSensitiveUrlConfigPath("gateway.remote.url")).toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it("recognizes cdpUrl config paths as sensitive (browser CDP URLs can embed credentials)", () => { |
| 55 | + expect(isSensitiveUrlConfigPath("browser.cdpUrl")).toBe(true); |
| 56 | + expect(isSensitiveUrlConfigPath("browser.profiles.remote.cdpUrl")).toBe(true); |
| 57 | + expect(isSensitiveUrlConfigPath("browser.profiles.staging.cdpUrl")).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it("uses an explicit url-secret hint tag", () => { |
| 61 | + expect(SENSITIVE_URL_HINT_TAG).toBe("url-secret"); |
| 62 | + expect(hasSensitiveUrlHintTag({ tags: [SENSITIVE_URL_HINT_TAG] })).toBe(true); |
| 63 | + expect(hasSensitiveUrlHintTag({ tags: ["security"] })).toBe(false); |
| 64 | + }); |
| 65 | +}); |
0 commit comments