diff --git a/README.md b/README.md index b7a5efa..9f09fb6 100644 --- a/README.md +++ b/README.md @@ -1095,7 +1095,7 @@ The rest of this section documents the variables themselves. Pass them using whi | `QDRANT_PORT` | `16333` | Qdrant REST API port (managed mode, or external without `QDRANT_URL`) | | `QDRANT_GRPC_PORT` | `16334` | Qdrant gRPC port (managed mode only) | | `QDRANT_HOST` | `localhost` | Qdrant hostname (alternative to `QDRANT_URL` for non-HTTPS external instances) | -| `QDRANT_API_KEY` | *(none)* | Qdrant API key (required for Qdrant Cloud and other authenticated deployments) | +| `QDRANT_API_KEY` | *(none)* | Qdrant API key (required for Qdrant Cloud and other authenticated deployments). When set, the URL must be `https://...` so the key is not transmitted over plain HTTP. Loopback URLs (`localhost`, `127.0.0.1`, `[::1]`) are accepted on `http://` for local development. | ### Indexing Behaviour diff --git a/src/services/docker.ts b/src/services/docker.ts index 39a095a..701410c 100644 --- a/src/services/docker.ts +++ b/src/services/docker.ts @@ -201,6 +201,32 @@ async function ensureExternalQdrantReady(onProgress?: InfraProgressCallback): Pr const baseUrl = QDRANT_URL ? QDRANT_URL.replace(/\/$/, "") : `http://${QDRANT_HOST}:${QDRANT_PORT}`; const healthUrl = `${baseUrl}/healthz`; + // If an api-key is configured, refuse to send it over a non-TLS connection. + // Localhost loopback URLs are accepted because some users run authenticated + // Qdrant locally during development. The URL is parsed (rather than checked + // with startsWith) so that hostnames like "http://localhost.evil.com" are + // not mistaken for loopback. Placed before the try/catch so the specific + // error is not masked by the generic "Cannot reach" message below. + if (QDRANT_API_KEY) { + let parsed: URL | null = null; + try { + parsed = new URL(baseUrl); + } catch { + /* fall through: the unparseable URL will fail the reachability check */ + } + const isHttps = parsed?.protocol === "https:"; + const isLoopback = parsed + ? ["localhost", "127.0.0.1", "[::1]"].includes(parsed.hostname) + : false; + if (parsed && !isHttps && !isLoopback) { + throw new Error( + `QDRANT_API_KEY is set but ${baseUrl} is not HTTPS. ` + + "Refusing to send the API key over a non-TLS connection. " + + "Use https://... for remote or cloud Qdrant (a localhost URL is also accepted for local development).", + ); + } + } + onProgress?.(`Checking external Qdrant at ${baseUrl}...`); logger.info("Checking external Qdrant", { url: baseUrl }); diff --git a/tests/unit/docker.test.ts b/tests/unit/docker.test.ts index ca29060..c4b264c 100644 --- a/tests/unit/docker.test.ts +++ b/tests/unit/docker.test.ts @@ -426,7 +426,9 @@ describe("ensureQdrantReady external mode", () => { const docker = await loadDockerWithExternalMode({ QDRANT_URL: "http://local-qdrant:6333", QDRANT_HOST: "local-qdrant", - // QDRANT_API_KEY intentionally undefined (matches local self-hosted Qdrant) + // Explicitly undefined so the test does not pick up a real + // QDRANT_API_KEY from the developer's shell environment. + QDRANT_API_KEY: undefined, }); const fetchSpy = vi @@ -442,4 +444,49 @@ describe("ensureQdrantReady external mode", () => { fetchSpy.mockRestore(); }); + + it("rejects QDRANT_API_KEY over plain HTTP for non-loopback hosts", async () => { + const docker = await loadDockerWithExternalMode({ + QDRANT_URL: "http://remote-qdrant.example:6333", + QDRANT_HOST: "remote-qdrant.example", + QDRANT_API_KEY: "secret-key-xyz", + }); + + const fetchSpy = vi + .spyOn(globalThis, "fetch") + .mockResolvedValue({ ok: true } as Response); + + await expect(docker.ensureQdrantReady()).rejects.toThrow( + /QDRANT_API_KEY is set but .* is not HTTPS/, + ); + // The guard runs before the readiness probe, so fetch is never called. + expect(fetchSpy).not.toHaveBeenCalled(); + + fetchSpy.mockRestore(); + }); + + it("allows QDRANT_API_KEY over plain HTTP when host is localhost (local dev)", async () => { + const docker = await loadDockerWithExternalMode({ + QDRANT_URL: "http://localhost:6333", + QDRANT_HOST: "localhost", + QDRANT_API_KEY: "secret-key-xyz", + }); + + const fetchSpy = vi + .spyOn(globalThis, "fetch") + .mockResolvedValue({ ok: true } as Response); + + await expect(docker.ensureQdrantReady()).resolves.toEqual({ + started: false, + pulled: false, + }); + expect(fetchSpy).toHaveBeenCalledWith( + "http://localhost:6333/healthz", + expect.objectContaining({ + headers: expect.objectContaining({ "api-key": "secret-key-xyz" }), + }), + ); + + fetchSpy.mockRestore(); + }); });