fix(docker): require HTTPS for QDRANT_API_KEY; deflake no-key test

Address CodeRabbit findings on PR #36:

1. The previous patch attached `QDRANT_API_KEY` as an `api-key` header
   regardless of URL scheme, which would leak the secret on the wire if
   a user configured an authenticated Qdrant over plain HTTP. Add a
   guard that rejects the combination and throws a specific error,
   placed before the readiness probe so its message is not masked by
   the generic "Cannot reach external Qdrant" handler. Loopback URLs
   (`localhost`, `127.0.0.1`, `[::1]`) are accepted on `http://` so
   local-dev workflows where users run authenticated Qdrant on plain
   HTTP keep working. The URL is parsed (rather than checked with
   startsWith) so hostnames like `http://localhost.evil.com` are not
   mistaken for loopback.

2. The "omits api-key header when QDRANT_API_KEY is not set" test
   relied on the spread of the real `constants.js` module, which means
   it would flake to a header-attached state on any developer machine
   with `QDRANT_API_KEY` exported in the shell. Pass
   `QDRANT_API_KEY: undefined` explicitly so the override always wins.

3. Add two tests covering the new guard: one asserting the rejection
   on plain HTTP for non-loopback hosts, and one asserting that the
   localhost exception still attaches the api-key header.

4. Document the HTTPS requirement (and localhost exception) in the
   `QDRANT_API_KEY` row of the README configuration table.
This commit is contained in:
Giancarlo Erra
2026-04-28 13:58:00 +01:00
parent 812fcd8905
commit 7cdf21a961
3 changed files with 75 additions and 2 deletions
+1 -1
View File
@@ -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
+26
View File
@@ -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 });
+48 -1
View File
@@ -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();
});
});