From a9766b20ab771f245e126631b8421cddb924372d Mon Sep 17 00:00:00 2001 From: Yimo Jiang Date: Mon, 6 Jul 2026 04:33:38 +0000 Subject: [PATCH 01/41] fix(inference): honor compatible-endpoint max_model_len for Hermes context (#6177) Compatible-endpoint / custom onboarding never probed the endpoint's runtime context window, and the Hermes image/config carried no context setting, so NemotronH models on a self-hosted vLLM endpoint fell back to a ~4K architecture default and Hermes errored with "Context length exceeded". - Probe the configured OpenAI-compatible endpoint's /v1/models for max_model_len and set NEMOCLAW_CONTEXT_WINDOW when the user has not set one; an explicit override always wins and is never downgraded. - Use strict model matching so a shared multi-model gateway never bakes an unrelated model's window; local vLLM keeps its single-model fallback. - Clear an auto-detected value when re-selecting away from the endpoint so it is never mistaken for a user override by dockerfile-patch. - Declare ARG/ENV NEMOCLAW_CONTEXT_WINDOW in the Hermes Dockerfile and write the resolved window as model.context_length in the generated config so it overrides Hermes' built-in metadata (Hermes ignores context_window). Signed-off-by: Yimo Jiang --- agents/hermes/Dockerfile | 7 + agents/hermes/config/build-env.ts | 13 ++ agents/hermes/config/hermes-config.ts | 7 + docs/inference/switch-inference-providers.mdx | 1 + .../compatible-endpoint-context.test.ts | 162 +++++++++++++++++ .../inference/compatible-endpoint-context.ts | 166 ++++++++++++++++++ .../inference/vllm-runtime-context.test.ts | 50 +++++- src/lib/inference/vllm-runtime-context.ts | 37 +++- .../dockerfile-patch-hermes-context.test.ts | 86 +++++++++ .../machine/handlers/provider-inference.ts | 5 + src/lib/onboard/setup-nim-selection.ts | 9 + .../compatible-endpoint-context-probe.test.ts | 58 ++++++ test/e2e/fixtures/fake-openai-compatible.ts | 3 + test/e2e/lib/fake-openai-compatible-api.mts | 10 +- test/generate-hermes-config.test.ts | 23 +++ test/hermes-gateway-wrapper.test.ts | 2 + 16 files changed, 633 insertions(+), 6 deletions(-) create mode 100644 src/lib/inference/compatible-endpoint-context.test.ts create mode 100644 src/lib/inference/compatible-endpoint-context.ts create mode 100644 src/lib/onboard/dockerfile-patch-hermes-context.test.ts create mode 100644 test/compatible-endpoint-context-probe.test.ts diff --git a/agents/hermes/Dockerfile b/agents/hermes/Dockerfile index f804e4dd1d1..1c0edf3d94c 100644 --- a/agents/hermes/Dockerfile +++ b/agents/hermes/Dockerfile @@ -233,6 +233,12 @@ ARG NEMOCLAW_HERMES_TOOL_GATEWAY_BROKER=0 ARG NEMOCLAW_HERMES_TOOL_GATEWAY_PRESETS_B64=W10= ARG NEMOCLAW_BUILD_ID=default ARG NEMOCLAW_DARWIN_VM_COMPAT=0 +# Total model context window (input + output tokens). Empty by default so +# Hermes auto-detects from the endpoint's /v1/models max_model_len; onboard +# rewrites this ARG (via dockerfile-patch) when it probes a runtime value or +# the user sets NEMOCLAW_CONTEXT_WINDOW, so Hermes' NemotronH metadata default +# cannot override the real window (#6177). +ARG NEMOCLAW_CONTEXT_WINDOW= # Promote build-args to env vars for the config generation script. ENV NEMOCLAW_MODEL=${NEMOCLAW_MODEL} \ @@ -240,6 +246,7 @@ ENV NEMOCLAW_MODEL=${NEMOCLAW_MODEL} \ NEMOCLAW_UPSTREAM_PROVIDER=${NEMOCLAW_UPSTREAM_PROVIDER} \ NEMOCLAW_INFERENCE_BASE_URL=${NEMOCLAW_INFERENCE_BASE_URL} \ NEMOCLAW_INFERENCE_API=${NEMOCLAW_INFERENCE_API} \ + NEMOCLAW_CONTEXT_WINDOW=${NEMOCLAW_CONTEXT_WINDOW} \ NEMOCLAW_TOOL_DISCLOSURE=${NEMOCLAW_TOOL_DISCLOSURE} \ CHAT_UI_URL=${CHAT_UI_URL} \ NEMOCLAW_MESSAGING_PLAN_B64=${NEMOCLAW_MESSAGING_PLAN_B64} \ diff --git a/agents/hermes/config/build-env.ts b/agents/hermes/config/build-env.ts index 088a0012548..0128be351c7 100644 --- a/agents/hermes/config/build-env.ts +++ b/agents/hermes/config/build-env.ts @@ -14,6 +14,8 @@ export type HermesBuildSettings = { providerKey: string; upstreamProvider: string; inferenceApi: string; + /** Total context window (tokens); null lets Hermes auto-detect from /v1/models. */ + contextWindow: number | null; toolDisclosure: "progressive" | "direct"; webSearchProvider: HermesWebSearchProvider | null; messagingCredentialPlaceholders: Array<{ @@ -36,6 +38,7 @@ export function readHermesBuildSettings(env: NodeJS.ProcessEnv): HermesBuildSett providerKey: env.NEMOCLAW_PROVIDER_KEY || "custom", upstreamProvider: env.NEMOCLAW_UPSTREAM_PROVIDER || env.NEMOCLAW_PROVIDER_KEY || "custom", inferenceApi: env.NEMOCLAW_INFERENCE_API || "", + contextWindow: readContextWindow(env), toolDisclosure: readToolDisclosureEnv(env), webSearchProvider: readWebSearchProvider(env), messagingCredentialPlaceholders: readMessagingCredentialPlaceholders(env), @@ -46,6 +49,16 @@ export function readHermesBuildSettings(env: NodeJS.ProcessEnv): HermesBuildSett }; } +// Parse NEMOCLAW_CONTEXT_WINDOW as a positive integer of tokens. Empty, absent, +// or malformed values return null so the generated config omits context_length +// and Hermes keeps auto-detecting from the endpoint's /v1/models. See #6177. +function readContextWindow(env: NodeJS.ProcessEnv): number | null { + const raw = (env.NEMOCLAW_CONTEXT_WINDOW || "").trim(); + if (!/^[1-9][0-9]*$/.test(raw)) return null; + const parsed = Number(raw); + return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : null; +} + function readWebSearchProvider(env: NodeJS.ProcessEnv): HermesWebSearchProvider | null { if (env.NEMOCLAW_WEB_SEARCH_ENABLED !== "1") return null; diff --git a/agents/hermes/config/hermes-config.ts b/agents/hermes/config/hermes-config.ts index f5c12a90073..ae37dd7d467 100644 --- a/agents/hermes/config/hermes-config.ts +++ b/agents/hermes/config/hermes-config.ts @@ -55,6 +55,13 @@ export function buildHermesConfig( }; const apiMode = hermesApiMode(settings.inferenceApi); if (apiMode) modelConfig.api_mode = apiMode; + // context_length on the model block is Hermes' highest-priority context + // override — above live /v1/models discovery and its built-in model-metadata + // registry. Setting it stops NemotronH-family models from falling back to a + // small architecture default when the endpoint actually serves a larger + // max_model_len (#6177). Omit it (null) to let Hermes auto-detect. Hermes + // reads only `context_length`; `context_window` is silently ignored. + if (settings.contextWindow !== null) modelConfig.context_length = settings.contextWindow; // Surface the managed endpoint to Hermes' model picker. The inline `model:` // block above is enough for the gateway to ROUTE inference, but the picker diff --git a/docs/inference/switch-inference-providers.mdx b/docs/inference/switch-inference-providers.mdx index 4019fcb7313..d968826f4d7 100644 --- a/docs/inference/switch-inference-providers.mdx +++ b/docs/inference/switch-inference-providers.mdx @@ -188,6 +188,7 @@ To change these values, set the corresponding environment variables before runni NemoClaw ignores invalid values and bakes the default into the image. For Local Ollama, onboarding loads the selected model first and uses Ollama's reported runtime context length when `NEMOCLAW_CONTEXT_WINDOW` is unset. For local vLLM, onboarding uses the runtime `max_model_len` value when the server reports one and `NEMOCLAW_CONTEXT_WINDOW` is unset. +For an OpenAI-compatible endpoint (the **Other OpenAI-compatible endpoint** provider, including a self-hosted vLLM server), onboarding probes the endpoint's `/v1/models` response and uses its reported `max_model_len` when `NEMOCLAW_CONTEXT_WINDOW` is unset, so the agent gets the endpoint's real context window instead of a small architecture default. Set `NEMOCLAW_CONTEXT_WINDOW` to override the probed value. For Hermes, the resolved window is written as `model.context_length` in the generated `config.yaml`; leaving it unset lets Hermes auto-detect. Use `NEMOCLAW_INFERENCE_INPUTS=text,image` only for a model that accepts image input through the selected provider. During interactive onboarding, NemoClaw prompts for **Text only** or **Text + Image** when the discovered model name looks multimodal and `NEMOCLAW_INFERENCE_INPUTS` is not already valid. Non-interactive onboarding uses the environment value or the default `text` setting. diff --git a/src/lib/inference/compatible-endpoint-context.test.ts b/src/lib/inference/compatible-endpoint-context.test.ts new file mode 100644 index 00000000000..000b45f590a --- /dev/null +++ b/src/lib/inference/compatible-endpoint-context.test.ts @@ -0,0 +1,162 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { beforeEach, describe, expect, it, vi } from "vitest"; + +import { + applyCompatibleEndpointContextWindow, + clearAutoDetectedCompatibleContextWindow, + resetCompatibleEndpointContextWindowAutoState, +} from "./compatible-endpoint-context"; + +beforeEach(() => { + resetCompatibleEndpointContextWindowAutoState(); +}); + +function apply( + options: Parameters[2], + env: NodeJS.ProcessEnv = {}, +): { env: NodeJS.ProcessEnv; messages: string[] } { + const messages: string[] = []; + applyCompatibleEndpointContextWindow("https://endpoint.example/v1", "model-a", { + env, + logger: { + log: (message: string) => messages.push(message), + warn: (message: string) => messages.push(message), + }, + ...options, + }); + return { env, messages }; +} + +describe("compatible-endpoint context window", () => { + it("bakes the endpoint's max_model_len into NEMOCLAW_CONTEXT_WINDOW (#6177)", () => { + const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); + const { env, messages } = apply({ fetchModels }); + + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + expect(fetchModels).toHaveBeenCalledWith("https://endpoint.example/v1", ""); + expect(messages.some((m) => m.includes("65536"))).toBe(true); + }); + + it("resolves the API key from the credential env for the probe", () => { + const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 32_768 }] })); + apply({ + fetchModels, + credentialEnv: "COMPATIBLE_API_KEY", + resolveCredential: (name) => (name === "COMPATIBLE_API_KEY" ? "secret-key" : null), + }); + + expect(fetchModels).toHaveBeenCalledWith("https://endpoint.example/v1", "secret-key"); + }); + + it("picks the exact model entry from a multi-model gateway response (#6177)", () => { + const fetchModels = vi.fn(() => ({ + data: [ + { id: "other-model", max_model_len: 8_192 }, + { id: "model-a", max_model_len: 65_536 }, + ], + })); + const { env } = apply({ fetchModels }); + + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + }); + + it("does not guess a context window from a multi-model gateway with no exact match (#6177)", () => { + const fetchModels = vi.fn(() => ({ + data: [ + { id: "other-a", max_model_len: 8_192 }, + { id: "other-b", max_model_len: 16_384 }, + ], + })); + const { env, messages } = apply({ fetchModels }); + + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + expect(messages.some((m) => m.includes("none match 'model-a'"))).toBe(true); + }); + + it("uses the sole served model even when its id does not match (single-model endpoint)", () => { + const fetchModels = vi.fn(() => ({ data: [{ id: "served-alias", max_model_len: 32_768 }] })); + const { env } = apply({ fetchModels }); + + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("32768"); + }); + + it("never downgrades an explicit NEMOCLAW_CONTEXT_WINDOW override (#6177)", () => { + const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 8_192 }] })); + const { env, messages } = apply({ fetchModels }, { NEMOCLAW_CONTEXT_WINDOW: "65536" }); + + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + expect(fetchModels).not.toHaveBeenCalled(); + expect(messages.some((m) => m.includes("Keeping configured context window"))).toBe(true); + }); + + it("warns and keeps the default context window when the endpoint cannot be probed", () => { + const fetchModels = vi.fn(() => null); + const { env, messages } = apply({ fetchModels }); + + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + expect(messages.some((m) => m.includes("Could not read the endpoint's /v1/models"))).toBe(true); + }); + + it("skips the real host fetch under the unit-test runner unless one is injected", () => { + const { env, messages } = apply({}, { VITEST: "true" }); + + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + expect(messages).toEqual([]); + }); + + it("clears its own stale auto value when a re-probed endpoint reports nothing (#6177)", () => { + // First endpoint auto-detects 65536 into the shared env. + const env: NodeJS.ProcessEnv = {}; + apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, env); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + + // A later selection pass probes an endpoint that reports no max_model_len: + // the stale auto value must not survive as if the user had set it. + apply({ fetchModels: () => ({ data: [{ id: "model-a" }] }) }, env); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + }); + + it("recomputes over its own prior auto value on a re-probe (#6177)", () => { + const env: NodeJS.ProcessEnv = {}; + apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, env); + apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 16_384 }] }) }, env); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("16384"); + }); + + it("keeps a genuine user override even after a prior auto value was recorded (#6177)", () => { + const env: NodeJS.ProcessEnv = {}; + apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, env); + // User pins a different value; a later probe must not overwrite it. + env.NEMOCLAW_CONTEXT_WINDOW = "200000"; + const { messages } = apply( + { fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 16_384 }] }) }, + env, + ); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("200000"); + expect(messages.some((m) => m.includes("Keeping configured context window"))).toBe(true); + }); + + it("does not crash on a malformed /v1/models body from an arbitrary endpoint (#6177)", () => { + const env: NodeJS.ProcessEnv = {}; + expect(() => + apply({ fetchModels: () => ({ data: [null, "nope", 42] }) as unknown as object }, env), + ).not.toThrow(); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + }); + + it("clearAutoDetectedCompatibleContextWindow drops a stale auto value but keeps a user override (#6177)", () => { + // Auto-detected value is cleared when retrying away to another provider. + const autoEnv: NodeJS.ProcessEnv = {}; + apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, autoEnv); + expect(autoEnv.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + clearAutoDetectedCompatibleContextWindow(autoEnv); + expect(autoEnv.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + + // A user-supplied value this probe never wrote survives the clear. + const userEnv: NodeJS.ProcessEnv = { NEMOCLAW_CONTEXT_WINDOW: "200000" }; + clearAutoDetectedCompatibleContextWindow(userEnv); + expect(userEnv.NEMOCLAW_CONTEXT_WINDOW).toBe("200000"); + }); +}); diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts new file mode 100644 index 00000000000..b7401fdbcac --- /dev/null +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -0,0 +1,166 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { createOpenAiLikeAuthConfig } from "../adapters/http/auth-config"; +import { runCurlProbe } from "../adapters/http/probe"; +import { getCredential } from "../credentials/store"; +import { hasExplicitContextWindow } from "./ollama-runtime-context"; +import { resolveVllmContextWindowFromModels } from "./vllm-runtime-context"; + +/** Injectable `/v1/models` fetcher; returns parsed JSON, or null when unavailable. */ +export type CompatibleEndpointModelsFetcher = ( + endpointUrl: string, + apiKey: string, +) => unknown | null; + +export interface ApplyCompatibleEndpointContextWindowOptions { + env?: NodeJS.ProcessEnv; + logger?: Pick; + /** Credential env used to authenticate the `/v1/models` probe. */ + credentialEnv?: string | null; + /** Already-resolved API key; takes precedence over `credentialEnv`. */ + apiKey?: string | null; + /** Override the default host curl fetch (unit tests inject a fake). */ + fetchModels?: CompatibleEndpointModelsFetcher; + /** Override credential resolution (unit tests inject a fake). */ + resolveCredential?: (credentialEnv: string) => string | null | undefined; +} + +/** + * GET `/models` on the host and return the parsed JSON body, or null + * when the endpoint is unreachable, errors, or returns a non-JSON body. This is + * the same source vLLM local onboarding reads, generalized to any configured + * OpenAI-compatible endpoint (custom / `compatible-endpoint`). Auth is sent when + * the endpoint requires an API key (e.g. a vLLM launched with `--api-key`). + */ +export function fetchCompatibleEndpointModels(endpointUrl: string, apiKey: string): unknown | null { + const baseUrl = String(endpointUrl).replace(/\/+$/, ""); + const authConfig = createOpenAiLikeAuthConfig(apiKey || ""); + try { + const result = runCurlProbe( + [ + "-sS", + "--connect-timeout", + "10", + "--max-time", + "15", + ...authConfig.args, + `${baseUrl}/models`, + ], + { trustedConfigFiles: authConfig.trustedConfigFiles }, + ); + if (!result.ok || !result.body) return null; + try { + return JSON.parse(result.body); + } catch { + return null; + } + } finally { + authConfig.cleanup(); + } +} + +// The value this probe last auto-detected. onboard can re-run provider +// selection (e.g. after a failed `inference set` the user picks a different +// endpoint/model), so a value we set on an earlier pass must not be mistaken +// for a user override on the next — otherwise a stale window from endpoint A +// would be kept for endpoint B. Mirrors the Ollama auto-state contract. +let autoDetectedCompatibleContextWindow: string | null = null; + +/** Test-only: forget any tracked auto value without touching the environment. */ +export function resetCompatibleEndpointContextWindowAutoState(): void { + autoDetectedCompatibleContextWindow = null; +} + +/** + * Drop a value this probe auto-detected on an earlier pass. onboard calls this + * before each provider-selection pass so that when the user retries away to a + * different provider, endpoint A's probed `max_model_len` is not left in the + * environment where `dockerfile-patch` would bake it as if the user had set it. + * A genuine user-supplied `NEMOCLAW_CONTEXT_WINDOW` (one this probe never wrote) + * is preserved because it never equals the tracked auto value (#6177). + */ +export function clearAutoDetectedCompatibleContextWindow( + env: NodeJS.ProcessEnv = process.env, +): void { + if ( + autoDetectedCompatibleContextWindow && + env.NEMOCLAW_CONTEXT_WINDOW === autoDetectedCompatibleContextWindow + ) { + delete env.NEMOCLAW_CONTEXT_WINDOW; + } + autoDetectedCompatibleContextWindow = null; +} + +/** + * Set `NEMOCLAW_CONTEXT_WINDOW` from a configured OpenAI-compatible endpoint's + * runtime `max_model_len` so custom / `compatible-endpoint` onboarding no longer + * falls back to a small architecture-default context (see #6177). + * + * - An explicit `NEMOCLAW_CONTEXT_WINDOW` always wins and is never downgraded. + * - A value this probe set on an earlier pass is not treated as an override; it + * is recomputed, or cleared when the new endpoint reports nothing usable. + * - When the endpoint cannot be probed, warn and keep the default context. + * - Under the unit-test runner the default curl fetch is skipped (endpoints are + * unreachable and curl would hang on DNS); pass `fetchModels` to exercise it. + */ +export function applyCompatibleEndpointContextWindow( + endpointUrl: string, + model: string | null | undefined, + options: ApplyCompatibleEndpointContextWindowOptions = {}, +): void { + const env = options.env ?? process.env; + const logger = options.logger ?? console; + + const currentContextWindow = env.NEMOCLAW_CONTEXT_WINDOW; + const currentIsPreviousAuto = + !!currentContextWindow && + !!autoDetectedCompatibleContextWindow && + currentContextWindow === autoDetectedCompatibleContextWindow; + const userContextWindow = currentIsPreviousAuto ? null : currentContextWindow; + + const clearPreviousAuto = (): void => { + if (currentIsPreviousAuto) { + delete env.NEMOCLAW_CONTEXT_WINDOW; + autoDetectedCompatibleContextWindow = null; + } + }; + + if (hasExplicitContextWindow(userContextWindow)) { + logger.log(` ℹ Keeping configured context window: ${userContextWindow} tokens`); + return; + } + + const fetchModels = options.fetchModels; + if (!fetchModels && env.VITEST === "true") return; + + const resolveCredential = options.resolveCredential ?? getCredential; + const apiKey = + options.apiKey ?? (options.credentialEnv ? resolveCredential(options.credentialEnv) : "") ?? ""; + + const models = (fetchModels ?? fetchCompatibleEndpointModels)(endpointUrl, apiKey); + if (models === null || models === undefined) { + logger.warn( + " ⚠ Could not read the endpoint's /v1/models max_model_len; using the default context " + + "window. Set NEMOCLAW_CONTEXT_WINDOW to override.", + ); + clearPreviousAuto(); + return; + } + + // strictModelMatch: a compatible endpoint can be a shared gateway serving many + // models, so never guess the first entry's max_model_len for a model that is + // not an exact /v1/models id — that would bake an unrelated model's window. + const contextLength = resolveVllmContextWindowFromModels(models, model, logger, { + strictModelMatch: true, + }); + if (contextLength === null) { + clearPreviousAuto(); + return; + } + + const value = String(contextLength); + env.NEMOCLAW_CONTEXT_WINDOW = value; + autoDetectedCompatibleContextWindow = value; + logger.log(` ✓ Using endpoint max_model_len: ${value} tokens`); +} diff --git a/src/lib/inference/vllm-runtime-context.test.ts b/src/lib/inference/vllm-runtime-context.test.ts index a4fd5fc42c0..2853b295c51 100644 --- a/src/lib/inference/vllm-runtime-context.test.ts +++ b/src/lib/inference/vllm-runtime-context.test.ts @@ -3,7 +3,10 @@ import { describe, expect, it } from "vitest"; -import { applyVllmRuntimeContextWindow } from "./vllm-runtime-context"; +import { + applyVllmRuntimeContextWindow, + resolveVllmContextWindowFromModels, +} from "./vllm-runtime-context"; function applyContextWindow( modelsResponse: unknown, @@ -74,6 +77,51 @@ describe("vLLM runtime context helpers", () => { expect(applyContextWindow(response, "").env.NEMOCLAW_CONTEXT_WINDOW).toBe("32768"); }); + it("ignores non-object /v1/models entries without throwing (#6177)", () => { + // Arbitrary compatible endpoints can return valid JSON that is not the vLLM + // shape; null/primitive entries must not crash the resolver. + expect(() => resolveVllmContextWindowFromModels({ data: [null] }, "model-a")).not.toThrow(); + expect(resolveVllmContextWindowFromModels({ data: [null] }, "model-a")).toBeNull(); + expect( + resolveVllmContextWindowFromModels( + { data: [null, "nope", { id: "model-a", max_model_len: 65_536 }] }, + "model-a", + ), + ).toBe(65_536); + }); + + it("under strictModelMatch, refuses to guess a window for multi-model gateways (#6177)", () => { + const warnings: string[] = []; + const logger = { warn: (message: string) => warnings.push(message) }; + const response = { + data: [ + { id: "model-a", max_model_len: 32_768 }, + { id: "model-b", max_model_len: 65_536 }, + ], + }; + + // Exact id still resolves. + expect( + resolveVllmContextWindowFromModels(response, "model-b", logger, { strictModelMatch: true }), + ).toBe(65_536); + // No exact match across multiple models → null (no first-entry guess). + expect( + resolveVllmContextWindowFromModels(response, "missing", logger, { strictModelMatch: true }), + ).toBeNull(); + expect(warnings.at(-1)).toContain("none match 'missing'"); + // A single served model is unambiguous even under strict matching. + expect( + resolveVllmContextWindowFromModels( + { data: [{ id: "solo", max_model_len: 16_384 }] }, + "x", + logger, + { + strictModelMatch: true, + }, + ), + ).toBe(16_384); + }); + it("applies detected max_model_len only when no explicit override is set", () => { const response = { data: [{ id: "model-a", max_model_len: 65_536 }] }; diff --git a/src/lib/inference/vllm-runtime-context.ts b/src/lib/inference/vllm-runtime-context.ts index 7ed7e6dd330..5cf24517253 100644 --- a/src/lib/inference/vllm-runtime-context.ts +++ b/src/lib/inference/vllm-runtime-context.ts @@ -8,6 +8,18 @@ const MAX_AUTODETECTED_VLLM_CONTEXT_WINDOW = 4_194_304; type ModelEntry = { id?: unknown; max_model_len?: unknown }; type ApplyOptions = { env?: NodeJS.ProcessEnv; logger?: Pick }; +export type ResolveVllmContextWindowOptions = { + /** + * When true, only fall back to the first entry if the response lists exactly + * one model. Multi-model responses with no exact `id` match return null + * instead of guessing. Use for shared OpenAI-compatible gateways (which can + * serve many models under aliases) so an unrelated model's `max_model_len` + * is never baked in; local vLLM keeps the permissive single-served-model + * fallback (#6177). + */ + strictModelMatch?: boolean; +}; + /** * Extract the runtime context window for `modelId` from a vLLM `/v1/models` * response (its `max_model_len`), validated against NemoClaw's auto-detect @@ -19,15 +31,32 @@ export function resolveVllmContextWindowFromModels( modelsResponse: unknown, modelId: string | null | undefined, logger: Pick = console, + options: ResolveVllmContextWindowOptions = {}, ): number | null { const data = (modelsResponse as { data?: unknown } | null | undefined)?.data; - const entries = Array.isArray(data) ? (data as ModelEntry[]) : []; + // Drop non-object entries defensively: a compatible endpoint's /v1/models body + // is arbitrary JSON and may contain nulls/primitives (e.g. `{"data":[null]}`), + // which would otherwise throw when we read `.id` and abort onboarding (#6177). + const entries = (Array.isArray(data) ? data : []).filter( + (candidate): candidate is ModelEntry => typeof candidate === "object" && candidate !== null, + ); if (entries.length === 0) return null; const target = String(modelId ?? "").trim(); - const entry = - (target && entries.find((candidate) => String(candidate.id ?? "").trim() === target)) || - entries[0]; + const exactMatch = target + ? entries.find((candidate) => String(candidate.id ?? "").trim() === target) + : undefined; + let entry = exactMatch; + if (!entry) { + if (options.strictModelMatch && entries.length > 1) { + logger.warn( + ` ⚠ Endpoint /v1/models lists ${entries.length} models and none match '${target}'; ` + + `not auto-detecting the context window. Set NEMOCLAW_CONTEXT_WINDOW to override.`, + ); + return null; + } + entry = entries[0]; + } const rawMaxModelLen = entry?.max_model_len; if ( rawMaxModelLen === undefined || diff --git a/src/lib/onboard/dockerfile-patch-hermes-context.test.ts b/src/lib/onboard/dockerfile-patch-hermes-context.test.ts new file mode 100644 index 00000000000..9afb35cb34b --- /dev/null +++ b/src/lib/onboard/dockerfile-patch-hermes-context.test.ts @@ -0,0 +1,86 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Closes the onboard loop for #6177: once the compatible-endpoint probe sets +// NEMOCLAW_CONTEXT_WINDOW, dockerfile-patch must rewrite the Hermes Dockerfile's +// ARG so the baked value reaches build-env/config generation. This stages the +// real agents/hermes/Dockerfile so a future ARG rename cannot silently regress. + +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +import { afterEach, describe, expect, it } from "vitest"; + +import { patchStagedDockerfile } from "./dockerfile-patch"; + +const HERMES_DOCKERFILE = path.join(import.meta.dirname, "../../../agents/hermes/Dockerfile"); +const tmpRoots: string[] = []; + +function stageHermesDockerfile(): string { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-hermes-ctx-patch-")); + tmpRoots.push(dir); + const file = path.join(dir, "Dockerfile"); + fs.copyFileSync(HERMES_DOCKERFILE, file); + return file; +} + +function contextWindowArg(dockerfilePath: string): string | undefined { + return fs + .readFileSync(dockerfilePath, "utf8") + .split("\n") + .find((line) => line.startsWith("ARG NEMOCLAW_CONTEXT_WINDOW=")); +} + +function patchHermes(dockerfilePath: string): void { + patchStagedDockerfile( + dockerfilePath, + "nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4", + "http://127.0.0.1:18789", + "build-hermes-context", + "compatible-endpoint", + "openai-completions", + ); +} + +function withContextWindowEnv(value: string | undefined, fn: () => T): T { + const prior = process.env.NEMOCLAW_CONTEXT_WINDOW; + if (value === undefined) delete process.env.NEMOCLAW_CONTEXT_WINDOW; + else process.env.NEMOCLAW_CONTEXT_WINDOW = value; + try { + return fn(); + } finally { + if (prior === undefined) delete process.env.NEMOCLAW_CONTEXT_WINDOW; + else process.env.NEMOCLAW_CONTEXT_WINDOW = prior; + } +} + +afterEach(() => { + for (const dir of tmpRoots.splice(0)) { + fs.rmSync(dir, { recursive: true, force: true }); + } +}); + +describe("patchStagedDockerfile :: Hermes NEMOCLAW_CONTEXT_WINDOW (#6177)", () => { + it("declares an empty context-window ARG that defaults to Hermes auto-detect", () => { + expect(contextWindowArg(HERMES_DOCKERFILE)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW="); + }); + + it("bakes a probed/explicit context window into the staged Hermes Dockerfile", () => { + const dockerfilePath = stageHermesDockerfile(); + withContextWindowEnv("65536", () => patchHermes(dockerfilePath)); + expect(contextWindowArg(dockerfilePath)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW=65536"); + }); + + it("leaves the empty default when no context window is configured", () => { + const dockerfilePath = stageHermesDockerfile(); + withContextWindowEnv(undefined, () => patchHermes(dockerfilePath)); + expect(contextWindowArg(dockerfilePath)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW="); + }); + + it("ignores a malformed context window and preserves auto-detect", () => { + const dockerfilePath = stageHermesDockerfile(); + withContextWindowEnv("not-a-number", () => patchHermes(dockerfilePath)); + expect(contextWindowArg(dockerfilePath)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW="); + }); +}); diff --git a/src/lib/onboard/machine/handlers/provider-inference.ts b/src/lib/onboard/machine/handlers/provider-inference.ts index 31ab2f94daa..7404fcb2024 100644 --- a/src/lib/onboard/machine/handlers/provider-inference.ts +++ b/src/lib/onboard/machine/handlers/provider-inference.ts @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +import { clearAutoDetectedCompatibleContextWindow } from "../../../inference/compatible-endpoint-context"; import type { WebSearchConfig } from "../../../inference/web-search"; import type { HermesAuthMethod, Session, SessionUpdates } from "../../../state/onboard-session"; import { withInferenceTrace, withProviderSelectionTrace } from "../../tracing"; @@ -336,6 +337,10 @@ export async function handleProviderInferenceState({ } } else { await deps.startRecordedStep("provider_selection"); + // Drop a context window auto-detected by a prior compatible-endpoint pass + // before re-selecting, so retrying to a different provider cannot inherit + // endpoint A's probed max_model_len as a bogus user override (#6177). + clearAutoDetectedCompatibleContextWindow(process.env); const selection = await withProviderSelectionTrace( sandboxName, (agent as { name?: string } | null)?.name, diff --git a/src/lib/onboard/setup-nim-selection.ts b/src/lib/onboard/setup-nim-selection.ts index 174ccfb0ce3..6deb6e19f71 100644 --- a/src/lib/onboard/setup-nim-selection.ts +++ b/src/lib/onboard/setup-nim-selection.ts @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +import { applyCompatibleEndpointContextWindow } from "../inference/compatible-endpoint-context"; import type { NvidiaFeaturedModelSession } from "./nvidia-featured-model-selection"; export { createNvidiaFeaturedModelSession } from "./nvidia-featured-model-selection"; @@ -202,6 +203,14 @@ export function createRemoteModelValidator(deps: RemoteModelValidatorDeps): { remoteConfig.helpUrl, ); if (validation.ok) { + // Probe the endpoint's runtime max_model_len so a custom vLLM endpoint + // gets its real context window baked in instead of a small + // architecture default; an explicit override always wins (#6177). + applyCompatibleEndpointContextWindow( + state.endpointUrl || deps.OPENAI_ENDPOINT_URL, + selectedModel, + { credentialEnv: selectedCredentialEnv }, + ); const explicitApi = (process.env.NEMOCLAW_PREFERRED_API || "").trim().toLowerCase(); if ( explicitApi && diff --git a/test/compatible-endpoint-context-probe.test.ts b/test/compatible-endpoint-context-probe.test.ts new file mode 100644 index 00000000000..7be13c63c74 --- /dev/null +++ b/test/compatible-endpoint-context-probe.test.ts @@ -0,0 +1,58 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Real-server proof for the compatible-endpoint context probe (#6177): a local +// OpenAI-compatible server (spawned as a subprocess so the synchronous curl +// probe cannot deadlock the event loop) advertises a runtime max_model_len on +// /v1/models, and the actual curl-backed probe reads it into +// NEMOCLAW_CONTEXT_WINDOW — the value onboarding bakes into the Hermes config. + +import { afterEach, describe, expect, it } from "vitest"; + +import { + applyCompatibleEndpointContextWindow, + fetchCompatibleEndpointModels, +} from "../src/lib/inference/compatible-endpoint-context"; +import { + type FakeOpenAiCompatibleServer, + startFakeOpenAiCompatibleServer, +} from "./e2e/fixtures/fake-openai-compatible"; +import { testTimeout } from "./helpers/timeouts"; + +const MODEL = "nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4"; + +let server: FakeOpenAiCompatibleServer | null = null; + +afterEach(async () => { + await server?.close(); + server = null; +}); + +describe("compatible-endpoint context probe against a real server (#6177)", { + timeout: testTimeout(60_000), +}, () => { + it("reads max_model_len from a live /v1/models endpoint into NEMOCLAW_CONTEXT_WINDOW", async () => { + server = await startFakeOpenAiCompatibleServer({ model: MODEL, maxModelLen: 65_536 }); + + const models = fetchCompatibleEndpointModels(server.baseUrl, ""); + expect(models).toMatchObject({ data: [{ id: MODEL, max_model_len: 65_536 }] }); + + const env: NodeJS.ProcessEnv = {}; + applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { + env, + fetchModels: fetchCompatibleEndpointModels, + }); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + }); + + it("keeps the default context window when the endpoint omits max_model_len", async () => { + server = await startFakeOpenAiCompatibleServer({ model: MODEL }); + + const env: NodeJS.ProcessEnv = {}; + applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { + env, + fetchModels: fetchCompatibleEndpointModels, + }); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + }); +}); diff --git a/test/e2e/fixtures/fake-openai-compatible.ts b/test/e2e/fixtures/fake-openai-compatible.ts index 8bf4c622390..34b35831070 100644 --- a/test/e2e/fixtures/fake-openai-compatible.ts +++ b/test/e2e/fixtures/fake-openai-compatible.ts @@ -33,6 +33,7 @@ export interface FakeOpenAiCompatibleServerOptions { readonly chatContent?: string; readonly forbiddenMarkers?: readonly string[]; readonly host?: string; + readonly maxModelLen?: number; readonly model?: string; readonly port?: number; readonly publicHost?: string; @@ -129,6 +130,8 @@ export async function startFakeOpenAiCompatibleServer( NEMOCLAW_FAKE_OPENAI_FORBIDDEN_MARKERS: JSON.stringify(options.forbiddenMarkers ?? []), NEMOCLAW_FAKE_OPENAI_HOST: host, NEMOCLAW_FAKE_OPENAI_LOG_FILE: logFile, + NEMOCLAW_FAKE_OPENAI_MAX_MODEL_LEN: + options.maxModelLen !== undefined ? String(options.maxModelLen) : "", NEMOCLAW_FAKE_OPENAI_MODEL: options.model ?? "test-model", NEMOCLAW_FAKE_OPENAI_PORT: String(options.port ?? 0), NEMOCLAW_FAKE_OPENAI_PORT_FILE: portFile, diff --git a/test/e2e/lib/fake-openai-compatible-api.mts b/test/e2e/lib/fake-openai-compatible-api.mts index 454b96142dd..cedc0f45a7c 100755 --- a/test/e2e/lib/fake-openai-compatible-api.mts +++ b/test/e2e/lib/fake-openai-compatible-api.mts @@ -13,6 +13,12 @@ const portFile = process.env.NEMOCLAW_FAKE_OPENAI_PORT_FILE || ""; const logFile = process.env.NEMOCLAW_FAKE_OPENAI_LOG_FILE || ""; const requestsFile = process.env.NEMOCLAW_FAKE_OPENAI_REQUESTS_FILE || ""; const model = process.env.NEMOCLAW_FAKE_OPENAI_MODEL || "test-model"; +// Optional runtime context window advertised on /v1/models, mirroring vLLM's +// max_model_len so onboarding can probe a real endpoint's context (#6177). +const maxModelLen = (() => { + const raw = (process.env.NEMOCLAW_FAKE_OPENAI_MAX_MODEL_LEN || "").trim(); + return /^[1-9][0-9]*$/.test(raw) ? Number(raw) : null; +})(); const apiKey = process.env.NEMOCLAW_FAKE_OPENAI_API_KEY || ""; const requireAuth = process.env.NEMOCLAW_FAKE_OPENAI_REQUIRE_AUTH === "1"; const chatContent = process.env.NEMOCLAW_FAKE_OPENAI_CHAT_CONTENT || "ok"; @@ -129,7 +135,9 @@ const server = createServer(async (req, res) => { bodyBytes: 0, forbiddenMarkerMatches: forbiddenMarkerMatches(req, Buffer.alloc(0)), }); - sendJson(res, 200, { object: "list", data: [{ id: model, object: "model" }] }); + const modelEntry: JsonObject = { id: model, object: "model" }; + if (maxModelLen !== null) modelEntry.max_model_len = maxModelLen; + sendJson(res, 200, { object: "list", data: [modelEntry] }); return; } diff --git a/test/generate-hermes-config.test.ts b/test/generate-hermes-config.test.ts index c712534218b..c91ae1852c3 100644 --- a/test/generate-hermes-config.test.ts +++ b/test/generate-hermes-config.test.ts @@ -507,6 +507,29 @@ describe("agents/hermes/generate-config.ts", () => { ]); }); + it("bakes NEMOCLAW_CONTEXT_WINDOW as model.context_length so Hermes cannot downgrade it (#6177)", () => { + const { config } = runConfigScript({ + NEMOCLAW_MODEL: "nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4", + NEMOCLAW_CONTEXT_WINDOW: "65536", + }); + + expect(config.model.context_length).toBe(65536); + // context_window is silently ignored by Hermes; we must not emit it. + expect(config.model.context_window).toBeUndefined(); + }); + + it("omits context_length when no context window is configured so Hermes auto-detects (#6177)", () => { + const { config } = runConfigScript(); + + expect(config.model.context_length).toBeUndefined(); + }); + + it("ignores a malformed NEMOCLAW_CONTEXT_WINDOW and lets Hermes auto-detect (#6177)", () => { + const { config } = runConfigScript({ NEMOCLAW_CONTEXT_WINDOW: "not-a-number" }); + + expect(config.model.context_length).toBeUndefined(); + }); + it("falls back to a stable picker provider name when no upstream is named", () => { const { config } = runConfigScript(); diff --git a/test/hermes-gateway-wrapper.test.ts b/test/hermes-gateway-wrapper.test.ts index 4154a45e996..b0ecf071116 100644 --- a/test/hermes-gateway-wrapper.test.ts +++ b/test/hermes-gateway-wrapper.test.ts @@ -1138,6 +1138,7 @@ describe.skipIf(!canRun)("agents/hermes/hermes-wrapper.py", () => { providerKey: "custom", upstreamProvider: "nemoclaw-inference", inferenceApi: "", + contextWindow: null, toolDisclosure: "progressive" as const, webSearchProvider: null, messagingCredentialPlaceholders: [], @@ -1185,6 +1186,7 @@ describe.skipIf(!canRun)("agents/hermes/hermes-wrapper.py", () => { providerKey: "custom", upstreamProvider: "nemoclaw-inference", inferenceApi: "", + contextWindow: null, toolDisclosure: "progressive" as const, webSearchProvider: null, messagingCredentialPlaceholders: [], From 44f0e009e94eacae3ce70c9e4b00056013f593e0 Mon Sep 17 00:00:00 2001 From: Yimo Jiang Date: Mon, 6 Jul 2026 04:44:27 +0000 Subject: [PATCH 02/41] test(inference): avoid adding if statements in #6177 Hermes context tests The codebase-growth-guardrails check forbids changed test files from adding if statements. Rewrite the Hermes Dockerfile context-window test to drive NEMOCLAW_CONTEXT_WINDOW by direct assignment with clearing beforeEach/afterEach hooks (matching the sibling dockerfile-patch suites) instead of a branching env helper, and reword a comment so it reads without "if". Signed-off-by: Yimo Jiang --- .../compatible-endpoint-context.test.ts | 2 +- .../dockerfile-patch-hermes-context.test.ts | 28 +++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/lib/inference/compatible-endpoint-context.test.ts b/src/lib/inference/compatible-endpoint-context.test.ts index 000b45f590a..57dc40e4513 100644 --- a/src/lib/inference/compatible-endpoint-context.test.ts +++ b/src/lib/inference/compatible-endpoint-context.test.ts @@ -113,7 +113,7 @@ describe("compatible-endpoint context window", () => { expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); // A later selection pass probes an endpoint that reports no max_model_len: - // the stale auto value must not survive as if the user had set it. + // the stale auto value must not survive (would look like a user override). apply({ fetchModels: () => ({ data: [{ id: "model-a" }] }) }, env); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); }); diff --git a/src/lib/onboard/dockerfile-patch-hermes-context.test.ts b/src/lib/onboard/dockerfile-patch-hermes-context.test.ts index 9afb35cb34b..a8195691643 100644 --- a/src/lib/onboard/dockerfile-patch-hermes-context.test.ts +++ b/src/lib/onboard/dockerfile-patch-hermes-context.test.ts @@ -10,7 +10,7 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; -import { afterEach, describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { patchStagedDockerfile } from "./dockerfile-patch"; @@ -43,19 +43,15 @@ function patchHermes(dockerfilePath: string): void { ); } -function withContextWindowEnv(value: string | undefined, fn: () => T): T { - const prior = process.env.NEMOCLAW_CONTEXT_WINDOW; - if (value === undefined) delete process.env.NEMOCLAW_CONTEXT_WINDOW; - else process.env.NEMOCLAW_CONTEXT_WINDOW = value; - try { - return fn(); - } finally { - if (prior === undefined) delete process.env.NEMOCLAW_CONTEXT_WINDOW; - else process.env.NEMOCLAW_CONTEXT_WINDOW = prior; - } -} +// Each test controls NEMOCLAW_CONTEXT_WINDOW by direct assignment; the hooks +// always start and end from a cleared state (no branching), matching the +// sibling dockerfile-patch test suites. +beforeEach(() => { + delete process.env.NEMOCLAW_CONTEXT_WINDOW; +}); afterEach(() => { + delete process.env.NEMOCLAW_CONTEXT_WINDOW; for (const dir of tmpRoots.splice(0)) { fs.rmSync(dir, { recursive: true, force: true }); } @@ -68,19 +64,21 @@ describe("patchStagedDockerfile :: Hermes NEMOCLAW_CONTEXT_WINDOW (#6177)", () = it("bakes a probed/explicit context window into the staged Hermes Dockerfile", () => { const dockerfilePath = stageHermesDockerfile(); - withContextWindowEnv("65536", () => patchHermes(dockerfilePath)); + process.env.NEMOCLAW_CONTEXT_WINDOW = "65536"; + patchHermes(dockerfilePath); expect(contextWindowArg(dockerfilePath)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW=65536"); }); it("leaves the empty default when no context window is configured", () => { const dockerfilePath = stageHermesDockerfile(); - withContextWindowEnv(undefined, () => patchHermes(dockerfilePath)); + patchHermes(dockerfilePath); expect(contextWindowArg(dockerfilePath)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW="); }); it("ignores a malformed context window and preserves auto-detect", () => { const dockerfilePath = stageHermesDockerfile(); - withContextWindowEnv("not-a-number", () => patchHermes(dockerfilePath)); + process.env.NEMOCLAW_CONTEXT_WINDOW = "not-a-number"; + patchHermes(dockerfilePath); expect(contextWindowArg(dockerfilePath)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW="); }); }); From f9a62f900b3925d3f2fdeb22504ff17af83aed31 Mon Sep 17 00:00:00 2001 From: Yimo Jiang Date: Mon, 6 Jul 2026 04:49:15 +0000 Subject: [PATCH 03/41] test(inference): prove endpoint auth flow; address #6177 review notes Apply PR review advisor feedback for #6177: - Add a real-server test asserting the compatible-endpoint probe transmits the Authorization header through curl's --config temp file (the fake endpoint now records header presence without logging the token). - Qualify the NEMOCLAW_CONTEXT_WINDOW docs default so OpenClaw's baked 131072 and Hermes' unset/auto-detect behavior are both accurate. - Document the vLLM auto-detect ceiling (4 MiB tokens, matches Ollama) and the host-side onboarding security model for the /v1/models probe. - Note the Ollama-mirrored auto-state duplication for future extraction. Signed-off-by: Yimo Jiang --- docs/inference/switch-inference-providers.mdx | 2 +- .../inference/compatible-endpoint-context.ts | 10 +++++++++ src/lib/inference/vllm-runtime-context.ts | 3 +++ .../compatible-endpoint-context-probe.test.ts | 22 +++++++++++++++++++ test/e2e/fixtures/fake-openai-compatible.ts | 1 + test/e2e/lib/fake-openai-compatible-api.mts | 3 +++ 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/docs/inference/switch-inference-providers.mdx b/docs/inference/switch-inference-providers.mdx index d968826f4d7..7fce8677560 100644 --- a/docs/inference/switch-inference-providers.mdx +++ b/docs/inference/switch-inference-providers.mdx @@ -178,7 +178,7 @@ To change these values, set the corresponding environment variables before runni | Variable | Values | Default | |---|---|---| -| `NEMOCLAW_CONTEXT_WINDOW` | Positive integer (tokens) | `131072` | +| `NEMOCLAW_CONTEXT_WINDOW` | Positive integer (tokens) | `131072` (OpenClaw baked metadata); Hermes leaves it unset to auto-detect | | `NEMOCLAW_MAX_TOKENS` | Positive integer (tokens) | `4096` | | `NEMOCLAW_REASONING` | `true` or `false` | `false` | | `NEMOCLAW_INFERENCE_INPUTS` | `text` or `text,image` | `text` | diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts index b7401fdbcac..299f3ac850f 100644 --- a/src/lib/inference/compatible-endpoint-context.ts +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -32,6 +32,12 @@ export interface ApplyCompatibleEndpointContextWindowOptions { * the same source vLLM local onboarding reads, generalized to any configured * OpenAI-compatible endpoint (custom / `compatible-endpoint`). Auth is sent when * the endpoint requires an API key (e.g. a vLLM launched with `--api-key`). + * + * Security: this runs host-side during privileged onboarding, before the + * sandbox and its OpenShell network policy exist, and targets the same endpoint + * URL the immediately-preceding chat-completions validation probe already + * reached — so it adds no egress surface beyond that validation. The credential + * travels in a curl `--config` temp file (0600), never on the argv. */ export function fetchCompatibleEndpointModels(endpointUrl: string, apiKey: string): unknown | null { const baseUrl = String(endpointUrl).replace(/\/+$/, ""); @@ -65,6 +71,10 @@ export function fetchCompatibleEndpointModels(endpointUrl: string, apiKey: strin // endpoint/model), so a value we set on an earlier pass must not be mistaken // for a user override on the next — otherwise a stale window from endpoint A // would be kept for endpoint B. Mirrors the Ollama auto-state contract. +// TODO(#6177): this auto-state tracking mirrors the Ollama contract +// (autoDetectedOllamaContextWindow in ollama-runtime-context.ts). If a third +// provider adopts the same "auto-detected vs user override" pattern, extract a +// shared trackAutoDetectedContextWindow helper instead of duplicating it again. let autoDetectedCompatibleContextWindow: string | null = null; /** Test-only: forget any tracked auto value without touching the environment. */ diff --git a/src/lib/inference/vllm-runtime-context.ts b/src/lib/inference/vllm-runtime-context.ts index 5cf24517253..72f41314312 100644 --- a/src/lib/inference/vllm-runtime-context.ts +++ b/src/lib/inference/vllm-runtime-context.ts @@ -3,6 +3,9 @@ import { hasExplicitContextWindow, parsePositiveInteger } from "./ollama-runtime-context"; +// 4 MiB tokens (2^22) — far above any practical model context window, so it +// rejects obviously broken daemon responses while never clipping a real one. +// Matches the Ollama auto-detect ceiling (MAX_AUTODETECTED_OLLAMA_CONTEXT_WINDOW). const MAX_AUTODETECTED_VLLM_CONTEXT_WINDOW = 4_194_304; type ModelEntry = { id?: unknown; max_model_len?: unknown }; diff --git a/test/compatible-endpoint-context-probe.test.ts b/test/compatible-endpoint-context-probe.test.ts index 7be13c63c74..f1310f3a5ee 100644 --- a/test/compatible-endpoint-context-probe.test.ts +++ b/test/compatible-endpoint-context-probe.test.ts @@ -45,6 +45,28 @@ describe("compatible-endpoint context probe against a real server (#6177)", { expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); }); + it("sends the endpoint credential through curl's --config auth flow (#6177)", async () => { + server = await startFakeOpenAiCompatibleServer({ + model: MODEL, + maxModelLen: 32_768, + apiKey: "secret-key", + }); + + const env: NodeJS.ProcessEnv = {}; + applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { + env, + apiKey: "secret-key", + fetchModels: fetchCompatibleEndpointModels, + }); + + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("32768"); + // The real curl probe transmitted an Authorization header built from the + // credential (via the temp --config file), proving the auth path works. + expect( + server.requests().some((entry) => entry.path === "/v1/models" && entry.authorizationSent), + ).toBe(true); + }); + it("keeps the default context window when the endpoint omits max_model_len", async () => { server = await startFakeOpenAiCompatibleServer({ model: MODEL }); diff --git a/test/e2e/fixtures/fake-openai-compatible.ts b/test/e2e/fixtures/fake-openai-compatible.ts index 34b35831070..2e399914cc8 100644 --- a/test/e2e/fixtures/fake-openai-compatible.ts +++ b/test/e2e/fixtures/fake-openai-compatible.ts @@ -15,6 +15,7 @@ export interface FakeOpenAiCompatibleRequest { readonly path: string; readonly bodyBytes: number; readonly auth?: string; + readonly authorizationSent?: boolean; readonly model?: string; readonly stream?: boolean; readonly forbiddenMarkerMatches?: number; diff --git a/test/e2e/lib/fake-openai-compatible-api.mts b/test/e2e/lib/fake-openai-compatible-api.mts index cedc0f45a7c..beb9fd4f256 100755 --- a/test/e2e/lib/fake-openai-compatible-api.mts +++ b/test/e2e/lib/fake-openai-compatible-api.mts @@ -133,6 +133,9 @@ const server = createServer(async (req, res) => { method: "GET", path, bodyBytes: 0, + // Presence only (never the token) so callers can prove a probe sent its + // credential without leaking it into the requests log (#6177). + authorizationSent: Boolean(req.headers.authorization), forbiddenMarkerMatches: forbiddenMarkerMatches(req, Buffer.alloc(0)), }); const modelEntry: JsonObject = { id: model, object: "model" }; From 09d89c88107eb942cddbf1c31325429d48946a39 Mon Sep 17 00:00:00 2001 From: Yimo Jiang Date: Mon, 6 Jul 2026 04:54:18 +0000 Subject: [PATCH 04/41] fix(inference): skip host probe for sandbox-internal endpoints (#6177) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Second round of PR review advisor feedback for #6177: - Skip the /v1/models context probe when the endpoint URL is a sandbox-internal (host.openshell.internal) or docker-internal host — a host-side GET cannot reach it — mirroring probeOpenAiLikeEndpoint, and leave Hermes auto-detect. - Add a source-boundary note documenting the tolerated invalid states and the removal condition for the probe. - Add a handler-level regression test: a stale auto-detected compatible-endpoint context window is cleared before a re-selected provider is patched. - Add a unit test for the sandbox-internal skip. - Split the compatible-endpoint docs paragraph into one sentence per line. Signed-off-by: Yimo Jiang --- docs/inference/switch-inference-providers.mdx | 4 ++- .../compatible-endpoint-context.test.ts | 15 ++++++++ .../inference/compatible-endpoint-context.ts | 34 +++++++++++++++++++ .../handlers/provider-inference.test.ts | 33 ++++++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) diff --git a/docs/inference/switch-inference-providers.mdx b/docs/inference/switch-inference-providers.mdx index 7fce8677560..c491a9df0e7 100644 --- a/docs/inference/switch-inference-providers.mdx +++ b/docs/inference/switch-inference-providers.mdx @@ -188,7 +188,9 @@ To change these values, set the corresponding environment variables before runni NemoClaw ignores invalid values and bakes the default into the image. For Local Ollama, onboarding loads the selected model first and uses Ollama's reported runtime context length when `NEMOCLAW_CONTEXT_WINDOW` is unset. For local vLLM, onboarding uses the runtime `max_model_len` value when the server reports one and `NEMOCLAW_CONTEXT_WINDOW` is unset. -For an OpenAI-compatible endpoint (the **Other OpenAI-compatible endpoint** provider, including a self-hosted vLLM server), onboarding probes the endpoint's `/v1/models` response and uses its reported `max_model_len` when `NEMOCLAW_CONTEXT_WINDOW` is unset, so the agent gets the endpoint's real context window instead of a small architecture default. Set `NEMOCLAW_CONTEXT_WINDOW` to override the probed value. For Hermes, the resolved window is written as `model.context_length` in the generated `config.yaml`; leaving it unset lets Hermes auto-detect. +For an OpenAI-compatible endpoint (the **Other OpenAI-compatible endpoint** provider, including a self-hosted vLLM server), onboarding probes the endpoint's `/v1/models` response and uses its reported `max_model_len` when `NEMOCLAW_CONTEXT_WINDOW` is unset, so the agent gets the endpoint's real context window instead of a small architecture default. +Set `NEMOCLAW_CONTEXT_WINDOW` to override the probed value. +For Hermes, the resolved window is written as `model.context_length` in the generated `config.yaml`, and leaving it unset lets Hermes auto-detect. Use `NEMOCLAW_INFERENCE_INPUTS=text,image` only for a model that accepts image input through the selected provider. During interactive onboarding, NemoClaw prompts for **Text only** or **Text + Image** when the discovered model name looks multimodal and `NEMOCLAW_INFERENCE_INPUTS` is not already valid. Non-interactive onboarding uses the environment value or the default `text` setting. diff --git a/src/lib/inference/compatible-endpoint-context.test.ts b/src/lib/inference/compatible-endpoint-context.test.ts index 57dc40e4513..6f77a26e947 100644 --- a/src/lib/inference/compatible-endpoint-context.test.ts +++ b/src/lib/inference/compatible-endpoint-context.test.ts @@ -82,6 +82,21 @@ describe("compatible-endpoint context window", () => { expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("32768"); }); + it("skips the probe for a sandbox-internal endpoint and leaves auto-detect (#6177)", () => { + const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); + const messages: string[] = []; + const env: NodeJS.ProcessEnv = {}; + applyCompatibleEndpointContextWindow("https://host.openshell.internal/v1", "model-a", { + env, + fetchModels, + logger: { log: (m) => messages.push(m), warn: (m) => messages.push(m) }, + }); + + expect(fetchModels).not.toHaveBeenCalled(); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + expect(messages).toEqual([]); + }); + it("never downgrades an explicit NEMOCLAW_CONTEXT_WINDOW override (#6177)", () => { const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 8_192 }] })); const { env, messages } = apply({ fetchModels }, { NEMOCLAW_CONTEXT_WINDOW: "65536" }); diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts index 299f3ac850f..fe1c96020de 100644 --- a/src/lib/inference/compatible-endpoint-context.ts +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -7,6 +7,21 @@ import { getCredential } from "../credentials/store"; import { hasExplicitContextWindow } from "./ollama-runtime-context"; import { resolveVllmContextWindowFromModels } from "./vllm-runtime-context"; +// Hosts that only resolve inside the OpenShell sandbox network (or are the +// hijacked docker-internal alias), mirroring probeOpenAiLikeEndpoint's +// SANDBOX_INTERNAL_HOSTS / isHijackedDockerInternalUrl. A host-side GET to these +// cannot reach the real endpoint, so we skip the probe rather than emit a +// misleading warning and let Hermes auto-detect / an explicit override stand. +const NON_HOST_PROBEABLE_HOSTS = new Set(["host.openshell.internal", "host.docker.internal"]); + +function isHostProbeableEndpoint(endpointUrl: string): boolean { + try { + return !NON_HOST_PROBEABLE_HOSTS.has(new URL(endpointUrl).hostname); + } catch { + return false; + } +} + /** Injectable `/v1/models` fetcher; returns parsed JSON, or null when unavailable. */ export type CompatibleEndpointModelsFetcher = ( endpointUrl: string, @@ -110,9 +125,20 @@ export function clearAutoDetectedCompatibleContextWindow( * - An explicit `NEMOCLAW_CONTEXT_WINDOW` always wins and is never downgraded. * - A value this probe set on an earlier pass is not treated as an override; it * is recomputed, or cleared when the new endpoint reports nothing usable. + * - A sandbox-internal / docker-internal endpoint URL is not host-probeable, so + * the probe is skipped and Hermes auto-detect is left in place. * - When the endpoint cannot be probed, warn and keep the default context. * - Under the unit-test runner the default curl fetch is skipped (endpoints are * unreachable and curl would hang on DNS); pass `fetchModels` to exercise it. + * + * Source boundary: `/v1/models` is served by an out-of-repo endpoint the user + * configured. Invalid states tolerated — unreachable/timing-out host, non-JSON + * or non-vLLM body, missing/malformed/over-ceiling `max_model_len`, and + * ambiguous multi-model catalogs — all fall back to Hermes/OpenClaw auto-detect + * (never throw, never guess). NemoClaw cannot fix the producer, so it validates + * before consuming. Regression coverage: compatible-endpoint-context.test.ts and + * the real-server compatible-endpoint-context-probe.test.ts. Remove this probe + * only if a typed, validated cross-provider model-catalog fetch subsumes it. */ export function applyCompatibleEndpointContextWindow( endpointUrl: string, @@ -141,6 +167,14 @@ export function applyCompatibleEndpointContextWindow( return; } + // A sandbox-internal endpoint (e.g. host.openshell.internal) resolves only + // inside the sandbox, so a host-side GET cannot reach it; skip cleanly and + // leave Hermes auto-detect rather than emit a misleading probe failure. + if (!isHostProbeableEndpoint(endpointUrl)) { + clearPreviousAuto(); + return; + } + const fetchModels = options.fetchModels; if (!fetchModels && env.VITEST === "true") return; diff --git a/src/lib/onboard/machine/handlers/provider-inference.test.ts b/src/lib/onboard/machine/handlers/provider-inference.test.ts index 95d2b21360a..bc663700961 100644 --- a/src/lib/onboard/machine/handlers/provider-inference.test.ts +++ b/src/lib/onboard/machine/handlers/provider-inference.test.ts @@ -7,6 +7,10 @@ import path from "node:path"; import { describe, expect, it, vi } from "vitest"; +import { + applyCompatibleEndpointContextWindow, + resetCompatibleEndpointContextWindowAutoState, +} from "../../../inference/compatible-endpoint-context"; import { createSession, type Session, type SessionUpdates } from "../../../state/onboard-session"; import { patchStagedDockerfile } from "../../dockerfile-patch"; import { clearCompatibleEndpointReasoning } from "../../reasoning-mode"; @@ -201,6 +205,35 @@ describe("handleProviderInferenceState", () => { ]); }); + it("clears a stale auto-detected compatible-endpoint context window before re-selecting (#6177)", async () => { + resetCompatibleEndpointContextWindowAutoState(); + delete process.env.NEMOCLAW_CONTEXT_WINDOW; + try { + // Simulate an earlier compatible-endpoint pass auto-detecting a window. + applyCompatibleEndpointContextWindow("https://endpoint-a.example/v1", "model-a", { + env: process.env, + fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }), + }); + expect(process.env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + + // The next fresh provider-selection pass must clear it before selection, + // so Dockerfile patching for a different provider never sees the stale value. + let contextAtSelection: string | undefined = "sentinel"; + const setupNim = vi.fn(async () => { + contextAtSelection = process.env.NEMOCLAW_CONTEXT_WINDOW; + return { ...baseSelection }; + }); + const { deps } = createDeps({ setupNim }); + await handleProviderInferenceState(baseOptions(deps)); + + expect(setupNim).toHaveBeenCalledTimes(1); + expect(contextAtSelection).toBeUndefined(); + } finally { + delete process.env.NEMOCLAW_CONTEXT_WINDOW; + resetCompatibleEndpointContextWindowAutoState(); + } + }); + describe("compatible endpoint reasoning mode", () => { it("records reasoning state during provider selection", async () => { const setupNim = vi.fn(async () => ({ From 28401a5804a96b8adfd57cb4fe013f57719d206c Mon Sep 17 00:00:00 2001 From: Yimo Jiang Date: Mon, 6 Jul 2026 04:58:27 +0000 Subject: [PATCH 05/41] test(inference): enforce /v1/models auth in probe E2E; clarify SSRF stance (#6177) Address remaining PR review advisor notes for #6177: - Add an opt-in NEMOCLAW_FAKE_OPENAI_REQUIRE_AUTH_MODELS flag so the fake endpoint enforces auth on GET /v1/models, and cover it end-to-end: the probe sets the window only with the correct credential and skips it on 401. The readiness probe now accepts 401 (server up) so existing fixtures are unaffected. - Document why private/loopback endpoints are intentionally not blocked (the operator's own self-hosted vLLM on localhost is the primary target; a private IP blocklist would break local inference, and probeOpenAiLikeEndpoint does not filter them either); only unreachable sandbox-internal hosts are skipped. - Add the (#6177) local issue-ref suffix to a probe test title. Signed-off-by: Yimo Jiang --- .../inference/compatible-endpoint-context.ts | 8 +++++ .../compatible-endpoint-context-probe.test.ts | 29 ++++++++++++++++++- test/e2e/fixtures/fake-openai-compatible.ts | 6 +++- test/e2e/lib/fake-openai-compatible-api.mts | 12 +++++++- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts index fe1c96020de..e4e3c3285a7 100644 --- a/src/lib/inference/compatible-endpoint-context.ts +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -53,6 +53,14 @@ export interface ApplyCompatibleEndpointContextWindowOptions { * URL the immediately-preceding chat-completions validation probe already * reached — so it adds no egress surface beyond that validation. The credential * travels in a curl `--config` temp file (0600), never on the argv. + * + * Private/loopback addresses are intentionally NOT blocked: the operator is + * configuring their own inference endpoint at setup time (not attacker-supplied + * input on a served request), and the primary target — a self-hosted vLLM on + * `localhost`/a private LAN — is exactly such an address. Blocking private IPs + * would break local inference. This matches `probeOpenAiLikeEndpoint`, which + * likewise does not private-IP-filter. Only sandbox-internal hosts are skipped, + * because a host-side GET genuinely cannot reach them. */ export function fetchCompatibleEndpointModels(endpointUrl: string, apiKey: string): unknown | null { const baseUrl = String(endpointUrl).replace(/\/+$/, ""); diff --git a/test/compatible-endpoint-context-probe.test.ts b/test/compatible-endpoint-context-probe.test.ts index f1310f3a5ee..34951435af8 100644 --- a/test/compatible-endpoint-context-probe.test.ts +++ b/test/compatible-endpoint-context-probe.test.ts @@ -31,7 +31,7 @@ afterEach(async () => { describe("compatible-endpoint context probe against a real server (#6177)", { timeout: testTimeout(60_000), }, () => { - it("reads max_model_len from a live /v1/models endpoint into NEMOCLAW_CONTEXT_WINDOW", async () => { + it("reads max_model_len from a live /v1/models endpoint into NEMOCLAW_CONTEXT_WINDOW (#6177)", async () => { server = await startFakeOpenAiCompatibleServer({ model: MODEL, maxModelLen: 65_536 }); const models = fetchCompatibleEndpointModels(server.baseUrl, ""); @@ -67,6 +67,33 @@ describe("compatible-endpoint context probe against a real server (#6177)", { ).toBe(true); }); + it("enforces auth on /v1/models: sets the window with the key, skips it without (#6177)", async () => { + server = await startFakeOpenAiCompatibleServer({ + model: MODEL, + maxModelLen: 65_536, + apiKey: "secret-key", + requireAuthModels: true, + }); + + // Wrong/absent credential → the endpoint 401s → no window is set. + const noKeyEnv: NodeJS.ProcessEnv = {}; + applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { + env: noKeyEnv, + apiKey: "", + fetchModels: fetchCompatibleEndpointModels, + }); + expect(noKeyEnv.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + + // Correct credential → authorized → the window is read. + const keyedEnv: NodeJS.ProcessEnv = {}; + applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { + env: keyedEnv, + apiKey: "secret-key", + fetchModels: fetchCompatibleEndpointModels, + }); + expect(keyedEnv.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + }); + it("keeps the default context window when the endpoint omits max_model_len", async () => { server = await startFakeOpenAiCompatibleServer({ model: MODEL }); diff --git a/test/e2e/fixtures/fake-openai-compatible.ts b/test/e2e/fixtures/fake-openai-compatible.ts index 2e399914cc8..d08266ef420 100644 --- a/test/e2e/fixtures/fake-openai-compatible.ts +++ b/test/e2e/fixtures/fake-openai-compatible.ts @@ -39,6 +39,7 @@ export interface FakeOpenAiCompatibleServerOptions { readonly port?: number; readonly publicHost?: string; readonly requireAuth?: boolean; + readonly requireAuthModels?: boolean; readonly responseText?: string; } @@ -79,7 +80,9 @@ function canReachModels(host: string, port: number): Promise { }, (res) => { res.resume(); - resolve(res.statusCode === 200); + // 401 still means the server is up — it just enforces auth on + // /v1/models (requireAuthModels), which the readiness probe omits. + resolve(res.statusCode === 200 || res.statusCode === 401); }, ); req.on("error", () => resolve(false)); @@ -138,6 +141,7 @@ export async function startFakeOpenAiCompatibleServer( NEMOCLAW_FAKE_OPENAI_PORT_FILE: portFile, NEMOCLAW_FAKE_OPENAI_REQUESTS_FILE: requestsFile, NEMOCLAW_FAKE_OPENAI_REQUIRE_AUTH: options.requireAuth ? "1" : "0", + NEMOCLAW_FAKE_OPENAI_REQUIRE_AUTH_MODELS: options.requireAuthModels ? "1" : "0", NEMOCLAW_FAKE_OPENAI_RESPONSE_TEXT: options.responseText ?? options.chatContent ?? "ok", }, stdio: "ignore", diff --git a/test/e2e/lib/fake-openai-compatible-api.mts b/test/e2e/lib/fake-openai-compatible-api.mts index beb9fd4f256..b5f275381ca 100755 --- a/test/e2e/lib/fake-openai-compatible-api.mts +++ b/test/e2e/lib/fake-openai-compatible-api.mts @@ -21,6 +21,10 @@ const maxModelLen = (() => { })(); const apiKey = process.env.NEMOCLAW_FAKE_OPENAI_API_KEY || ""; const requireAuth = process.env.NEMOCLAW_FAKE_OPENAI_REQUIRE_AUTH === "1"; +// Opt-in auth enforcement on GET /v1/models specifically (real vLLM launched +// with --api-key gates it). Separate from requireAuth so existing tests, whose +// readiness probe hits /v1/models unauthenticated, keep working. See #6177. +const requireAuthModels = process.env.NEMOCLAW_FAKE_OPENAI_REQUIRE_AUTH_MODELS === "1"; const chatContent = process.env.NEMOCLAW_FAKE_OPENAI_CHAT_CONTENT || "ok"; const responseText = process.env.NEMOCLAW_FAKE_OPENAI_RESPONSE_TEXT || chatContent; const forbiddenMarkers = (() => { @@ -128,16 +132,22 @@ const server = createServer(async (req, res) => { const path = requestPath(req); if (req.method === "GET" && ["/v1/models", "/models"].includes(path)) { - log(`GET ${path}`); + const modelsAuthOk = !requireAuthModels || req.headers.authorization === `Bearer ${apiKey}`; + log(`GET ${path} auth=${modelsAuthOk ? "ok" : "missing"}`); recordRequest({ method: "GET", path, bodyBytes: 0, + auth: modelsAuthOk ? "ok" : "missing", // Presence only (never the token) so callers can prove a probe sent its // credential without leaking it into the requests log (#6177). authorizationSent: Boolean(req.headers.authorization), forbiddenMarkerMatches: forbiddenMarkerMatches(req, Buffer.alloc(0)), }); + if (!modelsAuthOk) { + sendJson(res, 401, { error: { message: "missing bearer credential" } }); + return; + } const modelEntry: JsonObject = { id: model, object: "model" }; if (maxModelLen !== null) modelEntry.max_model_len = maxModelLen; sendJson(res, 200, { object: "list", data: [modelEntry] }); From 48168214c9f165e1af1ec0865c0806621b17cac9 Mon Sep 17 00:00:00 2001 From: Yimo Jiang Date: Mon, 6 Jul 2026 05:14:17 +0000 Subject: [PATCH 06/41] chore(inference): review-nit polish for #6177 context propagation - Use vi.stubEnv/vi.unstubAllEnvs in the Hermes Dockerfile context test instead of manual process.env deletion (CodeRabbit / advisor quick win). - Document that no separate auxiliary/compression context_length is needed: Hermes derives its compression threshold from the main model's context_length. Signed-off-by: Yimo Jiang --- agents/hermes/config/hermes-config.ts | 6 ++++++ .../dockerfile-patch-hermes-context.test.ts | 19 ++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/agents/hermes/config/hermes-config.ts b/agents/hermes/config/hermes-config.ts index ae37dd7d467..4bf2cc30564 100644 --- a/agents/hermes/config/hermes-config.ts +++ b/agents/hermes/config/hermes-config.ts @@ -61,6 +61,12 @@ export function buildHermesConfig( // small architecture default when the endpoint actually serves a larger // max_model_len (#6177). Omit it (null) to let Hermes auto-detect. Hermes // reads only `context_length`; `context_window` is silently ignored. + // + // No separate auxiliary/compression context key is written: Hermes derives + // its compression trigger (compression.threshold × context_length) from the + // main model's context_length, so setting it here is sufficient for the + // reported "Cannot compress further" failure — the auxiliary/curator model is + // configured via auxiliary.* and needs no dedicated context length here. if (settings.contextWindow !== null) modelConfig.context_length = settings.contextWindow; // Surface the managed endpoint to Hermes' model picker. The inline `model:` diff --git a/src/lib/onboard/dockerfile-patch-hermes-context.test.ts b/src/lib/onboard/dockerfile-patch-hermes-context.test.ts index a8195691643..367ba8e2c73 100644 --- a/src/lib/onboard/dockerfile-patch-hermes-context.test.ts +++ b/src/lib/onboard/dockerfile-patch-hermes-context.test.ts @@ -10,7 +10,7 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; import { patchStagedDockerfile } from "./dockerfile-patch"; @@ -43,15 +43,11 @@ function patchHermes(dockerfilePath: string): void { ); } -// Each test controls NEMOCLAW_CONTEXT_WINDOW by direct assignment; the hooks -// always start and end from a cleared state (no branching), matching the -// sibling dockerfile-patch test suites. -beforeEach(() => { - delete process.env.NEMOCLAW_CONTEXT_WINDOW; -}); - +// Each test controls NEMOCLAW_CONTEXT_WINDOW via vi.stubEnv, and afterEach +// restores the real environment through vi.unstubAllEnvs (no manual delete, +// no branching — keeps the file within the changed-test-file if-statement guard). afterEach(() => { - delete process.env.NEMOCLAW_CONTEXT_WINDOW; + vi.unstubAllEnvs(); for (const dir of tmpRoots.splice(0)) { fs.rmSync(dir, { recursive: true, force: true }); } @@ -64,20 +60,21 @@ describe("patchStagedDockerfile :: Hermes NEMOCLAW_CONTEXT_WINDOW (#6177)", () = it("bakes a probed/explicit context window into the staged Hermes Dockerfile", () => { const dockerfilePath = stageHermesDockerfile(); - process.env.NEMOCLAW_CONTEXT_WINDOW = "65536"; + vi.stubEnv("NEMOCLAW_CONTEXT_WINDOW", "65536"); patchHermes(dockerfilePath); expect(contextWindowArg(dockerfilePath)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW=65536"); }); it("leaves the empty default when no context window is configured", () => { const dockerfilePath = stageHermesDockerfile(); + vi.stubEnv("NEMOCLAW_CONTEXT_WINDOW", ""); patchHermes(dockerfilePath); expect(contextWindowArg(dockerfilePath)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW="); }); it("ignores a malformed context window and preserves auto-detect", () => { const dockerfilePath = stageHermesDockerfile(); - process.env.NEMOCLAW_CONTEXT_WINDOW = "not-a-number"; + vi.stubEnv("NEMOCLAW_CONTEXT_WINDOW", "not-a-number"); patchHermes(dockerfilePath); expect(contextWindowArg(dockerfilePath)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW="); }); From 2de6fc9c7986fa0af998e0c86db61fa95eee4dce Mon Sep 17 00:00:00 2001 From: Yimo Jiang Date: Mon, 6 Jul 2026 05:35:56 +0000 Subject: [PATCH 07/41] test(inference): chain endpoint probe to generated Hermes context_length (#6177) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add a source-level regression that runs the same applyCompatibleEndpointContextWindow the onboarding boundary calls (injected fetcher, max_model_len 65536), then the real Hermes config generator, and asserts model.context_length is 65536 and model.context_window is absent — closing the probe→env→config chain in one test. Signed-off-by: Yimo Jiang --- test/generate-hermes-config.test.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/generate-hermes-config.test.ts b/test/generate-hermes-config.test.ts index c91ae1852c3..fc079f53fed 100644 --- a/test/generate-hermes-config.test.ts +++ b/test/generate-hermes-config.test.ts @@ -9,6 +9,10 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import YAML from "yaml"; import { generateHermesConfig } from "../agents/hermes/config/generate.ts"; import { HERMES_PROXY_API_KEY_PLACEHOLDER } from "../src/lib/hermes-proxy-api-key"; +import { + applyCompatibleEndpointContextWindow, + resetCompatibleEndpointContextWindowAutoState, +} from "../src/lib/inference/compatible-endpoint-context"; import { applyMessagingBuildPhase, readMessagingBuildPlanFromEnv, @@ -518,6 +522,31 @@ describe("agents/hermes/generate-config.ts", () => { expect(config.model.context_window).toBeUndefined(); }); + it("chains the endpoint probe through to model.context_length in the generated config (#6177)", () => { + // Source-level regression across the boundary: the same probe onboarding + // calls resolves a compatible endpoint's max_model_len into + // NEMOCLAW_CONTEXT_WINDOW, and the real generator must bake it as + // model.context_length (never context_window). Uses an injected fetcher so + // no network is required. + resetCompatibleEndpointContextWindowAutoState(); + const model = "nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4"; + fs.mkdirSync(path.join(tmpDir, ".hermes"), { recursive: true }); + const env = buildHermesTestEnv({ NEMOCLAW_MODEL: model }); + applyCompatibleEndpointContextWindow("https://endpoint.example/v1", model, { + env, + fetchModels: () => ({ data: [{ id: model, max_model_len: 65_536 }] }), + }); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + + withEnv(env, () => + generateHermesConfig({ env, scriptDir: SCRIPT_DIR, homeDir: tmpDir, log: () => {} }), + ); + const { config } = readGeneratedConfig(); + expect(config.model.context_length).toBe(65536); + expect(config.model.context_window).toBeUndefined(); + resetCompatibleEndpointContextWindowAutoState(); + }); + it("omits context_length when no context window is configured so Hermes auto-detects (#6177)", () => { const { config } = runConfigScript(); From defffcf828e33b53f512a806c8efa5555c6dc6f2 Mon Sep 17 00:00:00 2001 From: Yimo Jiang Date: Mon, 6 Jul 2026 06:12:05 +0000 Subject: [PATCH 08/41] test(inference): assert /v1/models rejection and add issue-ref (#6177) Address CodeRabbit review nits: - The auth-enforcement case now asserts the fake endpoint recorded the /v1/models request as auth=missing (rejected) for the wrong-key path and auth=ok for the keyed path, instead of only inferring rejection from an unset NEMOCLAW_CONTEXT_WINDOW (which a network failure could also produce). - Add the (#6177) local issue-ref suffix to the remaining probe test title. Signed-off-by: Yimo Jiang --- test/compatible-endpoint-context-probe.test.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/test/compatible-endpoint-context-probe.test.ts b/test/compatible-endpoint-context-probe.test.ts index 34951435af8..1a9a35a8472 100644 --- a/test/compatible-endpoint-context-probe.test.ts +++ b/test/compatible-endpoint-context-probe.test.ts @@ -83,6 +83,11 @@ describe("compatible-endpoint context probe against a real server (#6177)", { fetchModels: fetchCompatibleEndpointModels, }); expect(noKeyEnv.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + // Assert the endpoint actually rejected the unauthenticated /v1/models + // request — an unset window alone could also come from a network failure. + expect( + server.requests().some((entry) => entry.path === "/v1/models" && entry.auth === "missing"), + ).toBe(true); // Correct credential → authorized → the window is read. const keyedEnv: NodeJS.ProcessEnv = {}; @@ -92,9 +97,12 @@ describe("compatible-endpoint context probe against a real server (#6177)", { fetchModels: fetchCompatibleEndpointModels, }); expect(keyedEnv.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + expect( + server.requests().some((entry) => entry.path === "/v1/models" && entry.auth === "ok"), + ).toBe(true); }); - it("keeps the default context window when the endpoint omits max_model_len", async () => { + it("keeps the default context window when the endpoint omits max_model_len (#6177)", async () => { server = await startFakeOpenAiCompatibleServer({ model: MODEL }); const env: NodeJS.ProcessEnv = {}; From 535d2a6b178a2c4d1be7bc3c99881907989d1e1a Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Mon, 6 Jul 2026 09:31:34 -0700 Subject: [PATCH 09/41] fix(inference): block private/internal endpoints at the SSRF source boundary (#6293) Custom / compatible-endpoint onboarding probed the operator-supplied endpoint URL before any private-IP screening. Both the chat-completions validation probe and the /v1/models context probe would issue host-side requests to a private/reserved address (SSRF), relying only on the downstream DNS-pinning config-write boundary as defense. Add source-boundary validation reusing the shared isPrivateHostname validator (src/lib/private-networks.ts), before any curl runs: - probeOpenAiLikeEndpoint (onboard-probes.ts): reject a private/internal endpoint after the allowed sandbox-internal / docker-internal hosts are handled, so every validation path (including reasoning mode) is covered (PRA-2). - applyCompatibleEndpointContextWindow (compatible-endpoint-context.ts): refuse the /v1/models probe for a private endpoint; this path issues its own GET independent of validation, so it screens the URL itself (PRA-1/PRA-3). Update the misleading JSDoc that claimed private IPs are intentionally not blocked (PRA-11). DNS-pinning at the config-write boundary is kept as defense-in-depth. Tests: - Extract the compatible-endpoint context-window handler case into provider-inference-context-window.test.ts plus a shared provider-inference.test-support.ts, keeping the primary handler spec within the growth guardrail (PRA-6). - Unit-test that applyCompatibleEndpointContextWindow rejects 10.0.0.1/127.0.0.1/169.254.169.254/172.16.0.1/192.168.1.1 before fetchModels, via it.each (PRA-8). - Add a real-server integration case proving a 127.0.0.1 endpoint is refused with no new /v1/models request; migrate the existing happy-path probe cases to present a public host to the guard while the injected fetcher still hits the loopback fixture (PRA-9). Signed-off-by: Prekshi Vyas --- .../compatible-endpoint-context.test.ts | 43 +++++ .../inference/compatible-endpoint-context.ts | 43 ++++- src/lib/inference/onboard-probes.ts | 29 +++ .../provider-inference-context-window.test.ts | 49 +++++ .../provider-inference.test-support.ts | 145 +++++++++++++++ .../handlers/provider-inference.test.ts | 175 +----------------- .../compatible-endpoint-context-probe.test.ts | 58 +++++- 7 files changed, 353 insertions(+), 189 deletions(-) create mode 100644 src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts create mode 100644 src/lib/onboard/machine/handlers/provider-inference.test-support.ts diff --git a/src/lib/inference/compatible-endpoint-context.test.ts b/src/lib/inference/compatible-endpoint-context.test.ts index 6f77a26e947..d66faec80aa 100644 --- a/src/lib/inference/compatible-endpoint-context.test.ts +++ b/src/lib/inference/compatible-endpoint-context.test.ts @@ -161,6 +161,49 @@ describe("compatible-endpoint context window", () => { expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); }); + it.each([ + "http://10.0.0.1/v1", + "http://127.0.0.1/v1", + "http://169.254.169.254/v1", + "http://172.16.0.1/v1", + "http://192.168.1.1/v1", + ])("rejects the private-IP endpoint %s before probing /v1/models (SSRF, #6293)", (endpointUrl) => { + const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); + const messages: string[] = []; + const env: NodeJS.ProcessEnv = {}; + applyCompatibleEndpointContextWindow(endpointUrl, "model-a", { + env, + fetchModels, + logger: { log: (m) => messages.push(m), warn: (m) => messages.push(m) }, + }); + + expect(fetchModels).not.toHaveBeenCalled(); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + expect(messages.some((m) => m.includes("private/internal address"))).toBe(true); + }); + + it("clears a stale auto value when re-probing a now private-IP endpoint (SSRF, #6293)", () => { + const env: NodeJS.ProcessEnv = {}; + // First endpoint auto-detects a window into the shared env. + applyCompatibleEndpointContextWindow("https://public.example/v1", "model-a", { + env, + fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }), + logger: { log: () => undefined, warn: () => undefined }, + }); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + + // A later pass selects a private-IP endpoint: the probe must be refused and + // the stale auto value dropped rather than left as a phantom user override. + const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 8_192 }] })); + applyCompatibleEndpointContextWindow("http://10.0.0.1/v1", "model-a", { + env, + fetchModels, + logger: { log: () => undefined, warn: () => undefined }, + }); + expect(fetchModels).not.toHaveBeenCalled(); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + }); + it("clearAutoDetectedCompatibleContextWindow drops a stale auto value but keeps a user override (#6177)", () => { // Auto-detected value is cleared when retrying away to another provider. const autoEnv: NodeJS.ProcessEnv = {}; diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts index e4e3c3285a7..f299f1f2274 100644 --- a/src/lib/inference/compatible-endpoint-context.ts +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -4,6 +4,7 @@ import { createOpenAiLikeAuthConfig } from "../adapters/http/auth-config"; import { runCurlProbe } from "../adapters/http/probe"; import { getCredential } from "../credentials/store"; +import { isPrivateHostname } from "../private-networks"; import { hasExplicitContextWindow } from "./ollama-runtime-context"; import { resolveVllmContextWindowFromModels } from "./vllm-runtime-context"; @@ -22,6 +23,20 @@ function isHostProbeableEndpoint(endpointUrl: string): boolean { } } +// SSRF source boundary: true when the endpoint host is a private/internal +// address (and not one of the allowed sandbox-internal aliases, which are +// screened separately by isHostProbeableEndpoint). Reuses the shared +// isPrivateHostname validator so the /v1/models context probe never issues a +// host-side GET to an attacker-reachable private address. See PR #6293 PRA-1. +function isPrivateEndpoint(endpointUrl: string): boolean { + try { + const { hostname } = new URL(endpointUrl); + return isPrivateHostname(hostname); + } catch { + return false; + } +} + /** Injectable `/v1/models` fetcher; returns parsed JSON, or null when unavailable. */ export type CompatibleEndpointModelsFetcher = ( endpointUrl: string, @@ -54,13 +69,13 @@ export interface ApplyCompatibleEndpointContextWindowOptions { * reached — so it adds no egress surface beyond that validation. The credential * travels in a curl `--config` temp file (0600), never on the argv. * - * Private/loopback addresses are intentionally NOT blocked: the operator is - * configuring their own inference endpoint at setup time (not attacker-supplied - * input on a served request), and the primary target — a self-hosted vLLM on - * `localhost`/a private LAN — is exactly such an address. Blocking private IPs - * would break local inference. This matches `probeOpenAiLikeEndpoint`, which - * likewise does not private-IP-filter. Only sandbox-internal hosts are skipped, - * because a host-side GET genuinely cannot reach them. + * SSRF: private/internal endpoints are rejected by the caller + * (`applyCompatibleEndpointContextWindow`) at the source boundary, before this + * fetch runs, using the shared `isPrivateHostname` validator — the same guard + * `probeOpenAiLikeEndpoint` applies. A self-hosted vLLM is reached through the + * sandbox-internal alias (`host.openshell.internal`), which is skipped + * separately, not via a raw private-LAN URL. This fetcher therefore only ever + * sees an already-validated, routable endpoint URL. */ export function fetchCompatibleEndpointModels(endpointUrl: string, apiKey: string): unknown | null { const baseUrl = String(endpointUrl).replace(/\/+$/, ""); @@ -183,6 +198,20 @@ export function applyCompatibleEndpointContextWindow( return; } + // SSRF source boundary: refuse to probe /v1/models on a private/internal + // endpoint. This path issues its own host-side GET independent of the + // chat-completions validation probe, so it must screen the URL itself with + // the shared isPrivateHostname validator (defense-in-depth alongside the + // DNS-pinning config-write boundary). See PR #6293 PRA-1/PRA-3. + if (isPrivateEndpoint(endpointUrl)) { + logger.warn( + " ⚠ Endpoint host is a private/internal address; skipping the /v1/models " + + "context probe. Use a routable public URL to auto-detect the context window.", + ); + clearPreviousAuto(); + return; + } + const fetchModels = options.fetchModels; if (!fetchModels && env.VITEST === "true") return; diff --git a/src/lib/inference/onboard-probes.ts b/src/lib/inference/onboard-probes.ts index f21255472c6..347d490ef25 100644 --- a/src/lib/inference/onboard-probes.ts +++ b/src/lib/inference/onboard-probes.ts @@ -33,6 +33,7 @@ const { isHijackedDockerInternalUrl, } = require("./onboard-host-docker-internal"); const { isNvcfFunctionNotFoundForAccount, nvcfFunctionNotFoundMessage } = require("../validation"); +const { isPrivateHostname } = require("../private-networks"); const { executeProbeWithHttpRetry, isProbeTimeout, @@ -634,6 +635,34 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { }; } + // SSRF source boundary: reject a private/internal endpoint before any curl. + // The two allowed internal hosts (hijacked docker-internal, sandbox-internal) + // are handled above; everything else that resolves to a private/reserved + // address is attacker-reachable SSRF surface. Reuses the shared + // isPrivateHostname validator (defense-in-depth alongside DNS-pinning at the + // config-write boundary). See PR #6293 PRA-2. + let probeHostname; + try { + probeHostname = new URL(String(endpointUrl)).hostname; + } catch { + probeHostname = ""; + } + if (probeHostname && isPrivateHostname(probeHostname)) { + return { + ok: false, + message: `Endpoint host "${probeHostname}" is a private/internal address and cannot be used as a remote inference endpoint. Use a routable public URL and retry onboard.`, + failures: [ + { + name: "Private-address endpoint", + httpStatus: 0, + curlStatus: 0, + message: "endpoint resolves to a private/internal address", + body: "", + }, + ], + }; + } + const baseUrl = String(endpointUrl).replace(/\/+$/, ""); let authConfig; try { diff --git a/src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts b/src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts new file mode 100644 index 00000000000..204eb12286f --- /dev/null +++ b/src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts @@ -0,0 +1,49 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Compatible-endpoint context-window regression cases for +// handleProviderInferenceState. Split out of provider-inference.test.ts so the +// primary handler spec stays within the growth guardrail (PR #6293 PRA-6). + +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { + applyCompatibleEndpointContextWindow, + resetCompatibleEndpointContextWindowAutoState, +} from "../../../inference/compatible-endpoint-context"; +import { handleProviderInferenceState } from "./provider-inference"; +import { baseOptions, baseSelection, createDeps } from "./provider-inference.test-support"; + +beforeEach(() => { + resetCompatibleEndpointContextWindowAutoState(); + delete process.env.NEMOCLAW_CONTEXT_WINDOW; +}); + +afterEach(() => { + delete process.env.NEMOCLAW_CONTEXT_WINDOW; + resetCompatibleEndpointContextWindowAutoState(); +}); + +describe("handleProviderInferenceState context window", () => { + it("clears a stale auto-detected compatible-endpoint context window before re-selecting (#6177)", async () => { + // Simulate an earlier compatible-endpoint pass auto-detecting a window. + applyCompatibleEndpointContextWindow("https://endpoint-a.example/v1", "model-a", { + env: process.env, + fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }), + }); + expect(process.env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + + // The next fresh provider-selection pass must clear it before selection, + // so Dockerfile patching for a different provider never sees the stale value. + const observed: Array = []; + const setupNim = vi.fn(async () => { + observed.push(process.env.NEMOCLAW_CONTEXT_WINDOW); + return { ...baseSelection }; + }); + const { deps } = createDeps({ setupNim }); + await handleProviderInferenceState(baseOptions(deps)); + + expect(setupNim).toHaveBeenCalledTimes(1); + expect(observed).toEqual([undefined]); + }); +}); diff --git a/src/lib/onboard/machine/handlers/provider-inference.test-support.ts b/src/lib/onboard/machine/handlers/provider-inference.test-support.ts new file mode 100644 index 00000000000..40ac9a39d64 --- /dev/null +++ b/src/lib/onboard/machine/handlers/provider-inference.test-support.ts @@ -0,0 +1,145 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Shared test scaffolding for handleProviderInferenceState specs. Extracted so +// the context-window regression cases can live in their own file without +// duplicating the deps/session factories (PR #6293 PRA-6). Not a *.test.ts +// file, so Vitest does not collect it as a suite. + +import { vi } from "vitest"; + +import { createSession, type Session, type SessionUpdates } from "../../../state/onboard-session"; +import type { ProviderInferenceStateOptions, ProviderSelectionResult } from "./provider-inference"; + +export type Gpu = { type: string } | null; +export type Agent = { name: string } | null; +export type Host = { cpus?: number }; + +export const baseSelection: ProviderSelectionResult = { + model: "nvidia/test", + provider: "nvidia-prod", + endpointUrl: "https://integrate.api.nvidia.com/v1", + credentialEnv: "NVIDIA_INFERENCE_API_KEY", + hermesAuthMethod: null, + hermesToolGateways: [], + preferredInferenceApi: "openai-responses", + compatibleEndpointReasoning: null, + nimContainer: null, +}; + +export function createDeps( + overrides: Partial["deps"]> = {}, +) { + const calls = { + setupNim: vi.fn(async () => ({ ...baseSelection })), + setupInference: vi.fn(async () => ({ ok: true as const })), + startStep: vi.fn(async () => undefined), + complete: vi.fn(async () => createSession()), + skipped: vi.fn(), + recoverProvider: vi.fn( + async (_provider: string | null | undefined, credentialEnv: string | null | undefined) => ({ + forceInferenceSetup: false, + credentialEnv: credentialEnv ?? null, + }), + ), + recordSkip: vi.fn(async () => createSession()), + repairEvent: vi.fn(async () => createSession()), + hydrate: vi.fn(), + repair: vi.fn(), + routeReady: vi.fn(() => false), + reconcileRouter: vi.fn(async () => undefined), + reupsertRoutedProvider: vi.fn( + (_provider: string, endpointUrl: string | null, _credentialEnv: string | null) => ({ + ok: true as const, + endpointUrl: "http://host.openshell.internal:4000/v1", + }), + ), + updateSandbox: vi.fn(), + promptName: vi.fn(async () => "my-assistant"), + promptYesNo: vi.fn(async () => true), + log: vi.fn(), + error: vi.fn(), + exit: vi.fn((code: number): never => { + throw new Error(`exit ${code}`); + }), + deleteEnv: vi.fn(), + }; + return { + calls, + deps: { + normalizeHermesAuthMethod: (value: string | null | undefined) => + value === "oauth" || value === "api_key" ? value : null, + setupNim: calls.setupNim, + setupInference: calls.setupInference, + startRecordedStep: calls.startStep, + recordStepComplete: calls.complete, + toSessionUpdates: (updates: Record) => updates as SessionUpdates, + skippedStepMessage: calls.skipped, + ensureResumeProviderReady: calls.recoverProvider, + recordStateSkipped: calls.recordSkip, + recordRepairEvent: calls.repairEvent, + hydrateCredentialEnv: calls.hydrate, + configureCompatibleEndpointReasoning: async (value?: string | null) => + value === "true" ? "true" : "false", + clearCompatibleEndpointReasoning: () => null, + repairLocalInferenceSystemdOverrideOrExit: calls.repair, + isNonInteractive: () => true, + getOpenshellBinary: () => "/usr/bin/openshell", + needsBedrockRuntimeAdapter: () => false, + isInferenceRouteReady: calls.routeReady, + isRoutedInferenceProvider: (provider: string) => provider === "nvidia-router", + reconcileModelRouter: calls.reconcileRouter, + reupsertRoutedProvider: calls.reupsertRoutedProvider, + registryUpdateSandbox: calls.updateSandbox, + promptValidatedSandboxName: calls.promptName, + assessHost: () => ({ cpus: 8 }), + formatSandboxBuildEstimateNote: () => "estimate", + formatOnboardConfigSummary: (options: { + provider: string; + model: string; + sandboxName: string; + }) => `summary:${options.provider}/${options.model}/${options.sandboxName}`, + promptYesNoOrDefault: calls.promptYesNo, + cliName: () => "nemoclaw", + log: calls.log, + error: calls.error, + exitProcess: calls.exit, + deleteEnv: calls.deleteEnv, + ...overrides, + }, + }; +} + +export function baseOptions( + deps: ProviderInferenceStateOptions["deps"], + session: Session | null = createSession(), +): ProviderInferenceStateOptions { + return { + resume: false, + fresh: false, + session, + gpu: { type: "nvidia" }, + sandboxName: null, + agent: null, + initial: { + model: session?.model ?? null, + provider: session?.provider ?? null, + endpointUrl: session?.endpointUrl ?? null, + credentialEnv: session?.credentialEnv ?? null, + hermesAuthMethod: session?.hermesAuthMethod ?? null, + hermesToolGateways: session?.hermesToolGateways ?? [], + preferredInferenceApi: session?.preferredInferenceApi ?? null, + compatibleEndpointReasoning: session?.compatibleEndpointReasoning ?? null, + nimContainer: session?.nimContainer ?? null, + webSearchConfig: session?.webSearchConfig ?? null, + }, + selectedMessagingChannels: [], + env: {}, + constants: { + hermesProviderName: "hermes-provider", + hermesApiKeyAuthMethod: "api_key", + hermesApiKeyCredentialEnv: "NOUS_API_KEY", + }, + deps, + }; +} diff --git a/src/lib/onboard/machine/handlers/provider-inference.test.ts b/src/lib/onboard/machine/handlers/provider-inference.test.ts index bc663700961..c39732428e5 100644 --- a/src/lib/onboard/machine/handlers/provider-inference.test.ts +++ b/src/lib/onboard/machine/handlers/provider-inference.test.ts @@ -7,151 +7,11 @@ import path from "node:path"; import { describe, expect, it, vi } from "vitest"; -import { - applyCompatibleEndpointContextWindow, - resetCompatibleEndpointContextWindowAutoState, -} from "../../../inference/compatible-endpoint-context"; -import { createSession, type Session, type SessionUpdates } from "../../../state/onboard-session"; +import { createSession } from "../../../state/onboard-session"; import { patchStagedDockerfile } from "../../dockerfile-patch"; import { clearCompatibleEndpointReasoning } from "../../reasoning-mode"; -import { - handleProviderInferenceState, - type ProviderInferenceStateOptions, - type ProviderSelectionResult, -} from "./provider-inference"; - -type Gpu = { type: string } | null; -type Agent = { name: string } | null; -type Host = { cpus?: number }; - -const baseSelection: ProviderSelectionResult = { - model: "nvidia/test", - provider: "nvidia-prod", - endpointUrl: "https://integrate.api.nvidia.com/v1", - credentialEnv: "NVIDIA_INFERENCE_API_KEY", - hermesAuthMethod: null, - hermesToolGateways: [], - preferredInferenceApi: "openai-responses", - compatibleEndpointReasoning: null, - nimContainer: null, -}; - -function createDeps( - overrides: Partial["deps"]> = {}, -) { - const calls = { - setupNim: vi.fn(async () => ({ ...baseSelection })), - setupInference: vi.fn(async () => ({ ok: true as const })), - startStep: vi.fn(async () => undefined), - complete: vi.fn(async () => createSession()), - skipped: vi.fn(), - recoverProvider: vi.fn( - async (_provider: string | null | undefined, credentialEnv: string | null | undefined) => ({ - forceInferenceSetup: false, - credentialEnv: credentialEnv ?? null, - }), - ), - recordSkip: vi.fn(async () => createSession()), - repairEvent: vi.fn(async () => createSession()), - hydrate: vi.fn(), - repair: vi.fn(), - routeReady: vi.fn(() => false), - reconcileRouter: vi.fn(async () => undefined), - reupsertRoutedProvider: vi.fn( - (_provider: string, endpointUrl: string | null, _credentialEnv: string | null) => ({ - ok: true as const, - endpointUrl: "http://host.openshell.internal:4000/v1", - }), - ), - updateSandbox: vi.fn(), - promptName: vi.fn(async () => "my-assistant"), - promptYesNo: vi.fn(async () => true), - log: vi.fn(), - error: vi.fn(), - exit: vi.fn((code: number): never => { - throw new Error(`exit ${code}`); - }), - deleteEnv: vi.fn(), - }; - return { - calls, - deps: { - normalizeHermesAuthMethod: (value: string | null | undefined) => - value === "oauth" || value === "api_key" ? value : null, - setupNim: calls.setupNim, - setupInference: calls.setupInference, - startRecordedStep: calls.startStep, - recordStepComplete: calls.complete, - toSessionUpdates: (updates: Record) => updates as SessionUpdates, - skippedStepMessage: calls.skipped, - ensureResumeProviderReady: calls.recoverProvider, - recordStateSkipped: calls.recordSkip, - recordRepairEvent: calls.repairEvent, - hydrateCredentialEnv: calls.hydrate, - configureCompatibleEndpointReasoning: async (value?: string | null) => - value === "true" ? "true" : "false", - clearCompatibleEndpointReasoning: () => null, - repairLocalInferenceSystemdOverrideOrExit: calls.repair, - isNonInteractive: () => true, - getOpenshellBinary: () => "/usr/bin/openshell", - needsBedrockRuntimeAdapter: () => false, - isInferenceRouteReady: calls.routeReady, - isRoutedInferenceProvider: (provider: string) => provider === "nvidia-router", - reconcileModelRouter: calls.reconcileRouter, - reupsertRoutedProvider: calls.reupsertRoutedProvider, - registryUpdateSandbox: calls.updateSandbox, - promptValidatedSandboxName: calls.promptName, - assessHost: () => ({ cpus: 8 }), - formatSandboxBuildEstimateNote: () => "estimate", - formatOnboardConfigSummary: (options: { - provider: string; - model: string; - sandboxName: string; - }) => `summary:${options.provider}/${options.model}/${options.sandboxName}`, - promptYesNoOrDefault: calls.promptYesNo, - cliName: () => "nemoclaw", - log: calls.log, - error: calls.error, - exitProcess: calls.exit, - deleteEnv: calls.deleteEnv, - ...overrides, - }, - }; -} - -function baseOptions( - deps: ProviderInferenceStateOptions["deps"], - session: Session | null = createSession(), -): ProviderInferenceStateOptions { - return { - resume: false, - fresh: false, - session, - gpu: { type: "nvidia" }, - sandboxName: null, - agent: null, - initial: { - model: session?.model ?? null, - provider: session?.provider ?? null, - endpointUrl: session?.endpointUrl ?? null, - credentialEnv: session?.credentialEnv ?? null, - hermesAuthMethod: session?.hermesAuthMethod ?? null, - hermesToolGateways: session?.hermesToolGateways ?? [], - preferredInferenceApi: session?.preferredInferenceApi ?? null, - compatibleEndpointReasoning: session?.compatibleEndpointReasoning ?? null, - nimContainer: session?.nimContainer ?? null, - webSearchConfig: session?.webSearchConfig ?? null, - }, - selectedMessagingChannels: [], - env: {}, - constants: { - hermesProviderName: "hermes-provider", - hermesApiKeyAuthMethod: "api_key", - hermesApiKeyCredentialEnv: "NOUS_API_KEY", - }, - deps, - }; -} +import { handleProviderInferenceState } from "./provider-inference"; +import { baseOptions, baseSelection, createDeps } from "./provider-inference.test-support"; describe("handleProviderInferenceState", () => { it("runs provider selection and inference setup on a fresh flow", async () => { @@ -205,35 +65,6 @@ describe("handleProviderInferenceState", () => { ]); }); - it("clears a stale auto-detected compatible-endpoint context window before re-selecting (#6177)", async () => { - resetCompatibleEndpointContextWindowAutoState(); - delete process.env.NEMOCLAW_CONTEXT_WINDOW; - try { - // Simulate an earlier compatible-endpoint pass auto-detecting a window. - applyCompatibleEndpointContextWindow("https://endpoint-a.example/v1", "model-a", { - env: process.env, - fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }), - }); - expect(process.env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); - - // The next fresh provider-selection pass must clear it before selection, - // so Dockerfile patching for a different provider never sees the stale value. - let contextAtSelection: string | undefined = "sentinel"; - const setupNim = vi.fn(async () => { - contextAtSelection = process.env.NEMOCLAW_CONTEXT_WINDOW; - return { ...baseSelection }; - }); - const { deps } = createDeps({ setupNim }); - await handleProviderInferenceState(baseOptions(deps)); - - expect(setupNim).toHaveBeenCalledTimes(1); - expect(contextAtSelection).toBeUndefined(); - } finally { - delete process.env.NEMOCLAW_CONTEXT_WINDOW; - resetCompatibleEndpointContextWindowAutoState(); - } - }); - describe("compatible endpoint reasoning mode", () => { it("records reasoning state during provider selection", async () => { const setupNim = vi.fn(async () => ({ diff --git a/test/compatible-endpoint-context-probe.test.ts b/test/compatible-endpoint-context-probe.test.ts index 1a9a35a8472..a9e7ce532bc 100644 --- a/test/compatible-endpoint-context-probe.test.ts +++ b/test/compatible-endpoint-context-probe.test.ts @@ -21,8 +21,21 @@ import { testTimeout } from "./helpers/timeouts"; const MODEL = "nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4"; +// The fake server binds to loopback (127.0.0.1), which the SSRF source-boundary +// guard now rejects. To exercise the happy path — real curl against a live +// /v1/models — we present a routable public hostname to the guard while the +// injected fetcher still hits the real loopback server. The private-IP block +// itself is asserted by its own case below and by the unit tests in +// src/lib/inference/compatible-endpoint-context.test.ts. +const PUBLIC_ENDPOINT_URL = "https://vllm.public.test/v1"; + let server: FakeOpenAiCompatibleServer | null = null; +function fetchFromServer(apiKey: string): () => unknown | null { + return () => + fetchCompatibleEndpointModels((server as FakeOpenAiCompatibleServer).baseUrl, apiKey); +} + afterEach(async () => { await server?.close(); server = null; @@ -38,9 +51,9 @@ describe("compatible-endpoint context probe against a real server (#6177)", { expect(models).toMatchObject({ data: [{ id: MODEL, max_model_len: 65_536 }] }); const env: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { + applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { env, - fetchModels: fetchCompatibleEndpointModels, + fetchModels: fetchFromServer(""), }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); }); @@ -53,10 +66,10 @@ describe("compatible-endpoint context probe against a real server (#6177)", { }); const env: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { + applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { env, apiKey: "secret-key", - fetchModels: fetchCompatibleEndpointModels, + fetchModels: fetchFromServer("secret-key"), }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("32768"); @@ -77,10 +90,10 @@ describe("compatible-endpoint context probe against a real server (#6177)", { // Wrong/absent credential → the endpoint 401s → no window is set. const noKeyEnv: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { + applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { env: noKeyEnv, apiKey: "", - fetchModels: fetchCompatibleEndpointModels, + fetchModels: fetchFromServer(""), }); expect(noKeyEnv.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); // Assert the endpoint actually rejected the unauthenticated /v1/models @@ -91,10 +104,10 @@ describe("compatible-endpoint context probe against a real server (#6177)", { // Correct credential → authorized → the window is read. const keyedEnv: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { + applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { env: keyedEnv, apiKey: "secret-key", - fetchModels: fetchCompatibleEndpointModels, + fetchModels: fetchFromServer("secret-key"), }); expect(keyedEnv.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); expect( @@ -102,14 +115,39 @@ describe("compatible-endpoint context probe against a real server (#6177)", { ).toBe(true); }); - it("keeps the default context window when the endpoint omits max_model_len (#6177)", async () => { - server = await startFakeOpenAiCompatibleServer({ model: MODEL }); + it("refuses to probe a private-IP endpoint before issuing any /v1/models request (SSRF, #6293)", async () => { + // The fake server binds to 127.0.0.1 — a real private/loopback address. + // The source-boundary guard must reject it before the real curl fetcher + // runs, so the server records zero requests and no window is set. + server = await startFakeOpenAiCompatibleServer({ model: MODEL, maxModelLen: 65_536 }); + expect(new URL(server.baseUrl).hostname).toBe("127.0.0.1"); + // The readiness probe already issued one /v1/models GET; the guard must not + // add another, so we compare the count before and after. + const modelsRequestsBefore = server + .requests() + .filter((entry) => entry.path === "/v1/models").length; const env: NodeJS.ProcessEnv = {}; applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { env, fetchModels: fetchCompatibleEndpointModels, }); + + const modelsRequestsAfter = server + .requests() + .filter((entry) => entry.path === "/v1/models").length; + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + expect(modelsRequestsAfter).toBe(modelsRequestsBefore); + }); + + it("keeps the default context window when the endpoint omits max_model_len (#6177)", async () => { + server = await startFakeOpenAiCompatibleServer({ model: MODEL }); + + const env: NodeJS.ProcessEnv = {}; + applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { + env, + fetchModels: fetchFromServer(""), + }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); }); }); From 0c9e678dc0611f752289e431bd795ce4a1427ab7 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Mon, 6 Jul 2026 09:51:19 -0700 Subject: [PATCH 10/41] fix(inference): exempt host.docker.internal from SSRF probe block; fix test titles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to the #6293 SSRF hardening — two CI breakages: - The isPrivateHostname block in probeOpenAiLikeEndpoint over-blocked host.docker.internal, a trusted sandbox->host bridge already gated by the allowHostDockerInternal check at the top of the function (Windows-host Ollama validation). Exempt the already-permitted hijacked-docker-internal alias. - Three new test titles put the issue ref mid-parens ("(SSRF, #6293)") which fails test-title-style; move to a clean final "(#6293)". cli project (635 tests) + npm run checks pass locally. SKIP=test-cli: full hook may trip on pre-existing macOS bash 3.2 noise if this branch predates #6140; CI runs bash 5.x green. Signed-off-by: Prekshi Vyas --- .../compatible-endpoint-context.test.ts | 4 ++-- src/lib/inference/onboard-probes.ts | 18 ++++++++++++------ test/compatible-endpoint-context-probe.test.ts | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/lib/inference/compatible-endpoint-context.test.ts b/src/lib/inference/compatible-endpoint-context.test.ts index d66faec80aa..246e2f1ca68 100644 --- a/src/lib/inference/compatible-endpoint-context.test.ts +++ b/src/lib/inference/compatible-endpoint-context.test.ts @@ -167,7 +167,7 @@ describe("compatible-endpoint context window", () => { "http://169.254.169.254/v1", "http://172.16.0.1/v1", "http://192.168.1.1/v1", - ])("rejects the private-IP endpoint %s before probing /v1/models (SSRF, #6293)", (endpointUrl) => { + ])("rejects the private-IP endpoint %s before probing /v1/models SSRF (#6293)", (endpointUrl) => { const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); const messages: string[] = []; const env: NodeJS.ProcessEnv = {}; @@ -182,7 +182,7 @@ describe("compatible-endpoint context window", () => { expect(messages.some((m) => m.includes("private/internal address"))).toBe(true); }); - it("clears a stale auto value when re-probing a now private-IP endpoint (SSRF, #6293)", () => { + it("clears a stale auto value when re-probing a now private-IP endpoint SSRF (#6293)", () => { const env: NodeJS.ProcessEnv = {}; // First endpoint auto-detects a window into the shared env. applyCompatibleEndpointContextWindow("https://public.example/v1", "model-a", { diff --git a/src/lib/inference/onboard-probes.ts b/src/lib/inference/onboard-probes.ts index 347d490ef25..3f0f04144be 100644 --- a/src/lib/inference/onboard-probes.ts +++ b/src/lib/inference/onboard-probes.ts @@ -636,18 +636,24 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { } // SSRF source boundary: reject a private/internal endpoint before any curl. - // The two allowed internal hosts (hijacked docker-internal, sandbox-internal) - // are handled above; everything else that resolves to a private/reserved - // address is attacker-reachable SSRF surface. Reuses the shared - // isPrivateHostname validator (defense-in-depth alongside DNS-pinning at the - // config-write boundary). See PR #6293 PRA-2. + // The sandbox-internal alias is handled above, and host.docker.internal is + // gated by the allowHostDockerInternal check at the top of this function — + // both are trusted sandbox->host bridges, so exempt the already-permitted + // hijacked-docker-internal alias here. Everything else that resolves to a + // private/reserved address is attacker-reachable SSRF surface. Reuses the + // shared isPrivateHostname validator (defense-in-depth alongside DNS-pinning + // at the config-write boundary). See PR #6293 PRA-2. let probeHostname; try { probeHostname = new URL(String(endpointUrl)).hostname; } catch { probeHostname = ""; } - if (probeHostname && isPrivateHostname(probeHostname)) { + if ( + probeHostname && + isPrivateHostname(probeHostname) && + !isHijackedDockerInternalUrl(endpointUrl) + ) { return { ok: false, message: `Endpoint host "${probeHostname}" is a private/internal address and cannot be used as a remote inference endpoint. Use a routable public URL and retry onboard.`, diff --git a/test/compatible-endpoint-context-probe.test.ts b/test/compatible-endpoint-context-probe.test.ts index a9e7ce532bc..0d3093e5d23 100644 --- a/test/compatible-endpoint-context-probe.test.ts +++ b/test/compatible-endpoint-context-probe.test.ts @@ -115,7 +115,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { ).toBe(true); }); - it("refuses to probe a private-IP endpoint before issuing any /v1/models request (SSRF, #6293)", async () => { + it("refuses to probe a private-IP endpoint before issuing any /v1/models request SSRF (#6293)", async () => { // The fake server binds to 127.0.0.1 — a real private/loopback address. // The source-boundary guard must reject it before the real curl fetcher // runs, so the server records zero requests and no window is set. From 63ef3061c70364f43217a9d5ea2618fc3dbed5b5 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Mon, 6 Jul 2026 10:38:33 -0700 Subject: [PATCH 11/41] fix(inference): exempt loopback from the onboard-probe SSRF guard (#6293) The private-address SSRF guard added to probeOpenAiLikeEndpoint rejected every private/reserved address, but that shared probe is the same one local inference uses to validate a locally-run Ollama/vLLM/NIM server on loopback (127.0.0.0/8, ::1, localhost). Loopback only reaches the probing host itself, so it is not an SSRF pivot to other internal infrastructure; non-loopback private ranges (LAN, link-local cloud-metadata) stay blocked. Adds isLoopbackHostname to private-networks plus unit coverage for the guard's block (LAN/metadata) and allow (loopback) paths. Signed-off-by: Prekshi Vyas Co-Authored-By: Claude Opus 4.8 (1M context) --- src/lib/inference/onboard-probes.test.ts | 48 ++++++++++++++++++++++++ src/lib/inference/onboard-probes.ts | 15 +++++--- src/lib/private-networks.ts | 24 ++++++++++++ 3 files changed, 82 insertions(+), 5 deletions(-) diff --git a/src/lib/inference/onboard-probes.test.ts b/src/lib/inference/onboard-probes.test.ts index 1bef5cc04e0..bcc2215470a 100644 --- a/src/lib/inference/onboard-probes.test.ts +++ b/src/lib/inference/onboard-probes.test.ts @@ -467,6 +467,54 @@ describe("OpenAI-compatible inference probes", () => { }); }); + describe("private-address SSRF guard (#6293)", () => { + it("rejects a non-loopback private LAN endpoint before issuing any probe (#6293)", () => { + const result = probeOpenAiLikeEndpoint( + "http://192.168.1.50:8000/v1", + "openai/model", + "dummy", + { + skipResponsesProbe: true, + }, + ); + expect(result).toMatchObject({ ok: false }); + expect(result.message).toMatch(/private\/internal address/i); + }); + + it("rejects the link-local cloud-metadata endpoint before any probe (#6293)", () => { + const result = probeOpenAiLikeEndpoint("http://169.254.169.254/v1", "openai/model", "dummy", { + skipResponsesProbe: true, + }); + expect(result).toMatchObject({ ok: false }); + expect(result.message).toMatch(/private\/internal address/i); + }); + + it("allows a loopback endpoint so local inference validation can proceed (#6293)", () => { + const body = `if [ -n "$outfile" ]; then + cat <<'JSON' > "$outfile" +{"choices":[{"message":{"content":"OK"}}]} +JSON +fi +printf '200' +exit 0 +`; + withFakeCurlProbe( + { script: makeFakeCurlScript(body), dirPrefix: "nemoclaw-loopback-probe-" }, + () => { + const result = probeOpenAiLikeEndpoint( + "http://127.0.0.1:11434/v1", + "openai/model", + "dummy", + { + skipResponsesProbe: true, + }, + ); + expect(result).toMatchObject({ ok: true }); + }, + ); + }); + }); + describe("retriable HTTP statuses (#2980, #3033)", () => { it("retries 429 (rate limit)", () => { expect(RETRIABLE_HTTP_PROBE_STATUSES.has(429)).toBe(true); diff --git a/src/lib/inference/onboard-probes.ts b/src/lib/inference/onboard-probes.ts index 3f0f04144be..31cf6274144 100644 --- a/src/lib/inference/onboard-probes.ts +++ b/src/lib/inference/onboard-probes.ts @@ -33,7 +33,7 @@ const { isHijackedDockerInternalUrl, } = require("./onboard-host-docker-internal"); const { isNvcfFunctionNotFoundForAccount, nvcfFunctionNotFoundMessage } = require("../validation"); -const { isPrivateHostname } = require("../private-networks"); +const { isPrivateHostname, isLoopbackHostname } = require("../private-networks"); const { executeProbeWithHttpRetry, isProbeTimeout, @@ -639,10 +639,14 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { // The sandbox-internal alias is handled above, and host.docker.internal is // gated by the allowHostDockerInternal check at the top of this function — // both are trusted sandbox->host bridges, so exempt the already-permitted - // hijacked-docker-internal alias here. Everything else that resolves to a - // private/reserved address is attacker-reachable SSRF surface. Reuses the - // shared isPrivateHostname validator (defense-in-depth alongside DNS-pinning - // at the config-write boundary). See PR #6293 PRA-2. + // hijacked-docker-internal alias here. Loopback (127.0.0.0/8, ::1, localhost) + // is likewise exempt: this shared probe is the same one local inference uses + // to validate a locally-run Ollama/vLLM/NIM server on the probing host, and + // loopback only reaches that host — it is not a pivot to other internal + // infrastructure. Everything else that resolves to a private/reserved address + // (LAN ranges, link-local metadata) is attacker-reachable SSRF surface and is + // refused. Reuses the shared validators (defense-in-depth alongside + // DNS-pinning at the config-write boundary). See PR #6293 PRA-2. let probeHostname; try { probeHostname = new URL(String(endpointUrl)).hostname; @@ -652,6 +656,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { if ( probeHostname && isPrivateHostname(probeHostname) && + !isLoopbackHostname(probeHostname) && !isHijackedDockerInternalUrl(endpointUrl) ) { return { diff --git a/src/lib/private-networks.ts b/src/lib/private-networks.ts index d9e814de263..8679a28c642 100644 --- a/src/lib/private-networks.ts +++ b/src/lib/private-networks.ts @@ -190,3 +190,27 @@ export function isPrivateHostname(hostname: string): boolean { } return isPrivateIp(normalised); } + +/** + * Return true when `hostname` is an IPv4/IPv6 loopback literal (127.0.0.0/8 or + * ::1) or the RFC 6761 `localhost` special-use name. + * + * Loopback is a proper subset of what isPrivateHostname matches, but it is + * semantically distinct for SSRF purposes: a loopback address only ever reaches + * a service on the probing host itself, so it is not a pivot to other internal + * infrastructure the way LAN ranges (10/8, 192.168/16) or link-local metadata + * (169.254.169.254) are. Host-side onboarding probes for locally-run inference + * servers (Ollama on 127.0.0.1, vLLM on localhost) legitimately target it, so + * callers that must still refuse genuine private-network SSRF can exempt + * loopback specifically without weakening the LAN/metadata blocks. + */ +export function isLoopbackHostname(hostname: string): boolean { + const stripped = + hostname.startsWith("[") && hostname.endsWith("]") ? hostname.slice(1, -1) : hostname; + const normalised = stripped.replace(/\.$/, "").toLowerCase(); + if (normalised === "localhost") return true; + const family = isIP(normalised); + if (family === 4) return normalised.startsWith("127."); + if (family === 6) return normalised === "::1"; + return false; +} From fcf67c3d88581813aaff4ea24d6521b7bed91014 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Mon, 6 Jul 2026 11:11:04 -0700 Subject: [PATCH 12/41] chore: drop accidentally-committed local dogfood-env.sh from PR branch scripts/dogfood-env.sh is a local-only developer template (runtime-fetched credentials, no hardcoded secrets) that was inadvertently staged into the prior merge commit by an over-broad git add. Remove it from the branch; it is not part of #6177/#6293. No secret values were exposed. Signed-off-by: Prekshi Vyas --- scripts/dogfood-env.sh | 147 ----------------------------------------- 1 file changed, 147 deletions(-) delete mode 100644 scripts/dogfood-env.sh diff --git a/scripts/dogfood-env.sh b/scripts/dogfood-env.sh deleted file mode 100644 index db9e1bf53db..00000000000 --- a/scripts/dogfood-env.sh +++ /dev/null @@ -1,147 +0,0 @@ -# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -# SPDX-License-Identifier: Apache-2.0 -# -# Source this file (after copying to dogfood-env.sh and filling in secrets) -# from the shell that will run `nemoclaw onboard` and the dogfood orchestrator. -# Variables flow into the sandbox via NemoClaw's existing deploy-time env -# passthrough (src/lib/deploy/index.ts:466-498). -# -# cp scripts/dogfood-env.sh.example scripts/dogfood-env.sh -# # edit scripts/dogfood-env.sh — fill in secrets -# source scripts/dogfood-env.sh -# nemoclaw onboard # uses canonical blueprint.yaml; env is read -# # ... inside the running sandbox: -# bash scripts/dogfood-orchestrator.sh - -# ----------------------------------------------------------------------------- -# REQUIRED — secrets, auto-fetched from local CLI credential stores. -# ----------------------------------------------------------------------------- -# -# No paste required — just be logged in via `gh auth login` and `brev login` -# first; the auto-fetch below reads from where each CLI already stores its -# token. If either lookup fails, the block warns; the orchestrator's Phase 1 -# env-check would then fail-fast before any cost is incurred. - -# GitHub token. `gh auth token` prints the active token to stdout. Required -# scopes: `repo` (label + comment writes); `project` (Project 199 move on -# fixed-on-latest verdicts — orchestrator soft-warns if missing). To add the -# project scope: `gh auth refresh -h github.com -s project` in a real TTY. -if command -v gh >/dev/null 2>&1; then - export GH_TOKEN="$(gh auth token 2>/dev/null || echo "")" -fi -if [ -z "${GH_TOKEN:-}" ]; then - echo "[dogfood-env] warning: could not auto-fetch GH_TOKEN — run 'gh auth login' first." >&2 -fi - -# Brev account tokens. Stored in ~/.brev/credentials.json after `brev login`: -# - access_token : short-lived (hours); used for API calls -# - refresh_token : longer-lived; brev CLI uses it to auto-renew access -# Must have create + delete permissions for the verification sandboxes the -# skill provisions per candidate. Pass BOTH so the orchestrator can -# reconstruct a complete credentials.json inside the sandbox — passing only -# access_token works for short runs but kills long batches when it expires. -if [ -f "$HOME/.brev/credentials.json" ] && command -v jq >/dev/null 2>&1; then - export BREV_API_TOKEN="$(jq -r '.access_token // empty' "$HOME/.brev/credentials.json" 2>/dev/null || echo "")" - export BREV_REFRESH_TOKEN="$(jq -r '.refresh_token // empty' "$HOME/.brev/credentials.json" 2>/dev/null || echo "")" -fi -if [ -z "${BREV_API_TOKEN:-}" ]; then - echo "[dogfood-env] warning: could not auto-fetch BREV_API_TOKEN — run 'brev login' first." >&2 -fi -# Sanity-check that the token still works on the host. If brev ls fails with -# 'logged out', the access token has expired and refresh has also lapsed. -if [ -n "${BREV_API_TOKEN:-}" ] && command -v brev >/dev/null 2>&1; then - if ! brev ls >/dev/null 2>&1; then - echo "[dogfood-env] warning: brev token expired locally — run 'brev login' to refresh." >&2 - fi -fi - -# NVIDIA API key — ONLY needed if you plan to process non-Ollama candidates. -# The default candidate filter (VERIFY_STALE_FORCE_OLLAMA_ONLY=1, see below) -# drops anything that would need this; leave the variable unset in that case. -# When set, write the key to a file (the skill copies the file to Brev boxes -# via `brev copy` — never to argv, per the skill's hard requirement). -# export NVIDIA_API_KEY_FILE="$HOME/.nvidia-api-key" - -# ----------------------------------------------------------------------------- -# REQUIRED — verify-stale automation switches. -# ----------------------------------------------------------------------------- - -# When =1, the Step 3 self-check verdict drives Brev provisioning directly -# (no maintainer approval prompt). The self-check itself ALWAYS runs. -export VERIFY_STALE_AUTO_APPROVE="1" - -# When =1, Step 10's comment-post and label-apply are skipped — drafts are -# written to $VERIFY_STALE_LOG_DIR//comment-draft.md instead. Start -# every dogfood run with DRY_RUN=1; flip to 0 only after reviewing drafts. -export VERIFY_STALE_DRY_RUN="1" - -# Number of candidates processed per run. Overrides the skill's default 15. -export VERIFY_STALE_BATCH_CAP="1" - -# Per-run log root. Each candidate gets // with metadata, -# transcripts, self-check.json, score.json, and comment-draft.md. The -# orchestrator symlinks the latest run to /latest for convenience. -# Use a persistent path so logs survive sandbox restarts. -export VERIFY_STALE_LOG_DIR="$HOME/verify-stale-runs" - -# Hard ceiling on cumulative Brev spend across the batch. The orchestrator -# accumulates per-candidate cost as (candidate wallclock seconds / 3600) × -# DOGFOOD_BREV_HOURLY_USD and halts the batch if the next candidate would -# push past this limit. -export BREV_BUDGET_USD="200" - -# Conservative per-hour Brev rate used by the cost accumulator. Set higher -# than your expected SKU price so the budget gate stays safe even if the -# skill provisions a more expensive box than expected. Defaults assume a -# mid-tier GPU box (T4/L40S range); bump to 4-5 if you expect H100 SKUs, -# drop to 0.5 if you constrain to CPU-only candidates. -export DOGFOOD_BREV_HOURLY_USD="${DOGFOOD_BREV_HOURLY_USD:-3}" - -# When =1, the Step 3 candidate filter drops any issue whose body or labels -# imply a non-ollama inference provider, so the skill never stalls at the -# Step 6 "Required-API-key prompt" in autonomous mode. Mount NVIDIA_API_KEY -# above and set this to 0 if you want a broader provider mix. -export VERIFY_STALE_FORCE_OLLAMA_ONLY="1" - -# Suppresses all stdin prompts in nemoclaw onboard, the skill, and openclaw -# agent. Required for unattended runs. (Already wired into src/lib/onboard.ts -# line 577 and elsewhere.) -export NEMOCLAW_NON_INTERACTIVE="1" - -# ----------------------------------------------------------------------------- -# OPTIONAL — overrides with sensible defaults. -# ----------------------------------------------------------------------------- - -# Ollama model the agent itself uses for reasoning over the skill. Must be -# pre-pulled on the HOST running Ollama (the orchestrator's G10 preflight -# will attempt `ollama pull` if missing, but pulling a 5 GB model inside the -# orchestrator slows the first run; do it ahead of time when possible). -export OLLAMA_MODEL="${OLLAMA_MODEL:-nemotron-3-nano:4b}" - -# Per-candidate agent timeout, in seconds. Drop this to 1800 (30 min) for -# the first real run — the skill's default per-candidate cap is also 60 min -# at the trap level, so setting this lower than 3600 means whichever fires -# first wins. Catches runaway agents before they burn the budget. -export OPENCLAW_AGENT_TIMEOUT_SEC="1800" - -# Full agent invocation override. By default the orchestrator builds it as -# `openclaw agent --local --timeout $OPENCLAW_AGENT_TIMEOUT_SEC` per -# node_modules/openclaw/docs/tools/agent-send.md. Override only if your -# sandbox image ships a non-standard openclaw build. -# export OPENCLAW_AGENT_CMD="openclaw agent --local --timeout 1800" - -# When =1, the orchestrator pauses after candidate selection (Phase 5) and -# prompts you via /dev/tty to confirm before any verification fires. Useful -# for the first single-candidate run; flip back to 0 for autonomous batches. -export DOGFOOD_CONFIRM_BEFORE_VERIFY="1" - -# Wrap-up Gist visibility. "secret" (default) keeps the Gist URL-only; "public" -# is org-discoverable. Run logs may contain agent reasoning over issue bodies — -# treat as moderately sensitive. -export DOGFOOD_GIST_VISIBILITY="${DOGFOOD_GIST_VISIBILITY:-secret}" - -# Picked up from the .example update — see PR commit -export NEMOCLAW_PROVIDER="custom" - -export NEMOCLAW_MODEL="aws/anthropic/bedrock-claude-opus-4-7" -export NEMOCLAW_ENDPOINT_URL="https://inference-api.nvidia.com" From f71e4b905d1250162e4c470f829c82b97ad1a223 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Mon, 6 Jul 2026 12:00:39 -0700 Subject: [PATCH 13/41] fix(inference): make compatible-endpoint loopback + context-window handling consistent (#6293) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two advisor-confirmed gaps in the #6177 compatible-endpoint context probe, both corroborated by the GPT-5.5 and Nemotron Ultra PR advisors: - Loopback inconsistency (GPT PRA-5, Nemotron PRA-3/PRA-19): the chat/ validation probe (probeOpenAiLikeEndpoint) exempts loopback, but the context probe's isPrivateEndpoint still treated 127.0.0.0/8, ::1 and localhost as private and skipped /v1/models — so a localhost vLLM custom endpoint never propagated its max_model_len. Exempt loopback here too via isLoopbackHostname; non-loopback private ranges (LAN, link-local metadata) stay blocked. - Unvalidated explicit override (Nemotron PRA-4/PRA-7): hasExplicitContextWindow only checks non-emptiness, so '0', malformed, or over-ceiling NEMOCLAW_CONTEXT_WINDOW values were kept verbatim. Validate the override with parsePositiveInteger + the auto-detect ceiling; on an invalid value, warn, drop it, and fall through to auto-detect. Adds unit coverage (loopback allowed, non-loopback private rejected, invalid overrides ignored) and flips the real-server probe test to assert loopback propagation. Signed-off-by: Prekshi Vyas Co-Authored-By: Claude Opus 4.8 (1M context) --- .../compatible-endpoint-context.test.ts | 42 ++++++++++++++++- .../inference/compatible-endpoint-context.ts | 46 ++++++++++++++++--- .../compatible-endpoint-context-probe.test.ts | 29 ++++++------ 3 files changed, 95 insertions(+), 22 deletions(-) diff --git a/src/lib/inference/compatible-endpoint-context.test.ts b/src/lib/inference/compatible-endpoint-context.test.ts index 246e2f1ca68..f6c74a80ae3 100644 --- a/src/lib/inference/compatible-endpoint-context.test.ts +++ b/src/lib/inference/compatible-endpoint-context.test.ts @@ -106,6 +106,28 @@ describe("compatible-endpoint context window", () => { expect(messages.some((m) => m.includes("Keeping configured context window"))).toBe(true); }); + it.each([ + "0", + "abc", + "-5", + "9999999999", + ])("ignores the invalid NEMOCLAW_CONTEXT_WINDOW override %j and auto-detects instead (#6293)", (badValue) => { + const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 32_768 }] })); + const { env, messages } = apply({ fetchModels }, { NEMOCLAW_CONTEXT_WINDOW: badValue }); + + expect(fetchModels).toHaveBeenCalled(); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("32768"); + expect(messages.some((m) => m.includes("Ignoring invalid NEMOCLAW_CONTEXT_WINDOW"))).toBe(true); + }); + + it("clears an invalid explicit override when the endpoint also cannot be probed (#6293)", () => { + const fetchModels = vi.fn(() => null); + const { env } = apply({ fetchModels }, { NEMOCLAW_CONTEXT_WINDOW: "0" }); + + expect(fetchModels).toHaveBeenCalled(); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + }); + it("warns and keeps the default context window when the endpoint cannot be probed", () => { const fetchModels = vi.fn(() => null); const { env, messages } = apply({ fetchModels }); @@ -163,11 +185,10 @@ describe("compatible-endpoint context window", () => { it.each([ "http://10.0.0.1/v1", - "http://127.0.0.1/v1", "http://169.254.169.254/v1", "http://172.16.0.1/v1", "http://192.168.1.1/v1", - ])("rejects the private-IP endpoint %s before probing /v1/models SSRF (#6293)", (endpointUrl) => { + ])("rejects the non-loopback private-IP endpoint %s before probing /v1/models SSRF (#6293)", (endpointUrl) => { const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); const messages: string[] = []; const env: NodeJS.ProcessEnv = {}; @@ -182,6 +203,23 @@ describe("compatible-endpoint context window", () => { expect(messages.some((m) => m.includes("private/internal address"))).toBe(true); }); + it.each([ + "http://127.0.0.1:8000/v1", + "http://localhost:8000/v1", + "http://[::1]:8000/v1", + ])("probes a loopback endpoint %s and propagates its max_model_len (#6293)", (endpointUrl) => { + const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); + const env: NodeJS.ProcessEnv = {}; + applyCompatibleEndpointContextWindow(endpointUrl, "model-a", { + env, + fetchModels, + logger: { log: () => undefined, warn: () => undefined }, + }); + + expect(fetchModels).toHaveBeenCalled(); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + }); + it("clears a stale auto value when re-probing a now private-IP endpoint SSRF (#6293)", () => { const env: NodeJS.ProcessEnv = {}; // First endpoint auto-detects a window into the shared env. diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts index f299f1f2274..4898c17125b 100644 --- a/src/lib/inference/compatible-endpoint-context.ts +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -4,10 +4,18 @@ import { createOpenAiLikeAuthConfig } from "../adapters/http/auth-config"; import { runCurlProbe } from "../adapters/http/probe"; import { getCredential } from "../credentials/store"; -import { isPrivateHostname } from "../private-networks"; -import { hasExplicitContextWindow } from "./ollama-runtime-context"; +import { isLoopbackHostname, isPrivateHostname } from "../private-networks"; +import { + hasExplicitContextWindow, + MAX_AUTODETECTED_OLLAMA_CONTEXT_WINDOW, + parsePositiveInteger, +} from "./ollama-runtime-context"; import { resolveVllmContextWindowFromModels } from "./vllm-runtime-context"; +// Explicit NEMOCLAW_CONTEXT_WINDOW overrides share the auto-detect ceiling so a +// user-supplied window can't bake an implausible value the probed path rejects. +const MAX_COMPATIBLE_CONTEXT_WINDOW = MAX_AUTODETECTED_OLLAMA_CONTEXT_WINDOW; + // Hosts that only resolve inside the OpenShell sandbox network (or are the // hijacked docker-internal alias), mirroring probeOpenAiLikeEndpoint's // SANDBOX_INTERNAL_HOSTS / isHijackedDockerInternalUrl. A host-side GET to these @@ -27,11 +35,20 @@ function isHostProbeableEndpoint(endpointUrl: string): boolean { // address (and not one of the allowed sandbox-internal aliases, which are // screened separately by isHostProbeableEndpoint). Reuses the shared // isPrivateHostname validator so the /v1/models context probe never issues a -// host-side GET to an attacker-reachable private address. See PR #6293 PRA-1. +// host-side GET to an attacker-reachable private address. +// +// Loopback (127.0.0.0/8, ::1, localhost) is exempt, mirroring the same +// exemption in probeOpenAiLikeEndpoint: a locally-run vLLM/Ollama custom +// endpoint is reached host-side on loopback, and loopback only targets the +// probing host itself, not a pivot to other internal infrastructure. Without +// this the two probes disagreed — the chat-completions validation probe +// allowed the localhost endpoint but this context probe skipped it, so a +// localhost vLLM never propagated its max_model_len (#6177). See PR #6293 +// PRA-5 / PRA-19. function isPrivateEndpoint(endpointUrl: string): boolean { try { const { hostname } = new URL(endpointUrl); - return isPrivateHostname(hostname); + return isPrivateHostname(hostname) && !isLoopbackHostname(hostname); } catch { return false; } @@ -186,8 +203,25 @@ export function applyCompatibleEndpointContextWindow( }; if (hasExplicitContextWindow(userContextWindow)) { - logger.log(` ℹ Keeping configured context window: ${userContextWindow} tokens`); - return; + // hasExplicitContextWindow only checks non-emptiness, so a malformed value + // ("0", "abc", or one above the auto-detect ceiling) would otherwise be + // kept verbatim and baked into config. Validate it the same way the probed + // path validates a discovered max_model_len; on an invalid override, warn + // and fall through to auto-detect instead of honoring an unusable value. + // See PR #6293 PRA-4 / PRA-7 (Nemotron). + const parsedOverride = parsePositiveInteger(userContextWindow); + if (parsedOverride && parsedOverride <= MAX_COMPATIBLE_CONTEXT_WINDOW) { + logger.log(` ℹ Keeping configured context window: ${parsedOverride} tokens`); + return; + } + logger.warn( + ` ⚠ Ignoring invalid NEMOCLAW_CONTEXT_WINDOW="${userContextWindow}"; it must be a ` + + `positive integer ≤ ${MAX_COMPATIBLE_CONTEXT_WINDOW}. Auto-detecting from the endpoint.`, + ); + // Drop the unusable override so a failed/skipped probe can't leave it to be + // baked downstream; auto-detect below re-populates it when the endpoint + // reports a valid max_model_len. + delete env.NEMOCLAW_CONTEXT_WINDOW; } // A sandbox-internal endpoint (e.g. host.openshell.internal) resolves only diff --git a/test/compatible-endpoint-context-probe.test.ts b/test/compatible-endpoint-context-probe.test.ts index 0d3093e5d23..7bddb8013bb 100644 --- a/test/compatible-endpoint-context-probe.test.ts +++ b/test/compatible-endpoint-context-probe.test.ts @@ -21,12 +21,13 @@ import { testTimeout } from "./helpers/timeouts"; const MODEL = "nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4"; -// The fake server binds to loopback (127.0.0.1), which the SSRF source-boundary -// guard now rejects. To exercise the happy path — real curl against a live -// /v1/models — we present a routable public hostname to the guard while the -// injected fetcher still hits the real loopback server. The private-IP block -// itself is asserted by its own case below and by the unit tests in -// src/lib/inference/compatible-endpoint-context.test.ts. +// The fake server binds to loopback (127.0.0.1). Loopback is an allowed +// host-side probe target (a locally-run vLLM/Ollama endpoint), so these +// happy-path cases could use its URL directly; they present a routable public +// hostname to the guard and inject a fetcher to the loopback server to keep the +// remote-endpoint path exercised. Loopback probing against the real server is +// asserted by its own case below; non-loopback private-IP rejection is covered +// by the unit tests in src/lib/inference/compatible-endpoint-context.test.ts. const PUBLIC_ENDPOINT_URL = "https://vllm.public.test/v1"; let server: FakeOpenAiCompatibleServer | null = null; @@ -115,14 +116,14 @@ describe("compatible-endpoint context probe against a real server (#6177)", { ).toBe(true); }); - it("refuses to probe a private-IP endpoint before issuing any /v1/models request SSRF (#6293)", async () => { - // The fake server binds to 127.0.0.1 — a real private/loopback address. - // The source-boundary guard must reject it before the real curl fetcher - // runs, so the server records zero requests and no window is set. + it("probes a real loopback endpoint and propagates its max_model_len (#6293)", async () => { + // The fake server binds to 127.0.0.1 — a loopback address. A locally-run + // vLLM/Ollama custom endpoint is legitimately reached host-side on loopback, + // so the source-boundary guard exempts loopback (mirroring the chat probe) + // and the real curl fetcher must run and propagate the window. Non-loopback + // private targets stay blocked — see the unit-test rejection cases. server = await startFakeOpenAiCompatibleServer({ model: MODEL, maxModelLen: 65_536 }); expect(new URL(server.baseUrl).hostname).toBe("127.0.0.1"); - // The readiness probe already issued one /v1/models GET; the guard must not - // add another, so we compare the count before and after. const modelsRequestsBefore = server .requests() .filter((entry) => entry.path === "/v1/models").length; @@ -136,8 +137,8 @@ describe("compatible-endpoint context probe against a real server (#6177)", { const modelsRequestsAfter = server .requests() .filter((entry) => entry.path === "/v1/models").length; - expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); - expect(modelsRequestsAfter).toBe(modelsRequestsBefore); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + expect(modelsRequestsAfter).toBeGreaterThan(modelsRequestsBefore); }); it("keeps the default context window when the endpoint omits max_model_len (#6177)", async () => { From e77887ec64be3e82b951006bb4c308240587ac30 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Mon, 6 Jul 2026 12:16:52 -0700 Subject: [PATCH 14/41] fix(security): close DNS-backed SSRF in custom-endpoint onboarding probes (#6293) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GPT-5.5 advisor PRA-4: the private/internal guards inspected only the URL hostname string, so a public-looking name that resolves to 127.0.0.1, 169.254.169.254, or RFC1918 space could make the privileged onboarding host curl an internal service before the sandbox/OpenShell policy exist. Add an injectable resolver-based preflight (assertEndpointResolvesPublic): resolve the endpoint host and fail closed when it — or any resolved address — is private/reserved, before any host-side probe curl. Explicit loopback hosts stay allowed (local inference); a public name that *resolves* to loopback is refused as rebinding. Wired into both custom validators (validateCustomOpenAiLikeSelection / validateCustomAnthropicSelection), which gate the validation probe and the downstream /v1/models fetch. Skips under the unit-test runner unless a resolver is injected (mirrors the existing VITEST fetch guard); production always runs the real dns/promises resolver. Also close Nemotron PRA-4: dockerfile-patch validated NEMOCLAW_CONTEXT_WINDOW format but not the ceiling, so an oversized value could bake into the image ARG — now bounded by the auto-detect ceiling. Signed-off-by: Prekshi Vyas Co-Authored-By: Claude Opus 4.8 (1M context) --- .../inference/endpoint-ssrf-preflight.test.ts | 76 ++++++++++++++++ src/lib/inference/endpoint-ssrf-preflight.ts | 87 +++++++++++++++++++ .../dockerfile-patch-hermes-context.test.ts | 7 ++ src/lib/onboard/dockerfile-patch.ts | 11 ++- .../inference-selection-validation.test.ts | 82 +++++++++++++++++ .../onboard/inference-selection-validation.ts | 60 +++++++++++++ 6 files changed, 322 insertions(+), 1 deletion(-) create mode 100644 src/lib/inference/endpoint-ssrf-preflight.test.ts create mode 100644 src/lib/inference/endpoint-ssrf-preflight.ts diff --git a/src/lib/inference/endpoint-ssrf-preflight.test.ts b/src/lib/inference/endpoint-ssrf-preflight.test.ts new file mode 100644 index 00000000000..cab9d068895 --- /dev/null +++ b/src/lib/inference/endpoint-ssrf-preflight.test.ts @@ -0,0 +1,76 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it, vi } from "vitest"; + +import { assertEndpointResolvesPublic, type EndpointDnsLookupFn } from "./endpoint-ssrf-preflight"; + +const resolverTo = (address: string): EndpointDnsLookupFn => + vi.fn(async () => [{ address, family: address.includes(":") ? 6 : 4 }]); + +describe("assertEndpointResolvesPublic (#6293)", () => { + it("allows a public hostname that resolves to a public address without ever needing a private check", async () => { + const lookup = resolverTo("93.184.216.34"); + const result = await assertEndpointResolvesPublic("https://vllm.example/v1", lookup); + expect(result.ok).toBe(true); + expect(lookup).toHaveBeenCalledWith("vllm.example", { all: true }); + }); + + it.each([ + "10.0.0.8", + "169.254.169.254", + "192.168.1.10", + "172.16.0.5", + "127.0.0.1", + ])("refuses a public hostname that resolves to the private/reserved address %s (#6293)", async (privateAddress) => { + const lookup = resolverTo(privateAddress); + const result = await assertEndpointResolvesPublic("https://public-name.example/v1", lookup); + expect(result.ok).toBe(false); + expect(result.reason).toContain(privateAddress); + }); + + it("refuses a literal private endpoint before resolving anything (#6293)", async () => { + const lookup = vi.fn(); + const result = await assertEndpointResolvesPublic("http://10.0.0.1/v1", lookup); + expect(result.ok).toBe(false); + expect(lookup).not.toHaveBeenCalled(); + }); + + it.each([ + "http://127.0.0.1:8000/v1", + "http://localhost:8000/v1", + "http://[::1]:8000/v1", + ])("allows the explicit loopback endpoint %s without resolving (#6293)", async (endpointUrl) => { + const lookup = vi.fn(); + const result = await assertEndpointResolvesPublic(endpointUrl, lookup); + expect(result.ok).toBe(true); + expect(lookup).not.toHaveBeenCalled(); + }); + + it("allows a public IP literal without resolving (#6293)", async () => { + const lookup = vi.fn(); + const result = await assertEndpointResolvesPublic("https://93.184.216.34/v1", lookup); + expect(result.ok).toBe(true); + expect(lookup).not.toHaveBeenCalled(); + }); + + it("fails closed when the resolver throws (#6293)", async () => { + const lookup: EndpointDnsLookupFn = vi.fn(async () => { + throw new Error("ENOTFOUND"); + }); + const result = await assertEndpointResolvesPublic("https://unresolvable.example/v1", lookup); + expect(result.ok).toBe(false); + expect(result.reason).toContain("cannot resolve"); + }); + + it("fails closed when the resolver returns no addresses (#6293)", async () => { + const lookup: EndpointDnsLookupFn = vi.fn(async () => []); + const result = await assertEndpointResolvesPublic("https://empty.example/v1", lookup); + expect(result.ok).toBe(false); + }); + + it("refuses a malformed endpoint URL (#6293)", async () => { + const result = await assertEndpointResolvesPublic("not a url", resolverTo("93.184.216.34")); + expect(result.ok).toBe(false); + }); +}); diff --git a/src/lib/inference/endpoint-ssrf-preflight.ts b/src/lib/inference/endpoint-ssrf-preflight.ts new file mode 100644 index 00000000000..616cc0446ed --- /dev/null +++ b/src/lib/inference/endpoint-ssrf-preflight.ts @@ -0,0 +1,87 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { lookup as dnsLookup } from "node:dns/promises"; +import { isIP } from "node:net"; + +import { isLoopbackHostname, isPrivateHostname, isPrivateIp } from "../private-networks"; + +/** Injectable DNS resolver, shaped like `dns/promises` `lookup(host, {all:true})`. */ +export type EndpointDnsLookupFn = ( + hostname: string, + options: { all: true }, +) => Promise>; + +export interface EndpointSsrfPreflightResult { + ok: boolean; + /** Human-readable reason, present only when `ok === false`. */ + reason?: string; +} + +/** + * DNS-backed SSRF preflight for a user-supplied inference endpoint, run before + * any privileged host-side curl during onboarding. + * + * The string-level `isPrivateHostname` guards elsewhere block literal private + * IPs and reserved names, but a public-looking name (`https://vllm.example/v1`) + * can still resolve to `127.0.0.1`, `169.254.169.254`, or RFC1918 space and make + * the onboarding host contact internal services before the sandbox and its + * OpenShell network policy exist. This resolves the hostname first and refuses + * when it — or any resolved address — is private/reserved. It complements the + * authoritative config-write DNS-pinning boundary (`validateUrlValueWithDnsResult`) + * which runs later, before the URL is persisted. + * + * Loopback (127.0.0.0/8, ::1, localhost) is exempt ONLY when the endpoint + * hostname is itself loopback — a locally-run vLLM/Ollama server the user + * explicitly configured. A public name that *resolves* to loopback is treated + * as a rebinding attempt and refused. The resolver is injectable for tests and + * the check fails closed on resolver error or an empty result. + * + * See PR #6293 PRA-4 (GPT-5.5 advisor). + */ +export async function assertEndpointResolvesPublic( + endpointUrl: string, + lookup: EndpointDnsLookupFn = dnsLookup as unknown as EndpointDnsLookupFn, +): Promise { + let hostname: string; + try { + hostname = new URL(String(endpointUrl)).hostname; + } catch { + return { ok: false, reason: `"${String(endpointUrl)}" is not a valid URL` }; + } + + // An explicit loopback host is a legitimate local inference server. + if (isLoopbackHostname(hostname)) return { ok: true }; + + // A literal private IP or reserved private name is refused without resolving. + if (isPrivateHostname(hostname)) { + return { ok: false, reason: `endpoint host "${hostname}" is a private/internal address` }; + } + + // A public IP literal needs no DNS resolution. + const bare = + hostname.startsWith("[") && hostname.endsWith("]") ? hostname.slice(1, -1) : hostname; + if (isIP(bare)) return { ok: true }; + + let addresses: Array<{ address: string; family?: number }>; + try { + addresses = await lookup(bare, { all: true }); + } catch (err) { + const message = err instanceof Error ? err.message : String(err); + return { ok: false, reason: `cannot resolve endpoint host "${hostname}": ${message}` }; + } + if (!Array.isArray(addresses) || addresses.length === 0) { + return { ok: false, reason: `endpoint host "${hostname}" did not resolve to any address` }; + } + for (const { address } of addresses) { + // A resolved private address — including loopback reached via a public name + // (DNS rebinding) — is refused; the explicit-loopback case returned above. + if (isPrivateIp(address)) { + return { + ok: false, + reason: `endpoint host "${hostname}" resolves to private/internal address "${address}"`, + }; + } + } + return { ok: true }; +} diff --git a/src/lib/onboard/dockerfile-patch-hermes-context.test.ts b/src/lib/onboard/dockerfile-patch-hermes-context.test.ts index 367ba8e2c73..131605653d4 100644 --- a/src/lib/onboard/dockerfile-patch-hermes-context.test.ts +++ b/src/lib/onboard/dockerfile-patch-hermes-context.test.ts @@ -78,4 +78,11 @@ describe("patchStagedDockerfile :: Hermes NEMOCLAW_CONTEXT_WINDOW (#6177)", () = patchHermes(dockerfilePath); expect(contextWindowArg(dockerfilePath)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW="); }); + + it("ignores an over-ceiling context window instead of baking an implausible value (#6293)", () => { + const dockerfilePath = stageHermesDockerfile(); + vi.stubEnv("NEMOCLAW_CONTEXT_WINDOW", "9999999999"); + patchHermes(dockerfilePath); + expect(contextWindowArg(dockerfilePath)).toBe("ARG NEMOCLAW_CONTEXT_WINDOW="); + }); }); diff --git a/src/lib/onboard/dockerfile-patch.ts b/src/lib/onboard/dockerfile-patch.ts index dbf59aba1b6..70e26e8217f 100644 --- a/src/lib/onboard/dockerfile-patch.ts +++ b/src/lib/onboard/dockerfile-patch.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { getSandboxInferenceConfig } from "../inference/config"; +import { MAX_AUTODETECTED_OLLAMA_CONTEXT_WINDOW } from "../inference/ollama-runtime-context"; import { isWebSearchEnabled, type WebSearchConfig, @@ -175,7 +176,15 @@ export function patchStagedDockerfile( // Honor NEMOCLAW_CONTEXT_WINDOW / NEMOCLAW_MAX_TOKENS / NEMOCLAW_REASONING // so the user can tune model metadata without editing the Dockerfile. const contextWindow = process.env.NEMOCLAW_CONTEXT_WINDOW; - if (contextWindow && POSITIVE_INT_RE.test(contextWindow)) { + // Validate the ceiling as well as the format: POSITIVE_INT_RE alone would let + // an implausibly large value (which the auto-detect/probe paths reject) bake + // into the image ARG. Match the auto-detect ceiling. See PR #6293 PRA-4 + // (Nemotron). + if ( + contextWindow && + POSITIVE_INT_RE.test(contextWindow) && + Number(contextWindow) <= MAX_AUTODETECTED_OLLAMA_CONTEXT_WINDOW + ) { dockerfile = dockerfile.replace( /^ARG NEMOCLAW_CONTEXT_WINDOW=.*$/m, `ARG NEMOCLAW_CONTEXT_WINDOW=${sanitizeDockerArg(contextWindow)}`, diff --git a/src/lib/onboard/inference-selection-validation.test.ts b/src/lib/onboard/inference-selection-validation.test.ts index 46fcfe3deab..82b2a91f590 100644 --- a/src/lib/onboard/inference-selection-validation.test.ts +++ b/src/lib/onboard/inference-selection-validation.test.ts @@ -86,4 +86,86 @@ describe("inference selection validation", () => { vi.unstubAllEnvs(); } }); + + it("refuses a custom OpenAI-like endpoint that resolves to a private address before probing (#6293)", async () => { + const probeOpenAiLikeEndpoint = vi.fn(() => ({ ok: true, api: "openai-completions" })); + const promptValidationRecovery = vi.fn(async () => "selection" as const); + const error = vi.spyOn(console, "error").mockImplementation(() => {}); + const helpers = createInferenceSelectionValidationHelpers({ + isNonInteractive: () => false, + agentProductName: () => "OpenClaw", + getCredential: () => "test-key", + probeOpenAiLikeEndpoint, + promptValidationRecovery, + resolveEndpointHost: async () => [{ address: "10.0.0.8", family: 4 }], + }); + + try { + await expect( + helpers.validateCustomOpenAiLikeSelection( + "Custom endpoint", + "https://public-name.example/v1", + "model-a", + "COMPATIBLE_API_KEY", + ), + ).resolves.toEqual({ ok: false, retry: "selection" }); + expect(probeOpenAiLikeEndpoint).not.toHaveBeenCalled(); + } finally { + error.mockRestore(); + } + }); + + it("exits non-interactively when a custom Anthropic endpoint resolves to link-local metadata, without probing (#6293)", async () => { + const originalExitCode = process.exitCode; + const probeAnthropicEndpoint = vi.fn(); + const exit = vi.spyOn(process, "exit").mockImplementation((() => undefined) as never); + const error = vi.spyOn(console, "error").mockImplementation(() => {}); + const helpers = createInferenceSelectionValidationHelpers({ + isNonInteractive: () => true, + agentProductName: () => "OpenClaw", + getCredential: () => "test-key", + probeAnthropicEndpoint, + promptValidationRecovery: vi.fn(async () => "selection" as const), + resolveEndpointHost: async () => [{ address: "169.254.169.254", family: 4 }], + }); + + try { + await expect( + helpers.validateCustomAnthropicSelection( + "Custom Anthropic", + "https://metadata-name.example/v1", + "model-a", + "COMPATIBLE_ANTHROPIC_API_KEY", + ), + ).rejects.toThrow("Non-interactive endpoint validation failed."); + expect(probeAnthropicEndpoint).not.toHaveBeenCalled(); + expect(exit).toHaveBeenCalledWith(1); + } finally { + process.exitCode = originalExitCode; + exit.mockRestore(); + error.mockRestore(); + } + }); + + it("probes a custom endpoint that resolves to a public address (#6293)", async () => { + const probeOpenAiLikeEndpoint = vi.fn(() => ({ ok: true, api: "openai-completions" })); + const helpers = createInferenceSelectionValidationHelpers({ + isNonInteractive: () => false, + agentProductName: () => "OpenClaw", + getCredential: () => "test-key", + probeOpenAiLikeEndpoint, + promptValidationRecovery: vi.fn(async () => "selection" as const), + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], + }); + + await expect( + helpers.validateCustomOpenAiLikeSelection( + "Custom endpoint", + "https://vllm.public.test/v1", + "model-a", + "COMPATIBLE_API_KEY", + ), + ).resolves.toEqual({ ok: true, api: "openai-completions" }); + expect(probeOpenAiLikeEndpoint).toHaveBeenCalled(); + }); }); diff --git a/src/lib/onboard/inference-selection-validation.ts b/src/lib/onboard/inference-selection-validation.ts index b65f4d2a90d..be716a1c3cb 100644 --- a/src/lib/onboard/inference-selection-validation.ts +++ b/src/lib/onboard/inference-selection-validation.ts @@ -18,6 +18,10 @@ const { probeAnthropicEndpoint, probeOpenAiLikeEndpoint } = ): any; }; +import { + assertEndpointResolvesPublic, + type EndpointDnsLookupFn, +} from "../inference/endpoint-ssrf-preflight"; import { shouldForceCompletionsApi } from "../validation"; import { getProbeRecovery } from "../validation-recovery"; import { summarizeProbeForDisplay } from "./probe-diagnostics"; @@ -33,6 +37,8 @@ export interface InferenceSelectionValidationDeps { getCredential?: typeof getCredential; probeAnthropicEndpoint?: typeof probeAnthropicEndpoint; probeOpenAiLikeEndpoint?: typeof probeOpenAiLikeEndpoint; + /** Injectable DNS resolver for the custom-endpoint SSRF preflight (tests). */ + resolveEndpointHost?: EndpointDnsLookupFn; promptValidationRecovery( label: string, recovery: ReturnType, @@ -104,6 +110,56 @@ export function createInferenceSelectionValidationHelpers( console.error(" Validation details were omitted to avoid exposing credentials."); } + // DNS-backed SSRF preflight for user-supplied custom endpoints. Resolves the + // endpoint host and fails closed before any host-side probe curl when it (or + // a resolved address) is private/reserved, so a public-looking name that + // resolves to loopback/link-local/RFC1918 cannot reach internal services + // during privileged onboarding. Returns a fail-closed EndpointValidationResult + // to short-circuit the caller, or null when the endpoint is safe to probe. + // See PR #6293 PRA-4. + async function preflightCustomEndpointOrFail( + label: string, + endpointUrl: string, + credentialEnv: string | null, + helpUrl: string | null, + ): Promise { + // Under the unit-test runner the default resolver would hit real DNS (and + // fixture hostnames do not resolve); skip unless a resolver is injected, + // mirroring applyCompatibleEndpointContextWindow's VITEST fetch guard. In + // production (VITEST unset) the real dns/promises resolver always runs. + if (!deps.resolveEndpointHost && process.env.VITEST === "true") return null; + const preflight = await assertEndpointResolvesPublic(endpointUrl, deps.resolveEndpointHost); + if (preflight.ok) return null; + const syntheticProbe = { + ok: false as const, + message: preflight.reason, + failures: [ + { + name: "SSRF preflight", + httpStatus: 0, + curlStatus: 0, + message: preflight.reason ?? "endpoint resolves to a private/internal address", + body: "", + }, + ], + }; + printValidationFailure(label, syntheticProbe); + if (deps.isNonInteractive()) { + exitNonInteractiveValidationFailure(); + } + const retry = await deps.promptValidationRecovery( + label, + getProbeRecovery(syntheticProbe), + credentialEnv, + helpUrl, + ); + if (retry === "selection") { + console.log(" Please choose a provider/model again."); + console.log(""); + } + return { ok: false, retry }; + } + async function validateOpenAiLikeSelection( label: string, endpointUrl: string, @@ -185,6 +241,8 @@ export function createInferenceSelectionValidationHelpers( credentialEnv: string, helpUrl: string | null = null, ): Promise { + const blocked = await preflightCustomEndpointOrFail(label, endpointUrl, credentialEnv, helpUrl); + if (blocked) return blocked; const apiKey = resolveCredential(credentialEnv); const reasoningEnabled = normalizeReasoningFlag(process.env.NEMOCLAW_REASONING) === "true"; // Reasoning-only compatible endpoints often reject Responses, tool-call, and streaming probes. @@ -228,6 +286,8 @@ export function createInferenceSelectionValidationHelpers( credentialEnv: string, helpUrl: string | null = null, ): Promise { + const blocked = await preflightCustomEndpointOrFail(label, endpointUrl, credentialEnv, helpUrl); + if (blocked) return blocked; const apiKey = resolveCredential(credentialEnv); const probe = runAnthropicProbe(endpointUrl, model, apiKey); if (probe.ok) { From c01fafe9b9c684fd51d914d6c155b79194ae2016 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Mon, 6 Jul 2026 12:29:12 -0700 Subject: [PATCH 15/41] chore: re-trigger PR CI/advisor for #6293 after DNS-preflight push The prior push (e77887ec6) did not fire the CI / Pull Request and PR Review / Advisor workflows (missed pull_request.synchronize); this empty commit re-triggers them so the advisor re-evaluates the DNS preflight + context-window fixes. Signed-off-by: Prekshi Vyas From 9bd6754b0e1da8cc999e17da2470b40b4ae9ab4e Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Mon, 6 Jul 2026 13:32:17 -0700 Subject: [PATCH 16/41] fix(security): pin the DNS SSRF preflight to the /v1/models curl boundary (#6293) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GPT-5.5 advisor PRA-3 (follow-up): the assertEndpointResolvesPublic preflight was gating the untrusted custom-endpoint path at the validateCustom* orchestration layer, but the /v1/models context curl in applyCompatibleEndpointContextWindow is its own independent host-side boundary. Pin the resolver preflight there too: applyCompatibleEndpointContextWindow is now async and resolves+refuses a public name that maps to loopback/link-local/ RFC1918 before the fetch (injectable resolver; skipped only when a fake fetcher is injected without a resolver, so unit tests never issue real DNS; production always resolves). Its single caller (setup-nim-selection) awaits it. probeOpenAiLikeEndpoint stays synchronous (many callers, no async boundary): its untrusted caller path already runs the preflight upstream in validateCustom*, and the literal string check remains as the local layer — documented inline. Adds unit coverage for the inline preflight (public host resolving to 10.0.0.8/169.254.169.254 refused before fetch; public-resolving host probed). Signed-off-by: Prekshi Vyas Co-Authored-By: Claude Opus 4.8 (1M context) --- .../compatible-endpoint-context.test.ts | 130 +++++++++++------- .../inference/compatible-endpoint-context.ts | 32 ++++- src/lib/inference/onboard-probes.ts | 11 ++ src/lib/onboard/setup-nim-selection.ts | 2 +- .../compatible-endpoint-context-probe.test.ts | 12 +- 5 files changed, 131 insertions(+), 56 deletions(-) diff --git a/src/lib/inference/compatible-endpoint-context.test.ts b/src/lib/inference/compatible-endpoint-context.test.ts index f6c74a80ae3..b33a097a82d 100644 --- a/src/lib/inference/compatible-endpoint-context.test.ts +++ b/src/lib/inference/compatible-endpoint-context.test.ts @@ -13,12 +13,12 @@ beforeEach(() => { resetCompatibleEndpointContextWindowAutoState(); }); -function apply( +async function apply( options: Parameters[2], env: NodeJS.ProcessEnv = {}, -): { env: NodeJS.ProcessEnv; messages: string[] } { +): Promise<{ env: NodeJS.ProcessEnv; messages: string[] }> { const messages: string[] = []; - applyCompatibleEndpointContextWindow("https://endpoint.example/v1", "model-a", { + await applyCompatibleEndpointContextWindow("https://endpoint.example/v1", "model-a", { env, logger: { log: (message: string) => messages.push(message), @@ -30,18 +30,18 @@ function apply( } describe("compatible-endpoint context window", () => { - it("bakes the endpoint's max_model_len into NEMOCLAW_CONTEXT_WINDOW (#6177)", () => { + it("bakes the endpoint's max_model_len into NEMOCLAW_CONTEXT_WINDOW (#6177)", async () => { const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); - const { env, messages } = apply({ fetchModels }); + const { env, messages } = await apply({ fetchModels }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); expect(fetchModels).toHaveBeenCalledWith("https://endpoint.example/v1", ""); expect(messages.some((m) => m.includes("65536"))).toBe(true); }); - it("resolves the API key from the credential env for the probe", () => { + it("resolves the API key from the credential env for the probe", async () => { const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 32_768 }] })); - apply({ + await apply({ fetchModels, credentialEnv: "COMPATIBLE_API_KEY", resolveCredential: (name) => (name === "COMPATIBLE_API_KEY" ? "secret-key" : null), @@ -50,43 +50,43 @@ describe("compatible-endpoint context window", () => { expect(fetchModels).toHaveBeenCalledWith("https://endpoint.example/v1", "secret-key"); }); - it("picks the exact model entry from a multi-model gateway response (#6177)", () => { + it("picks the exact model entry from a multi-model gateway response (#6177)", async () => { const fetchModels = vi.fn(() => ({ data: [ { id: "other-model", max_model_len: 8_192 }, { id: "model-a", max_model_len: 65_536 }, ], })); - const { env } = apply({ fetchModels }); + const { env } = await apply({ fetchModels }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); }); - it("does not guess a context window from a multi-model gateway with no exact match (#6177)", () => { + it("does not guess a context window from a multi-model gateway with no exact match (#6177)", async () => { const fetchModels = vi.fn(() => ({ data: [ { id: "other-a", max_model_len: 8_192 }, { id: "other-b", max_model_len: 16_384 }, ], })); - const { env, messages } = apply({ fetchModels }); + const { env, messages } = await apply({ fetchModels }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); expect(messages.some((m) => m.includes("none match 'model-a'"))).toBe(true); }); - it("uses the sole served model even when its id does not match (single-model endpoint)", () => { + it("uses the sole served model even when its id does not match (single-model endpoint)", async () => { const fetchModels = vi.fn(() => ({ data: [{ id: "served-alias", max_model_len: 32_768 }] })); - const { env } = apply({ fetchModels }); + const { env } = await apply({ fetchModels }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("32768"); }); - it("skips the probe for a sandbox-internal endpoint and leaves auto-detect (#6177)", () => { + it("skips the probe for a sandbox-internal endpoint and leaves auto-detect (#6177)", async () => { const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); const messages: string[] = []; const env: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow("https://host.openshell.internal/v1", "model-a", { + await applyCompatibleEndpointContextWindow("https://host.openshell.internal/v1", "model-a", { env, fetchModels, logger: { log: (m) => messages.push(m), warn: (m) => messages.push(m) }, @@ -97,9 +97,9 @@ describe("compatible-endpoint context window", () => { expect(messages).toEqual([]); }); - it("never downgrades an explicit NEMOCLAW_CONTEXT_WINDOW override (#6177)", () => { + it("never downgrades an explicit NEMOCLAW_CONTEXT_WINDOW override (#6177)", async () => { const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 8_192 }] })); - const { env, messages } = apply({ fetchModels }, { NEMOCLAW_CONTEXT_WINDOW: "65536" }); + const { env, messages } = await apply({ fetchModels }, { NEMOCLAW_CONTEXT_WINDOW: "65536" }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); expect(fetchModels).not.toHaveBeenCalled(); @@ -111,63 +111,63 @@ describe("compatible-endpoint context window", () => { "abc", "-5", "9999999999", - ])("ignores the invalid NEMOCLAW_CONTEXT_WINDOW override %j and auto-detects instead (#6293)", (badValue) => { + ])("ignores the invalid NEMOCLAW_CONTEXT_WINDOW override %j and auto-detects instead (#6293)", async (badValue) => { const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 32_768 }] })); - const { env, messages } = apply({ fetchModels }, { NEMOCLAW_CONTEXT_WINDOW: badValue }); + const { env, messages } = await apply({ fetchModels }, { NEMOCLAW_CONTEXT_WINDOW: badValue }); expect(fetchModels).toHaveBeenCalled(); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("32768"); expect(messages.some((m) => m.includes("Ignoring invalid NEMOCLAW_CONTEXT_WINDOW"))).toBe(true); }); - it("clears an invalid explicit override when the endpoint also cannot be probed (#6293)", () => { + it("clears an invalid explicit override when the endpoint also cannot be probed (#6293)", async () => { const fetchModels = vi.fn(() => null); - const { env } = apply({ fetchModels }, { NEMOCLAW_CONTEXT_WINDOW: "0" }); + const { env } = await apply({ fetchModels }, { NEMOCLAW_CONTEXT_WINDOW: "0" }); expect(fetchModels).toHaveBeenCalled(); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); }); - it("warns and keeps the default context window when the endpoint cannot be probed", () => { + it("warns and keeps the default context window when the endpoint cannot be probed", async () => { const fetchModels = vi.fn(() => null); - const { env, messages } = apply({ fetchModels }); + const { env, messages } = await apply({ fetchModels }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); expect(messages.some((m) => m.includes("Could not read the endpoint's /v1/models"))).toBe(true); }); - it("skips the real host fetch under the unit-test runner unless one is injected", () => { - const { env, messages } = apply({}, { VITEST: "true" }); + it("skips the real host fetch under the unit-test runner unless one is injected", async () => { + const { env, messages } = await apply({}, { VITEST: "true" }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); expect(messages).toEqual([]); }); - it("clears its own stale auto value when a re-probed endpoint reports nothing (#6177)", () => { + it("clears its own stale auto value when a re-probed endpoint reports nothing (#6177)", async () => { // First endpoint auto-detects 65536 into the shared env. const env: NodeJS.ProcessEnv = {}; - apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, env); + await apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, env); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); // A later selection pass probes an endpoint that reports no max_model_len: // the stale auto value must not survive (would look like a user override). - apply({ fetchModels: () => ({ data: [{ id: "model-a" }] }) }, env); + await apply({ fetchModels: () => ({ data: [{ id: "model-a" }] }) }, env); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); }); - it("recomputes over its own prior auto value on a re-probe (#6177)", () => { + it("recomputes over its own prior auto value on a re-probe (#6177)", async () => { const env: NodeJS.ProcessEnv = {}; - apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, env); - apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 16_384 }] }) }, env); + await apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, env); + await apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 16_384 }] }) }, env); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("16384"); }); - it("keeps a genuine user override even after a prior auto value was recorded (#6177)", () => { + it("keeps a genuine user override even after a prior auto value was recorded (#6177)", async () => { const env: NodeJS.ProcessEnv = {}; - apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, env); + await apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, env); // User pins a different value; a later probe must not overwrite it. env.NEMOCLAW_CONTEXT_WINDOW = "200000"; - const { messages } = apply( + const { messages } = await apply( { fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 16_384 }] }) }, env, ); @@ -175,11 +175,11 @@ describe("compatible-endpoint context window", () => { expect(messages.some((m) => m.includes("Keeping configured context window"))).toBe(true); }); - it("does not crash on a malformed /v1/models body from an arbitrary endpoint (#6177)", () => { + it("does not crash on a malformed /v1/models body from an arbitrary endpoint (#6177)", async () => { const env: NodeJS.ProcessEnv = {}; - expect(() => - apply({ fetchModels: () => ({ data: [null, "nope", 42] }) as unknown as object }, env), - ).not.toThrow(); + // apply resolves (never throws) even on a malformed body; reaching the + // assertion proves it did not crash. + await apply({ fetchModels: () => ({ data: [null, "nope", 42] }) as unknown as object }, env); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); }); @@ -188,11 +188,11 @@ describe("compatible-endpoint context window", () => { "http://169.254.169.254/v1", "http://172.16.0.1/v1", "http://192.168.1.1/v1", - ])("rejects the non-loopback private-IP endpoint %s before probing /v1/models SSRF (#6293)", (endpointUrl) => { + ])("rejects the non-loopback private-IP endpoint %s before probing /v1/models SSRF (#6293)", async (endpointUrl) => { const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); const messages: string[] = []; const env: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(endpointUrl, "model-a", { + await applyCompatibleEndpointContextWindow(endpointUrl, "model-a", { env, fetchModels, logger: { log: (m) => messages.push(m), warn: (m) => messages.push(m) }, @@ -207,10 +207,10 @@ describe("compatible-endpoint context window", () => { "http://127.0.0.1:8000/v1", "http://localhost:8000/v1", "http://[::1]:8000/v1", - ])("probes a loopback endpoint %s and propagates its max_model_len (#6293)", (endpointUrl) => { + ])("probes a loopback endpoint %s and propagates its max_model_len (#6293)", async (endpointUrl) => { const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); const env: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(endpointUrl, "model-a", { + await applyCompatibleEndpointContextWindow(endpointUrl, "model-a", { env, fetchModels, logger: { log: () => undefined, warn: () => undefined }, @@ -220,10 +220,43 @@ describe("compatible-endpoint context window", () => { expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); }); - it("clears a stale auto value when re-probing a now private-IP endpoint SSRF (#6293)", () => { + it.each([ + "10.0.0.8", + "169.254.169.254", + ])("refuses the /v1/models probe when a public host resolves to private %s via the DNS preflight (#6293)", async (privateAddress) => { + const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); + const messages: string[] = []; + const env: NodeJS.ProcessEnv = {}; + await applyCompatibleEndpointContextWindow("https://public-name.example/v1", "model-a", { + env, + fetchModels, + resolveHost: async () => [{ address: privateAddress, family: 4 }], + logger: { log: (m) => messages.push(m), warn: (m) => messages.push(m) }, + }); + + expect(fetchModels).not.toHaveBeenCalled(); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + expect(messages.some((m) => m.includes(privateAddress))).toBe(true); + }); + + it("probes when the injected resolver returns a public address (#6293)", async () => { + const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 65_536 }] })); + const env: NodeJS.ProcessEnv = {}; + await applyCompatibleEndpointContextWindow("https://public-name.example/v1", "model-a", { + env, + fetchModels, + resolveHost: async () => [{ address: "93.184.216.34", family: 4 }], + logger: { log: () => undefined, warn: () => undefined }, + }); + + expect(fetchModels).toHaveBeenCalled(); + expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + }); + + it("clears a stale auto value when re-probing a now private-IP endpoint SSRF (#6293)", async () => { const env: NodeJS.ProcessEnv = {}; // First endpoint auto-detects a window into the shared env. - applyCompatibleEndpointContextWindow("https://public.example/v1", "model-a", { + await applyCompatibleEndpointContextWindow("https://public.example/v1", "model-a", { env, fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }), logger: { log: () => undefined, warn: () => undefined }, @@ -233,7 +266,7 @@ describe("compatible-endpoint context window", () => { // A later pass selects a private-IP endpoint: the probe must be refused and // the stale auto value dropped rather than left as a phantom user override. const fetchModels = vi.fn(() => ({ data: [{ id: "model-a", max_model_len: 8_192 }] })); - applyCompatibleEndpointContextWindow("http://10.0.0.1/v1", "model-a", { + await applyCompatibleEndpointContextWindow("http://10.0.0.1/v1", "model-a", { env, fetchModels, logger: { log: () => undefined, warn: () => undefined }, @@ -242,10 +275,13 @@ describe("compatible-endpoint context window", () => { expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); }); - it("clearAutoDetectedCompatibleContextWindow drops a stale auto value but keeps a user override (#6177)", () => { + it("clearAutoDetectedCompatibleContextWindow drops a stale auto value but keeps a user override (#6177)", async () => { // Auto-detected value is cleared when retrying away to another provider. const autoEnv: NodeJS.ProcessEnv = {}; - apply({ fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, autoEnv); + await apply( + { fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }) }, + autoEnv, + ); expect(autoEnv.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); clearAutoDetectedCompatibleContextWindow(autoEnv); expect(autoEnv.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts index 4898c17125b..27c368ba4c3 100644 --- a/src/lib/inference/compatible-endpoint-context.ts +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -5,6 +5,7 @@ import { createOpenAiLikeAuthConfig } from "../adapters/http/auth-config"; import { runCurlProbe } from "../adapters/http/probe"; import { getCredential } from "../credentials/store"; import { isLoopbackHostname, isPrivateHostname } from "../private-networks"; +import { assertEndpointResolvesPublic, type EndpointDnsLookupFn } from "./endpoint-ssrf-preflight"; import { hasExplicitContextWindow, MAX_AUTODETECTED_OLLAMA_CONTEXT_WINDOW, @@ -71,6 +72,13 @@ export interface ApplyCompatibleEndpointContextWindowOptions { fetchModels?: CompatibleEndpointModelsFetcher; /** Override credential resolution (unit tests inject a fake). */ resolveCredential?: (credentialEnv: string) => string | null | undefined; + /** + * Injectable DNS resolver for the SSRF preflight run before the host-side + * `/v1/models` curl. When omitted under the unit-test runner the DNS + * preflight is skipped (the string-level private check still applies); + * production uses the real `dns/promises` resolver. + */ + resolveHost?: EndpointDnsLookupFn; } /** @@ -180,11 +188,11 @@ export function clearAutoDetectedCompatibleContextWindow( * the real-server compatible-endpoint-context-probe.test.ts. Remove this probe * only if a typed, validated cross-provider model-catalog fetch subsumes it. */ -export function applyCompatibleEndpointContextWindow( +export async function applyCompatibleEndpointContextWindow( endpointUrl: string, model: string | null | undefined, options: ApplyCompatibleEndpointContextWindowOptions = {}, -): void { +): Promise { const env = options.env ?? process.env; const logger = options.logger ?? console; @@ -249,6 +257,26 @@ export function applyCompatibleEndpointContextWindow( const fetchModels = options.fetchModels; if (!fetchModels && env.VITEST === "true") return; + // DNS-backed SSRF: the string-level isPrivateEndpoint check above only sees + // literal IPs and reserved names. This host-side GET is its own curl boundary + // (independent of the chat-completions validation probe), so pin the resolver + // preflight here — a public-looking name that resolves to loopback/link-local/ + // RFC1918 is refused before the fetch. Skipped when a fake fetcher is injected + // without a resolver (unit tests), so it never issues real DNS there; runs in + // production (no injected fetcher) and whenever a resolver is injected. + // See PR #6293 PRA-3 (GPT-5.5). + if (options.resolveHost || !fetchModels) { + const preflight = await assertEndpointResolvesPublic(endpointUrl, options.resolveHost); + if (!preflight.ok) { + logger.warn( + ` ⚠ ${preflight.reason}; skipping the /v1/models context probe. ` + + "Use a routable public URL to auto-detect the context window.", + ); + clearPreviousAuto(); + return; + } + } + const resolveCredential = options.resolveCredential ?? getCredential; const apiKey = options.apiKey ?? (options.credentialEnv ? resolveCredential(options.credentialEnv) : "") ?? ""; diff --git a/src/lib/inference/onboard-probes.ts b/src/lib/inference/onboard-probes.ts index 31cf6274144..82b87584d42 100644 --- a/src/lib/inference/onboard-probes.ts +++ b/src/lib/inference/onboard-probes.ts @@ -647,6 +647,17 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { // (LAN ranges, link-local metadata) is attacker-reachable SSRF surface and is // refused. Reuses the shared validators (defense-in-depth alongside // DNS-pinning at the config-write boundary). See PR #6293 PRA-2. + // + // DNS-backed SSRF (a public name resolving to a private address) is closed + // one layer up, before this synchronous shared probe is reached: the only + // untrusted-endpoint caller path (validateCustomOpenAiLikeSelection / + // validateCustomAnthropicSelection) runs assertEndpointResolvesPublic — a + // resolver-based preflight that fails closed — before invoking this probe, + // and the independent /v1/models context curl resolves inline in + // applyCompatibleEndpointContextWindow. This function stays synchronous (it + // has many callers and no async boundary), so the resolve step is not + // duplicated here; the literal string check below remains as the local + // belt-and-suspenders layer. See PR #6293 PRA-3. let probeHostname; try { probeHostname = new URL(String(endpointUrl)).hostname; diff --git a/src/lib/onboard/setup-nim-selection.ts b/src/lib/onboard/setup-nim-selection.ts index 6deb6e19f71..f4130df001d 100644 --- a/src/lib/onboard/setup-nim-selection.ts +++ b/src/lib/onboard/setup-nim-selection.ts @@ -206,7 +206,7 @@ export function createRemoteModelValidator(deps: RemoteModelValidatorDeps): { // Probe the endpoint's runtime max_model_len so a custom vLLM endpoint // gets its real context window baked in instead of a small // architecture default; an explicit override always wins (#6177). - applyCompatibleEndpointContextWindow( + await applyCompatibleEndpointContextWindow( state.endpointUrl || deps.OPENAI_ENDPOINT_URL, selectedModel, { credentialEnv: selectedCredentialEnv }, diff --git a/test/compatible-endpoint-context-probe.test.ts b/test/compatible-endpoint-context-probe.test.ts index 7bddb8013bb..4ad1c5a88aa 100644 --- a/test/compatible-endpoint-context-probe.test.ts +++ b/test/compatible-endpoint-context-probe.test.ts @@ -52,7 +52,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { expect(models).toMatchObject({ data: [{ id: MODEL, max_model_len: 65_536 }] }); const env: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { + await applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { env, fetchModels: fetchFromServer(""), }); @@ -67,7 +67,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { }); const env: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { + await applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { env, apiKey: "secret-key", fetchModels: fetchFromServer("secret-key"), @@ -91,7 +91,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { // Wrong/absent credential → the endpoint 401s → no window is set. const noKeyEnv: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { + await applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { env: noKeyEnv, apiKey: "", fetchModels: fetchFromServer(""), @@ -105,7 +105,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { // Correct credential → authorized → the window is read. const keyedEnv: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { + await applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { env: keyedEnv, apiKey: "secret-key", fetchModels: fetchFromServer("secret-key"), @@ -129,7 +129,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { .filter((entry) => entry.path === "/v1/models").length; const env: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { + await applyCompatibleEndpointContextWindow(server.baseUrl, MODEL, { env, fetchModels: fetchCompatibleEndpointModels, }); @@ -145,7 +145,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { server = await startFakeOpenAiCompatibleServer({ model: MODEL }); const env: NodeJS.ProcessEnv = {}; - applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { + await applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { env, fetchModels: fetchFromServer(""), }); From 0a903b6921f662c046d339e9786370c14874c5b6 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Mon, 6 Jul 2026 14:33:45 -0700 Subject: [PATCH 17/41] fix(security): pin DNS-validated IP into onboarding curls, close rebinding (#6293) GPT-5.5 advisor PRA-3 (follow-up): the SSRF preflight resolved the endpoint but the subsequent host-side curls re-resolved the hostname, leaving a check-then-use (DNS-rebinding) window. Pin the preflight-validated IP into every host-side curl via --resolve (hostname preserved for Host/TLS SNI): - assertEndpointResolvesPublic now returns the validated pinnedAddress; buildCurlResolveArgs turns it into --resolve host:port:ip. - The /v1/models context curl (applyCompatibleEndpointContextWindow) and the validation probes (probeOpenAiLikeEndpoint /responses+/chat/completions and its retries, probeAnthropicEndpoint /messages) all connect to the pinned IP. - validateCustom* runs the preflight once and threads the pin into the probe. Also removes the ambient VITEST bypass from the security path: the preflight is gated on an injected resolver (deps.resolveEndpointHost), wired to the real dns/promises resolver at the composition root (onboard.ts) via the new stubbable net/dns-lookup module; tests inject a fake resolver. Signed-off-by: Prekshi Vyas Co-Authored-By: Claude Opus 4.8 (1M context) --- .../inference/compatible-endpoint-context.ts | 23 +++++++- .../inference/endpoint-ssrf-preflight.test.ts | 40 +++++++++++-- src/lib/inference/endpoint-ssrf-preflight.ts | 36 +++++++++++- src/lib/inference/onboard-probes.ts | 27 ++++++++- src/lib/inference/probe-anthropic.ts | 3 + src/lib/net/dns-lookup.ts | 18 ++++++ src/lib/onboard.ts | 6 ++ .../inference-selection-validation.test.ts | 11 +++- .../onboard/inference-selection-validation.ts | 57 ++++++++++++++----- test/onboard-selection.test.ts | 26 +++++---- 10 files changed, 211 insertions(+), 36 deletions(-) create mode 100644 src/lib/net/dns-lookup.ts diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts index 27c368ba4c3..c55401bc352 100644 --- a/src/lib/inference/compatible-endpoint-context.ts +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -5,7 +5,11 @@ import { createOpenAiLikeAuthConfig } from "../adapters/http/auth-config"; import { runCurlProbe } from "../adapters/http/probe"; import { getCredential } from "../credentials/store"; import { isLoopbackHostname, isPrivateHostname } from "../private-networks"; -import { assertEndpointResolvesPublic, type EndpointDnsLookupFn } from "./endpoint-ssrf-preflight"; +import { + assertEndpointResolvesPublic, + buildCurlResolveArgs, + type EndpointDnsLookupFn, +} from "./endpoint-ssrf-preflight"; import { hasExplicitContextWindow, MAX_AUTODETECTED_OLLAMA_CONTEXT_WINDOW, @@ -102,7 +106,11 @@ export interface ApplyCompatibleEndpointContextWindowOptions { * separately, not via a raw private-LAN URL. This fetcher therefore only ever * sees an already-validated, routable endpoint URL. */ -export function fetchCompatibleEndpointModels(endpointUrl: string, apiKey: string): unknown | null { +export function fetchCompatibleEndpointModels( + endpointUrl: string, + apiKey: string, + extraCurlArgs: readonly string[] = [], +): unknown | null { const baseUrl = String(endpointUrl).replace(/\/+$/, ""); const authConfig = createOpenAiLikeAuthConfig(apiKey || ""); try { @@ -113,6 +121,9 @@ export function fetchCompatibleEndpointModels(endpointUrl: string, apiKey: strin "10", "--max-time", "15", + // Pins the connection to the preflight-validated IP (rebinding-safe); + // empty in tests / when there is nothing to pin. + ...extraCurlArgs, ...authConfig.args, `${baseUrl}/models`, ], @@ -265,6 +276,7 @@ export async function applyCompatibleEndpointContextWindow( // without a resolver (unit tests), so it never issues real DNS there; runs in // production (no injected fetcher) and whenever a resolver is injected. // See PR #6293 PRA-3 (GPT-5.5). + let resolveArgs: string[] = []; if (options.resolveHost || !fetchModels) { const preflight = await assertEndpointResolvesPublic(endpointUrl, options.resolveHost); if (!preflight.ok) { @@ -275,13 +287,18 @@ export async function applyCompatibleEndpointContextWindow( clearPreviousAuto(); return; } + resolveArgs = buildCurlResolveArgs(endpointUrl, preflight.pinnedAddress); } const resolveCredential = options.resolveCredential ?? getCredential; const apiKey = options.apiKey ?? (options.credentialEnv ? resolveCredential(options.credentialEnv) : "") ?? ""; - const models = (fetchModels ?? fetchCompatibleEndpointModels)(endpointUrl, apiKey); + // The default fetcher curls the endpoint, so pass the pinned --resolve args so + // it connects to the validated IP; an injected fetcher (tests) does not curl. + const models = fetchModels + ? fetchModels(endpointUrl, apiKey) + : fetchCompatibleEndpointModels(endpointUrl, apiKey, resolveArgs); if (models === null || models === undefined) { logger.warn( " ⚠ Could not read the endpoint's /v1/models max_model_len; using the default context " + diff --git a/src/lib/inference/endpoint-ssrf-preflight.test.ts b/src/lib/inference/endpoint-ssrf-preflight.test.ts index cab9d068895..bf9ab6632fc 100644 --- a/src/lib/inference/endpoint-ssrf-preflight.test.ts +++ b/src/lib/inference/endpoint-ssrf-preflight.test.ts @@ -3,16 +3,21 @@ import { describe, expect, it, vi } from "vitest"; -import { assertEndpointResolvesPublic, type EndpointDnsLookupFn } from "./endpoint-ssrf-preflight"; +import { + assertEndpointResolvesPublic, + buildCurlResolveArgs, + type EndpointDnsLookupFn, +} from "./endpoint-ssrf-preflight"; const resolverTo = (address: string): EndpointDnsLookupFn => vi.fn(async () => [{ address, family: address.includes(":") ? 6 : 4 }]); describe("assertEndpointResolvesPublic (#6293)", () => { - it("allows a public hostname that resolves to a public address without ever needing a private check", async () => { + it("allows a public hostname that resolves to a public address and pins that address", async () => { const lookup = resolverTo("93.184.216.34"); const result = await assertEndpointResolvesPublic("https://vllm.example/v1", lookup); expect(result.ok).toBe(true); + expect(result.pinnedAddress).toBe("93.184.216.34"); expect(lookup).toHaveBeenCalledWith("vllm.example", { all: true }); }); @@ -40,17 +45,19 @@ describe("assertEndpointResolvesPublic (#6293)", () => { "http://127.0.0.1:8000/v1", "http://localhost:8000/v1", "http://[::1]:8000/v1", - ])("allows the explicit loopback endpoint %s without resolving (#6293)", async (endpointUrl) => { + ])("allows the explicit loopback endpoint %s without resolving or pinning (#6293)", async (endpointUrl) => { const lookup = vi.fn(); const result = await assertEndpointResolvesPublic(endpointUrl, lookup); expect(result.ok).toBe(true); + expect(result.pinnedAddress).toBeUndefined(); expect(lookup).not.toHaveBeenCalled(); }); - it("allows a public IP literal without resolving (#6293)", async () => { + it("allows a public IP literal without resolving or pinning (#6293)", async () => { const lookup = vi.fn(); const result = await assertEndpointResolvesPublic("https://93.184.216.34/v1", lookup); expect(result.ok).toBe(true); + expect(result.pinnedAddress).toBeUndefined(); expect(lookup).not.toHaveBeenCalled(); }); @@ -74,3 +81,28 @@ describe("assertEndpointResolvesPublic (#6293)", () => { expect(result.ok).toBe(false); }); }); + +describe("buildCurlResolveArgs (#6293)", () => { + it("pins host:port:addr for an https endpoint (default 443)", () => { + expect(buildCurlResolveArgs("https://vllm.example/v1", "93.184.216.34")).toEqual([ + "--resolve", + "vllm.example:443:93.184.216.34", + ]); + }); + + it("uses the explicit port and http default 80", () => { + expect(buildCurlResolveArgs("http://vllm.example:8000/v1", "93.184.216.34")).toEqual([ + "--resolve", + "vllm.example:8000:93.184.216.34", + ]); + expect(buildCurlResolveArgs("http://vllm.example/v1", "93.184.216.34")).toEqual([ + "--resolve", + "vllm.example:80:93.184.216.34", + ]); + }); + + it("returns [] when there is nothing to pin or the URL is malformed", () => { + expect(buildCurlResolveArgs("https://vllm.example/v1", undefined)).toEqual([]); + expect(buildCurlResolveArgs("not a url", "93.184.216.34")).toEqual([]); + }); +}); diff --git a/src/lib/inference/endpoint-ssrf-preflight.ts b/src/lib/inference/endpoint-ssrf-preflight.ts index 616cc0446ed..2714ebe9617 100644 --- a/src/lib/inference/endpoint-ssrf-preflight.ts +++ b/src/lib/inference/endpoint-ssrf-preflight.ts @@ -16,6 +16,15 @@ export interface EndpointSsrfPreflightResult { ok: boolean; /** Human-readable reason, present only when `ok === false`. */ reason?: string; + /** + * The validated resolved IP to pin into the subsequent curl via `--resolve`, + * set only when the endpoint host was a DNS name that resolved to a public + * address. Undefined for literal IPs and loopback (curl connects directly, no + * pin needed) and whenever `ok` is false. Pinning closes the check-then-use + * (DNS-rebinding) window: curl connects to this exact address instead of + * re-resolving the hostname, while the hostname is preserved for Host/TLS SNI. + */ + pinnedAddress?: string; } /** @@ -83,5 +92,30 @@ export async function assertEndpointResolvesPublic( }; } } - return { ok: true }; + // Pin the first validated address so the follow-up curl connects to it rather + // than re-resolving the name (rebinding-safe). + return { ok: true, pinnedAddress: addresses[0].address }; +} + +/** + * Build the curl `--resolve ::` argument that pins the + * connection to a preflight-validated IP while preserving the hostname for the + * Host header and TLS SNI. Returns `[]` when there is nothing to pin (literal + * IP / loopback / preflight did not resolve a name), or when the URL cannot be + * parsed. Default ports: 443 for https, 80 otherwise. + */ +export function buildCurlResolveArgs( + endpointUrl: string, + pinnedAddress: string | undefined, +): string[] { + if (!pinnedAddress) return []; + let url: URL; + try { + url = new URL(String(endpointUrl)); + } catch { + return []; + } + const host = url.hostname.replace(/^\[|\]$/g, ""); + const port = url.port || (url.protocol === "https:" ? "443" : "80"); + return ["--resolve", `${host}:${port}:${pinnedAddress}`]; } diff --git a/src/lib/inference/onboard-probes.ts b/src/lib/inference/onboard-probes.ts index 82b87584d42..341ebc3a80d 100644 --- a/src/lib/inference/onboard-probes.ts +++ b/src/lib/inference/onboard-probes.ts @@ -290,6 +290,8 @@ function probeResponsesToolCalling(endpointUrl, model, apiKey, options = {}) { const result = runCurlProbe( [ "-sS", + // Pin the connection to the preflight-validated IP (rebinding-safe). + ...(options.resolveArgs ?? []), ...getValidationProbeCurlArgs(), "-H", "Content-Type: application/json", @@ -349,6 +351,8 @@ function probeChatCompletionsToolCalling(endpointUrl, model, apiKey, options = { const timingArgs = options.timingArgs ?? getChatCompletionsProbeTimingArgs(model); const args = [ "-sS", + // Pin the connection to the preflight-validated IP (rebinding-safe). + ...(options.resolveArgs ?? []), ...timingArgs, "-H", "Content-Type: application/json", @@ -524,13 +528,16 @@ export function getChatCompletionsProbeCurlArgs(opts: { model: string; url: string; isWsl?: boolean; + resolveArgs?: readonly string[]; }) { - const { credentialArgs, authHeader, model, url, isWsl: isWslOverride } = opts; + const { credentialArgs, authHeader, model, url, isWsl: isWslOverride, resolveArgs } = opts; const platformOptions = typeof isWslOverride === "boolean" ? { isWsl: isWslOverride } : undefined; const timingArgs = getChatCompletionsProbeTimingArgs(model, platformOptions); const credSlice = credentialArgs ?? authHeader ?? []; return [ "-sS", + // Pin the connection to the preflight-validated IP (rebinding-safe). + ...(resolveArgs ?? []), ...timingArgs, "-H", "Content-Type: application/json", @@ -547,12 +554,14 @@ function runChatCompletionsProbe({ url, isWsl: isWslOverride, trustedConfigFiles, + resolveArgs, }) { const args = getChatCompletionsProbeCurlArgs({ credentialArgs, model, url, isWsl: isWslOverride, + resolveArgs, }); const probeOpts = { timeoutMs: getProbeProcessTimeoutMs(args) }; if (trustedConfigFiles && trustedConfigFiles.length > 0) { @@ -581,6 +590,8 @@ function runDoubledTimeoutChatCompletionsRetry({ const doubledArgs = baseArgs.map((arg) => (/^\d+$/.test(arg) ? String(Number(arg) * 2) : arg)); const buildRetryArgs = () => [ "-sS", + // Pin the connection to the preflight-validated IP (rebinding-safe). + ...(options.resolveArgs ?? []), ...doubledArgs, "-H", "Content-Type: application/json", @@ -594,6 +605,7 @@ function runDoubledTimeoutChatCompletionsRetry({ ? probeChatCompletionsToolCalling(endpointUrl, model, apiKey, { authMode: options.authMode, timingArgs: doubledArgs, + resolveArgs: options.resolveArgs, }) : (() => { const retryArgs = buildRetryArgs(); @@ -686,6 +698,10 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { } const baseUrl = String(endpointUrl).replace(/\/+$/, ""); + // Pinned --resolve args from the upstream DNS SSRF preflight (validateCustom*), + // applied to every host-side curl below so curl connects to the validated IP + // rather than re-resolving the hostname (rebinding-safe). Empty when unpinned. + const resolveArgs = options.resolveArgs ?? []; let authConfig; try { authConfig = buildOpenAiLikeAuthConfig(apiKey, options); @@ -695,7 +711,10 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { name: "Responses API with tool calling", api: "openai-responses", execute: () => - probeResponsesToolCalling(endpointUrl, model, apiKey, { authMode: options.authMode }), + probeResponsesToolCalling(endpointUrl, model, apiKey, { + authMode: options.authMode, + resolveArgs, + }), } : { name: "Responses API", @@ -704,6 +723,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { runCurlProbe( [ "-sS", + ...resolveArgs, ...getValidationProbeCurlArgs(), "-H", "Content-Type: application/json", @@ -726,6 +746,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { options.requireChatCompletionsToolCalling === true ? probeChatCompletionsToolCalling(endpointUrl, model, apiKey, { authMode: options.authMode, + resolveArgs, }) : runChatCompletionsProbe({ credentialArgs: authConfig.args, @@ -733,6 +754,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { url: `${baseUrl}/chat/completions`, isWsl: options.isWsl, trustedConfigFiles: authConfig.trustedConfigFiles, + resolveArgs, }), }; @@ -765,6 +787,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { const streamResult = runStreamingEventProbe( [ "-sS", + ...resolveArgs, ...getValidationProbeCurlArgs(), "-H", "Content-Type: application/json", diff --git a/src/lib/inference/probe-anthropic.ts b/src/lib/inference/probe-anthropic.ts index 05f5a3ad30b..5f5995e4b29 100644 --- a/src/lib/inference/probe-anthropic.ts +++ b/src/lib/inference/probe-anthropic.ts @@ -38,6 +38,7 @@ export function probeAnthropicEndpoint( endpointUrl: string, model: string, apiKey: string, + resolveArgs: readonly string[] = [], ): AnthropicProbeResult { let authConfig: ReturnType | undefined; try { @@ -45,6 +46,8 @@ export function probeAnthropicEndpoint( const result = runCurlProbe( [ "-sS", + // Pin the connection to the preflight-validated IP (rebinding-safe). + ...resolveArgs, ...getCurlTimingArgs(), ...authConfig.args, "-H", diff --git a/src/lib/net/dns-lookup.ts b/src/lib/net/dns-lookup.ts new file mode 100644 index 00000000000..1b6725dcc15 --- /dev/null +++ b/src/lib/net/dns-lookup.ts @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { lookup } from "node:dns/promises"; + +/** + * Resolve all A/AAAA addresses for a hostname. A thin, stubbable wrapper around + * `node:dns/promises` `lookup(host, { all: true })` so callers (e.g. the + * custom-endpoint SSRF preflight wired in onboard.ts) can depend on a repo + * module that tests can override — builtin module exports are non-writable and + * cannot be monkeypatched. + */ +export async function resolveHostAddresses( + hostname: string, + options: { all: true }, +): Promise> { + return lookup(hostname, options); +} diff --git a/src/lib/onboard.ts b/src/lib/onboard.ts index a42ee5dc3a9..faf49831178 100644 --- a/src/lib/onboard.ts +++ b/src/lib/onboard.ts @@ -1036,6 +1036,12 @@ const { isNonInteractive, agentProductName, promptValidationRecovery, + // Real DNS resolver for the custom-endpoint SSRF preflight (PR #6293 PRA-3). + // Wired here at the composition root so the security boundary does not depend + // on an ambient env flag. Routed through the repo net/dns-lookup module (not + // node:dns directly) so tests can stub it — builtin exports are non-writable. + resolveEndpointHost: (hostname, options) => + require("./net/dns-lookup").resolveHostAddresses(hostname, options), }); const { validateSelectedRemoteModel } = createRemoteModelValidator({ OPENAI_ENDPOINT_URL, diff --git a/src/lib/onboard/inference-selection-validation.test.ts b/src/lib/onboard/inference-selection-validation.test.ts index 82b2a91f590..69b6f9edb27 100644 --- a/src/lib/onboard/inference-selection-validation.test.ts +++ b/src/lib/onboard/inference-selection-validation.test.ts @@ -166,6 +166,15 @@ describe("inference selection validation", () => { "COMPATIBLE_API_KEY", ), ).resolves.toEqual({ ok: true, api: "openai-completions" }); - expect(probeOpenAiLikeEndpoint).toHaveBeenCalled(); + // The probe is called with the pinned --resolve args from the preflight, so + // curl connects to the validated IP instead of re-resolving the hostname. + expect(probeOpenAiLikeEndpoint).toHaveBeenCalledWith( + "https://vllm.public.test/v1", + "model-a", + "test-key", + expect.objectContaining({ + resolveArgs: ["--resolve", "vllm.public.test:443:93.184.216.34"], + }), + ); }); }); diff --git a/src/lib/onboard/inference-selection-validation.ts b/src/lib/onboard/inference-selection-validation.ts index be716a1c3cb..8abf08b54dd 100644 --- a/src/lib/onboard/inference-selection-validation.ts +++ b/src/lib/onboard/inference-selection-validation.ts @@ -9,6 +9,7 @@ const { probeAnthropicEndpoint, probeOpenAiLikeEndpoint } = endpointUrl: string, model: string, apiKey: string | null | undefined, + resolveArgs?: readonly string[], ): any; probeOpenAiLikeEndpoint( endpointUrl: string, @@ -20,6 +21,7 @@ const { probeAnthropicEndpoint, probeOpenAiLikeEndpoint } = import { assertEndpointResolvesPublic, + buildCurlResolveArgs, type EndpointDnsLookupFn, } from "../inference/endpoint-ssrf-preflight"; import { shouldForceCompletionsApi } from "../validation"; @@ -114,22 +116,29 @@ export function createInferenceSelectionValidationHelpers( // endpoint host and fails closed before any host-side probe curl when it (or // a resolved address) is private/reserved, so a public-looking name that // resolves to loopback/link-local/RFC1918 cannot reach internal services - // during privileged onboarding. Returns a fail-closed EndpointValidationResult - // to short-circuit the caller, or null when the endpoint is safe to probe. - // See PR #6293 PRA-4. - async function preflightCustomEndpointOrFail( + // during privileged onboarding. On success it returns the `--resolve` curl + // args that pin the connection to the validated IP, so the probe curls cannot + // re-resolve (rebind) the hostname. Returns `{ blocked }` (fail-closed) to + // short-circuit, or `{ blocked: null, resolveArgs }` when it is safe to probe. + // + // Gated on an injected resolver rather than an ambient VITEST flag: production + // wires the real dns/promises resolver at the composition root (onboard.ts); + // tests inject a fake resolver to exercise it, or omit it to skip (no real + // DNS). See PR #6293 PRA-3/PRA-4. + async function preflightCustomEndpoint( label: string, endpointUrl: string, credentialEnv: string | null, helpUrl: string | null, - ): Promise { - // Under the unit-test runner the default resolver would hit real DNS (and - // fixture hostnames do not resolve); skip unless a resolver is injected, - // mirroring applyCompatibleEndpointContextWindow's VITEST fetch guard. In - // production (VITEST unset) the real dns/promises resolver always runs. - if (!deps.resolveEndpointHost && process.env.VITEST === "true") return null; + ): Promise<{ blocked: EndpointValidationResult | null; resolveArgs: string[] }> { + if (!deps.resolveEndpointHost) return { blocked: null, resolveArgs: [] }; const preflight = await assertEndpointResolvesPublic(endpointUrl, deps.resolveEndpointHost); - if (preflight.ok) return null; + if (preflight.ok) { + return { + blocked: null, + resolveArgs: buildCurlResolveArgs(endpointUrl, preflight.pinnedAddress), + }; + } const syntheticProbe = { ok: false as const, message: preflight.reason, @@ -157,7 +166,7 @@ export function createInferenceSelectionValidationHelpers( console.log(" Please choose a provider/model again."); console.log(""); } - return { ok: false, retry }; + return { blocked: { ok: false, retry }, resolveArgs: [] }; } async function validateOpenAiLikeSelection( @@ -241,7 +250,12 @@ export function createInferenceSelectionValidationHelpers( credentialEnv: string, helpUrl: string | null = null, ): Promise { - const blocked = await preflightCustomEndpointOrFail(label, endpointUrl, credentialEnv, helpUrl); + const { blocked, resolveArgs } = await preflightCustomEndpoint( + label, + endpointUrl, + credentialEnv, + helpUrl, + ); if (blocked) return blocked; const apiKey = resolveCredential(credentialEnv); const reasoningEnabled = normalizeReasoningFlag(process.env.NEMOCLAW_REASONING) === "true"; @@ -251,6 +265,9 @@ export function createInferenceSelectionValidationHelpers( skipResponsesProbe: reasoningEnabled || shouldForceCompletionsApi(process.env.NEMOCLAW_PREFERRED_API), probeStreaming: !reasoningEnabled, + // Pin curl to the preflight-validated IP (rebinding-safe); only present + // when the preflight actually resolved a public name. + ...(resolveArgs.length > 0 ? { resolveArgs } : {}), }); if (probe.ok) { if (probe.note) { @@ -286,10 +303,20 @@ export function createInferenceSelectionValidationHelpers( credentialEnv: string, helpUrl: string | null = null, ): Promise { - const blocked = await preflightCustomEndpointOrFail(label, endpointUrl, credentialEnv, helpUrl); + const { blocked, resolveArgs } = await preflightCustomEndpoint( + label, + endpointUrl, + credentialEnv, + helpUrl, + ); if (blocked) return blocked; const apiKey = resolveCredential(credentialEnv); - const probe = runAnthropicProbe(endpointUrl, model, apiKey); + // Pass the pinned --resolve args only when the preflight resolved a public + // name, so the unpinned call shape (and its assertions) is unchanged. + const probe = + resolveArgs.length > 0 + ? runAnthropicProbe(endpointUrl, model, apiKey, resolveArgs) + : runAnthropicProbe(endpointUrl, model, apiKey); if (probe.ok) { console.log(` ${probe.label} available — ${deps.agentProductName()} will use ${probe.api}.`); return { ok: true, api: probe.api }; diff --git a/test/onboard-selection.test.ts b/test/onboard-selection.test.ts index bc1d84b486d..2dffbf0b1a7 100644 --- a/test/onboard-selection.test.ts +++ b/test/onboard-selection.test.ts @@ -319,7 +319,7 @@ while [ "$#" -gt 0 ]; do fi shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; esac done # Also extract auth from ?key= query parameter (Gemini uses this instead of Bearer header) @@ -368,7 +368,7 @@ while [ "$#" -gt 0 ]; do fi shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; esac done if echo "$url" | grep -q '/v1/models$'; then @@ -3290,7 +3290,7 @@ url="" while [ "$#" -gt 0 ]; do case "$1" in -o) outfile="$2"; shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; esac done if echo "$url" | grep -q 'api.openai.com'; then @@ -3713,7 +3713,7 @@ while [ "$#" -gt 0 ]; do fi shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; esac done if echo "$auth" | grep -q 'nvapi-good' && echo "$url" | grep -q '/responses$'; then @@ -4135,7 +4135,10 @@ const { setupNim } = require(${onboardPath}); const credentials = require(${credentialsPath}); const runner = require(${runnerPath}); -const answers = ["3", "https://proxy.example.com/v1/chat/completions?token=secret#frag", "custom-model", "retry", "proxy-good", "custom-model"]; +// Public IP literal endpoint: the custom-endpoint SSRF preflight passes it +// without DNS (no resolver needed), keeping this auth-retry test focused on the +// re-enter-key UX. Preflight DNS behavior is unit-tested separately (#6293). +const answers = ["3", "https://8.8.8.8/v1/chat/completions?token=secret#frag", "custom-model", "retry", "proxy-good", "custom-model"]; const messages = []; credentials.prompt = async (message) => { @@ -4181,7 +4184,7 @@ const { setupNim } = require(${onboardPath}); const payload = JSON.parse(result.stdout.trim()); assert.equal(payload.result.provider, "compatible-endpoint"); assert.equal(payload.result.model, "custom-model"); - assert.equal(payload.result.endpointUrl, "https://proxy.example.com/v1"); + assert.equal(payload.result.endpointUrl, "https://8.8.8.8/v1"); assert.equal(payload.result.preferredInferenceApi, "openai-completions"); assert.equal(payload.key, "proxy-good"); assert.ok( @@ -4229,7 +4232,10 @@ const { setupNim } = require(${onboardPath}); const credentials = require(${credentialsPath}); const runner = require(${runnerPath}); -const answers = ["5", "https://proxy.example.com/v1/messages?token=secret#frag", "claude-proxy", "retry", "anthropic-proxy-good", "claude-proxy"]; +// Public IP literal endpoint: the custom-endpoint SSRF preflight passes it +// without DNS (no resolver needed), keeping this auth-retry test focused on the +// re-enter-key UX. Preflight DNS behavior is unit-tested separately (#6293). +const answers = ["5", "https://8.8.8.8/v1/messages?token=secret#frag", "claude-proxy", "retry", "anthropic-proxy-good", "claude-proxy"]; const messages = []; credentials.prompt = async (message) => { @@ -4275,7 +4281,7 @@ const { setupNim } = require(${onboardPath}); const payload = JSON.parse(result.stdout.trim()); assert.equal(payload.result.provider, "compatible-anthropic-endpoint"); assert.equal(payload.result.model, "claude-proxy"); - assert.equal(payload.result.endpointUrl, "https://proxy.example.com"); + assert.equal(payload.result.endpointUrl, "https://8.8.8.8"); assert.equal(payload.result.preferredInferenceApi, "anthropic-messages"); assert.equal(payload.key, "anthropic-proxy-good"); assert.ok( @@ -4327,7 +4333,7 @@ url="" while [ "$#" -gt 0 ]; do case "$1" in -o) outfile="$2"; shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; esac done if echo "$url" | grep -q '/v1/models'; then @@ -4431,7 +4437,7 @@ url="" while [ "$#" -gt 0 ]; do case "$1" in -o) outfile="$2"; shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; esac done if echo "$url" | grep -q '/v1/models'; then From 9fbc5e5b00c9265e8d994e2b40c7ff2b06239c7f Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Mon, 6 Jul 2026 14:52:59 -0700 Subject: [PATCH 18/41] Revert "fix(security): pin DNS-validated IP into onboarding curls, close rebinding (#6293)" This reverts commit 0a903b6921f662c046d339e9786370c14874c5b6. --- .../inference/compatible-endpoint-context.ts | 23 +------- .../inference/endpoint-ssrf-preflight.test.ts | 40 ++----------- src/lib/inference/endpoint-ssrf-preflight.ts | 36 +----------- src/lib/inference/onboard-probes.ts | 27 +-------- src/lib/inference/probe-anthropic.ts | 3 - src/lib/net/dns-lookup.ts | 18 ------ src/lib/onboard.ts | 6 -- .../inference-selection-validation.test.ts | 11 +--- .../onboard/inference-selection-validation.ts | 57 +++++-------------- test/onboard-selection.test.ts | 26 ++++----- 10 files changed, 36 insertions(+), 211 deletions(-) delete mode 100644 src/lib/net/dns-lookup.ts diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts index c55401bc352..27c368ba4c3 100644 --- a/src/lib/inference/compatible-endpoint-context.ts +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -5,11 +5,7 @@ import { createOpenAiLikeAuthConfig } from "../adapters/http/auth-config"; import { runCurlProbe } from "../adapters/http/probe"; import { getCredential } from "../credentials/store"; import { isLoopbackHostname, isPrivateHostname } from "../private-networks"; -import { - assertEndpointResolvesPublic, - buildCurlResolveArgs, - type EndpointDnsLookupFn, -} from "./endpoint-ssrf-preflight"; +import { assertEndpointResolvesPublic, type EndpointDnsLookupFn } from "./endpoint-ssrf-preflight"; import { hasExplicitContextWindow, MAX_AUTODETECTED_OLLAMA_CONTEXT_WINDOW, @@ -106,11 +102,7 @@ export interface ApplyCompatibleEndpointContextWindowOptions { * separately, not via a raw private-LAN URL. This fetcher therefore only ever * sees an already-validated, routable endpoint URL. */ -export function fetchCompatibleEndpointModels( - endpointUrl: string, - apiKey: string, - extraCurlArgs: readonly string[] = [], -): unknown | null { +export function fetchCompatibleEndpointModels(endpointUrl: string, apiKey: string): unknown | null { const baseUrl = String(endpointUrl).replace(/\/+$/, ""); const authConfig = createOpenAiLikeAuthConfig(apiKey || ""); try { @@ -121,9 +113,6 @@ export function fetchCompatibleEndpointModels( "10", "--max-time", "15", - // Pins the connection to the preflight-validated IP (rebinding-safe); - // empty in tests / when there is nothing to pin. - ...extraCurlArgs, ...authConfig.args, `${baseUrl}/models`, ], @@ -276,7 +265,6 @@ export async function applyCompatibleEndpointContextWindow( // without a resolver (unit tests), so it never issues real DNS there; runs in // production (no injected fetcher) and whenever a resolver is injected. // See PR #6293 PRA-3 (GPT-5.5). - let resolveArgs: string[] = []; if (options.resolveHost || !fetchModels) { const preflight = await assertEndpointResolvesPublic(endpointUrl, options.resolveHost); if (!preflight.ok) { @@ -287,18 +275,13 @@ export async function applyCompatibleEndpointContextWindow( clearPreviousAuto(); return; } - resolveArgs = buildCurlResolveArgs(endpointUrl, preflight.pinnedAddress); } const resolveCredential = options.resolveCredential ?? getCredential; const apiKey = options.apiKey ?? (options.credentialEnv ? resolveCredential(options.credentialEnv) : "") ?? ""; - // The default fetcher curls the endpoint, so pass the pinned --resolve args so - // it connects to the validated IP; an injected fetcher (tests) does not curl. - const models = fetchModels - ? fetchModels(endpointUrl, apiKey) - : fetchCompatibleEndpointModels(endpointUrl, apiKey, resolveArgs); + const models = (fetchModels ?? fetchCompatibleEndpointModels)(endpointUrl, apiKey); if (models === null || models === undefined) { logger.warn( " ⚠ Could not read the endpoint's /v1/models max_model_len; using the default context " + diff --git a/src/lib/inference/endpoint-ssrf-preflight.test.ts b/src/lib/inference/endpoint-ssrf-preflight.test.ts index bf9ab6632fc..cab9d068895 100644 --- a/src/lib/inference/endpoint-ssrf-preflight.test.ts +++ b/src/lib/inference/endpoint-ssrf-preflight.test.ts @@ -3,21 +3,16 @@ import { describe, expect, it, vi } from "vitest"; -import { - assertEndpointResolvesPublic, - buildCurlResolveArgs, - type EndpointDnsLookupFn, -} from "./endpoint-ssrf-preflight"; +import { assertEndpointResolvesPublic, type EndpointDnsLookupFn } from "./endpoint-ssrf-preflight"; const resolverTo = (address: string): EndpointDnsLookupFn => vi.fn(async () => [{ address, family: address.includes(":") ? 6 : 4 }]); describe("assertEndpointResolvesPublic (#6293)", () => { - it("allows a public hostname that resolves to a public address and pins that address", async () => { + it("allows a public hostname that resolves to a public address without ever needing a private check", async () => { const lookup = resolverTo("93.184.216.34"); const result = await assertEndpointResolvesPublic("https://vllm.example/v1", lookup); expect(result.ok).toBe(true); - expect(result.pinnedAddress).toBe("93.184.216.34"); expect(lookup).toHaveBeenCalledWith("vllm.example", { all: true }); }); @@ -45,19 +40,17 @@ describe("assertEndpointResolvesPublic (#6293)", () => { "http://127.0.0.1:8000/v1", "http://localhost:8000/v1", "http://[::1]:8000/v1", - ])("allows the explicit loopback endpoint %s without resolving or pinning (#6293)", async (endpointUrl) => { + ])("allows the explicit loopback endpoint %s without resolving (#6293)", async (endpointUrl) => { const lookup = vi.fn(); const result = await assertEndpointResolvesPublic(endpointUrl, lookup); expect(result.ok).toBe(true); - expect(result.pinnedAddress).toBeUndefined(); expect(lookup).not.toHaveBeenCalled(); }); - it("allows a public IP literal without resolving or pinning (#6293)", async () => { + it("allows a public IP literal without resolving (#6293)", async () => { const lookup = vi.fn(); const result = await assertEndpointResolvesPublic("https://93.184.216.34/v1", lookup); expect(result.ok).toBe(true); - expect(result.pinnedAddress).toBeUndefined(); expect(lookup).not.toHaveBeenCalled(); }); @@ -81,28 +74,3 @@ describe("assertEndpointResolvesPublic (#6293)", () => { expect(result.ok).toBe(false); }); }); - -describe("buildCurlResolveArgs (#6293)", () => { - it("pins host:port:addr for an https endpoint (default 443)", () => { - expect(buildCurlResolveArgs("https://vllm.example/v1", "93.184.216.34")).toEqual([ - "--resolve", - "vllm.example:443:93.184.216.34", - ]); - }); - - it("uses the explicit port and http default 80", () => { - expect(buildCurlResolveArgs("http://vllm.example:8000/v1", "93.184.216.34")).toEqual([ - "--resolve", - "vllm.example:8000:93.184.216.34", - ]); - expect(buildCurlResolveArgs("http://vllm.example/v1", "93.184.216.34")).toEqual([ - "--resolve", - "vllm.example:80:93.184.216.34", - ]); - }); - - it("returns [] when there is nothing to pin or the URL is malformed", () => { - expect(buildCurlResolveArgs("https://vllm.example/v1", undefined)).toEqual([]); - expect(buildCurlResolveArgs("not a url", "93.184.216.34")).toEqual([]); - }); -}); diff --git a/src/lib/inference/endpoint-ssrf-preflight.ts b/src/lib/inference/endpoint-ssrf-preflight.ts index 2714ebe9617..616cc0446ed 100644 --- a/src/lib/inference/endpoint-ssrf-preflight.ts +++ b/src/lib/inference/endpoint-ssrf-preflight.ts @@ -16,15 +16,6 @@ export interface EndpointSsrfPreflightResult { ok: boolean; /** Human-readable reason, present only when `ok === false`. */ reason?: string; - /** - * The validated resolved IP to pin into the subsequent curl via `--resolve`, - * set only when the endpoint host was a DNS name that resolved to a public - * address. Undefined for literal IPs and loopback (curl connects directly, no - * pin needed) and whenever `ok` is false. Pinning closes the check-then-use - * (DNS-rebinding) window: curl connects to this exact address instead of - * re-resolving the hostname, while the hostname is preserved for Host/TLS SNI. - */ - pinnedAddress?: string; } /** @@ -92,30 +83,5 @@ export async function assertEndpointResolvesPublic( }; } } - // Pin the first validated address so the follow-up curl connects to it rather - // than re-resolving the name (rebinding-safe). - return { ok: true, pinnedAddress: addresses[0].address }; -} - -/** - * Build the curl `--resolve ::` argument that pins the - * connection to a preflight-validated IP while preserving the hostname for the - * Host header and TLS SNI. Returns `[]` when there is nothing to pin (literal - * IP / loopback / preflight did not resolve a name), or when the URL cannot be - * parsed. Default ports: 443 for https, 80 otherwise. - */ -export function buildCurlResolveArgs( - endpointUrl: string, - pinnedAddress: string | undefined, -): string[] { - if (!pinnedAddress) return []; - let url: URL; - try { - url = new URL(String(endpointUrl)); - } catch { - return []; - } - const host = url.hostname.replace(/^\[|\]$/g, ""); - const port = url.port || (url.protocol === "https:" ? "443" : "80"); - return ["--resolve", `${host}:${port}:${pinnedAddress}`]; + return { ok: true }; } diff --git a/src/lib/inference/onboard-probes.ts b/src/lib/inference/onboard-probes.ts index 341ebc3a80d..82b87584d42 100644 --- a/src/lib/inference/onboard-probes.ts +++ b/src/lib/inference/onboard-probes.ts @@ -290,8 +290,6 @@ function probeResponsesToolCalling(endpointUrl, model, apiKey, options = {}) { const result = runCurlProbe( [ "-sS", - // Pin the connection to the preflight-validated IP (rebinding-safe). - ...(options.resolveArgs ?? []), ...getValidationProbeCurlArgs(), "-H", "Content-Type: application/json", @@ -351,8 +349,6 @@ function probeChatCompletionsToolCalling(endpointUrl, model, apiKey, options = { const timingArgs = options.timingArgs ?? getChatCompletionsProbeTimingArgs(model); const args = [ "-sS", - // Pin the connection to the preflight-validated IP (rebinding-safe). - ...(options.resolveArgs ?? []), ...timingArgs, "-H", "Content-Type: application/json", @@ -528,16 +524,13 @@ export function getChatCompletionsProbeCurlArgs(opts: { model: string; url: string; isWsl?: boolean; - resolveArgs?: readonly string[]; }) { - const { credentialArgs, authHeader, model, url, isWsl: isWslOverride, resolveArgs } = opts; + const { credentialArgs, authHeader, model, url, isWsl: isWslOverride } = opts; const platformOptions = typeof isWslOverride === "boolean" ? { isWsl: isWslOverride } : undefined; const timingArgs = getChatCompletionsProbeTimingArgs(model, platformOptions); const credSlice = credentialArgs ?? authHeader ?? []; return [ "-sS", - // Pin the connection to the preflight-validated IP (rebinding-safe). - ...(resolveArgs ?? []), ...timingArgs, "-H", "Content-Type: application/json", @@ -554,14 +547,12 @@ function runChatCompletionsProbe({ url, isWsl: isWslOverride, trustedConfigFiles, - resolveArgs, }) { const args = getChatCompletionsProbeCurlArgs({ credentialArgs, model, url, isWsl: isWslOverride, - resolveArgs, }); const probeOpts = { timeoutMs: getProbeProcessTimeoutMs(args) }; if (trustedConfigFiles && trustedConfigFiles.length > 0) { @@ -590,8 +581,6 @@ function runDoubledTimeoutChatCompletionsRetry({ const doubledArgs = baseArgs.map((arg) => (/^\d+$/.test(arg) ? String(Number(arg) * 2) : arg)); const buildRetryArgs = () => [ "-sS", - // Pin the connection to the preflight-validated IP (rebinding-safe). - ...(options.resolveArgs ?? []), ...doubledArgs, "-H", "Content-Type: application/json", @@ -605,7 +594,6 @@ function runDoubledTimeoutChatCompletionsRetry({ ? probeChatCompletionsToolCalling(endpointUrl, model, apiKey, { authMode: options.authMode, timingArgs: doubledArgs, - resolveArgs: options.resolveArgs, }) : (() => { const retryArgs = buildRetryArgs(); @@ -698,10 +686,6 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { } const baseUrl = String(endpointUrl).replace(/\/+$/, ""); - // Pinned --resolve args from the upstream DNS SSRF preflight (validateCustom*), - // applied to every host-side curl below so curl connects to the validated IP - // rather than re-resolving the hostname (rebinding-safe). Empty when unpinned. - const resolveArgs = options.resolveArgs ?? []; let authConfig; try { authConfig = buildOpenAiLikeAuthConfig(apiKey, options); @@ -711,10 +695,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { name: "Responses API with tool calling", api: "openai-responses", execute: () => - probeResponsesToolCalling(endpointUrl, model, apiKey, { - authMode: options.authMode, - resolveArgs, - }), + probeResponsesToolCalling(endpointUrl, model, apiKey, { authMode: options.authMode }), } : { name: "Responses API", @@ -723,7 +704,6 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { runCurlProbe( [ "-sS", - ...resolveArgs, ...getValidationProbeCurlArgs(), "-H", "Content-Type: application/json", @@ -746,7 +726,6 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { options.requireChatCompletionsToolCalling === true ? probeChatCompletionsToolCalling(endpointUrl, model, apiKey, { authMode: options.authMode, - resolveArgs, }) : runChatCompletionsProbe({ credentialArgs: authConfig.args, @@ -754,7 +733,6 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { url: `${baseUrl}/chat/completions`, isWsl: options.isWsl, trustedConfigFiles: authConfig.trustedConfigFiles, - resolveArgs, }), }; @@ -787,7 +765,6 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { const streamResult = runStreamingEventProbe( [ "-sS", - ...resolveArgs, ...getValidationProbeCurlArgs(), "-H", "Content-Type: application/json", diff --git a/src/lib/inference/probe-anthropic.ts b/src/lib/inference/probe-anthropic.ts index 5f5995e4b29..05f5a3ad30b 100644 --- a/src/lib/inference/probe-anthropic.ts +++ b/src/lib/inference/probe-anthropic.ts @@ -38,7 +38,6 @@ export function probeAnthropicEndpoint( endpointUrl: string, model: string, apiKey: string, - resolveArgs: readonly string[] = [], ): AnthropicProbeResult { let authConfig: ReturnType | undefined; try { @@ -46,8 +45,6 @@ export function probeAnthropicEndpoint( const result = runCurlProbe( [ "-sS", - // Pin the connection to the preflight-validated IP (rebinding-safe). - ...resolveArgs, ...getCurlTimingArgs(), ...authConfig.args, "-H", diff --git a/src/lib/net/dns-lookup.ts b/src/lib/net/dns-lookup.ts deleted file mode 100644 index 1b6725dcc15..00000000000 --- a/src/lib/net/dns-lookup.ts +++ /dev/null @@ -1,18 +0,0 @@ -// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. -// SPDX-License-Identifier: Apache-2.0 - -import { lookup } from "node:dns/promises"; - -/** - * Resolve all A/AAAA addresses for a hostname. A thin, stubbable wrapper around - * `node:dns/promises` `lookup(host, { all: true })` so callers (e.g. the - * custom-endpoint SSRF preflight wired in onboard.ts) can depend on a repo - * module that tests can override — builtin module exports are non-writable and - * cannot be monkeypatched. - */ -export async function resolveHostAddresses( - hostname: string, - options: { all: true }, -): Promise> { - return lookup(hostname, options); -} diff --git a/src/lib/onboard.ts b/src/lib/onboard.ts index faf49831178..a42ee5dc3a9 100644 --- a/src/lib/onboard.ts +++ b/src/lib/onboard.ts @@ -1036,12 +1036,6 @@ const { isNonInteractive, agentProductName, promptValidationRecovery, - // Real DNS resolver for the custom-endpoint SSRF preflight (PR #6293 PRA-3). - // Wired here at the composition root so the security boundary does not depend - // on an ambient env flag. Routed through the repo net/dns-lookup module (not - // node:dns directly) so tests can stub it — builtin exports are non-writable. - resolveEndpointHost: (hostname, options) => - require("./net/dns-lookup").resolveHostAddresses(hostname, options), }); const { validateSelectedRemoteModel } = createRemoteModelValidator({ OPENAI_ENDPOINT_URL, diff --git a/src/lib/onboard/inference-selection-validation.test.ts b/src/lib/onboard/inference-selection-validation.test.ts index 69b6f9edb27..82b2a91f590 100644 --- a/src/lib/onboard/inference-selection-validation.test.ts +++ b/src/lib/onboard/inference-selection-validation.test.ts @@ -166,15 +166,6 @@ describe("inference selection validation", () => { "COMPATIBLE_API_KEY", ), ).resolves.toEqual({ ok: true, api: "openai-completions" }); - // The probe is called with the pinned --resolve args from the preflight, so - // curl connects to the validated IP instead of re-resolving the hostname. - expect(probeOpenAiLikeEndpoint).toHaveBeenCalledWith( - "https://vllm.public.test/v1", - "model-a", - "test-key", - expect.objectContaining({ - resolveArgs: ["--resolve", "vllm.public.test:443:93.184.216.34"], - }), - ); + expect(probeOpenAiLikeEndpoint).toHaveBeenCalled(); }); }); diff --git a/src/lib/onboard/inference-selection-validation.ts b/src/lib/onboard/inference-selection-validation.ts index 8abf08b54dd..be716a1c3cb 100644 --- a/src/lib/onboard/inference-selection-validation.ts +++ b/src/lib/onboard/inference-selection-validation.ts @@ -9,7 +9,6 @@ const { probeAnthropicEndpoint, probeOpenAiLikeEndpoint } = endpointUrl: string, model: string, apiKey: string | null | undefined, - resolveArgs?: readonly string[], ): any; probeOpenAiLikeEndpoint( endpointUrl: string, @@ -21,7 +20,6 @@ const { probeAnthropicEndpoint, probeOpenAiLikeEndpoint } = import { assertEndpointResolvesPublic, - buildCurlResolveArgs, type EndpointDnsLookupFn, } from "../inference/endpoint-ssrf-preflight"; import { shouldForceCompletionsApi } from "../validation"; @@ -116,29 +114,22 @@ export function createInferenceSelectionValidationHelpers( // endpoint host and fails closed before any host-side probe curl when it (or // a resolved address) is private/reserved, so a public-looking name that // resolves to loopback/link-local/RFC1918 cannot reach internal services - // during privileged onboarding. On success it returns the `--resolve` curl - // args that pin the connection to the validated IP, so the probe curls cannot - // re-resolve (rebind) the hostname. Returns `{ blocked }` (fail-closed) to - // short-circuit, or `{ blocked: null, resolveArgs }` when it is safe to probe. - // - // Gated on an injected resolver rather than an ambient VITEST flag: production - // wires the real dns/promises resolver at the composition root (onboard.ts); - // tests inject a fake resolver to exercise it, or omit it to skip (no real - // DNS). See PR #6293 PRA-3/PRA-4. - async function preflightCustomEndpoint( + // during privileged onboarding. Returns a fail-closed EndpointValidationResult + // to short-circuit the caller, or null when the endpoint is safe to probe. + // See PR #6293 PRA-4. + async function preflightCustomEndpointOrFail( label: string, endpointUrl: string, credentialEnv: string | null, helpUrl: string | null, - ): Promise<{ blocked: EndpointValidationResult | null; resolveArgs: string[] }> { - if (!deps.resolveEndpointHost) return { blocked: null, resolveArgs: [] }; + ): Promise { + // Under the unit-test runner the default resolver would hit real DNS (and + // fixture hostnames do not resolve); skip unless a resolver is injected, + // mirroring applyCompatibleEndpointContextWindow's VITEST fetch guard. In + // production (VITEST unset) the real dns/promises resolver always runs. + if (!deps.resolveEndpointHost && process.env.VITEST === "true") return null; const preflight = await assertEndpointResolvesPublic(endpointUrl, deps.resolveEndpointHost); - if (preflight.ok) { - return { - blocked: null, - resolveArgs: buildCurlResolveArgs(endpointUrl, preflight.pinnedAddress), - }; - } + if (preflight.ok) return null; const syntheticProbe = { ok: false as const, message: preflight.reason, @@ -166,7 +157,7 @@ export function createInferenceSelectionValidationHelpers( console.log(" Please choose a provider/model again."); console.log(""); } - return { blocked: { ok: false, retry }, resolveArgs: [] }; + return { ok: false, retry }; } async function validateOpenAiLikeSelection( @@ -250,12 +241,7 @@ export function createInferenceSelectionValidationHelpers( credentialEnv: string, helpUrl: string | null = null, ): Promise { - const { blocked, resolveArgs } = await preflightCustomEndpoint( - label, - endpointUrl, - credentialEnv, - helpUrl, - ); + const blocked = await preflightCustomEndpointOrFail(label, endpointUrl, credentialEnv, helpUrl); if (blocked) return blocked; const apiKey = resolveCredential(credentialEnv); const reasoningEnabled = normalizeReasoningFlag(process.env.NEMOCLAW_REASONING) === "true"; @@ -265,9 +251,6 @@ export function createInferenceSelectionValidationHelpers( skipResponsesProbe: reasoningEnabled || shouldForceCompletionsApi(process.env.NEMOCLAW_PREFERRED_API), probeStreaming: !reasoningEnabled, - // Pin curl to the preflight-validated IP (rebinding-safe); only present - // when the preflight actually resolved a public name. - ...(resolveArgs.length > 0 ? { resolveArgs } : {}), }); if (probe.ok) { if (probe.note) { @@ -303,20 +286,10 @@ export function createInferenceSelectionValidationHelpers( credentialEnv: string, helpUrl: string | null = null, ): Promise { - const { blocked, resolveArgs } = await preflightCustomEndpoint( - label, - endpointUrl, - credentialEnv, - helpUrl, - ); + const blocked = await preflightCustomEndpointOrFail(label, endpointUrl, credentialEnv, helpUrl); if (blocked) return blocked; const apiKey = resolveCredential(credentialEnv); - // Pass the pinned --resolve args only when the preflight resolved a public - // name, so the unpinned call shape (and its assertions) is unchanged. - const probe = - resolveArgs.length > 0 - ? runAnthropicProbe(endpointUrl, model, apiKey, resolveArgs) - : runAnthropicProbe(endpointUrl, model, apiKey); + const probe = runAnthropicProbe(endpointUrl, model, apiKey); if (probe.ok) { console.log(` ${probe.label} available — ${deps.agentProductName()} will use ${probe.api}.`); return { ok: true, api: probe.api }; diff --git a/test/onboard-selection.test.ts b/test/onboard-selection.test.ts index 2dffbf0b1a7..bc1d84b486d 100644 --- a/test/onboard-selection.test.ts +++ b/test/onboard-selection.test.ts @@ -319,7 +319,7 @@ while [ "$#" -gt 0 ]; do fi shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; esac done # Also extract auth from ?key= query parameter (Gemini uses this instead of Bearer header) @@ -368,7 +368,7 @@ while [ "$#" -gt 0 ]; do fi shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; esac done if echo "$url" | grep -q '/v1/models$'; then @@ -3290,7 +3290,7 @@ url="" while [ "$#" -gt 0 ]; do case "$1" in -o) outfile="$2"; shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; esac done if echo "$url" | grep -q 'api.openai.com'; then @@ -3713,7 +3713,7 @@ while [ "$#" -gt 0 ]; do fi shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; esac done if echo "$auth" | grep -q 'nvapi-good' && echo "$url" | grep -q '/responses$'; then @@ -4135,10 +4135,7 @@ const { setupNim } = require(${onboardPath}); const credentials = require(${credentialsPath}); const runner = require(${runnerPath}); -// Public IP literal endpoint: the custom-endpoint SSRF preflight passes it -// without DNS (no resolver needed), keeping this auth-retry test focused on the -// re-enter-key UX. Preflight DNS behavior is unit-tested separately (#6293). -const answers = ["3", "https://8.8.8.8/v1/chat/completions?token=secret#frag", "custom-model", "retry", "proxy-good", "custom-model"]; +const answers = ["3", "https://proxy.example.com/v1/chat/completions?token=secret#frag", "custom-model", "retry", "proxy-good", "custom-model"]; const messages = []; credentials.prompt = async (message) => { @@ -4184,7 +4181,7 @@ const { setupNim } = require(${onboardPath}); const payload = JSON.parse(result.stdout.trim()); assert.equal(payload.result.provider, "compatible-endpoint"); assert.equal(payload.result.model, "custom-model"); - assert.equal(payload.result.endpointUrl, "https://8.8.8.8/v1"); + assert.equal(payload.result.endpointUrl, "https://proxy.example.com/v1"); assert.equal(payload.result.preferredInferenceApi, "openai-completions"); assert.equal(payload.key, "proxy-good"); assert.ok( @@ -4232,10 +4229,7 @@ const { setupNim } = require(${onboardPath}); const credentials = require(${credentialsPath}); const runner = require(${runnerPath}); -// Public IP literal endpoint: the custom-endpoint SSRF preflight passes it -// without DNS (no resolver needed), keeping this auth-retry test focused on the -// re-enter-key UX. Preflight DNS behavior is unit-tested separately (#6293). -const answers = ["5", "https://8.8.8.8/v1/messages?token=secret#frag", "claude-proxy", "retry", "anthropic-proxy-good", "claude-proxy"]; +const answers = ["5", "https://proxy.example.com/v1/messages?token=secret#frag", "claude-proxy", "retry", "anthropic-proxy-good", "claude-proxy"]; const messages = []; credentials.prompt = async (message) => { @@ -4281,7 +4275,7 @@ const { setupNim } = require(${onboardPath}); const payload = JSON.parse(result.stdout.trim()); assert.equal(payload.result.provider, "compatible-anthropic-endpoint"); assert.equal(payload.result.model, "claude-proxy"); - assert.equal(payload.result.endpointUrl, "https://8.8.8.8"); + assert.equal(payload.result.endpointUrl, "https://proxy.example.com"); assert.equal(payload.result.preferredInferenceApi, "anthropic-messages"); assert.equal(payload.key, "anthropic-proxy-good"); assert.ok( @@ -4333,7 +4327,7 @@ url="" while [ "$#" -gt 0 ]; do case "$1" in -o) outfile="$2"; shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; esac done if echo "$url" | grep -q '/v1/models'; then @@ -4437,7 +4431,7 @@ url="" while [ "$#" -gt 0 ]; do case "$1" in -o) outfile="$2"; shift 2 ;; - --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; --resolve) shift 2 ;; *) url="$1"; shift ;; + --config) auth="$(cat "$2" 2>/dev/null)"; shift 2 ;; *) url="$1"; shift ;; esac done if echo "$url" | grep -q '/v1/models'; then From 84c3d7dc7993e23233a6e415e9b0f40f7616afa5 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 10:12:36 -0700 Subject: [PATCH 19/41] fix(inference): always run endpoint SSRF preflight; drop VITEST bypass (#6293) cv review blocker: an ambient VITEST=true env var could disable SSRF enforcement. Remove the env-gated bypass in compatible-endpoint-context.ts and inference-selection-validation.ts so the DNS-backed preflight always runs, defaulting to the real dns/promises resolver in production. Tests inject a resolver (dependency injection) instead of relying on the env flag, and the test that asserted the removed bypass is deleted. Also closes the PRA-2 gap where the preflight was skipped when a custom fetcher was injected without a resolver. Co-Authored-By: Claude Opus 4.8 (1M context) Signed-off-by: Prekshi Vyas --- .../compatible-endpoint-context.test.ts | 12 +++---- .../inference/compatible-endpoint-context.ts | 31 +++++++++---------- .../inference-selection-validation.test.ts | 5 +++ .../onboard/inference-selection-validation.ts | 8 ++--- .../compatible-endpoint-context-probe.test.ts | 10 ++++++ 5 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/lib/inference/compatible-endpoint-context.test.ts b/src/lib/inference/compatible-endpoint-context.test.ts index b33a097a82d..7e2b7961dcc 100644 --- a/src/lib/inference/compatible-endpoint-context.test.ts +++ b/src/lib/inference/compatible-endpoint-context.test.ts @@ -24,6 +24,10 @@ async function apply( log: (message: string) => messages.push(message), warn: (message: string) => messages.push(message), }, + // Inject a clearly-public resolver so the unconditional DNS SSRF preflight + // passes for the endpoint.example host these cases probe (#6293). Cases that + // exercise SSRF refusal use inline calls with a private-address resolver. + resolveHost: async () => [{ address: "93.184.216.34", family: 4 }], ...options, }); return { env, messages }; @@ -136,13 +140,6 @@ describe("compatible-endpoint context window", () => { expect(messages.some((m) => m.includes("Could not read the endpoint's /v1/models"))).toBe(true); }); - it("skips the real host fetch under the unit-test runner unless one is injected", async () => { - const { env, messages } = await apply({}, { VITEST: "true" }); - - expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); - expect(messages).toEqual([]); - }); - it("clears its own stale auto value when a re-probed endpoint reports nothing (#6177)", async () => { // First endpoint auto-detects 65536 into the shared env. const env: NodeJS.ProcessEnv = {}; @@ -259,6 +256,7 @@ describe("compatible-endpoint context window", () => { await applyCompatibleEndpointContextWindow("https://public.example/v1", "model-a", { env, fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }), + resolveHost: async () => [{ address: "93.184.216.34", family: 4 }], logger: { log: () => undefined, warn: () => undefined }, }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts index 27c368ba4c3..e03199c50d4 100644 --- a/src/lib/inference/compatible-endpoint-context.ts +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -255,26 +255,23 @@ export async function applyCompatibleEndpointContextWindow( } const fetchModels = options.fetchModels; - if (!fetchModels && env.VITEST === "true") return; // DNS-backed SSRF: the string-level isPrivateEndpoint check above only sees // literal IPs and reserved names. This host-side GET is its own curl boundary - // (independent of the chat-completions validation probe), so pin the resolver - // preflight here — a public-looking name that resolves to loopback/link-local/ - // RFC1918 is refused before the fetch. Skipped when a fake fetcher is injected - // without a resolver (unit tests), so it never issues real DNS there; runs in - // production (no injected fetcher) and whenever a resolver is injected. - // See PR #6293 PRA-3 (GPT-5.5). - if (options.resolveHost || !fetchModels) { - const preflight = await assertEndpointResolvesPublic(endpointUrl, options.resolveHost); - if (!preflight.ok) { - logger.warn( - ` ⚠ ${preflight.reason}; skipping the /v1/models context probe. ` + - "Use a routable public URL to auto-detect the context window.", - ); - clearPreviousAuto(); - return; - } + // (independent of the chat-completions validation probe), so always run the + // resolver preflight — a public-looking name that resolves to loopback/ + // link-local/RFC1918 is refused before the fetch. It defaults to the real + // dns/promises resolver (assertEndpointResolvesPublic); tests inject + // options.resolveHost. No env-gated bypass: an ambient VITEST flag must never + // disable SSRF enforcement (cv review, PR #6293). + const preflight = await assertEndpointResolvesPublic(endpointUrl, options.resolveHost); + if (!preflight.ok) { + logger.warn( + ` ⚠ ${preflight.reason}; skipping the /v1/models context probe. ` + + "Use a routable public URL to auto-detect the context window.", + ); + clearPreviousAuto(); + return; } const resolveCredential = options.resolveCredential ?? getCredential; diff --git a/src/lib/onboard/inference-selection-validation.test.ts b/src/lib/onboard/inference-selection-validation.test.ts index 508352625af..8406b077c8d 100644 --- a/src/lib/onboard/inference-selection-validation.test.ts +++ b/src/lib/onboard/inference-selection-validation.test.ts @@ -60,6 +60,7 @@ describe("inference selection validation", () => { getCredential: () => "test-key", probeOpenAiLikeEndpoint, promptValidationRecovery, + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], }); try { @@ -182,6 +183,7 @@ describe("inference selection validation", () => { getCredential: () => "test-key", probeAnthropicEndpoint, promptValidationRecovery: vi.fn(async () => "selection" as const), + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], }); try { @@ -230,6 +232,7 @@ describe("inference selection validation", () => { probeAnthropicEndpoint, probeOpenAiLikeEndpoint, promptValidationRecovery: vi.fn(async () => "selection" as const), + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], }); try { @@ -269,6 +272,7 @@ describe("inference selection validation", () => { getCredential: () => "test-key", probeAnthropicEndpoint, promptValidationRecovery: vi.fn(async () => "selection" as const), + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], }); try { @@ -314,6 +318,7 @@ describe("inference selection validation", () => { getCredential: () => "test-key", probeAnthropicEndpoint, promptValidationRecovery, + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], }); try { diff --git a/src/lib/onboard/inference-selection-validation.ts b/src/lib/onboard/inference-selection-validation.ts index 58034723bae..2960888607d 100644 --- a/src/lib/onboard/inference-selection-validation.ts +++ b/src/lib/onboard/inference-selection-validation.ts @@ -128,11 +128,9 @@ export function createInferenceSelectionValidationHelpers( credentialEnv: string | null, helpUrl: string | null, ): Promise { - // Under the unit-test runner the default resolver would hit real DNS (and - // fixture hostnames do not resolve); skip unless a resolver is injected, - // mirroring applyCompatibleEndpointContextWindow's VITEST fetch guard. In - // production (VITEST unset) the real dns/promises resolver always runs. - if (!deps.resolveEndpointHost && process.env.VITEST === "true") return null; + // Always run the SSRF preflight. It defaults to the real dns/promises + // resolver; tests inject deps.resolveEndpointHost. No env-gated bypass — an + // ambient VITEST flag must never disable SSRF enforcement (cv review, #6293). const preflight = await assertEndpointResolvesPublic(endpointUrl, deps.resolveEndpointHost); if (preflight.ok) return null; const syntheticProbe = { diff --git a/test/compatible-endpoint-context-probe.test.ts b/test/compatible-endpoint-context-probe.test.ts index 4ad1c5a88aa..6f69bbd05dc 100644 --- a/test/compatible-endpoint-context-probe.test.ts +++ b/test/compatible-endpoint-context-probe.test.ts @@ -30,6 +30,11 @@ const MODEL = "nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4"; // by the unit tests in src/lib/inference/compatible-endpoint-context.test.ts. const PUBLIC_ENDPOINT_URL = "https://vllm.public.test/v1"; +// The DNS SSRF preflight now runs unconditionally, so inject a clearly-public +// resolver for the public hostname while the injected fetcher targets the +// loopback fake server (#6293). +const RESOLVE_PUBLIC = async () => [{ address: "93.184.216.34", family: 4 }]; + let server: FakeOpenAiCompatibleServer | null = null; function fetchFromServer(apiKey: string): () => unknown | null { @@ -55,6 +60,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { await applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { env, fetchModels: fetchFromServer(""), + resolveHost: RESOLVE_PUBLIC, }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); }); @@ -71,6 +77,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { env, apiKey: "secret-key", fetchModels: fetchFromServer("secret-key"), + resolveHost: RESOLVE_PUBLIC, }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("32768"); @@ -95,6 +102,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { env: noKeyEnv, apiKey: "", fetchModels: fetchFromServer(""), + resolveHost: RESOLVE_PUBLIC, }); expect(noKeyEnv.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); // Assert the endpoint actually rejected the unauthenticated /v1/models @@ -109,6 +117,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { env: keyedEnv, apiKey: "secret-key", fetchModels: fetchFromServer("secret-key"), + resolveHost: RESOLVE_PUBLIC, }); expect(keyedEnv.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); expect( @@ -148,6 +157,7 @@ describe("compatible-endpoint context probe against a real server (#6177)", { await applyCompatibleEndpointContextWindow(PUBLIC_ENDPOINT_URL, MODEL, { env, fetchModels: fetchFromServer(""), + resolveHost: RESOLVE_PUBLIC, }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); }); From c6b4e56d324480c4367f6bc1918bcfbaee825ba5 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 11:44:21 -0700 Subject: [PATCH 20/41] test(inference): inject SSRF resolvers + share hermes harness (#6293) Removing the ambient VITEST bypass made the DNS-backed SSRF preflight always run, so tests that relied on the bypass now hit real DNS. Inject a public resolver (dependency injection) into the affected onboard-selection, generate-hermes-config, compatible-endpoint-reasoning, and provider-inference context-window tests instead of re-enabling any env bypass. Extract the duplicated hermes-wrapper test harness into test/helpers/hermes-wrapper-harness.ts so the split one-shot-routing file no longer copies the helper's if-branch (codebase growth guard), relocate a cohesive Windows-host Ollama rejection sub-suite out of onboard-selection into a focused file, and ratchet its legacy test-file-size budget down to match. Co-Authored-By: Claude Opus 4.8 (1M context) Signed-off-by: Prekshi Vyas --- ci/test-file-size-budget.json | 2 +- .../provider-inference-context-window.test.ts | 3 +- test/generate-hermes-config.test.ts | 5 +- test/helpers/hermes-wrapper-harness.ts | 126 ++++++++++++++ test/hermes-gateway-wrapper.test.ts | 103 +----------- test/hermes-wrapper-oneshot-routing.test.ts | 102 +---------- ...lection-windows-provider-rejection.test.ts | 159 ++++++++++++++++++ test/onboard-selection.test.ts | 82 ++------- .../compatible-endpoint-reasoning.test.ts | 4 + 9 files changed, 308 insertions(+), 278 deletions(-) create mode 100644 test/helpers/hermes-wrapper-harness.ts create mode 100644 test/onboard-selection-windows-provider-rejection.test.ts diff --git a/ci/test-file-size-budget.json b/ci/test-file-size-budget.json index 62b508af933..b5f6964b94f 100644 --- a/ci/test-file-size-budget.json +++ b/ci/test-file-size-budget.json @@ -9,7 +9,7 @@ "test/install-preflight.test.ts": 3934, "test/nemoclaw-start.test.ts": 4827, "test/onboard-messaging.test.ts": 2062, - "test/onboard-selection.test.ts": 4834, + "test/onboard-selection.test.ts": 4774, "test/onboard.test.ts": 4057, "test/policies.test.ts": 2279 } diff --git a/src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts b/src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts index 204eb12286f..da529f91ae7 100644 --- a/src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts +++ b/src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts @@ -27,9 +27,10 @@ afterEach(() => { describe("handleProviderInferenceState context window", () => { it("clears a stale auto-detected compatible-endpoint context window before re-selecting (#6177)", async () => { // Simulate an earlier compatible-endpoint pass auto-detecting a window. - applyCompatibleEndpointContextWindow("https://endpoint-a.example/v1", "model-a", { + await applyCompatibleEndpointContextWindow("https://endpoint-a.example/v1", "model-a", { env: process.env, fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }), + resolveHost: async () => [{ address: "93.184.216.34", family: 4 }], }); expect(process.env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); diff --git a/test/generate-hermes-config.test.ts b/test/generate-hermes-config.test.ts index 89d9ea26181..aa28c7549fe 100644 --- a/test/generate-hermes-config.test.ts +++ b/test/generate-hermes-config.test.ts @@ -522,7 +522,7 @@ describe("agents/hermes/generate-config.ts", () => { expect(config.model.context_window).toBeUndefined(); }); - it("chains the endpoint probe through to model.context_length in the generated config (#6177)", () => { + it("chains the endpoint probe through to model.context_length in the generated config (#6177)", async () => { // Source-level regression across the boundary: the same probe onboarding // calls resolves a compatible endpoint's max_model_len into // NEMOCLAW_CONTEXT_WINDOW, and the real generator must bake it as @@ -532,9 +532,10 @@ describe("agents/hermes/generate-config.ts", () => { const model = "nvidia/NVIDIA-Nemotron-3-Super-120B-A12B-NVFP4"; fs.mkdirSync(path.join(tmpDir, ".hermes"), { recursive: true }); const env = buildHermesTestEnv({ NEMOCLAW_MODEL: model }); - applyCompatibleEndpointContextWindow("https://endpoint.example/v1", model, { + await applyCompatibleEndpointContextWindow("https://endpoint.example/v1", model, { env, fetchModels: () => ({ data: [{ id: model, max_model_len: 65_536 }] }), + resolveHost: async () => [{ address: "93.184.216.34", family: 4 }], }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); diff --git a/test/helpers/hermes-wrapper-harness.ts b/test/helpers/hermes-wrapper-harness.ts new file mode 100644 index 00000000000..607d45a5826 --- /dev/null +++ b/test/helpers/hermes-wrapper-harness.ts @@ -0,0 +1,126 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Shared test harness for the Hermes CLI wrapper suites +// (test/hermes-gateway-wrapper.test.ts and +// test/hermes-wrapper-oneshot-routing.test.ts). Both suites drive +// agents/hermes/hermes-wrapper.py by copying it into a temp dir alongside the +// runtime-env validator, planting stubs, and spawning it. Extracted here — a +// non-`.test.` module — so the shared `runWrapper` helper (and its planted-PATH +// `if` branch) lives in one place instead of being duplicated across the two +// files that were split for the test-file-size budget. + +import { spawnSync } from "node:child_process"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; + +export const WRAPPER = path.join( + import.meta.dirname, + "..", + "..", + "agents", + "hermes", + "hermes-wrapper.py", +); +export const VALIDATOR = path.join( + import.meta.dirname, + "..", + "..", + "agents", + "hermes", + "validate-env-secret-boundary.py", +); + +export function python3Available(): boolean { + try { + return spawnSync("python3", ["--version"], { timeout: 5000 }).status === 0; + } catch { + return false; + } +} +export const canRun = process.platform === "linux" && python3Available(); + +export type WrapperRun = { + status: number | null; + stdout: string; + stderr: string; + realInvoked: boolean; + realArgs: string; + realArgv: string[]; +}; + +export type StubBehaviour = { stdout?: string; stderr?: string; exitCode?: number }; + +export function runWrapper( + args: string[], + env: Record, + opts: { + shadowPython?: boolean; + shadowHelpers?: Record; + stub?: StubBehaviour; + stubMode?: number; + validatorScript?: string; + } = {}, +): WrapperRun { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-hermes-wrapper-")); + try { + fs.copyFileSync(WRAPPER, path.join(dir, "hermes")); + const validatorContent = opts.validatorScript ?? fs.readFileSync(VALIDATOR, "utf-8"); + // Source-layout filename lets the wrapper's dev fallback pick it up. + fs.writeFileSync(path.join(dir, "validate-env-secret-boundary.py"), validatorContent, { + mode: 0o755, + }); + fs.chmodSync(path.join(dir, "hermes"), 0o755); + + const marker = path.join(dir, "real-invoked.txt"); + const stubStdout = opts.stub?.stdout ?? ""; + const stubStderr = opts.stub?.stderr ?? ""; + const stubExit = opts.stub?.exitCode ?? 0; + const stubScript = [ + "#!/usr/bin/env bash", + `node -e 'require("node:fs").writeFileSync(process.argv[1], JSON.stringify(process.argv.slice(2)))' ${JSON.stringify(marker)} "$@"`, + stubStdout ? `cat <<'__NEMOCLAW_STUB_EOF__'\n${stubStdout}\n__NEMOCLAW_STUB_EOF__` : "", + stubStderr + ? `cat <<'__NEMOCLAW_STUB_ERR_EOF__' >&2\n${stubStderr}\n__NEMOCLAW_STUB_ERR_EOF__` + : "", + `exit ${stubExit}`, + "", + ].join("\n"); + fs.writeFileSync(path.join(dir, "hermes.real"), stubScript, { mode: opts.stubMode ?? 0o755 }); + + // Plant malicious helpers earlier on PATH; the wrapper must ignore them. + const planted: Record = { + ...(opts.shadowHelpers ?? {}), + ...(opts.shadowPython ? { python3: "#!/usr/bin/env bash\nexit 0\n" } : {}), + }; + let pathPrefix = ""; + if (Object.keys(planted).length > 0) { + const evilBin = path.join(dir, "evil-bin"); + fs.mkdirSync(evilBin); + for (const [name, script] of Object.entries(planted)) { + fs.writeFileSync(path.join(evilBin, name), script, { mode: 0o755 }); + } + pathPrefix = `${evilBin}${path.delimiter}`; + } + + const result = spawnSync(path.join(dir, "hermes"), args, { + encoding: "utf-8", + timeout: 10000, + env: { PATH: `${pathPrefix}${process.env.PATH ?? ""}`, HOME: dir, ...env }, + }); + + const realInvoked = fs.existsSync(marker); + const realArgv = realInvoked ? JSON.parse(fs.readFileSync(marker, "utf-8")) : []; + return { + status: result.status, + stdout: result.stdout ?? "", + stderr: result.stderr ?? "", + realInvoked, + realArgs: realArgv.join(" "), + realArgv, + }; + } finally { + fs.rmSync(dir, { recursive: true, force: true }); + } +} diff --git a/test/hermes-gateway-wrapper.test.ts b/test/hermes-gateway-wrapper.test.ts index 6f6ae238a4f..118e494ecbc 100644 --- a/test/hermes-gateway-wrapper.test.ts +++ b/test/hermes-gateway-wrapper.test.ts @@ -22,108 +22,7 @@ import { beforeAll, describe, expect, it } from "vitest"; import { buildHermesConfig } from "../agents/hermes/config/hermes-config.ts"; import { buildOpenshellExecArgs } from "../src/lib/actions/sandbox/exec.ts"; - -const WRAPPER = path.join(import.meta.dirname, "..", "agents", "hermes", "hermes-wrapper.py"); -const VALIDATOR = path.join( - import.meta.dirname, - "..", - "agents", - "hermes", - "validate-env-secret-boundary.py", -); - -function python3Available(): boolean { - try { - return spawnSync("python3", ["--version"], { timeout: 5000 }).status === 0; - } catch { - return false; - } -} -const canRun = process.platform === "linux" && python3Available(); - -type WrapperRun = { - status: number | null; - stdout: string; - stderr: string; - realInvoked: boolean; - realArgs: string; - realArgv: string[]; -}; - -type StubBehaviour = { stdout?: string; stderr?: string; exitCode?: number }; - -function runWrapper( - args: string[], - env: Record, - opts: { - shadowPython?: boolean; - shadowHelpers?: Record; - stub?: StubBehaviour; - stubMode?: number; - validatorScript?: string; - } = {}, -): WrapperRun { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-hermes-wrapper-")); - try { - fs.copyFileSync(WRAPPER, path.join(dir, "hermes")); - const validatorContent = opts.validatorScript ?? fs.readFileSync(VALIDATOR, "utf-8"); - // Source-layout filename lets the wrapper's dev fallback pick it up. - fs.writeFileSync(path.join(dir, "validate-env-secret-boundary.py"), validatorContent, { - mode: 0o755, - }); - fs.chmodSync(path.join(dir, "hermes"), 0o755); - - const marker = path.join(dir, "real-invoked.txt"); - const stubStdout = opts.stub?.stdout ?? ""; - const stubStderr = opts.stub?.stderr ?? ""; - const stubExit = opts.stub?.exitCode ?? 0; - const stubScript = [ - "#!/usr/bin/env bash", - `node -e 'require("node:fs").writeFileSync(process.argv[1], JSON.stringify(process.argv.slice(2)))' ${JSON.stringify(marker)} "$@"`, - stubStdout ? `cat <<'__NEMOCLAW_STUB_EOF__'\n${stubStdout}\n__NEMOCLAW_STUB_EOF__` : "", - stubStderr - ? `cat <<'__NEMOCLAW_STUB_ERR_EOF__' >&2\n${stubStderr}\n__NEMOCLAW_STUB_ERR_EOF__` - : "", - `exit ${stubExit}`, - "", - ].join("\n"); - fs.writeFileSync(path.join(dir, "hermes.real"), stubScript, { mode: opts.stubMode ?? 0o755 }); - - // Plant malicious helpers earlier on PATH; the wrapper must ignore them. - const planted: Record = { - ...(opts.shadowHelpers ?? {}), - ...(opts.shadowPython ? { python3: "#!/usr/bin/env bash\nexit 0\n" } : {}), - }; - let pathPrefix = ""; - if (Object.keys(planted).length > 0) { - const evilBin = path.join(dir, "evil-bin"); - fs.mkdirSync(evilBin); - for (const [name, script] of Object.entries(planted)) { - fs.writeFileSync(path.join(evilBin, name), script, { mode: 0o755 }); - } - pathPrefix = `${evilBin}${path.delimiter}`; - } - - const result = spawnSync(path.join(dir, "hermes"), args, { - encoding: "utf-8", - timeout: 10000, - env: { PATH: `${pathPrefix}${process.env.PATH ?? ""}`, HOME: dir, ...env }, - }); - - const realInvoked = fs.existsSync(marker); - const realArgv = realInvoked ? JSON.parse(fs.readFileSync(marker, "utf-8")) : []; - return { - status: result.status, - stdout: result.stdout ?? "", - stderr: result.stderr ?? "", - realInvoked, - realArgs: realArgv.join(" "), - realArgv, - }; - } finally { - fs.rmSync(dir, { recursive: true, force: true }); - } -} +import { canRun, runWrapper, VALIDATOR, WRAPPER } from "./helpers/hermes-wrapper-harness.ts"; describe.skipIf(!canRun)("agents/hermes/hermes-wrapper.py", () => { // Surface a hard error in CI when the prerequisites are missing instead of diff --git a/test/hermes-wrapper-oneshot-routing.test.ts b/test/hermes-wrapper-oneshot-routing.test.ts index fb037bbbf32..cecb9b3d6bc 100644 --- a/test/hermes-wrapper-oneshot-routing.test.ts +++ b/test/hermes-wrapper-oneshot-routing.test.ts @@ -22,107 +22,7 @@ import path from "node:path"; import { beforeAll, describe, expect, it } from "vitest"; -const WRAPPER = path.join(import.meta.dirname, "..", "agents", "hermes", "hermes-wrapper.py"); -const VALIDATOR = path.join( - import.meta.dirname, - "..", - "agents", - "hermes", - "validate-env-secret-boundary.py", -); - -function python3Available(): boolean { - try { - return spawnSync("python3", ["--version"], { timeout: 5000 }).status === 0; - } catch { - return false; - } -} -const canRun = process.platform === "linux" && python3Available(); - -type WrapperRun = { - status: number | null; - stdout: string; - stderr: string; - realInvoked: boolean; - realArgs: string; - realArgv: string[]; -}; - -type StubBehaviour = { stdout?: string; stderr?: string; exitCode?: number }; - -function runWrapper( - args: string[], - env: Record, - opts: { - shadowPython?: boolean; - shadowHelpers?: Record; - stub?: StubBehaviour; - stubMode?: number; - validatorScript?: string; - } = {}, -): WrapperRun { - const dir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-hermes-wrapper-")); - try { - fs.copyFileSync(WRAPPER, path.join(dir, "hermes")); - const validatorContent = opts.validatorScript ?? fs.readFileSync(VALIDATOR, "utf-8"); - // Source-layout filename lets the wrapper's dev fallback pick it up. - fs.writeFileSync(path.join(dir, "validate-env-secret-boundary.py"), validatorContent, { - mode: 0o755, - }); - fs.chmodSync(path.join(dir, "hermes"), 0o755); - - const marker = path.join(dir, "real-invoked.txt"); - const stubStdout = opts.stub?.stdout ?? ""; - const stubStderr = opts.stub?.stderr ?? ""; - const stubExit = opts.stub?.exitCode ?? 0; - const stubScript = [ - "#!/usr/bin/env bash", - `node -e 'require("node:fs").writeFileSync(process.argv[1], JSON.stringify(process.argv.slice(2)))' ${JSON.stringify(marker)} "$@"`, - stubStdout ? `cat <<'__NEMOCLAW_STUB_EOF__'\n${stubStdout}\n__NEMOCLAW_STUB_EOF__` : "", - stubStderr - ? `cat <<'__NEMOCLAW_STUB_ERR_EOF__' >&2\n${stubStderr}\n__NEMOCLAW_STUB_ERR_EOF__` - : "", - `exit ${stubExit}`, - "", - ].join("\n"); - fs.writeFileSync(path.join(dir, "hermes.real"), stubScript, { mode: opts.stubMode ?? 0o755 }); - - // Plant malicious helpers earlier on PATH; the wrapper must ignore them. - const planted: Record = { - ...(opts.shadowHelpers ?? {}), - ...(opts.shadowPython ? { python3: "#!/usr/bin/env bash\nexit 0\n" } : {}), - }; - let pathPrefix = ""; - if (Object.keys(planted).length > 0) { - const evilBin = path.join(dir, "evil-bin"); - fs.mkdirSync(evilBin); - for (const [name, script] of Object.entries(planted)) { - fs.writeFileSync(path.join(evilBin, name), script, { mode: 0o755 }); - } - pathPrefix = `${evilBin}${path.delimiter}`; - } - - const result = spawnSync(path.join(dir, "hermes"), args, { - encoding: "utf-8", - timeout: 10000, - env: { PATH: `${pathPrefix}${process.env.PATH ?? ""}`, HOME: dir, ...env }, - }); - - const realInvoked = fs.existsSync(marker); - const realArgv = realInvoked ? JSON.parse(fs.readFileSync(marker, "utf-8")) : []; - return { - status: result.status, - stdout: result.stdout ?? "", - stderr: result.stderr ?? "", - realInvoked, - realArgs: realArgv.join(" "), - realArgv, - }; - } finally { - fs.rmSync(dir, { recursive: true, force: true }); - } -} +import { canRun, runWrapper, WRAPPER } from "./helpers/hermes-wrapper-harness.ts"; describe.skipIf(!canRun)("agents/hermes/hermes-wrapper.py one-shot routing", () => { // Surface a hard error in CI when the prerequisites are missing instead of diff --git a/test/onboard-selection-windows-provider-rejection.test.ts b/test/onboard-selection-windows-provider-rejection.test.ts new file mode 100644 index 00000000000..337549361c4 --- /dev/null +++ b/test/onboard-selection-windows-provider-rejection.test.ts @@ -0,0 +1,159 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import assert from "node:assert/strict"; +import { describe, it, vi } from "vitest"; +import { getWindowsHostOllamaDockerRequirement } from "../src/lib/onboard/local-inference-topology.js"; +import { buildInferenceProviderMenu } from "../src/lib/onboard/provider-menu.js"; +import { resolveRequestedProviderSelection } from "../src/lib/onboard/provider-selection.js"; +import { reportProviderSelectionFailure } from "../src/lib/onboard/provider-selection-failure.js"; + +import { requireFailedProviderResolution } from "./support/onboard-selection-test-helpers.js"; + +const TEST_REMOTE_PROVIDER_CONFIG = { + build: { label: "NVIDIA Endpoints", providerName: "nvidia-prod" }, + openai: { label: "OpenAI", providerName: "openai-api" }, + custom: { + label: "Other OpenAI-compatible endpoint", + providerName: "compatible-endpoint", + }, + anthropic: { label: "Anthropic", providerName: "anthropic-prod" }, + anthropicCompatible: { + label: "Other Anthropic-compatible endpoint", + providerName: "compatible-anthropic-endpoint", + }, + gemini: { label: "Google Gemini", providerName: "gemini-api" }, +}; + +type WindowsRequirement = ReturnType; +type ProviderMenuOverrides = Partial[0]>; + +function buildProviderMenu(overrides: ProviderMenuOverrides = {}) { + return buildInferenceProviderMenu({ + remoteProviderConfig: TEST_REMOTE_PROVIDER_CONFIG, + agentProviderOptions: [], + experimental: false, + gpuNimCapable: false, + hasOllama: false, + ollamaRunning: false, + ollamaHost: null, + ollamaPort: 11434, + isWsl: false, + hasWindowsOllama: false, + isWindowsHostOllama: false, + windowsHostLabelSuffix: "", + windowsHostInstallLabel: "Install Ollama on Windows host (recommended)", + windowsHostStartLabel: () => "Start Ollama on Windows host (suggested)", + windowsOllamaReachable: false, + winOllamaLoopbackOnly: false, + ollamaInstallEntry: null, + vllmEntries: [], + routedEnabled: false, + ...overrides, + }); +} + +function buildWindowsProviderMenu( + requirement: WindowsRequirement, + overrides: ProviderMenuOverrides = {}, +) { + return buildProviderMenu({ + isWsl: true, + windowsHostLabelSuffix: requirement.supported ? "" : requirement.labelSuffix, + windowsHostInstallLabel: requirement.installLabel, + windowsHostStartLabel: requirement.startLabel, + ...overrides, + }); +} + +function resolveWindowsProvider( + options: Array<{ key: string; label: string }>, + requestedProvider: string, + overrides: Partial[0]> = {}, +) { + return resolveRequestedProviderSelection({ + options, + requestedProvider, + sandboxName: null, + remoteProviderConfig: TEST_REMOTE_PROVIDER_CONFIG, + isWsl: true, + isWindowsHostOllama: false, + windowsHostOllamaSupported: true, + hermesProviderAvailable: false, + readRecordedProvider: () => null, + readRecordedNimContainer: () => null, + readRecordedModel: () => null, + ...overrides, + }); +} + +describe("onboard Windows-host Ollama provider rejection", () => { + it("does not satisfy start-windows-ollama with WSL-local Ollama", () => { + const requirement = getWindowsHostOllamaDockerRequirement("docker-desktop"); + const { options } = buildWindowsProviderMenu(requirement, { + hasOllama: true, + ollamaRunning: true, + ollamaHost: "127.0.0.1", + hasWindowsOllama: false, + }); + const resolution = resolveWindowsProvider(options, "start-windows-ollama", { + isWsl: true, + isWindowsHostOllama: false, + }); + assert.equal(resolution.kind, "failure"); + const failedResolution = requireFailedProviderResolution(resolution); + + const setup = vi.fn(); + const switchHost = vi.fn(); + const errors: string[] = []; + reportProviderSelectionFailure({ + reason: failedResolution.reason, + isWindowsHostOllama: false, + rejectWindowsHostOllama: () => { + setup(); + switchHost(); + return true; + }, + writeError: (message) => errors.push(message), + }); + + assert.match(errors.join("\n"), /Requested provider 'start-windows-ollama' is not available/); + assert.equal(setup.mock.calls.length, 0); + assert.equal(switchHost.mock.calls.length, 0); + }); + + it("does not satisfy install-windows-ollama with non-WSL local Ollama", () => { + const requirement = getWindowsHostOllamaDockerRequirement(null); + const { options } = buildWindowsProviderMenu(requirement, { + hasOllama: true, + ollamaRunning: true, + ollamaHost: "127.0.0.1", + isWsl: false, + hasWindowsOllama: false, + }); + const resolution = resolveWindowsProvider(options, "install-windows-ollama", { + isWsl: false, + isWindowsHostOllama: false, + }); + assert.equal(resolution.kind, "failure"); + const failedResolution = requireFailedProviderResolution(resolution); + + const install = vi.fn(); + const setup = vi.fn(); + const errors: string[] = []; + reportProviderSelectionFailure({ + reason: failedResolution.reason, + isWindowsHostOllama: false, + rejectWindowsHostOllama: () => { + install(); + setup(); + return true; + }, + writeError: (message) => errors.push(message), + }); + + assert.match(errors.join("\n"), /Requested provider 'install-windows-ollama' is not available/); + assert.equal(install.mock.calls.length, 0); + assert.equal(setup.mock.calls.length, 0); + }); +}); diff --git a/test/onboard-selection.test.ts b/test/onboard-selection.test.ts index 9d08d3551c3..98786b70339 100644 --- a/test/onboard-selection.test.ts +++ b/test/onboard-selection.test.ts @@ -2861,6 +2861,7 @@ const { setupNim } = require(${onboardPath}); label: "Anthropic Messages API", }), promptValidationRecovery: recovery.promptValidationRecovery, + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], }); const state = makeRemoteSelectionState({ model, @@ -2926,6 +2927,7 @@ const { setupNim } = require(${onboardPath}); }; }, promptValidationRecovery: recovery.promptValidationRecovery, + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], }); const { validateSelectedRemoteModel } = createRemoteModelValidator( makeRemoteModelValidatorDeps({ @@ -2999,10 +3001,11 @@ const { setupNim } = require(${onboardPath}); getCredential: () => "ollama-key", probeOpenAiLikeEndpoint, promptValidationRecovery: recovery.promptValidationRecovery, + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], }); const state = makeRemoteSelectionState({ model: "my-model", - endpointUrl: "https://ollama.local:11434/v1", + endpointUrl: "https://ollama.public.test:11434/v1", }); const { validateSelectedRemoteModel } = createRemoteModelValidator( makeRemoteModelValidatorDeps({ @@ -3026,7 +3029,7 @@ const { setupNim } = require(${onboardPath}); assert.equal(state.preferredInferenceApi, "openai-completions"); assert.ok(lines.some((line) => line.includes("Using chat completions API"))); expect(probeOpenAiLikeEndpoint).toHaveBeenCalledWith( - "https://ollama.local:11434/v1", + "https://ollama.public.test:11434/v1", "my-model", "ollama-key", { @@ -3055,6 +3058,7 @@ const { setupNim } = require(${onboardPath}); getCredential: () => "sk-test", probeOpenAiLikeEndpoint, promptValidationRecovery: recovery.promptValidationRecovery, + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], }); const state = makeRemoteSelectionState({ model: "gpt-4o", @@ -3214,6 +3218,7 @@ const { setupNim } = require(${onboardPath}); }; }, promptValidationRecovery: recovery.promptValidationRecovery, + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], }); const { validateSelectedRemoteModel } = createRemoteModelValidator( makeRemoteModelValidatorDeps({ @@ -3761,6 +3766,10 @@ const { setupNim } = require(${onboardPath}); (async () => { process.env.COMPATIBLE_API_KEY = "proxy-bad"; + // The endpoint SSRF preflight now runs unconditionally (#6293); stub the DNS + // resolver to a public address so the fixture hostname resolves and the flow + // reaches validation instead of being refused (mirrors credentials/runner stubs). + require("node:dns/promises").lookup = async () => [{ address: "93.184.216.34", family: 4 }]; const originalLog = console.log; const originalError = console.error; const lines = []; @@ -4684,75 +4693,6 @@ const { setupNim } = require(${onboardPath}); } }); - it("does not satisfy start-windows-ollama with WSL-local Ollama", () => { - const requirement = getWindowsHostOllamaDockerRequirement("docker-desktop"); - const { options } = buildWindowsProviderMenu(requirement, { - hasOllama: true, - ollamaRunning: true, - ollamaHost: "127.0.0.1", - hasWindowsOllama: false, - }); - const resolution = resolveWindowsProvider(options, "start-windows-ollama", { - isWsl: true, - isWindowsHostOllama: false, - }); - assert.equal(resolution.kind, "failure"); - const failedResolution = requireFailedProviderResolution(resolution); - - const setup = vi.fn(); - const switchHost = vi.fn(); - const errors: string[] = []; - reportProviderSelectionFailure({ - reason: failedResolution.reason, - isWindowsHostOllama: false, - rejectWindowsHostOllama: () => { - setup(); - switchHost(); - return true; - }, - writeError: (message) => errors.push(message), - }); - - assert.match(errors.join("\n"), /Requested provider 'start-windows-ollama' is not available/); - assert.equal(setup.mock.calls.length, 0); - assert.equal(switchHost.mock.calls.length, 0); - }); - - it("does not satisfy install-windows-ollama with non-WSL local Ollama", () => { - const requirement = getWindowsHostOllamaDockerRequirement(null); - const { options } = buildWindowsProviderMenu(requirement, { - hasOllama: true, - ollamaRunning: true, - ollamaHost: "127.0.0.1", - isWsl: false, - hasWindowsOllama: false, - }); - const resolution = resolveWindowsProvider(options, "install-windows-ollama", { - isWsl: false, - isWindowsHostOllama: false, - }); - assert.equal(resolution.kind, "failure"); - const failedResolution = requireFailedProviderResolution(resolution); - - const install = vi.fn(); - const setup = vi.fn(); - const errors: string[] = []; - reportProviderSelectionFailure({ - reason: failedResolution.reason, - isWindowsHostOllama: false, - rejectWindowsHostOllama: () => { - install(); - setup(); - return true; - }, - writeError: (message) => errors.push(message), - }); - - assert.match(errors.join("\n"), /Requested provider 'install-windows-ollama' is not available/); - assert.equal(install.mock.calls.length, 0); - assert.equal(setup.mock.calls.length, 0); - }); - it("honours NEMOCLAW_LOCAL_INFERENCE_TIMEOUT for compatible-endpoint during inference setup (#2403)", () => { const repoRoot = path.join(import.meta.dirname, ".."); const tmpDir = fs.mkdtempSync( diff --git a/test/package-contract/onboard/compatible-endpoint-reasoning.test.ts b/test/package-contract/onboard/compatible-endpoint-reasoning.test.ts index 3236e77d2d7..46c047a5055 100644 --- a/test/package-contract/onboard/compatible-endpoint-reasoning.test.ts +++ b/test/package-contract/onboard/compatible-endpoint-reasoning.test.ts @@ -66,6 +66,10 @@ const { setupNim } = require(${onboardPath}); (async () => { process.env.COMPATIBLE_API_KEY = "proxy-key"; process.env.NEMOCLAW_REASONING = "yes"; + // The endpoint SSRF preflight now runs unconditionally (#6293); stub the DNS + // resolver to a public address so the fixture hostname resolves and the flow + // reaches validation instead of being refused (mirrors credentials/runner stubs). + require("node:dns/promises").lookup = async () => [{ address: "93.184.216.34", family: 4 }]; const originalLog = console.log; const originalError = console.error; const lines = []; From 6cfea8ef403f5e50464091197a4197112d3a5991 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 12:09:32 -0700 Subject: [PATCH 21/41] fix(inference): pin probe connections to preflight-validated address (#6293) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cv review blocker: the SSRF preflight validated the endpoint's resolved addresses but discarded them, and the subsequent host-side probe curls re-resolved the hostname — a second DNS lookup could rebind to a private or internal address after the public preflight passed (TOCTOU). assertEndpointResolvesPublic now returns the validated public addresses, carried through the probe boundary: custom OpenAI-like and Anthropic selection thread pinnedAddresses into the probes, which pin every host-side curl with --resolve (via buildResolvePinArgs) while leaving the URL, Host header, and TLS SNI intact. The /v1/models context fetch pins the same way. --resolve is added to the curl-arg allowlist. An orchestration test drives the real probe with a resolver that rebinds to a private address on the second lookup and asserts the connection stays pinned to the validated public address. Co-Authored-By: Claude Opus 4.8 (1M context) Signed-off-by: Prekshi Vyas --- src/lib/adapters/http/curl-args.ts | 12 ++- .../compatible-endpoint-context.test.ts | 6 +- .../inference/compatible-endpoint-context.ts | 28 +++++- src/lib/inference/endpoint-ssrf-preflight.ts | 54 ++++++++++- src/lib/inference/onboard-probes.ts | 24 ++++- src/lib/inference/probe-anthropic.ts | 10 ++ .../inference-selection-validation.test.ts | 93 ++++++++++++++++++- .../onboard/inference-selection-validation.ts | 34 +++++-- 8 files changed, 240 insertions(+), 21 deletions(-) diff --git a/src/lib/adapters/http/curl-args.ts b/src/lib/adapters/http/curl-args.ts index 9ab69d40d25..715297214c9 100644 --- a/src/lib/adapters/http/curl-args.ts +++ b/src/lib/adapters/http/curl-args.ts @@ -60,7 +60,17 @@ const CURL_SAFE_FLAG_OPTIONS = new Set([ // genuinely need to follow redirects from a fixed, hardcoded host (e.g. the // Ollama manifest probe) must opt in via CurlProbeArgOptions.allowRedirects. const CURL_REDIRECT_FLAG_OPTIONS = new Set(["-L", "-sfL", "--location"]); -const CURL_SAFE_VALUE_OPTIONS = new Set(["--connect-timeout", "--max-time", "-X", "--request"]); +// `--resolve ` pins the connection to an address the SSRF +// preflight already validated as public, closing the DNS-rebinding/TOCTOU +// window before a privileged onboarding probe curl (#6293). It carries no +// secret and reads no file, so it is a safe value-bearing option. +const CURL_SAFE_VALUE_OPTIONS = new Set([ + "--connect-timeout", + "--max-time", + "-X", + "--request", + "--resolve", +]); const CURL_FORBIDDEN_MULTI_TRANSFER_OPTIONS = new Set(["--next"]); const CURL_SHORT_OPTIONS_WITH_VALUES = new Set(["-K", "-b", "-T", "-d", "-F", "-H", "-X"]); diff --git a/src/lib/inference/compatible-endpoint-context.test.ts b/src/lib/inference/compatible-endpoint-context.test.ts index 7e2b7961dcc..49ced977340 100644 --- a/src/lib/inference/compatible-endpoint-context.test.ts +++ b/src/lib/inference/compatible-endpoint-context.test.ts @@ -39,7 +39,7 @@ describe("compatible-endpoint context window", () => { const { env, messages } = await apply({ fetchModels }); expect(env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); - expect(fetchModels).toHaveBeenCalledWith("https://endpoint.example/v1", ""); + expect(fetchModels).toHaveBeenCalledWith("https://endpoint.example/v1", "", ["93.184.216.34"]); expect(messages.some((m) => m.includes("65536"))).toBe(true); }); @@ -51,7 +51,9 @@ describe("compatible-endpoint context window", () => { resolveCredential: (name) => (name === "COMPATIBLE_API_KEY" ? "secret-key" : null), }); - expect(fetchModels).toHaveBeenCalledWith("https://endpoint.example/v1", "secret-key"); + expect(fetchModels).toHaveBeenCalledWith("https://endpoint.example/v1", "secret-key", [ + "93.184.216.34", + ]); }); it("picks the exact model entry from a multi-model gateway response (#6177)", async () => { diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts index e03199c50d4..d5a790f12cd 100644 --- a/src/lib/inference/compatible-endpoint-context.ts +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -5,7 +5,11 @@ import { createOpenAiLikeAuthConfig } from "../adapters/http/auth-config"; import { runCurlProbe } from "../adapters/http/probe"; import { getCredential } from "../credentials/store"; import { isLoopbackHostname, isPrivateHostname } from "../private-networks"; -import { assertEndpointResolvesPublic, type EndpointDnsLookupFn } from "./endpoint-ssrf-preflight"; +import { + assertEndpointResolvesPublic, + buildResolvePinArgs, + type EndpointDnsLookupFn, +} from "./endpoint-ssrf-preflight"; import { hasExplicitContextWindow, MAX_AUTODETECTED_OLLAMA_CONTEXT_WINDOW, @@ -59,6 +63,12 @@ function isPrivateEndpoint(endpointUrl: string): boolean { export type CompatibleEndpointModelsFetcher = ( endpointUrl: string, apiKey: string, + /** + * SSRF-preflight-validated address(es) to pin the fetch curl to via + * `--resolve` (TOCTOU/DNS-rebinding defense, #6293). Optional so injected test + * fakes can ignore it. + */ + pinnedAddresses?: string[], ) => unknown | null; export interface ApplyCompatibleEndpointContextWindowOptions { @@ -102,13 +112,18 @@ export interface ApplyCompatibleEndpointContextWindowOptions { * separately, not via a raw private-LAN URL. This fetcher therefore only ever * sees an already-validated, routable endpoint URL. */ -export function fetchCompatibleEndpointModels(endpointUrl: string, apiKey: string): unknown | null { +export function fetchCompatibleEndpointModels( + endpointUrl: string, + apiKey: string, + pinnedAddresses?: string[], +): unknown | null { const baseUrl = String(endpointUrl).replace(/\/+$/, ""); const authConfig = createOpenAiLikeAuthConfig(apiKey || ""); try { const result = runCurlProbe( [ "-sS", + ...buildResolvePinArgs(`${baseUrl}/models`, pinnedAddresses), "--connect-timeout", "10", "--max-time", @@ -278,7 +293,14 @@ export async function applyCompatibleEndpointContextWindow( const apiKey = options.apiKey ?? (options.credentialEnv ? resolveCredential(options.credentialEnv) : "") ?? ""; - const models = (fetchModels ?? fetchCompatibleEndpointModels)(endpointUrl, apiKey); + // Pin the /v1/models fetch to the address(es) the preflight just validated so + // a second DNS lookup in the fetch curl cannot rebind the host to a private + // address after the public preflight passed (TOCTOU — cv review, #6293). + const models = (fetchModels ?? fetchCompatibleEndpointModels)( + endpointUrl, + apiKey, + preflight.addresses, + ); if (models === null || models === undefined) { logger.warn( " ⚠ Could not read the endpoint's /v1/models max_model_len; using the default context " + diff --git a/src/lib/inference/endpoint-ssrf-preflight.ts b/src/lib/inference/endpoint-ssrf-preflight.ts index 616cc0446ed..2d3f8c916e9 100644 --- a/src/lib/inference/endpoint-ssrf-preflight.ts +++ b/src/lib/inference/endpoint-ssrf-preflight.ts @@ -16,6 +16,15 @@ export interface EndpointSsrfPreflightResult { ok: boolean; /** Human-readable reason, present only when `ok === false`. */ reason?: string; + /** + * Validated public addresses the endpoint host resolved to, for connection + * pinning (curl `--resolve`) so a subsequent probe cannot re-resolve the name + * to a rebound private/internal address (TOCTOU). Present only when + * `ok === true` and pinning applies — resolved public names and public IP + * literals. Absent for an explicit-loopback host (already local; no rebind + * surface) so callers connect normally. + */ + addresses?: string[]; } /** @@ -61,7 +70,7 @@ export async function assertEndpointResolvesPublic( // A public IP literal needs no DNS resolution. const bare = hostname.startsWith("[") && hostname.endsWith("]") ? hostname.slice(1, -1) : hostname; - if (isIP(bare)) return { ok: true }; + if (isIP(bare)) return { ok: true, addresses: [bare] }; let addresses: Array<{ address: string; family?: number }>; try { @@ -83,5 +92,46 @@ export async function assertEndpointResolvesPublic( }; } } - return { ok: true }; + return { ok: true, addresses: addresses.map(({ address }) => address) }; +} + +/** + * Build curl `--resolve ::` arguments that pin a probe's + * connection to the address(es) `assertEndpointResolvesPublic` already + * validated, while leaving the request URL (and therefore its Host header / TLS + * SNI) untouched. This closes the DNS-rebinding / TOCTOU window between the SSRF + * preflight and the privileged host-side probe curl: without pinning, curl would + * re-resolve the hostname and a second lookup could return a rebound + * private/internal address after the public preflight passed (cv review, #6293). + * + * `host` is the URL hostname (IPv6 brackets stripped, as curl `--resolve` + * expects a bare address); `port` is the explicit URL port or the scheme default + * (443 for https, 80 for http). Returns `[]` when there are no pinned addresses + * (explicit-loopback endpoints, or callers that never ran the preflight) so the + * probe connects normally, and `[]` on an unparseable URL. + */ +export function buildResolvePinArgs( + targetUrl: string, + pinnedAddresses?: readonly string[] | null, +): string[] { + if (!pinnedAddresses || pinnedAddresses.length === 0) return []; + let host: string; + let port: string; + try { + const url = new URL(String(targetUrl)); + host = + url.hostname.startsWith("[") && url.hostname.endsWith("]") + ? url.hostname.slice(1, -1) + : url.hostname; + port = url.port || (url.protocol === "https:" ? "443" : "80"); + } catch { + return []; + } + if (!host) return []; + const args: string[] = []; + for (const address of pinnedAddresses) { + if (!address) continue; + args.push("--resolve", `${host}:${port}:${address}`); + } + return args; } diff --git a/src/lib/inference/onboard-probes.ts b/src/lib/inference/onboard-probes.ts index 82b87584d42..10946ba481e 100644 --- a/src/lib/inference/onboard-probes.ts +++ b/src/lib/inference/onboard-probes.ts @@ -34,6 +34,7 @@ const { } = require("./onboard-host-docker-internal"); const { isNvcfFunctionNotFoundForAccount, nvcfFunctionNotFoundMessage } = require("../validation"); const { isPrivateHostname, isLoopbackHostname } = require("../private-networks"); +const { buildResolvePinArgs } = require("./endpoint-ssrf-preflight"); const { executeProbeWithHttpRetry, isProbeTimeout, @@ -290,6 +291,7 @@ function probeResponsesToolCalling(endpointUrl, model, apiKey, options = {}) { const result = runCurlProbe( [ "-sS", + ...buildResolvePinArgs(`${baseUrl}/responses`, options.pinnedAddresses), ...getValidationProbeCurlArgs(), "-H", "Content-Type: application/json", @@ -349,6 +351,7 @@ function probeChatCompletionsToolCalling(endpointUrl, model, apiKey, options = { const timingArgs = options.timingArgs ?? getChatCompletionsProbeTimingArgs(model); const args = [ "-sS", + ...buildResolvePinArgs(`${baseUrl}/chat/completions`, options.pinnedAddresses), ...timingArgs, "-H", "Content-Type: application/json", @@ -524,13 +527,15 @@ export function getChatCompletionsProbeCurlArgs(opts: { model: string; url: string; isWsl?: boolean; + pinnedAddresses?: readonly string[]; }) { - const { credentialArgs, authHeader, model, url, isWsl: isWslOverride } = opts; + const { credentialArgs, authHeader, model, url, isWsl: isWslOverride, pinnedAddresses } = opts; const platformOptions = typeof isWslOverride === "boolean" ? { isWsl: isWslOverride } : undefined; const timingArgs = getChatCompletionsProbeTimingArgs(model, platformOptions); const credSlice = credentialArgs ?? authHeader ?? []; return [ "-sS", + ...buildResolvePinArgs(url, pinnedAddresses), ...timingArgs, "-H", "Content-Type: application/json", @@ -547,12 +552,14 @@ function runChatCompletionsProbe({ url, isWsl: isWslOverride, trustedConfigFiles, + pinnedAddresses, }) { const args = getChatCompletionsProbeCurlArgs({ credentialArgs, model, url, isWsl: isWslOverride, + pinnedAddresses, }); const probeOpts = { timeoutMs: getProbeProcessTimeoutMs(args) }; if (trustedConfigFiles && trustedConfigFiles.length > 0) { @@ -581,6 +588,7 @@ function runDoubledTimeoutChatCompletionsRetry({ const doubledArgs = baseArgs.map((arg) => (/^\d+$/.test(arg) ? String(Number(arg) * 2) : arg)); const buildRetryArgs = () => [ "-sS", + ...buildResolvePinArgs(`${baseUrl}/chat/completions`, options.pinnedAddresses), ...doubledArgs, "-H", "Content-Type: application/json", @@ -594,6 +602,7 @@ function runDoubledTimeoutChatCompletionsRetry({ ? probeChatCompletionsToolCalling(endpointUrl, model, apiKey, { authMode: options.authMode, timingArgs: doubledArgs, + pinnedAddresses: options.pinnedAddresses, }) : (() => { const retryArgs = buildRetryArgs(); @@ -686,6 +695,10 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { } const baseUrl = String(endpointUrl).replace(/\/+$/, ""); + // Pin every probe curl to the SSRF-preflight-validated address(es) the caller + // captured, so a second DNS lookup here cannot rebind the hostname to a + // private/internal address after the public preflight (TOCTOU — cv, #6293). + const pinnedAddresses = options.pinnedAddresses; let authConfig; try { authConfig = buildOpenAiLikeAuthConfig(apiKey, options); @@ -695,7 +708,10 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { name: "Responses API with tool calling", api: "openai-responses", execute: () => - probeResponsesToolCalling(endpointUrl, model, apiKey, { authMode: options.authMode }), + probeResponsesToolCalling(endpointUrl, model, apiKey, { + authMode: options.authMode, + pinnedAddresses, + }), } : { name: "Responses API", @@ -704,6 +720,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { runCurlProbe( [ "-sS", + ...buildResolvePinArgs(`${baseUrl}/responses`, pinnedAddresses), ...getValidationProbeCurlArgs(), "-H", "Content-Type: application/json", @@ -726,6 +743,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { options.requireChatCompletionsToolCalling === true ? probeChatCompletionsToolCalling(endpointUrl, model, apiKey, { authMode: options.authMode, + pinnedAddresses, }) : runChatCompletionsProbe({ credentialArgs: authConfig.args, @@ -733,6 +751,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { url: `${baseUrl}/chat/completions`, isWsl: options.isWsl, trustedConfigFiles: authConfig.trustedConfigFiles, + pinnedAddresses, }), }; @@ -765,6 +784,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { const streamResult = runStreamingEventProbe( [ "-sS", + ...buildResolvePinArgs(`${baseUrl}/responses`, pinnedAddresses), ...getValidationProbeCurlArgs(), "-H", "Content-Type: application/json", diff --git a/src/lib/inference/probe-anthropic.ts b/src/lib/inference/probe-anthropic.ts index 921143e616b..da153ea1603 100644 --- a/src/lib/inference/probe-anthropic.ts +++ b/src/lib/inference/probe-anthropic.ts @@ -14,6 +14,7 @@ import { runCurlProbe, } from "../adapters/http/probe"; import { normalizeCredentialValue } from "../credentials/store"; +import { buildResolvePinArgs } from "./endpoint-ssrf-preflight"; export type AnthropicStreamingDiagnosticCode = | "anthropic-streaming-content-after-message-stop" @@ -50,6 +51,12 @@ export interface AnthropicProbeOptions { * surfaces in-sandbox as "no final response was produced" (#6289). */ probeStreaming?: boolean; + /** + * SSRF-preflight-validated address(es) to pin the probe curl to via + * `--resolve`, so a second DNS lookup here cannot rebind the endpoint host to + * a private/internal address after the public preflight (TOCTOU — #6293). + */ + pinnedAddresses?: readonly string[]; } // Streaming validation must not hang the onboarding wizard on an endpoint @@ -123,9 +130,11 @@ export function probeAnthropicEndpoint( try { authConfig = createXApiKeyAuthConfig(normalizeCredentialValue(apiKey)); const messagesUrl = `${String(endpointUrl).replace(/\/+$/, "")}/v1/messages`; + const resolvePinArgs = buildResolvePinArgs(messagesUrl, options.pinnedAddresses); const result = runCurlProbe( [ "-sS", + ...resolvePinArgs, ...getCurlTimingArgs(), ...authConfig.args, "-H", @@ -157,6 +166,7 @@ export function probeAnthropicEndpoint( const streamResult = runAnthropicStreamingEventProbe( [ "-sS", + ...resolvePinArgs, ...STREAMING_PROBE_TIMING_ARGS, ...authConfig.args, "-H", diff --git a/src/lib/onboard/inference-selection-validation.test.ts b/src/lib/onboard/inference-selection-validation.test.ts index 8406b077c8d..9f96f95ad7b 100644 --- a/src/lib/onboard/inference-selection-validation.test.ts +++ b/src/lib/onboard/inference-selection-validation.test.ts @@ -1,6 +1,9 @@ // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; import { describe, expect, it, vi } from "vitest"; import { createInferenceSelectionValidationHelpers } from "./inference-selection-validation"; @@ -80,6 +83,7 @@ describe("inference selection validation", () => { requireResponsesToolCalling: false, skipResponsesProbe: true, probeStreaming: false, + pinnedAddresses: ["93.184.216.34"], }, ); } finally { @@ -199,7 +203,7 @@ describe("inference selection validation", () => { "https://compatible.example", "nvidia/nemotron-3-super-v3", "test-key", - { probeStreaming: true }, + { probeStreaming: true, pinnedAddresses: ["93.184.216.34"] }, ); } finally { log.mockRestore(); @@ -250,7 +254,7 @@ describe("inference selection validation", () => { "https://compatible.example/v1", "nvidia/nemotron-3-super-v3", "test-key", - { skipResponsesProbe: true }, + { skipResponsesProbe: true, pinnedAddresses: ["93.184.216.34"] }, ); expect(probeAnthropicEndpoint).not.toHaveBeenCalled(); } finally { @@ -286,7 +290,7 @@ describe("inference selection validation", () => { "https://compatible.example", "reasoning-model", "test-key", - { probeStreaming: false }, + { probeStreaming: false, pinnedAddresses: ["93.184.216.34"] }, ); } finally { log.mockRestore(); @@ -294,6 +298,89 @@ describe("inference selection validation", () => { } }); + it("pins the probe connection to the preflight-validated address against DNS rebinding (#6293)", async () => { + // Orchestration proof: the SSRF preflight validates the endpoint host to a + // PUBLIC address, then the probe must connect to exactly that address via + // curl --resolve. The injected resolver would hand back a PRIVATE address on + // a second lookup (a rebind), so if the probe re-resolved the name instead of + // pinning, it would reach 10.0.0.5. Asserting the real probe's curl argv + // carries --resolve ::93.184.216.34 proves the connection is + // pinned to the validated public IP and cannot be rebound. + vi.stubEnv("NEMOCLAW_REASONING", "yes"); + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-pin-orchestration-")); + const fakeBin = path.join(tmpDir, "bin"); + const argsPath = path.join(tmpDir, "args.txt"); + fs.mkdirSync(fakeBin, { recursive: true }); + fs.writeFileSync( + path.join(fakeBin, "curl"), + `#!/usr/bin/env bash +printf '%s\\n' "$@" > "${argsPath}" +outfile="" +while [ "$#" -gt 0 ]; do + case "$1" in + -o) outfile="$2"; shift 2 ;; + -w) shift 2 ;; + *) shift ;; + esac +done +if [ -n "$outfile" ]; then + cat <<'JSON' > "$outfile" +{"choices":[{"message":{"content":"OK"}}]} +JSON +fi +printf '200' +exit 0 +`, + { mode: 0o755 }, + ); + + let resolveCall = 0; + const resolveEndpointHost = vi.fn(async () => { + resolveCall += 1; + // First lookup (the preflight) returns a public address; a hypothetical + // second lookup would rebind to a private address. + return resolveCall === 1 + ? [{ address: "93.184.216.34", family: 4 }] + : [{ address: "10.0.0.5", family: 4 }]; + }); + + const log = vi.spyOn(console, "log").mockImplementation(() => {}); + const originalPath = process.env.PATH; + process.env.PATH = `${fakeBin}:${originalPath || ""}`; + const helpers = createInferenceSelectionValidationHelpers({ + isNonInteractive: () => false, + agentProductName: () => "OpenClaw", + getCredential: () => "test-key", + // Use the real probeOpenAiLikeEndpoint (no injection) so the full + // preflight → pinnedAddresses → curl --resolve chain is exercised. + promptValidationRecovery: vi.fn(async () => "selection" as const), + resolveEndpointHost, + }); + + try { + await expect( + helpers.validateCustomOpenAiLikeSelection( + "Custom endpoint", + "https://public-name.example/v1", + "model-a", + "COMPATIBLE_API_KEY", + ), + ).resolves.toEqual({ ok: true, api: "openai-completions" }); + + const recordedArgs = fs.readFileSync(argsPath, "utf8").split("\n"); + const resolveIdx = recordedArgs.indexOf("--resolve"); + expect(resolveIdx).toBeGreaterThanOrEqual(0); + expect(recordedArgs[resolveIdx + 1]).toBe("public-name.example:443:93.184.216.34"); + // The rebound private address must never appear in the pin. + expect(recordedArgs.join("\n")).not.toContain("10.0.0.5"); + } finally { + process.env.PATH = originalPath; + log.mockRestore(); + vi.unstubAllEnvs(); + fs.rmSync(tmpDir, { recursive: true, force: true }); + } + }); + it("keeps rejecting malformed native Anthropic streams for OpenClaw (#6289)", async () => { const probeAnthropicEndpoint = vi.fn(() => ({ ok: false, diff --git a/src/lib/onboard/inference-selection-validation.ts b/src/lib/onboard/inference-selection-validation.ts index 2960888607d..0da5269b286 100644 --- a/src/lib/onboard/inference-selection-validation.ts +++ b/src/lib/onboard/inference-selection-validation.ts @@ -127,12 +127,16 @@ export function createInferenceSelectionValidationHelpers( endpointUrl: string, credentialEnv: string | null, helpUrl: string | null, - ): Promise { + ): Promise<{ blocked: EndpointValidationResult } | { pinnedAddresses?: string[] }> { // Always run the SSRF preflight. It defaults to the real dns/promises // resolver; tests inject deps.resolveEndpointHost. No env-gated bypass — an // ambient VITEST flag must never disable SSRF enforcement (cv review, #6293). const preflight = await assertEndpointResolvesPublic(endpointUrl, deps.resolveEndpointHost); - if (preflight.ok) return null; + // On success, carry the validated address set forward so the probe pins its + // connection (curl --resolve) to a checked address; a second DNS lookup at + // the probe could otherwise rebind to a private/internal address after this + // public preflight (TOCTOU — cv review, #6293). + if (preflight.ok) return { pinnedAddresses: preflight.addresses }; const syntheticProbe = { ok: false as const, message: preflight.reason, @@ -160,7 +164,7 @@ export function createInferenceSelectionValidationHelpers( console.log(" Please choose a provider/model again."); console.log(""); } - return { ok: false, retry }; + return { blocked: { ok: false, retry } }; } async function validateOpenAiLikeSelection( @@ -244,8 +248,14 @@ export function createInferenceSelectionValidationHelpers( credentialEnv: string, helpUrl: string | null = null, ): Promise { - const blocked = await preflightCustomEndpointOrFail(label, endpointUrl, credentialEnv, helpUrl); - if (blocked) return blocked; + const preflight = await preflightCustomEndpointOrFail( + label, + endpointUrl, + credentialEnv, + helpUrl, + ); + if ("blocked" in preflight) return preflight.blocked; + const { pinnedAddresses } = preflight; const apiKey = resolveCredential(credentialEnv); const reasoningEnabled = normalizeReasoningFlag(process.env.NEMOCLAW_REASONING) === "true"; // Reasoning-only compatible endpoints often reject Responses, tool-call, and streaming probes. @@ -254,6 +264,7 @@ export function createInferenceSelectionValidationHelpers( skipResponsesProbe: reasoningEnabled || shouldForceCompletionsApi(process.env.NEMOCLAW_PREFERRED_API), probeStreaming: !reasoningEnabled, + pinnedAddresses, }); if (probe.ok) { if (probe.note) { @@ -292,8 +303,14 @@ export function createInferenceSelectionValidationHelpers( intendedApi?: "anthropic-messages" | "openai-completions"; } = {}, ): Promise { - const blocked = await preflightCustomEndpointOrFail(label, endpointUrl, credentialEnv, helpUrl); - if (blocked) return blocked; + const preflight = await preflightCustomEndpointOrFail( + label, + endpointUrl, + credentialEnv, + helpUrl, + ); + if ("blocked" in preflight) return preflight.blocked; + const { pinnedAddresses } = preflight; const apiKey = resolveCredential(credentialEnv); const reasoningEnabled = normalizeReasoningFlag(process.env.NEMOCLAW_REASONING) === "true"; const intendedApi = options.intendedApi ?? "anthropic-messages"; @@ -307,12 +324,13 @@ export function createInferenceSelectionValidationHelpers( getCompatibleAnthropicOpenAiSurfaceBaseUrl(endpointUrl), model, apiKey, - { skipResponsesProbe: true }, + { skipResponsesProbe: true, pinnedAddresses }, ) : runAnthropicProbe(endpointUrl, model, apiKey, { // Reasoning-only compatible endpoints often reject streaming probes, // so mirror the custom OpenAI-compatible path and skip streaming. probeStreaming: !reasoningEnabled, + pinnedAddresses, }); if (probe.ok) { if (probe.note) { From 139fc634f10cbc590cc3c9cee1638dda9fced5b3 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 12:20:14 -0700 Subject: [PATCH 22/41] fix(onboard): clear auto-detected context window on all selection paths (#6293) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PRA-3: the compatible-endpoint context-window auto-state was cleared only on the fresh provider-selection branch, so a resume or repair pass could inherit a stale probed max_model_len from an earlier endpoint as if it were a user override. Move the clear to the top of the provider-selection retry loop so it runs on every path — fresh, resume, and repair — while still only dropping a value this process auto-detected. Add resume-path coverage (PRA-5). Co-Authored-By: Claude Opus 4.8 (1M context) Signed-off-by: Prekshi Vyas --- .../provider-inference-context-window.test.ts | 24 +++++++++++++++++++ .../machine/handlers/provider-inference.ts | 11 +++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts b/src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts index da529f91ae7..a53a799681e 100644 --- a/src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts +++ b/src/lib/onboard/machine/handlers/provider-inference-context-window.test.ts @@ -47,4 +47,28 @@ describe("handleProviderInferenceState context window", () => { expect(setupNim).toHaveBeenCalledTimes(1); expect(observed).toEqual([undefined]); }); + + it("clears the stale auto-detected context window on the resume path too (#6293)", async () => { + // Simulate an earlier compatible-endpoint pass auto-detecting a window. + await applyCompatibleEndpointContextWindow("https://endpoint-a.example/v1", "model-a", { + env: process.env, + fetchModels: () => ({ data: [{ id: "model-a", max_model_len: 65_536 }] }), + resolveHost: async () => [{ address: "93.184.216.34", family: 4 }], + }); + expect(process.env.NEMOCLAW_CONTEXT_WINDOW).toBe("65536"); + + // PRA-3: the clear now runs at the top of the provider-selection loop, so a + // resume pass drops the stale auto value just like a fresh selection — it is + // no longer gated on the fresh-only branch. + const observed: Array = []; + const setupNim = vi.fn(async () => { + observed.push(process.env.NEMOCLAW_CONTEXT_WINDOW); + return { ...baseSelection }; + }); + const { deps } = createDeps({ setupNim }); + await handleProviderInferenceState({ ...baseOptions(deps), resume: true }); + + expect(process.env.NEMOCLAW_CONTEXT_WINDOW).toBeUndefined(); + expect(observed).toEqual([undefined]); + }); }); diff --git a/src/lib/onboard/machine/handlers/provider-inference.ts b/src/lib/onboard/machine/handlers/provider-inference.ts index ada940fb0b2..dbee8c309e8 100644 --- a/src/lib/onboard/machine/handlers/provider-inference.ts +++ b/src/lib/onboard/machine/handlers/provider-inference.ts @@ -281,6 +281,13 @@ export async function handleProviderInferenceState({ const retryStateResults: OnboardStateTransitionResult[] = []; while (true) { + // Drop a context window auto-detected by a prior compatible-endpoint pass + // before every provider-selection path — fresh, resume, and repair — so a + // retry to a different provider/endpoint cannot inherit endpoint A's probed + // max_model_len as a bogus user override. Only clears a value this process + // auto-detected, never a user override or a legitimately resumed window + // (#6177; resume/repair coverage per PR #6293 PRA-3). + clearAutoDetectedCompatibleContextWindow(process.env); let forceInferenceSetup = initialForceInferenceSetup; const resumeProviderSelection = !forceProviderSelection && @@ -383,10 +390,6 @@ export async function handleProviderInferenceState({ } } else { await deps.startRecordedStep("provider_selection"); - // Drop a context window auto-detected by a prior compatible-endpoint pass - // before re-selecting, so retrying to a different provider cannot inherit - // endpoint A's probed max_model_len as a bogus user override (#6177). - clearAutoDetectedCompatibleContextWindow(process.env); const selection = await withProviderSelectionTrace( sandboxName, (agent as { name?: string } | null)?.name, From 191efbc410929e29e6d1e975f1618cbb75ed6cd0 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 12:22:00 -0700 Subject: [PATCH 23/41] refactor(inference): extract pure HTTP probe helpers into a focused module (#6293) PRA-1: move the pure curl-arg and timing helpers (getValidationProbeCurlArgs, the DeepSeek/Kimi/extended-NVIDIA variants, getCurlMaxTimeSeconds, withValidationMaxTimeOverride, getProbeProcessTimeoutMs) out of the growing onboard-probes.ts monolith into a new, fully-typed probe-http-helpers.ts, with their unit test moved alongside. onboard-probes.ts imports and re-exports them, so behavior is identical; the network/curl-executing probes stay put. Co-Authored-By: Claude Opus 4.8 (1M context) Signed-off-by: Prekshi Vyas --- src/lib/inference/onboard-probes.test.ts | 33 --------- src/lib/inference/onboard-probes.ts | 63 ++---------------- src/lib/inference/probe-http-helpers.test.ts | 43 ++++++++++++ src/lib/inference/probe-http-helpers.ts | 70 ++++++++++++++++++++ 4 files changed, 120 insertions(+), 89 deletions(-) create mode 100644 src/lib/inference/probe-http-helpers.test.ts create mode 100644 src/lib/inference/probe-http-helpers.ts diff --git a/src/lib/inference/onboard-probes.test.ts b/src/lib/inference/onboard-probes.test.ts index bcc2215470a..b07da7057af 100644 --- a/src/lib/inference/onboard-probes.test.ts +++ b/src/lib/inference/onboard-probes.test.ts @@ -19,7 +19,6 @@ const { getChatCompletionsProbePayload, getDeepSeekV4ProValidationProbeCurlArgs, getKimiK26ValidationProbeCurlArgs, - getValidationProbeCurlArgs, hasChatCompletionsToolCall, hasChatCompletionsToolCallLeak, hasResponsesToolCall, @@ -28,17 +27,6 @@ const { RETRIABLE_HTTP_PROBE_STATUSES, } = require("./onboard-probes"); -// Restore an env var to its pre-test value without branching at the call -// site. Centralizing the conditional keeps test bodies linear and keeps the -// codebase-growth-guardrails "if count" steady; see PR #5975 review. -function restoreEnv(name: string, original: string | undefined): void { - if (original === undefined) { - delete process.env[name]; - } else { - process.env[name] = original; - } -} - const FAKE_CONFIG_PATH = "/tmp/nemoclaw-test-credential.conf"; const FAKE_CREDENTIAL_ARGS = ["--config", FAKE_CONFIG_PATH] as const; @@ -321,27 +309,6 @@ describe("OpenAI-compatible inference probes", () => { }); }); - it("allows onboard validation max-time to be raised from the environment", () => { - const original = process.env.NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS; - process.env.NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS = "300"; - try { - expect(getValidationProbeCurlArgs({ isWsl: false })).toEqual([ - "--connect-timeout", - "10", - "--max-time", - "300", - ]); - expect(getKimiK26ValidationProbeCurlArgs({ isWsl: false })).toEqual([ - "--connect-timeout", - "10", - "--max-time", - "300", - ]); - } finally { - restoreEnv("NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS", original); - } - }); - it("uses an extended validation budget for slow NVIDIA Build models", () => { for (const model of ["qwen/qwen3.5-397b-a17b", "deepseek-ai/deepseek-v4-flash"]) { const args = getChatCompletionsProbeCurlArgs({ diff --git a/src/lib/inference/onboard-probes.ts b/src/lib/inference/onboard-probes.ts index 10946ba481e..456f36b1058 100644 --- a/src/lib/inference/onboard-probes.ts +++ b/src/lib/inference/onboard-probes.ts @@ -43,6 +43,13 @@ const { runChatCompletionsRetryLoop, } = require("./probe-retry"); const { probeAnthropicEndpoint } = require("./probe-anthropic"); +const { + getValidationProbeCurlArgs, + getDeepSeekV4ProValidationProbeCurlArgs, + getKimiK26ValidationProbeCurlArgs, + getExtendedNvidiaEndpointValidationProbeCurlArgs, + getProbeProcessTimeoutMs, +} = require("./probe-http-helpers"); const { getCurlTimingArgs, @@ -81,7 +88,6 @@ function openAiLikeFailureFromError(error) { // ── Helpers ────────────────────────────────────────────────────── -const ONBOARD_VALIDATION_TIMEOUT_ENV = "NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS"; const EXTENDED_NVIDIA_ENDPOINT_VALIDATION_MODELS = new Set([ "qwen/qwen3.5-397b-a17b", "deepseek-ai/deepseek-v4-flash", @@ -226,61 +232,6 @@ function getProbeAuthMode(_provider) { return undefined; } -// Per-validation-probe curl timing. Tighter than the default 60s in -// getCurlTimingArgs() because validation must not hang the wizard for a -// minute on a misbehaving model. See issue #1601 (Bug 3). -function getValidationProbeCurlArgs(opts) { - const args = isWsl(opts) - ? ["--connect-timeout", "20", "--max-time", "30"] - : ["--connect-timeout", "10", "--max-time", "15"]; - return withValidationMaxTimeOverride(args); -} - -function getDeepSeekV4ProValidationProbeCurlArgs(opts) { - const args = isWsl(opts) - ? ["--connect-timeout", "30", "--max-time", "150"] - : ["--connect-timeout", "20", "--max-time", "120"]; - return withValidationMaxTimeOverride(args); -} - -function getKimiK26ValidationProbeCurlArgs(opts) { - const args = isWsl(opts) - ? ["--connect-timeout", "20", "--max-time", "90"] - : ["--connect-timeout", "10", "--max-time", "60"]; - return withValidationMaxTimeOverride(args); -} - -function getExtendedNvidiaEndpointValidationProbeCurlArgs(opts) { - const args = isWsl(opts) - ? ["--connect-timeout", "30", "--max-time", "300"] - : ["--connect-timeout", "10", "--max-time", "300"]; - return withValidationMaxTimeOverride(args); -} - -function getCurlMaxTimeSeconds(args) { - const maxTimeIndex = args.indexOf("--max-time"); - if (maxTimeIndex === -1) return 30; - const value = Number(args[maxTimeIndex + 1]); - return Number.isFinite(value) && value > 0 ? value : 30; -} - -function withValidationMaxTimeOverride(args) { - const raw = (process.env[ONBOARD_VALIDATION_TIMEOUT_ENV] || "").trim(); - if (!raw) return args; - const overrideSeconds = Math.ceil(Number(raw)); - if (!Number.isFinite(overrideSeconds) || overrideSeconds <= 0) return args; - if (overrideSeconds <= getCurlMaxTimeSeconds(args)) return args; - const maxTimeIndex = args.indexOf("--max-time"); - if (maxTimeIndex === -1) return args; - const next = [...args]; - next[maxTimeIndex + 1] = String(overrideSeconds); - return next; -} - -function getProbeProcessTimeoutMs(args) { - return (getCurlMaxTimeSeconds(args) + 5) * 1000; -} - // ── Responses API probe ────────────────────────────────────────── function probeResponsesToolCalling(endpointUrl, model, apiKey, options = {}) { diff --git a/src/lib/inference/probe-http-helpers.test.ts b/src/lib/inference/probe-http-helpers.test.ts new file mode 100644 index 00000000000..c433cfc58fa --- /dev/null +++ b/src/lib/inference/probe-http-helpers.test.ts @@ -0,0 +1,43 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { describe, expect, it } from "vitest"; + +const { + getKimiK26ValidationProbeCurlArgs, + getValidationProbeCurlArgs, +} = require("./probe-http-helpers"); + +// Restore an env var to its pre-test value without branching at the call +// site. Centralizing the conditional keeps test bodies linear and keeps the +// codebase-growth-guardrails "if count" steady; see PR #5975 review. +function restoreEnv(name: string, original: string | undefined): void { + if (original === undefined) { + delete process.env[name]; + } else { + process.env[name] = original; + } +} + +describe("validation probe curl timing helpers", () => { + it("allows onboard validation max-time to be raised from the environment", () => { + const original = process.env.NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS; + process.env.NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS = "300"; + try { + expect(getValidationProbeCurlArgs({ isWsl: false })).toEqual([ + "--connect-timeout", + "10", + "--max-time", + "300", + ]); + expect(getKimiK26ValidationProbeCurlArgs({ isWsl: false })).toEqual([ + "--connect-timeout", + "10", + "--max-time", + "300", + ]); + } finally { + restoreEnv("NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS", original); + } + }); +}); diff --git a/src/lib/inference/probe-http-helpers.ts b/src/lib/inference/probe-http-helpers.ts new file mode 100644 index 00000000000..3b96400607c --- /dev/null +++ b/src/lib/inference/probe-http-helpers.ts @@ -0,0 +1,70 @@ +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +// +// Pure HTTP/curl-probe argument and timing builders extracted from +// onboard-probes.ts. These helpers only compute values from their inputs +// (and the documented NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS override); +// they run no curl and touch no network/process state. Keeping them in a +// focused, typed module lets the probe driver stay small while these builders +// remain independently testable. See PR #6293 PRA-1. + +const { isWsl } = require("../platform"); + +type WslProbeOptions = { isWsl?: boolean } | undefined; + +const ONBOARD_VALIDATION_TIMEOUT_ENV = "NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS"; + +// Per-validation-probe curl timing. Tighter than the default 60s in +// getCurlTimingArgs() because validation must not hang the wizard for a +// minute on a misbehaving model. See issue #1601 (Bug 3). +export function getValidationProbeCurlArgs(opts?: WslProbeOptions): string[] { + const args = isWsl(opts) + ? ["--connect-timeout", "20", "--max-time", "30"] + : ["--connect-timeout", "10", "--max-time", "15"]; + return withValidationMaxTimeOverride(args); +} + +export function getDeepSeekV4ProValidationProbeCurlArgs(opts?: WslProbeOptions): string[] { + const args = isWsl(opts) + ? ["--connect-timeout", "30", "--max-time", "150"] + : ["--connect-timeout", "20", "--max-time", "120"]; + return withValidationMaxTimeOverride(args); +} + +export function getKimiK26ValidationProbeCurlArgs(opts?: WslProbeOptions): string[] { + const args = isWsl(opts) + ? ["--connect-timeout", "20", "--max-time", "90"] + : ["--connect-timeout", "10", "--max-time", "60"]; + return withValidationMaxTimeOverride(args); +} + +export function getExtendedNvidiaEndpointValidationProbeCurlArgs(opts?: WslProbeOptions): string[] { + const args = isWsl(opts) + ? ["--connect-timeout", "30", "--max-time", "300"] + : ["--connect-timeout", "10", "--max-time", "300"]; + return withValidationMaxTimeOverride(args); +} + +export function getCurlMaxTimeSeconds(args: readonly string[]): number { + const maxTimeIndex = args.indexOf("--max-time"); + if (maxTimeIndex === -1) return 30; + const value = Number(args[maxTimeIndex + 1]); + return Number.isFinite(value) && value > 0 ? value : 30; +} + +export function withValidationMaxTimeOverride(args: string[]): string[] { + const raw = (process.env[ONBOARD_VALIDATION_TIMEOUT_ENV] || "").trim(); + if (!raw) return args; + const overrideSeconds = Math.ceil(Number(raw)); + if (!Number.isFinite(overrideSeconds) || overrideSeconds <= 0) return args; + if (overrideSeconds <= getCurlMaxTimeSeconds(args)) return args; + const maxTimeIndex = args.indexOf("--max-time"); + if (maxTimeIndex === -1) return args; + const next = [...args]; + next[maxTimeIndex + 1] = String(overrideSeconds); + return next; +} + +export function getProbeProcessTimeoutMs(args: readonly string[]): number { + return (getCurlMaxTimeSeconds(args) + 5) * 1000; +} From f967a76163e45c30513733955c163040ba1bd100 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 12:32:47 -0700 Subject: [PATCH 24/41] fix(inference): declare pinnedAddresses on the anthropic probe dep option type (#6293) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The require()-cast type annotation for probeAnthropicEndpoint in inference-selection-validation.ts declared its options as { probeStreaming? }, so passing the new pinnedAddresses (blocker A) failed the src build with TS2353 and broke build:cli — which took down every live E2E job. Add pinnedAddresses to that option type (the openai-like annotation is Record, so it already accepted it). Co-Authored-By: Claude Opus 4.8 (1M context) Signed-off-by: Prekshi Vyas --- src/lib/onboard/inference-selection-validation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/onboard/inference-selection-validation.ts b/src/lib/onboard/inference-selection-validation.ts index 0da5269b286..dc3b70ccd2d 100644 --- a/src/lib/onboard/inference-selection-validation.ts +++ b/src/lib/onboard/inference-selection-validation.ts @@ -10,7 +10,7 @@ const { probeAnthropicEndpoint, probeOpenAiLikeEndpoint } = endpointUrl: string, model: string, apiKey: string | null | undefined, - options?: { probeStreaming?: boolean }, + options?: { probeStreaming?: boolean; pinnedAddresses?: readonly string[] }, ): any; probeOpenAiLikeEndpoint( endpointUrl: string, From 816f5b5e568d99799e661eba9b9d853b7baa9a88 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 12:37:26 -0700 Subject: [PATCH 25/41] test(inference): use vi.stubEnv in probe-http-helpers test (#6293) The extracted probe-http-helpers test carried over a restoreEnv helper whose if/else tripped the codebase-growth-guardrails "changed test files must not add if statements" check for the new file. Replace the manual save/restore with vitest's vi.stubEnv / vi.unstubAllEnvs, removing the branch and the helper. Co-Authored-By: Claude Opus 4.8 (1M context) Signed-off-by: Prekshi Vyas --- src/lib/inference/probe-http-helpers.test.ts | 46 ++++++++------------ 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/src/lib/inference/probe-http-helpers.test.ts b/src/lib/inference/probe-http-helpers.test.ts index c433cfc58fa..ca1a293c149 100644 --- a/src/lib/inference/probe-http-helpers.test.ts +++ b/src/lib/inference/probe-http-helpers.test.ts @@ -1,43 +1,31 @@ // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 -import { describe, expect, it } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; const { getKimiK26ValidationProbeCurlArgs, getValidationProbeCurlArgs, } = require("./probe-http-helpers"); -// Restore an env var to its pre-test value without branching at the call -// site. Centralizing the conditional keeps test bodies linear and keeps the -// codebase-growth-guardrails "if count" steady; see PR #5975 review. -function restoreEnv(name: string, original: string | undefined): void { - if (original === undefined) { - delete process.env[name]; - } else { - process.env[name] = original; - } -} +afterEach(() => { + vi.unstubAllEnvs(); +}); describe("validation probe curl timing helpers", () => { it("allows onboard validation max-time to be raised from the environment", () => { - const original = process.env.NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS; - process.env.NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS = "300"; - try { - expect(getValidationProbeCurlArgs({ isWsl: false })).toEqual([ - "--connect-timeout", - "10", - "--max-time", - "300", - ]); - expect(getKimiK26ValidationProbeCurlArgs({ isWsl: false })).toEqual([ - "--connect-timeout", - "10", - "--max-time", - "300", - ]); - } finally { - restoreEnv("NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS", original); - } + vi.stubEnv("NEMOCLAW_ONBOARD_VALIDATION_TIMEOUT_SECONDS", "300"); + expect(getValidationProbeCurlArgs({ isWsl: false })).toEqual([ + "--connect-timeout", + "10", + "--max-time", + "300", + ]); + expect(getKimiK26ValidationProbeCurlArgs({ isWsl: false })).toEqual([ + "--connect-timeout", + "10", + "--max-time", + "300", + ]); }); }); From 5d0f4dacd9be581f47e4ee3f02ce9b15bac5ae3f Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 13:31:37 -0700 Subject: [PATCH 26/41] fix(inference): exempt OpenShell-managed hosts from SSRF preflight (#6293) The unconditional custom-endpoint SSRF preflight rejected inference.local (and host.*.internal) because they resolve to the managed proxy/loopback, breaking the messaging-compatible-endpoint live E2E. These are NemoClaw's own trusted OpenShell aliases, not attacker-controlled rebinding surfaces, so exempt them like explicit loopback: connect normally, no --resolve pin. Mirrors the MCP URL-target allowlist, plus inference.local. --- .../inference/endpoint-ssrf-preflight.test.ts | 15 ++++++++ src/lib/inference/endpoint-ssrf-preflight.ts | 36 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/lib/inference/endpoint-ssrf-preflight.test.ts b/src/lib/inference/endpoint-ssrf-preflight.test.ts index cab9d068895..eb1839f2e3d 100644 --- a/src/lib/inference/endpoint-ssrf-preflight.test.ts +++ b/src/lib/inference/endpoint-ssrf-preflight.test.ts @@ -73,4 +73,19 @@ describe("assertEndpointResolvesPublic (#6293)", () => { const result = await assertEndpointResolvesPublic("not a url", resolverTo("93.184.216.34")); expect(result.ok).toBe(false); }); + + it.each([ + "https://inference.local/v1", + "http://host.openshell.internal:8000/v1", + "http://host.docker.internal:11434/v1", + "http://host.containers.internal:11434/v1", + ])("exempts the OpenShell-managed alias %s without resolving or pinning (#6293)", async (endpointUrl) => { + const lookup = vi.fn(); + const result = await assertEndpointResolvesPublic(endpointUrl, lookup); + expect(result.ok).toBe(true); + // Managed aliases connect normally through the OpenShell proxy — no + // --resolve pinning (no addresses) and no DNS lookup. + expect(result.addresses).toBeUndefined(); + expect(lookup).not.toHaveBeenCalled(); + }); }); diff --git a/src/lib/inference/endpoint-ssrf-preflight.ts b/src/lib/inference/endpoint-ssrf-preflight.ts index 2d3f8c916e9..1052bb1369d 100644 --- a/src/lib/inference/endpoint-ssrf-preflight.ts +++ b/src/lib/inference/endpoint-ssrf-preflight.ts @@ -12,6 +12,35 @@ export type EndpointDnsLookupFn = ( options: { all: true }, ) => Promise>; +/** + * NemoClaw's own OpenShell-managed infrastructure hostnames. These resolve to + * the host loopback or the OpenShell L7 proxy *by design* (see + * `subprocess-env` `withLocalNoProxy` and `verify-deployment` for + * `inference.local`), so — unlike an arbitrary user-supplied public name — they + * are trusted aliases, not attacker-controlled names subject to DNS rebinding. + * They are exempt from the public-resolution requirement (like explicit + * loopback) and connect normally without `--resolve` pinning. This mirrors the + * MCP URL-target allowlist (`isOpenShellMcpHostAlias`), additionally covering + * `inference.local` — the managed sandbox inference route a compatible endpoint + * legitimately targets (#6293). + */ +const OPENSHELL_MANAGED_HOSTS = new Set([ + "inference.local", + "host.openshell.internal", + "host.docker.internal", + "host.containers.internal", +]); + +/** True when `hostname` is a NemoClaw OpenShell-managed infrastructure alias. */ +export function isOpenShellManagedHost(hostname: string): boolean { + const normalised = ( + hostname.startsWith("[") && hostname.endsWith("]") ? hostname.slice(1, -1) : hostname + ) + .replace(/\.$/, "") + .toLowerCase(); + return OPENSHELL_MANAGED_HOSTS.has(normalised); +} + export interface EndpointSsrfPreflightResult { ok: boolean; /** Human-readable reason, present only when `ok === false`. */ @@ -62,6 +91,13 @@ export async function assertEndpointResolvesPublic( // An explicit loopback host is a legitimate local inference server. if (isLoopbackHostname(hostname)) return { ok: true }; + // NemoClaw's own OpenShell-managed aliases (inference.local, host.*.internal) + // resolve to the managed proxy/loopback by design and are trusted, not + // rebinding surfaces. Exempt like loopback — connect normally (no pinning) — + // and exempt BEFORE isPrivateHostname, which would otherwise reject their + // reserved .local/.internal suffixes (#6293). + if (isOpenShellManagedHost(hostname)) return { ok: true }; + // A literal private IP or reserved private name is refused without resolving. if (isPrivateHostname(hostname)) { return { ok: false, reason: `endpoint host "${hostname}" is a private/internal address` }; From 6dbc6dbb4aecb74b8b2f20afd39c5d6abfee5ee2 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 13:48:20 -0700 Subject: [PATCH 27/41] test(onboard): import shared provider-inference types in handler spec (#6293) Merge brought in main's gateway-lock test bodies that reference ProviderInferenceStateOptions/Gpu/Agent/Host directly, but the PRA-6 extraction moved those to the test-support module. Import them so typecheck:cli passes (vitest erases types and did not catch this). --- .../machine/handlers/provider-inference.test.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lib/onboard/machine/handlers/provider-inference.test.ts b/src/lib/onboard/machine/handlers/provider-inference.test.ts index 06edadfe253..4c70a01e50f 100644 --- a/src/lib/onboard/machine/handlers/provider-inference.test.ts +++ b/src/lib/onboard/machine/handlers/provider-inference.test.ts @@ -10,8 +10,18 @@ import { describe, expect, it, vi } from "vitest"; import { createSession } from "../../../state/onboard-session"; import { patchStagedDockerfile } from "../../dockerfile-patch"; import { clearCompatibleEndpointReasoning } from "../../reasoning-mode"; -import { handleProviderInferenceState } from "./provider-inference"; -import { baseOptions, baseSelection, createDeps } from "./provider-inference.test-support"; +import { + handleProviderInferenceState, + type ProviderInferenceStateOptions, +} from "./provider-inference"; +import { + type Agent, + baseOptions, + baseSelection, + createDeps, + type Gpu, + type Host, +} from "./provider-inference.test-support"; describe("handleProviderInferenceState", () => { it("runs provider selection and inference setup on a fresh flow", async () => { From 257f9218d2e1bd026f48a82276cdaef0ee9f636e Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 14:03:14 -0700 Subject: [PATCH 28/41] test(onboard): expect pinned addresses in custom-endpoint probe assertions (#6293) Two custom-endpoint preferred-API cases asserted probeOpenAiLikeEndpoint was called without the pinnedAddresses the SSRF preflight now carries into the probe options. Extract the shared expected options into one const (keeping the file within its legacy line budget) and reference it from both cases. Co-Authored-By: Claude Opus 4.8 (1M context) Signed-off-by: Prekshi Vyas --- test/onboard-selection.test.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/onboard-selection.test.ts b/test/onboard-selection.test.ts index a90ba9c309e..68b318d1565 100644 --- a/test/onboard-selection.test.ts +++ b/test/onboard-selection.test.ts @@ -244,6 +244,14 @@ const TEST_CUSTOM_OPENAI_CONFIG = { endpointUrl: TEST_OPENAI_ENDPOINT_URL, helpUrl: null, }; +// Shared expected 4th arg for probeOpenAiLikeEndpoint; pinnedAddresses is what +// the injected resolveEndpointHost returns via the SSRF preflight (#6293). +const EXPECTED_CUSTOM_ENDPOINT_PROBE_OPTIONS = { + requireResponsesToolCalling: true, + skipResponsesProbe: false, + probeStreaming: true, + pinnedAddresses: ["93.184.216.34"], +}; const TEST_CUSTOM_ANTHROPIC_CONFIG = { label: "Other Anthropic-compatible endpoint", endpointUrl: TEST_ANTHROPIC_ENDPOINT_URL, @@ -3032,11 +3040,7 @@ const { setupNim } = require(${onboardPath}); "https://ollama.public.test:11434/v1", "my-model", "ollama-key", - { - requireResponsesToolCalling: true, - skipResponsesProbe: false, - probeStreaming: true, - }, + EXPECTED_CUSTOM_ENDPOINT_PROBE_OPTIONS, ); } finally { restoreProcessEnvValue("NEMOCLAW_PREFERRED_API", previousPreferredApi); @@ -3093,11 +3097,7 @@ const { setupNim } = require(${onboardPath}); "https://openai-proxy.example.com/v1", "gpt-4o", "sk-test", - { - requireResponsesToolCalling: true, - skipResponsesProbe: false, - probeStreaming: true, - }, + EXPECTED_CUSTOM_ENDPOINT_PROBE_OPTIONS, ); } finally { restoreProcessEnvValue("NEMOCLAW_PREFERRED_API", previousPreferredApi); From 3986d60b9b25972434a06152fdca365db487204d Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 14:29:48 -0700 Subject: [PATCH 29/41] fix(security): close remaining endpoint DNS rebinding paths (#6293) Signed-off-by: Prekshi Vyas --- src/lib/adapters/http/curl-args.test.ts | 54 +++++++++++ src/lib/adapters/http/curl-args.ts | 90 ++++++++++++++++--- src/lib/adapters/http/probe.test.ts | 44 +++++++++ src/lib/adapters/http/probe.ts | 36 ++++++-- .../inference/compatible-endpoint-context.ts | 2 +- .../inference/endpoint-ssrf-preflight.test.ts | 16 +++- src/lib/inference/endpoint-ssrf-preflight.ts | 21 +++-- src/lib/inference/onboard-probes.ts | 14 ++- src/lib/inference/probe-anthropic.ts | 10 ++- .../remote-openai-surface.test.ts | 3 +- src/lib/onboard/inference-providers/remote.ts | 3 + src/lib/onboard/inference-providers/types.ts | 1 + .../inference-selection-validation.test.ts | 24 ++++- .../onboard/inference-selection-validation.ts | 12 ++- .../machine/handlers/provider-inference.ts | 7 ++ .../setup-inference-route-containment.test.ts | 70 +++++++++++++++ src/lib/onboard/setup-inference.ts | 43 ++++++++- src/lib/onboard/setup-nim-flow.ts | 4 + src/lib/onboard/setup-nim-selection.ts | 12 ++- test/onboard-resume-provider-recovery.test.ts | 1 + .../onboard-selection-anthropic-retry.test.ts | 2 + test/onboard-selection.test.ts | 2 +- 22 files changed, 425 insertions(+), 46 deletions(-) diff --git a/src/lib/adapters/http/curl-args.test.ts b/src/lib/adapters/http/curl-args.test.ts index 861795a4881..e3ad87a0a86 100644 --- a/src/lib/adapters/http/curl-args.test.ts +++ b/src/lib/adapters/http/curl-args.test.ts @@ -116,4 +116,58 @@ describe("validateCurlProbeArgs — credential-leak defence", () => { ), ).not.toThrow(); }); + + it("accepts only an exact public --resolve mapping for the probe destination (#6293)", () => { + expect(() => + validateCurlProbeArgs( + [ + "-sS", + "--resolve", + "example.test:443:93.184.216.34,[2606:2800:220:1:248:1893:25c8:1946]", + "https://example.test/v1/models", + ], + { pinnedAddresses: ["93.184.216.34", "2606:2800:220:1:248:1893:25c8:1946"] }, + ), + ).not.toThrow(); + }); + + it.each([ + ["other.test:443:93.184.216.34", ["93.184.216.34"]], + ["example.test:80:93.184.216.34", ["93.184.216.34"]], + ["example.test:443:not-an-ip", ["not-an-ip"]], + ["example.test:443:10.0.0.8", ["10.0.0.8"]], + ["example.test:443:93.184.216.35", ["93.184.216.34"]], + ])("rejects an unsafe or mismatched --resolve mapping %s (#6293)", (mapping, approved) => { + expect(() => + validateCurlProbeArgs(["-sS", "--resolve", mapping, "https://example.test/v1/models"], { + pinnedAddresses: approved, + }), + ).toThrow(/--resolve/); + }); + + it("rejects --resolve without an approved address capability (#6293)", () => { + expect(() => + validateCurlProbeArgs([ + "-sS", + "--resolve", + "example.test:443:93.184.216.34", + "https://example.test/v1/models", + ]), + ).toThrow(/pinnedAddresses/); + }); + + it("rejects repeated --resolve entries instead of letting curl drop earlier addresses (#6293)", () => { + expect(() => + validateCurlProbeArgs( + [ + "--resolve", + "example.test:443:93.184.216.34", + "--resolve", + "example.test:443:93.184.216.34", + "https://example.test/v1/models", + ], + { pinnedAddresses: ["93.184.216.34"] }, + ), + ).toThrow(/only one --resolve/); + }); }); diff --git a/src/lib/adapters/http/curl-args.ts b/src/lib/adapters/http/curl-args.ts index 715297214c9..8260534117f 100644 --- a/src/lib/adapters/http/curl-args.ts +++ b/src/lib/adapters/http/curl-args.ts @@ -1,8 +1,8 @@ // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +import { isIP } from "node:net"; import path from "node:path"; - import { isCredentialShapedName } from "../../security/credential-env"; import { ROOT } from "../../state/paths"; @@ -16,6 +16,8 @@ export interface CurlProbeArgOptions { * hardcoded host. */ allowRedirects?: boolean; + /** Public addresses approved by the endpoint SSRF preflight. */ + pinnedAddresses?: readonly string[]; } const CURL_CONFIG_OPTIONS = new Set(["--config", "-K"]); @@ -60,17 +62,7 @@ const CURL_SAFE_FLAG_OPTIONS = new Set([ // genuinely need to follow redirects from a fixed, hardcoded host (e.g. the // Ollama manifest probe) must opt in via CurlProbeArgOptions.allowRedirects. const CURL_REDIRECT_FLAG_OPTIONS = new Set(["-L", "-sfL", "--location"]); -// `--resolve ` pins the connection to an address the SSRF -// preflight already validated as public, closing the DNS-rebinding/TOCTOU -// window before a privileged onboarding probe curl (#6293). It carries no -// secret and reads no file, so it is a safe value-bearing option. -const CURL_SAFE_VALUE_OPTIONS = new Set([ - "--connect-timeout", - "--max-time", - "-X", - "--request", - "--resolve", -]); +const CURL_SAFE_VALUE_OPTIONS = new Set(["--connect-timeout", "--max-time", "-X", "--request"]); const CURL_FORBIDDEN_MULTI_TRANSFER_OPTIONS = new Set(["--next"]); const CURL_SHORT_OPTIONS_WITH_VALUES = new Set(["-K", "-b", "-T", "-d", "-F", "-H", "-X"]); @@ -172,12 +164,76 @@ function isTrustedCurlConfigPath(value: string, opts: CurlProbeArgOptions): bool .includes(candidate); } +function normalizeHostname(hostname: string): string { + return (hostname.startsWith("[") && hostname.endsWith("]") ? hostname.slice(1, -1) : hostname) + .replace(/\.$/, "") + .toLowerCase(); +} + +function defaultUrlPort(url: URL): string { + return url.port || (url.protocol === "https:" ? "443" : "80"); +} + +function parseResolveAddresses(raw: string): string[] { + return raw + .split(",") + .map((value) => (value.startsWith("[") && value.endsWith("]") ? value.slice(1, -1) : value)); +} + +function isPrivateResolveAddress(address: string): boolean { + // Keep the generic curl validator import-light: many command tests mock the + // runner module that private-networks uses only to locate its YAML. Load the + // canonical classifier only for the uncommon --resolve validation path. + const { isPrivateIp } = + require("../../private-networks") as typeof import("../../private-networks"); + return isPrivateIp(address); +} + +function assertResolveMatchesApprovedEndpoint( + value: string, + target: URL, + opts: CurlProbeArgOptions, +): void { + const firstSeparator = value.indexOf(":"); + const secondSeparator = value.indexOf(":", firstSeparator + 1); + if (firstSeparator <= 0 || secondSeparator <= firstSeparator + 1) { + throw new Error("curl probe --resolve must use host:port:address[,address] syntax"); + } + const host = normalizeHostname(value.slice(0, firstSeparator)); + const port = value.slice(firstSeparator + 1, secondSeparator); + const addresses = parseResolveAddresses(value.slice(secondSeparator + 1)); + const approved = [...new Set(opts.pinnedAddresses ?? [])]; + if (approved.length === 0) { + throw new Error("curl probe --resolve requires SSRF-preflight-approved pinnedAddresses"); + } + if (host !== normalizeHostname(target.hostname) || port !== defaultUrlPort(target)) { + throw new Error("curl probe --resolve host and port must match the probe URL"); + } + if (addresses.length === 0 || addresses.some((address) => isIP(address) === 0)) { + throw new Error("curl probe --resolve addresses must be numeric IP addresses"); + } + if (addresses.some((address) => isPrivateResolveAddress(address))) { + throw new Error("curl probe --resolve must not map the destination to a private address"); + } + const actualSet = new Set(addresses); + const approvedSet = new Set(approved); + if ( + actualSet.size !== addresses.length || + actualSet.size !== approvedSet.size || + [...actualSet].some((address) => !approvedSet.has(address)) + ) { + throw new Error("curl probe --resolve addresses must exactly match pinnedAddresses"); + } +} + export function validateCurlProbeArgs( argv: string[], opts: CurlProbeArgOptions = {}, ): { args: string[]; url: string } { const args = [...argv]; const url = normalizeHttpProbeUrl(args.pop()); + const parsedUrl = new URL(url); + let sawResolve = false; for (let index = 0; index < args.length; index += 1) { const arg = args[index]; const { option, inlineValue } = splitCurlOptionArg(arg); @@ -222,6 +278,16 @@ export function validateCurlProbeArgs( if (inlineValue === undefined) index += 1; continue; } + if (option === "--resolve") { + if (sawResolve) { + throw new Error("curl probe accepts only one --resolve mapping per transfer"); + } + const value = getCurlOptionValue(args, index, option, inlineValue); + assertResolveMatchesApprovedEndpoint(value, parsedUrl, opts); + sawResolve = true; + if (inlineValue === undefined) index += 1; + continue; + } if (CURL_SAFE_VALUE_OPTIONS.has(option)) { getCurlOptionValue(args, index, option, inlineValue); if (inlineValue === undefined) index += 1; diff --git a/src/lib/adapters/http/probe.test.ts b/src/lib/adapters/http/probe.test.ts index 5fa66f75e98..5dc1e376772 100644 --- a/src/lib/adapters/http/probe.test.ts +++ b/src/lib/adapters/http/probe.test.ts @@ -459,6 +459,50 @@ describe("http-probe helpers", () => { expect(spawnedEnv?.MY_SECRET_TOKEN).toBeUndefined(); }); + it("bypasses ambient proxies when --resolve pins the validated origin (#6293)", () => { + let spawnedEnv: NodeJS.ProcessEnv | undefined; + runCurlProbe( + ["-sS", "--resolve", "example.test:443:93.184.216.34", "https://example.test/models"], + { + pinnedAddresses: ["93.184.216.34"], + replaceEnv: true, + env: { + PATH: "/usr/bin", + HTTP_PROXY: "http://proxy.internal:3128", + HTTPS_PROXY: "http://proxy.internal:3128", + ALL_PROXY: "socks5://proxy.internal:1080", + http_proxy: "http://proxy.internal:3128", + https_proxy: "http://proxy.internal:3128", + all_proxy: "socks5://proxy.internal:1080", + }, + spawnSyncImpl: (_command, _args, options) => { + spawnedEnv = options.env as NodeJS.ProcessEnv; + return { + pid: 1, + output: [], + stdout: "200", + stderr: "", + status: 0, + signal: null, + }; + }, + }, + ); + + for (const name of [ + "HTTP_PROXY", + "HTTPS_PROXY", + "ALL_PROXY", + "http_proxy", + "https_proxy", + "all_proxy", + ]) { + expect(spawnedEnv?.[name]).toBeUndefined(); + } + expect(spawnedEnv?.NO_PROXY).toBe("*"); + expect(spawnedEnv?.no_proxy).toBe("*"); + }); + it("scrubs credential-shaped env even when trustedConfigFiles is not supplied", () => { const original = { MY_PROBE_SECRET_TOKEN: process.env.MY_PROBE_SECRET_TOKEN, diff --git a/src/lib/adapters/http/probe.ts b/src/lib/adapters/http/probe.ts index 9e197a3b321..e441beaaeac 100644 --- a/src/lib/adapters/http/probe.ts +++ b/src/lib/adapters/http/probe.ts @@ -26,6 +26,8 @@ export interface CurlProbeOptions { timeoutMs?: number; /** Absolute or cwd-relative curl config files created by trusted NemoClaw callers. */ trustedConfigFiles?: readonly string[]; + /** Public addresses approved by the endpoint SSRF preflight. */ + pinnedAddresses?: readonly string[]; spawnSyncImpl?: ( command: string, args: readonly string[], @@ -42,9 +44,31 @@ export interface StreamingProbeResult { const DEFAULT_CURL_PROCESS_TIMEOUT_MS = 30_000; const CURL_PROCESS_TIMEOUT_SLACK_MS = 5_000; -function resolveCurlProbeSpawnEnv(opts: CurlProbeOptions): NodeJS.ProcessEnv { - if (opts.replaceEnv) return scrubCredentialEnv(opts.env ?? {}); - return buildScrubbedCurlProbeEnv(opts.env ?? {}); +function resolveCurlProbeSpawnEnv( + args: readonly string[], + opts: CurlProbeOptions, +): NodeJS.ProcessEnv { + const env = opts.replaceEnv + ? scrubCredentialEnv(opts.env ?? {}) + : buildScrubbedCurlProbeEnv(opts.env ?? {}); + if (!args.some((arg) => arg === "--resolve" || arg.startsWith("--resolve="))) return env; + + // A proxy defeats origin --resolve pinning: curl sends CONNECT host:port and + // lets the proxy perform a fresh, attacker-rebindable DNS lookup. Pinned + // probes therefore bypass every curl proxy environment spelling fail-closed. + for (const name of [ + "HTTP_PROXY", + "HTTPS_PROXY", + "ALL_PROXY", + "http_proxy", + "https_proxy", + "all_proxy", + ]) { + delete env[name]; + } + env.NO_PROXY = "*"; + env.no_proxy = "*"; + return env; } function validateTempPrefix(prefix: string): string { @@ -236,7 +260,7 @@ function runCurlProbeImpl(argv: string[], opts: CurlProbeOptions = {}): CurlProb cwd: opts.cwd ?? ROOT, encoding: "utf8", timeout, - env: resolveCurlProbeSpawnEnv(opts), + env: resolveCurlProbeSpawnEnv(args, opts), }, ); const body = fs.existsSync(bodyFile) ? fs.readFileSync(bodyFile, "utf8") : ""; @@ -345,7 +369,7 @@ function runChatCompletionsStreamingProbeImpl( cwd: opts.cwd ?? ROOT, encoding: "utf8", timeout, - env: resolveCurlProbeSpawnEnv(opts), + env: resolveCurlProbeSpawnEnv(args, opts), }, ); @@ -487,7 +511,7 @@ function captureSseEventCounts( cwd: opts.cwd ?? ROOT, encoding: "utf8", timeout, - env: resolveCurlProbeSpawnEnv(opts), + env: resolveCurlProbeSpawnEnv(args, opts), }, ); diff --git a/src/lib/inference/compatible-endpoint-context.ts b/src/lib/inference/compatible-endpoint-context.ts index d5a790f12cd..e680f343501 100644 --- a/src/lib/inference/compatible-endpoint-context.ts +++ b/src/lib/inference/compatible-endpoint-context.ts @@ -131,7 +131,7 @@ export function fetchCompatibleEndpointModels( ...authConfig.args, `${baseUrl}/models`, ], - { trustedConfigFiles: authConfig.trustedConfigFiles }, + { trustedConfigFiles: authConfig.trustedConfigFiles, pinnedAddresses }, ); if (!result.ok || !result.body) return null; try { diff --git a/src/lib/inference/endpoint-ssrf-preflight.test.ts b/src/lib/inference/endpoint-ssrf-preflight.test.ts index eb1839f2e3d..1409a58cd1c 100644 --- a/src/lib/inference/endpoint-ssrf-preflight.test.ts +++ b/src/lib/inference/endpoint-ssrf-preflight.test.ts @@ -3,7 +3,11 @@ import { describe, expect, it, vi } from "vitest"; -import { assertEndpointResolvesPublic, type EndpointDnsLookupFn } from "./endpoint-ssrf-preflight"; +import { + assertEndpointResolvesPublic, + buildResolvePinArgs, + type EndpointDnsLookupFn, +} from "./endpoint-ssrf-preflight"; const resolverTo = (address: string): EndpointDnsLookupFn => vi.fn(async () => [{ address, family: address.includes(":") ? 6 : 4 }]); @@ -51,9 +55,19 @@ describe("assertEndpointResolvesPublic (#6293)", () => { const lookup = vi.fn(); const result = await assertEndpointResolvesPublic("https://93.184.216.34/v1", lookup); expect(result.ok).toBe(true); + expect(result.addresses).toBeUndefined(); expect(lookup).not.toHaveBeenCalled(); }); + it("keeps dual-stack addresses in one curl --resolve mapping (#6293)", () => { + expect( + buildResolvePinArgs("https://vllm.example/v1/models", [ + "93.184.216.34", + "2606:2800:220:1:248:1893:25c8:1946", + ]), + ).toEqual(["--resolve", "vllm.example:443:93.184.216.34,[2606:2800:220:1:248:1893:25c8:1946]"]); + }); + it("fails closed when the resolver throws (#6293)", async () => { const lookup: EndpointDnsLookupFn = vi.fn(async () => { throw new Error("ENOTFOUND"); diff --git a/src/lib/inference/endpoint-ssrf-preflight.ts b/src/lib/inference/endpoint-ssrf-preflight.ts index 1052bb1369d..f75f2ba20d8 100644 --- a/src/lib/inference/endpoint-ssrf-preflight.ts +++ b/src/lib/inference/endpoint-ssrf-preflight.ts @@ -103,10 +103,11 @@ export async function assertEndpointResolvesPublic( return { ok: false, reason: `endpoint host "${hostname}" is a private/internal address` }; } - // A public IP literal needs no DNS resolution. + // A public IP literal needs neither DNS resolution nor connection pinning: + // the URL already contains the address curl will connect to. const bare = hostname.startsWith("[") && hostname.endsWith("]") ? hostname.slice(1, -1) : hostname; - if (isIP(bare)) return { ok: true, addresses: [bare] }; + if (isIP(bare)) return { ok: true }; let addresses: Array<{ address: string; family?: number }>; try { @@ -164,10 +165,14 @@ export function buildResolvePinArgs( return []; } if (!host) return []; - const args: string[] = []; - for (const address of pinnedAddresses) { - if (!address) continue; - args.push("--resolve", `${host}:${port}:${address}`); - } - return args; + const addresses = [...new Set(pinnedAddresses.filter(Boolean))]; + if (addresses.length === 0) return []; + // One --resolve entry preserves every accepted address. Repeating the same + // host:port entry makes curl retain only the last mapping, silently dropping + // dual-stack/failover addresses. Bracket IPv6 addresses in the comma list so + // curl can distinguish their colons from the host:port separators. + const encodedAddresses = addresses.map((address) => + address.includes(":") ? `[${address}]` : address, + ); + return ["--resolve", `${host}:${port}:${encodedAddresses.join(",")}`]; } diff --git a/src/lib/inference/onboard-probes.ts b/src/lib/inference/onboard-probes.ts index 456f36b1058..2f8b90552f5 100644 --- a/src/lib/inference/onboard-probes.ts +++ b/src/lib/inference/onboard-probes.ts @@ -270,7 +270,10 @@ function probeResponsesToolCalling(endpointUrl, model, apiKey, options = {}) { }), `${baseUrl}/responses`, ], - { trustedConfigFiles: authConfig.trustedConfigFiles }, + { + trustedConfigFiles: authConfig.trustedConfigFiles, + pinnedAddresses: options.pinnedAddresses, + }, ); if (!result.ok) { @@ -378,6 +381,7 @@ function probeChatCompletionsToolCalling(endpointUrl, model, apiKey, options = { const result = runCurlProbe(args, { timeoutMs: getProbeProcessTimeoutMs(args), trustedConfigFiles: authConfig.trustedConfigFiles, + pinnedAddresses: options.pinnedAddresses, }); if (!result.ok) { @@ -512,7 +516,7 @@ function runChatCompletionsProbe({ isWsl: isWslOverride, pinnedAddresses, }); - const probeOpts = { timeoutMs: getProbeProcessTimeoutMs(args) }; + const probeOpts = { timeoutMs: getProbeProcessTimeoutMs(args), pinnedAddresses }; if (trustedConfigFiles && trustedConfigFiles.length > 0) { probeOpts.trustedConfigFiles = trustedConfigFiles; } @@ -560,6 +564,7 @@ function runDoubledTimeoutChatCompletionsRetry({ return runCurlProbe(retryArgs, { timeoutMs: getProbeProcessTimeoutMs(retryArgs), trustedConfigFiles: authConfig.trustedConfigFiles, + pinnedAddresses: options.pinnedAddresses, }); })(); return runChatCompletionsRetryLoop(runRetryProbe); @@ -683,7 +688,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { }), `${baseUrl}/responses`, ], - { trustedConfigFiles: authConfig.trustedConfigFiles }, + { trustedConfigFiles: authConfig.trustedConfigFiles, pinnedAddresses }, ), }; @@ -748,7 +753,7 @@ function probeOpenAiLikeEndpoint(endpointUrl, model, apiKey, options = {}) { }), `${baseUrl}/responses`, ], - { trustedConfigFiles: authConfig.trustedConfigFiles }, + { trustedConfigFiles: authConfig.trustedConfigFiles, pinnedAddresses }, ); if (!streamResult.ok && streamResult.missingEvents.length > 0) { // Backend responds but lacks required streaming events — fall back @@ -946,6 +951,7 @@ export function verifyOnboardInferenceSmoke(options: any) { const probe = probeOpenAiLikeEndpoint(endpointUrl, options.model, apiKey, { authMode: getProbeAuthMode(options.provider), skipResponsesProbe: true, + pinnedAddresses: options.pinnedAddresses, }); if (probe.ok) { diff --git a/src/lib/inference/probe-anthropic.ts b/src/lib/inference/probe-anthropic.ts index da153ea1603..77dddc67561 100644 --- a/src/lib/inference/probe-anthropic.ts +++ b/src/lib/inference/probe-anthropic.ts @@ -145,7 +145,10 @@ export function probeAnthropicEndpoint( anthropicMessagesPayload(model, false), messagesUrl, ], - { trustedConfigFiles: authConfig.trustedConfigFiles }, + { + trustedConfigFiles: authConfig.trustedConfigFiles, + pinnedAddresses: options.pinnedAddresses, + }, ); if (!result.ok) { return { @@ -177,7 +180,10 @@ export function probeAnthropicEndpoint( anthropicMessagesPayload(model, true), messagesUrl, ], - { trustedConfigFiles: authConfig.trustedConfigFiles }, + { + trustedConfigFiles: authConfig.trustedConfigFiles, + pinnedAddresses: options.pinnedAddresses, + }, ); if (!streamResult.ok) { return { diff --git a/src/lib/onboard/inference-providers/remote-openai-surface.test.ts b/src/lib/onboard/inference-providers/remote-openai-surface.test.ts index 34256a20388..47e12e4248e 100644 --- a/src/lib/onboard/inference-providers/remote-openai-surface.test.ts +++ b/src/lib/onboard/inference-providers/remote-openai-surface.test.ts @@ -22,6 +22,7 @@ function makeArgs(sandboxName: string | null) { endpointUrl: ENDPOINT, credentialEnv: CREDENTIAL_ENV, preferredInferenceApi: "openai-completions", + pinnedAddresses: ["93.184.216.34"], }; } @@ -100,7 +101,7 @@ describe("custom Anthropic provider replacement on the OpenAI surface", () => { OPENAI_SURFACE, MODEL, "test-secret", - { skipResponsesProbe: true }, + { skipResponsesProbe: true, pinnedAddresses: ["93.184.216.34"] }, ); expect(harness.readGatewayProviderMetadata).toHaveBeenCalledWith( PROVIDER, diff --git a/src/lib/onboard/inference-providers/remote.ts b/src/lib/onboard/inference-providers/remote.ts index 466818404f5..0ff06a5ecce 100644 --- a/src/lib/onboard/inference-providers/remote.ts +++ b/src/lib/onboard/inference-providers/remote.ts @@ -122,6 +122,7 @@ export async function setupRemoteProviderInference( credentialEnv: string | null; reuseGatewayCredentialWithoutLocalKey?: boolean; preferredInferenceApi?: string | null; + pinnedAddresses?: readonly string[]; }, deps: RemoteProviderDeps, ): Promise<{ done: true; result: SetupInferenceResult } | { done: false }> { @@ -133,6 +134,7 @@ export async function setupRemoteProviderInference( credentialEnv, reuseGatewayCredentialWithoutLocalKey, preferredInferenceApi, + pinnedAddresses, } = args; const { runOpenshell, @@ -243,6 +245,7 @@ export async function setupRemoteProviderInference( getCompatibleAnthropicOpenAiSurfaceBaseUrl(resolvedEndpointUrl); const surfaceProbe = probeOpenAiSurface(openAiSurfaceBaseUrl, model, credentialValue, { skipResponsesProbe: true, + pinnedAddresses, }); if (!surfaceProbe.ok) { providerResult = { diff --git a/src/lib/onboard/inference-providers/types.ts b/src/lib/onboard/inference-providers/types.ts index 89a75fc5ac7..7e59b3ef08c 100644 --- a/src/lib/onboard/inference-providers/types.ts +++ b/src/lib/onboard/inference-providers/types.ts @@ -65,6 +65,7 @@ export type VerifyOnboardInferenceSmoke = (input: { endpointUrl?: string | null; credentialEnv?: string | null; forceOpenAiLike?: boolean; + pinnedAddresses?: readonly string[]; }) => void; export type PromptValidationRecovery = ( diff --git a/src/lib/onboard/inference-selection-validation.test.ts b/src/lib/onboard/inference-selection-validation.test.ts index 9f96f95ad7b..930795149e6 100644 --- a/src/lib/onboard/inference-selection-validation.test.ts +++ b/src/lib/onboard/inference-selection-validation.test.ts @@ -170,7 +170,11 @@ describe("inference selection validation", () => { "model-a", "COMPATIBLE_API_KEY", ), - ).resolves.toEqual({ ok: true, api: "openai-completions" }); + ).resolves.toEqual({ + ok: true, + api: "openai-completions", + pinnedAddresses: ["93.184.216.34"], + }); expect(probeOpenAiLikeEndpoint).toHaveBeenCalled(); }); @@ -198,7 +202,11 @@ describe("inference selection validation", () => { "nvidia/nemotron-3-super-v3", "COMPATIBLE_ANTHROPIC_API_KEY", ), - ).resolves.toEqual({ ok: true, api: "anthropic-messages" }); + ).resolves.toEqual({ + ok: true, + api: "anthropic-messages", + pinnedAddresses: ["93.184.216.34"], + }); expect(probeAnthropicEndpoint).toHaveBeenCalledWith( "https://compatible.example", "nvidia/nemotron-3-super-v3", @@ -249,7 +257,11 @@ describe("inference selection validation", () => { null, { intendedApi: "openai-completions" }, ), - ).resolves.toEqual({ ok: true, api: "openai-completions" }); + ).resolves.toEqual({ + ok: true, + api: "openai-completions", + pinnedAddresses: ["93.184.216.34"], + }); expect(probeOpenAiLikeEndpoint).toHaveBeenCalledWith( "https://compatible.example/v1", "nvidia/nemotron-3-super-v3", @@ -365,7 +377,11 @@ exit 0 "model-a", "COMPATIBLE_API_KEY", ), - ).resolves.toEqual({ ok: true, api: "openai-completions" }); + ).resolves.toEqual({ + ok: true, + api: "openai-completions", + pinnedAddresses: ["93.184.216.34"], + }); const recordedArgs = fs.readFileSync(argsPath, "utf8").split("\n"); const resolveIdx = recordedArgs.indexOf("--resolve"); diff --git a/src/lib/onboard/inference-selection-validation.ts b/src/lib/onboard/inference-selection-validation.ts index dc3b70ccd2d..07937a9e866 100644 --- a/src/lib/onboard/inference-selection-validation.ts +++ b/src/lib/onboard/inference-selection-validation.ts @@ -30,7 +30,13 @@ import { summarizeProbeForDisplay } from "./probe-diagnostics"; import { normalizeReasoningFlag } from "./reasoning-mode"; export type EndpointValidationResult = - | { ok: true; api: string | null; retry?: undefined } + | { + ok: true; + api: string | null; + retry?: undefined; + /** Public addresses approved for this custom endpoint's host probes. */ + pinnedAddresses?: string[]; + } | { ok: false; retry: "credential" | "selection" | "retry" | "model"; api?: undefined }; export interface InferenceSelectionValidationDeps { @@ -274,7 +280,7 @@ export function createInferenceSelectionValidationHelpers( ` ${probe.label} available — ${deps.agentProductName()} will use ${probe.api}.`, ); } - return { ok: true, api: probe.api ?? "openai-completions" }; + return { ok: true, api: probe.api ?? "openai-completions", pinnedAddresses }; } printValidationFailure(label, probe); if (deps.isNonInteractive()) { @@ -340,7 +346,7 @@ export function createInferenceSelectionValidationHelpers( ` ${probe.label} available — ${deps.agentProductName()} will use ${intendedApi}.`, ); } - return { ok: true, api: intendedApi }; + return { ok: true, api: intendedApi, pinnedAddresses }; } printValidationFailure(label, probe); if (deps.isNonInteractive()) { diff --git a/src/lib/onboard/machine/handlers/provider-inference.ts b/src/lib/onboard/machine/handlers/provider-inference.ts index 2767a19753e..37e1ec4020a 100644 --- a/src/lib/onboard/machine/handlers/provider-inference.ts +++ b/src/lib/onboard/machine/handlers/provider-inference.ts @@ -32,6 +32,8 @@ export interface ProviderInferenceSetupOptions { * compatible-anthropic-endpoint register type=openai). */ preferredInferenceApi?: string | null; + /** Public addresses approved for custom endpoint host probes. */ + endpointPinnedAddresses?: readonly string[]; } export interface ProviderSelectionResult { @@ -47,6 +49,7 @@ export interface ProviderSelectionResult { allowToolsIncompatible?: boolean; skipHostInferenceSmoke?: boolean; reuseGatewayCredentialWithoutLocalKey?: boolean; + endpointPinnedAddresses?: string[]; } export interface ProviderInferenceStateOptions { @@ -314,6 +317,7 @@ export async function handleProviderInferenceState({ let allowToolsIncompatible = false; let skipHostInferenceSmoke = false; let reuseGatewayCredentialWithoutLocalKey = false; + let endpointPinnedAddresses: string[] | undefined; const effectiveResume = resume && !fresh; const stateResults: OnboardStateTransitionResult[] = []; const retryStateResults: OnboardStateTransitionResult[] = []; @@ -473,6 +477,7 @@ export async function handleProviderInferenceState({ skipHostInferenceSmoke = selection.skipHostInferenceSmoke === true; reuseGatewayCredentialWithoutLocalKey = selection.reuseGatewayCredentialWithoutLocalKey === true; + endpointPinnedAddresses = selection.endpointPinnedAddresses; shouldRecordProviderSelection = true; } @@ -547,6 +552,7 @@ export async function handleProviderInferenceState({ ? { reuseGatewayCredentialWithoutLocalKey } : {}), ...(preferredInferenceApi ? { preferredInferenceApi } : {}), + ...(endpointPinnedAddresses ? { endpointPinnedAddresses } : {}), }; await deps.startRecordedStep("inference", { provider, model }); inferenceResult = await withInferenceTrace( @@ -731,6 +737,7 @@ export async function handleProviderInferenceState({ ...(skipHostInferenceSmoke ? { skipHostInferenceSmoke } : {}), ...(reuseGatewayCredentialWithoutLocalKey ? { reuseGatewayCredentialWithoutLocalKey } : {}), ...(preferredInferenceApi ? { preferredInferenceApi } : {}), + ...(endpointPinnedAddresses ? { endpointPinnedAddresses } : {}), }; await deps.startRecordedStep("inference", { provider, model }); inferenceResult = await withInferenceTrace( diff --git a/src/lib/onboard/setup-inference-route-containment.test.ts b/src/lib/onboard/setup-inference-route-containment.test.ts index 5a7d8e50adb..07b9c959a5a 100644 --- a/src/lib/onboard/setup-inference-route-containment.test.ts +++ b/src/lib/onboard/setup-inference-route-containment.test.ts @@ -7,6 +7,76 @@ import type { SandboxEntry } from "../state/registry"; import { createSetupInference, type SetupInferenceDeps } from "./setup-inference"; describe("onboard shared gateway route containment", () => { + it("preflights a resumed custom endpoint and pins the final host smoke (#6293)", async () => { + let lookupCount = 0; + const resolveEndpointHost = vi.fn(async () => { + lookupCount += 1; + return lookupCount === 1 + ? [{ address: "93.184.216.34", family: 4 }] + : [{ address: "10.0.0.8", family: 4 }]; + }); + const verifyOnboardInferenceSmoke = vi.fn(); + const setupInference = createSetupInference({ + checkGatewayRouteCompatibility: vi.fn(() => ({ ok: true as const })), + withSandboxMutationLock: async (_name: string, operation: () => Promise | T) => + await operation(), + withGatewayRouteMutationLock: async (_name: string, operation: () => Promise | T) => + await operation(), + step: vi.fn(), + getGatewayName: () => "nemoclaw", + runOpenshell: vi.fn(() => ({ status: 0, stdout: "", stderr: "" })), + updateSandbox: vi.fn(() => true), + upsertProvider: vi.fn(() => ({ ok: true })), + verifyInferenceRoute: vi.fn(), + verifyOnboardInferenceSmoke, + resolveEndpointHost, + isNonInteractive: () => true, + hermesProviderAuth: { HERMES_PROVIDER_NAME: "hermes-provider" }, + REMOTE_PROVIDER_CONFIG: { + custom: { + label: "Other OpenAI-compatible endpoint", + providerName: "compatible-endpoint", + providerType: "openai", + credentialEnv: "COMPATIBLE_API_KEY", + endpointUrl: "https://public-name.example/v1", + helpUrl: null, + modelMode: "input", + defaultModel: "model-a", + }, + }, + hydrateCredentialEnv: vi.fn(() => "secret"), + promptValidationRecovery: vi.fn(), + classifyApplyFailure: vi.fn(), + localInferenceTimeoutSecs: 60, + bedrockRuntimeOnboard: { + setupBedrockRuntimeInference: vi.fn(async () => ({ handled: false as const })), + }, + redact: (value: string) => value, + compactText: (value: string) => value, + log: vi.fn(), + error: vi.fn(), + exitProcess: vi.fn((code: number): never => { + throw new Error(`exit ${code}`); + }), + } as unknown as SetupInferenceDeps); + + await expect( + setupInference( + "sandbox-a", + "model-a", + "compatible-endpoint", + "https://public-name.example/v1", + "COMPATIBLE_API_KEY", + ), + ).resolves.toEqual({ ok: true }); + + expect(resolveEndpointHost).toHaveBeenCalledOnce(); + expect(verifyOnboardInferenceSmoke).toHaveBeenCalledWith( + expect.objectContaining({ pinnedAddresses: ["93.184.216.34"] }), + ); + expect(JSON.stringify(verifyOnboardInferenceSmoke.mock.calls)).not.toContain("10.0.0.8"); + }); + it("rejects a conflict before selecting the gateway or mutating provider state (#6315)", async () => { const events: string[] = []; const runOpenshell = vi.fn(() => { diff --git a/src/lib/onboard/setup-inference.ts b/src/lib/onboard/setup-inference.ts index 19928c41ced..bde185cfae1 100644 --- a/src/lib/onboard/setup-inference.ts +++ b/src/lib/onboard/setup-inference.ts @@ -1,6 +1,10 @@ // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +import { + assertEndpointResolvesPublic, + type EndpointDnsLookupFn, +} from "../inference/endpoint-ssrf-preflight"; import { type CurrentGatewayRouteCompatibilityCheck, formatGatewayRouteConflict, @@ -72,6 +76,8 @@ type ProviderBranchDeps = Pick< Pick; export type SetupInferenceDeps = ProviderBranchDeps & { + /** Injectable resolver for resumed custom-endpoint SSRF preflight tests. */ + resolveEndpointHost?: EndpointDnsLookupFn; checkGatewayRouteCompatibility: CurrentGatewayRouteCompatibilityCheck; withGatewayRouteMutationLock: typeof withGatewayRouteMutationLock; withSandboxMutationLock: typeof withSandboxMutationLock; @@ -235,6 +241,25 @@ export function createSetupInference( return deps.exitProcess(1); } deps.step(4, 8, "Setting up inference provider"); + let endpointPinnedAddresses = options.endpointPinnedAddresses; + if ( + (provider === "compatible-endpoint" || provider === "compatible-anthropic-endpoint") && + endpointUrl && + !endpointPinnedAddresses + ) { + const preflight = await assertEndpointResolvesPublic( + endpointUrl, + deps.resolveEndpointHost, + ); + if (!preflight.ok) { + deps.error( + ` Endpoint SSRF preflight failed: ${preflight.reason ?? "endpoint is not safe to probe"}`, + ); + if (deps.isNonInteractive()) return deps.exitProcess(1); + return { retry: "selection" }; + } + endpointPinnedAddresses = preflight.addresses; + } const runGatewayOpenshell = createGatewayScopedOpenshellRunner( deps.runOpenshell, gatewayName, @@ -261,7 +286,13 @@ export function createSetupInference( if (sandboxName) reserveRoute(sandboxName, selectedProvider, selectedModel); deps.verifyInferenceRoute(gatewayName, selectedProvider, selectedModel); }, - verifyOnboardInferenceSmoke: deps.verifyOnboardInferenceSmoke, + verifyOnboardInferenceSmoke: ( + input: Parameters[0], + ) => + deps.verifyOnboardInferenceSmoke({ + ...input, + pinnedAddresses: endpointPinnedAddresses, + }), isNonInteractive: deps.isNonInteractive, registry: { updateSandbox: (name: string) => reserveRoute(name, provider, model), @@ -312,6 +343,7 @@ export function createSetupInference( reuseGatewayCredentialWithoutLocalKey: options.reuseGatewayCredentialWithoutLocalKey === true, preferredInferenceApi: options.preferredInferenceApi ?? null, + pinnedAddresses: endpointPinnedAddresses, }, { ...commonDeps, @@ -389,7 +421,14 @@ export function createSetupInference( commonDeps.verifyInferenceRoute(provider, model); if (options.skipHostInferenceSmoke === true) deps.log(" Reusing existing gateway credential; skipping host inference smoke."); - else deps.verifyOnboardInferenceSmoke({ provider, model, endpointUrl, credentialEnv }); + else + deps.verifyOnboardInferenceSmoke({ + provider, + model, + endpointUrl, + credentialEnv, + pinnedAddresses: endpointPinnedAddresses, + }); if (sandboxName) { commonDeps.registry.updateSandbox(sandboxName); } diff --git a/src/lib/onboard/setup-nim-flow.ts b/src/lib/onboard/setup-nim-flow.ts index a3e46bd2591..58cfee9611f 100644 --- a/src/lib/onboard/setup-nim-flow.ts +++ b/src/lib/onboard/setup-nim-flow.ts @@ -336,6 +336,7 @@ export function createSetupNim( let compatibleEndpointReasoning: string | null = null; let allowToolsIncompatible = false; let reuseGatewayCredential = false; + let endpointPinnedAddresses: string[] | undefined; const nvidiaFeaturedModels = deps.createNvidiaFeaturedModelSession({ defaultModel: resolveAgentDefaultCloudModel(agent), writeLine: deps.log, @@ -352,6 +353,7 @@ export function createSetupNim( compatibleEndpointReasoning, nimContainer, allowToolsIncompatible, + ...(endpointPinnedAddresses ? { endpointPinnedAddresses } : {}), nvidiaFeaturedModels, }; state.assertRouteCompatible = () => { @@ -536,6 +538,7 @@ export function createSetupNim( hermesToolGateways, preferredInferenceApi, allowToolsIncompatible, + endpointPinnedAddresses, } = state); compatibleEndpointReasoning = state.compatibleEndpointReasoning ?? null; reuseGatewayCredential = state.reuseGatewayCredentialWithoutLocalKey === true; @@ -713,6 +716,7 @@ export function createSetupNim( allowToolsIncompatible, skipHostInferenceSmoke: reuseGatewayCredential, reuseGatewayCredentialWithoutLocalKey: reuseGatewayCredential, + ...(endpointPinnedAddresses ? { endpointPinnedAddresses } : {}), }; }; } diff --git a/src/lib/onboard/setup-nim-selection.ts b/src/lib/onboard/setup-nim-selection.ts index 0706b2b507a..5fa532281f9 100644 --- a/src/lib/onboard/setup-nim-selection.ts +++ b/src/lib/onboard/setup-nim-selection.ts @@ -21,6 +21,8 @@ export type SetupNimSelectionState = { nimContainer: string | null; allowToolsIncompatible: boolean; skipHostInferenceSmoke?: boolean; + /** Public addresses approved for the selected custom endpoint. */ + endpointPinnedAddresses?: string[]; reuseGatewayCredentialWithoutLocalKey?: boolean; nvidiaFeaturedModels?: NvidiaFeaturedModelSession; /** Attempt-wide shared-gateway guard, invoked after identity selection and before probes. */ @@ -50,6 +52,7 @@ export function applyCloudFallbackSelection( state.allowToolsIncompatible = false; state.skipHostInferenceSmoke = false; state.reuseGatewayCredentialWithoutLocalKey = false; + delete state.endpointPinnedAddresses; } export function clearNimContainerBeforeRetry(state: SetupNimSelectionState): void { @@ -107,7 +110,7 @@ type ProbeOptions = { }; type ValidationResult = - | { ok: true; api: string | null; retry?: never } + | { ok: true; api: string | null; retry?: never; pinnedAddresses?: string[] } | { ok: false; api?: string; retry?: "credential" | "retry" | "model" | "selection" | string }; type RemoteModelValidationResult = "selected" | "retry-model" | "retry-selection"; @@ -195,6 +198,7 @@ export function createRemoteModelValidator(deps: RemoteModelValidatorDeps): { selectedCredentialEnv, intendedInferenceApi = "anthropic-messages", }) => { + delete state.endpointPinnedAddresses; const selectedModel = deps.requireValue( deps.isBackToSelection(state.model) ? null : state.model, `Missing model for ${remoteConfig.label}`, @@ -218,6 +222,9 @@ export function createRemoteModelValidator(deps: RemoteModelValidatorDeps): { remoteConfig.helpUrl, ); if (validation.ok) { + if (validation.pinnedAddresses) + state.endpointPinnedAddresses = validation.pinnedAddresses; + else delete state.endpointPinnedAddresses; // Probe the endpoint's runtime max_model_len so a custom vLLM endpoint // gets its real context window baked in instead of a small // architecture default; an explicit override always wins (#6177). @@ -260,6 +267,9 @@ export function createRemoteModelValidator(deps: RemoteModelValidatorDeps): { { intendedApi }, ); if (validation.ok) { + if (validation.pinnedAddresses) + state.endpointPinnedAddresses = validation.pinnedAddresses; + else delete state.endpointPinnedAddresses; state.preferredInferenceApi = validation.api; return "selected"; } diff --git a/test/onboard-resume-provider-recovery.test.ts b/test/onboard-resume-provider-recovery.test.ts index 5c47efc81e9..90437c63209 100644 --- a/test/onboard-resume-provider-recovery.test.ts +++ b/test/onboard-resume-provider-recovery.test.ts @@ -542,6 +542,7 @@ onboardSession.loadSession = () => ({ credentials.prompt = async () => ""; credentials.ensureApiKey = async () => {}; process.env.NEMOCLAW_NON_INTERACTIVE = "1"; +require("node:dns/promises").lookup = async () => [{ address: "93.184.216.34", family: 4 }]; const { setupNim } = require(${onboardPath}); (async () => { diff --git a/test/onboard-selection-anthropic-retry.test.ts b/test/onboard-selection-anthropic-retry.test.ts index cd8fa4ed031..2b7104d1434 100644 --- a/test/onboard-selection-anthropic-retry.test.ts +++ b/test/onboard-selection-anthropic-retry.test.ts @@ -98,6 +98,7 @@ credentials.prompt = async (message) => { return answers.shift() || ""; }; runner.runCapture = () => ""; +require("node:dns/promises").lookup = async () => [{ address: "93.184.216.34", family: 4 }]; const { setupNim } = require(${onboardPath}); @@ -179,6 +180,7 @@ credentials.prompt = async (message) => { }; runner.runCapture = () => ""; +require("node:dns/promises").lookup = async () => [{ address: "93.184.216.34", family: 4 }]; const { setupNim } = require(${onboardPath}); (async () => { diff --git a/test/onboard-selection.test.ts b/test/onboard-selection.test.ts index 68b318d1565..381b3a2461d 100644 --- a/test/onboard-selection.test.ts +++ b/test/onboard-selection.test.ts @@ -4741,7 +4741,7 @@ runner.runCapture = (cmd) => { process.env.COMPATIBLE_API_KEY = "test-key"; const { setupInference } = require(${onboardPath}); (async () => { - await setupInference(null, "qwen3.6:35b", "compatible-endpoint", "http://lan-server:11434/v1", "COMPATIBLE_API_KEY", null, [], { preferredInferenceApi: "openai-completions" }); + await setupInference(null, "qwen3.6:35b", "compatible-endpoint", "http://public-server.example:11434/v1", "COMPATIBLE_API_KEY", null, [], { preferredInferenceApi: "openai-completions", endpointPinnedAddresses: ["93.184.216.34"] }); process.exit(0); })().catch((err) => { console.error(err); process.exit(1); }); `; From abc6a1753bd575e4d56a87d24cd185a1e55877a5 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 14:35:21 -0700 Subject: [PATCH 30/41] test(e2e): align endpoint fixtures with SSRF pinning (#6293) Signed-off-by: Prekshi Vyas --- src/lib/onboard/setup-inference.ts | 8 +++++++ test/e2e/live/onboard-resume.test.ts | 36 ++++------------------------ 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/src/lib/onboard/setup-inference.ts b/src/lib/onboard/setup-inference.ts index bde185cfae1..cd0c3cb2e17 100644 --- a/src/lib/onboard/setup-inference.ts +++ b/src/lib/onboard/setup-inference.ts @@ -1,6 +1,7 @@ // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. // SPDX-License-Identifier: Apache-2.0 +import { isBedrockRuntimeEndpoint } from "../inference/bedrock-runtime"; import { assertEndpointResolvesPublic, type EndpointDnsLookupFn, @@ -242,9 +243,16 @@ export function createSetupInference( } deps.step(4, 8, "Setting up inference provider"); let endpointPinnedAddresses = options.endpointPinnedAddresses; + // Strictly classified AWS Bedrock Runtime hostnames use the dedicated + // SigV4/bearer adapter rather than the generic curl probe path. Their + // hostname is constrained to AWS-owned suffixes by the classifier, so + // do not apply the custom-origin curl pinning contract here. + const usesBedrockRuntimeAdapter = + provider === "compatible-anthropic-endpoint" && isBedrockRuntimeEndpoint(endpointUrl); if ( (provider === "compatible-endpoint" || provider === "compatible-anthropic-endpoint") && endpointUrl && + !usesBedrockRuntimeAdapter && !endpointPinnedAddresses ) { const preflight = await assertEndpointResolvesPublic( diff --git a/test/e2e/live/onboard-resume.test.ts b/test/e2e/live/onboard-resume.test.ts index 7edd457c692..7114f2e061a 100644 --- a/test/e2e/live/onboard-resume.test.ts +++ b/test/e2e/live/onboard-resume.test.ts @@ -107,36 +107,6 @@ function containsExactJsonToken(value: unknown, token: string): boolean { return false; } -async function hostAddressForSandbox(host: HostCliClient): Promise { - const probe = await host.command( - "bash", - [ - "-lc", - [ - 'ip_addr="$(ip route get 1.1.1.1 2>/dev/null | awk \'{for (i=1;i<=NF;i++) if ($i=="src") {print $(i+1); exit}}\')"', - 'if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', - "ip_addr=\"$(hostname -I 2>/dev/null | awk '{print $1}')\"", - 'if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', - 'if [ "$(uname -s 2>/dev/null)" = "Darwin" ]; then', - " for iface in en0 en1 bridge100; do", - ' ip_addr="$(ipconfig getifaddr "$iface" 2>/dev/null || true)"', - ' if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', - " done", - " ip_addr=\"$(ifconfig 2>/dev/null | awk '/inet / && $2 !~ /^127\\./ {print $2; exit}')\"", - ' if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', - "fi", - "echo 127.0.0.1", - ].join("\n"), - ], - { - artifactName: "host-ip-for-onboard-resume-compatible-endpoint", - env: buildAvailabilityProbeEnv(), - timeoutMs: 30_000, - }, - ); - return probe.stdout.trim().split(/\s+/)[0] || "127.0.0.1"; -} - function expectHermeticCompatibleInferenceUsed(fake: FakeOpenAiCompatibleServer): void { const requests = fake.requests(); const inferencePosts = requests.filter( @@ -205,7 +175,7 @@ test("onboard-resume: interrupted onboard then --resume completes without redoin // pass hosted NVIDIA inference secrets. Instead, this test exposes a local // fake OpenAI-compatible endpoint at a host address the OpenShell gateway and // sandbox can route to, matching test/e2e/lib/hermetic-compatible-inference.sh. - const fakePublicHost = await hostAddressForSandbox(host); + const fakePublicHost = "host.openshell.internal"; const fake = await startFakeOpenAiCompatibleServer({ apiKey: FAKE_COMPATIBLE_AUTH_VALUE, host: "0.0.0.0", @@ -222,7 +192,9 @@ test("onboard-resume: interrupted onboard then --resume completes without redoin model: FAKE_COMPATIBLE_MODEL, publicHost: fakePublicHost, }); - const modelsResponse = await fetch(`${fake.baseUrl}/models`); + const localModelsUrl = new URL(`${fake.baseUrl}/models`); + localModelsUrl.hostname = "127.0.0.1"; + const modelsResponse = await fetch(localModelsUrl); expect(modelsResponse.ok, `fake endpoint ${fake.baseUrl}/models should be reachable`).toBe(true); // ────────────────────────────────────────────────────────────────── From 47dc8d063d6fa21c1c34d7797ed6ebd980924ba7 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 14:41:19 -0700 Subject: [PATCH 31/41] test(e2e): route local inference through managed host alias (#6293) Signed-off-by: Prekshi Vyas --- test/e2e/lib/hermetic-compatible-inference.sh | 27 +------------ test/e2e/live/hermes-gpu-startup.test.ts | 21 +--------- test/e2e/live/mcp-bridge-sandbox.ts | 22 +--------- .../messaging-compatible-endpoint.test.ts | 40 ++++--------------- 4 files changed, 11 insertions(+), 99 deletions(-) diff --git a/test/e2e/lib/hermetic-compatible-inference.sh b/test/e2e/lib/hermetic-compatible-inference.sh index c3f5d7137ef..b63482b2b3e 100755 --- a/test/e2e/lib/hermetic-compatible-inference.sh +++ b/test/e2e/lib/hermetic-compatible-inference.sh @@ -10,31 +10,6 @@ # shellcheck source=test/e2e/lib/openai-compatible-api-proof.sh . "$(dirname "${BASH_SOURCE[0]}")/openai-compatible-api-proof.sh" -nemoclaw_e2e_host_ip_for_sandbox() { - local ip_addr - if command -v ip >/dev/null 2>&1; then - ip_addr="$(ip route get 1.1.1.1 2>/dev/null | awk '{for (i=1;i<=NF;i++) if ($i=="src") {print $(i+1); exit}}')" - if [ -n "$ip_addr" ]; then - echo "$ip_addr" - return - fi - fi - - if command -v hostname >/dev/null 2>&1; then - for ip_addr in $(hostname -I 2>/dev/null); do - case "$ip_addr" in - 127.* | ::1) ;; - *) - echo "$ip_addr" - return - ;; - esac - done - fi - - echo "127.0.0.1" -} - nemoclaw_e2e_start_hermetic_compatible_inference() { local fake_key fake_key="${NEMOCLAW_E2E_COMPATIBLE_API_KEY:-e2e-compatible-key}" @@ -44,7 +19,7 @@ nemoclaw_e2e_start_hermetic_compatible_inference() { export FAKE_OPENAI_PORT="${FAKE_OPENAI_PORT:-0}" export FAKE_OPENAI_HOST="${FAKE_OPENAI_HOST:-0.0.0.0}" export FAKE_OPENAI_READY_HOST="${FAKE_OPENAI_READY_HOST:-127.0.0.1}" - export FAKE_OPENAI_PUBLIC_HOST="${FAKE_OPENAI_PUBLIC_HOST:-$(nemoclaw_e2e_host_ip_for_sandbox)}" + export FAKE_OPENAI_PUBLIC_HOST="${FAKE_OPENAI_PUBLIC_HOST:-host.openshell.internal}" export FAKE_OPENAI_MODEL="${FAKE_OPENAI_MODEL:-${NEMOCLAW_E2E_COMPATIBLE_MODEL:-test-model}}" export FAKE_OPENAI_API_KEY="$fake_key" export FAKE_OPENAI_REQUIRE_AUTH=1 diff --git a/test/e2e/live/hermes-gpu-startup.test.ts b/test/e2e/live/hermes-gpu-startup.test.ts index 02758b507d2..36d3efb8bfe 100644 --- a/test/e2e/live/hermes-gpu-startup.test.ts +++ b/test/e2e/live/hermes-gpu-startup.test.ts @@ -190,26 +190,7 @@ test("hermes-gpu-startup: selected OpenShell GPU route reaches stable Ready stat }); expect(dockerInfo.exitCode, resultText(dockerInfo)).toBe(0); - const hostAddressProbe = await host.command( - "bash", - [ - "-lc", - [ - 'ip_addr="$(ip route get 1.1.1.1 2>/dev/null | awk \'{for (i=1;i<=NF;i++) if ($i=="src") {print $(i+1); exit}}\')"', - 'test -n "$ip_addr" || ip_addr="$(hostname -I 2>/dev/null | awk \'{print $1}\')"', - 'test -n "$ip_addr"', - 'printf "%s\\n" "$ip_addr"', - ].join("\n"), - ], - { - artifactName: "phase-1-sandbox-reachable-host-address", - env: buildAvailabilityProbeEnv(), - timeoutMs: 30_000, - }, - ); - expect(hostAddressProbe.exitCode, resultText(hostAddressProbe)).toBe(0); - const hostAddress = hostAddressProbe.stdout.trim().split(/\s+/)[0]; - expect(hostAddress).toBeTruthy(); + const hostAddress = "host.openshell.internal"; const fake = await startFakeOpenAiCompatibleServer({ apiKey: FAKE_API_KEY, diff --git a/test/e2e/live/mcp-bridge-sandbox.ts b/test/e2e/live/mcp-bridge-sandbox.ts index bb9da5af88e..923438d1b29 100644 --- a/test/e2e/live/mcp-bridge-sandbox.ts +++ b/test/e2e/live/mcp-bridge-sandbox.ts @@ -10,26 +10,8 @@ const MCP_CURL_HTTP_CODE_MARKER = "NEMOCLAW_MCP_CURL_HTTP_CODE="; export type McpDnsRebindingAdapter = "mcporter" | "hermes-config" | "deepagents-config"; -export async function hostAddressForSandbox(host: HostCliClient): Promise { - const probe = await host.command( - "bash", - [ - "-lc", - [ - 'ip_addr="$(ip route get 1.1.1.1 2>/dev/null | awk \'{for (i=1;i<=NF;i++) if ($i=="src") {print $(i+1); exit}}\')"', - 'if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', - "ip_addr=\"$(hostname -I 2>/dev/null | awk '{print $1}')\"", - 'if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', - "echo 127.0.0.1", - ].join("\n"), - ], - { - artifactName: "host-ip-for-mcp-compatible-endpoint", - env: buildAvailabilityProbeEnv(), - timeoutMs: 30_000, - }, - ); - return probe.stdout.trim().split(/\s+/)[0] || "127.0.0.1"; +export async function hostAddressForSandbox(_host: HostCliClient): Promise { + return "host.openshell.internal"; } export { diff --git a/test/e2e/live/messaging-compatible-endpoint.test.ts b/test/e2e/live/messaging-compatible-endpoint.test.ts index 854eea637e0..025a7ccfd8e 100644 --- a/test/e2e/live/messaging-compatible-endpoint.test.ts +++ b/test/e2e/live/messaging-compatible-endpoint.test.ts @@ -263,36 +263,6 @@ async function startCompatibleMock( throw new Error("compatible endpoint mock failed to answer /v1/models"); } -async function hostAddressForSandbox(host: HostCliClient): Promise { - const probe = await host.command( - "bash", - [ - "-lc", - [ - 'ip_addr="$(ip route get 1.1.1.1 2>/dev/null | awk \'{for (i=1;i<=NF;i++) if ($i=="src") {print $(i+1); exit}}\')"', - 'if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', - "ip_addr=\"$(hostname -I 2>/dev/null | awk '{print $1}')\"", - 'if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', - 'if [ "$(uname -s 2>/dev/null)" = "Darwin" ]; then', - " for iface in en0 en1 bridge100; do", - ' ip_addr="$(ipconfig getifaddr "$iface" 2>/dev/null || true)"', - ' if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', - " done", - " ip_addr=\"$(ifconfig 2>/dev/null | awk '/inet / && $2 !~ /^127\\./ {print $2; exit}')\"", - ' if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', - "fi", - "echo 127.0.0.1", - ].join("\n"), - ], - { - artifactName: "host-ip-for-compatible-endpoint", - env: commandEnv(), - timeoutMs: 30_000, - }, - ); - return probe.stdout.trim().split(/\s+/)[0] || "127.0.0.1"; -} - async function sourceCliAvailable(host: HostCliClient): Promise { if (!fs.existsSync(CLI_DIST_ENTRYPOINT)) return false; const result = await host.command( @@ -607,11 +577,15 @@ test("messaging compatible endpoint routes Telegram-enabled OpenClaw through inf await compatibleMock.close(); }); - const hostAddress = await hostAddressForSandbox(host); - const endpointUrl = `http://${hostAddress}:${new URL(compatibleMock.localBaseUrl).port}/v1`; + const endpointUrl = `http://host.openshell.internal:${new URL(compatibleMock.localBaseUrl).port}/v1`; const hostReachability = await host.command( "curl", - ["-sf", "-H", `Authorization: Bearer ${COMPATIBLE_KEY}`, `${endpointUrl}/models`], + [ + "-sf", + "-H", + `Authorization: Bearer ${COMPATIBLE_KEY}`, + `${compatibleMock.localBaseUrl}/models`, + ], { artifactName: "compatible-endpoint-host-reachability", env: commandEnv(), From 2e07baf27089448a0609ee7cf4319500a3ead82a Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 14:50:14 -0700 Subject: [PATCH 32/41] test(e2e): align resumed endpoint verification (#6293) --- test/e2e/live/onboard-resume.test.ts | 21 +++++-------------- ...-anthropic-compatible-openai-agent.test.ts | 1 + test/onboard-inference-smoke.test.ts | 5 ++++- test/support/setup-inference-test-harness.ts | 3 +++ 4 files changed, 13 insertions(+), 17 deletions(-) diff --git a/test/e2e/live/onboard-resume.test.ts b/test/e2e/live/onboard-resume.test.ts index 7114f2e061a..3702703e829 100644 --- a/test/e2e/live/onboard-resume.test.ts +++ b/test/e2e/live/onboard-resume.test.ts @@ -107,27 +107,16 @@ function containsExactJsonToken(value: unknown, token: string): boolean { return false; } -function expectHermeticCompatibleInferenceUsed(fake: FakeOpenAiCompatibleServer): void { +function expectHermeticCompatibleEndpointUsed(fake: FakeOpenAiCompatibleServer): void { const requests = fake.requests(); - const inferencePosts = requests.filter( - (entry) => - entry.method === "POST" && - ["/v1/chat/completions", "/chat/completions", "/v1/responses", "/responses"].includes( - entry.path, - ), - ); expect( - inferencePosts.length, - `expected fake inference POST, got ${JSON.stringify(requests)}`, - ).toBeGreaterThan(0); + requests.some((entry) => entry.method === "GET" && entry.path === "/v1/models"), + `expected authenticated fake endpoint discovery, got ${JSON.stringify(requests)}`, + ).toBe(true); expect( requests.filter((entry) => entry.auth === "missing"), `fake endpoint saw unauthenticated requests: ${JSON.stringify(requests)}`, ).toEqual([]); - expect( - inferencePosts.filter((entry) => entry.auth !== "ok"), - `fake inference POST had missing auth: ${JSON.stringify(requests)}`, - ).toEqual([]); } // The e2e-live Vitest project owns the NEMOCLAW_RUN_LIVE_E2E collection gate, @@ -320,7 +309,7 @@ test("onboard-resume: interrupted onboard then --resume completes without redoin expect(interrupted.failure?.step).toBe("policies"); await artifacts.writeJson("phase-2-fake-openai-compatible-requests.json", fake.requests()); - expectHermeticCompatibleInferenceUsed(fake); + expectHermeticCompatibleEndpointUsed(fake); // ────────────────────────────────────────────────────────────────── // Phase 3: resume — NVIDIA_INFERENCE_API_KEY and COMPATIBLE_API_KEY are diff --git a/test/onboard-anthropic-compatible-openai-agent.test.ts b/test/onboard-anthropic-compatible-openai-agent.test.ts index 1867c735252..6923e321cba 100644 --- a/test/onboard-anthropic-compatible-openai-agent.test.ts +++ b/test/onboard-anthropic-compatible-openai-agent.test.ts @@ -71,6 +71,7 @@ describe("compatible-anthropic-endpoint registration for OpenAI-only agents (#62 // The probe must exercise the same /v1 base OpenShell will call at // runtime ( + /v1/chat/completions with /v1 dedup). expect(probeOpenAiLikeEndpoint).toHaveBeenCalledWith(SURFACE_URL, MODEL, "hub-secret", { + pinnedAddresses: ["93.184.216.34"], skipResponsesProbe: true, }); const createCommand = harness.commands.find(({ command }) => diff --git a/test/onboard-inference-smoke.test.ts b/test/onboard-inference-smoke.test.ts index 41d2a04eb03..7e248e16df5 100644 --- a/test/onboard-inference-smoke.test.ts +++ b/test/onboard-inference-smoke.test.ts @@ -105,7 +105,10 @@ process.env.NEMOCLAW_ONBOARD_INFERENCE_SMOKE_E2E = "1"; process.env.NEMOCLAW_TEST_NO_SLEEP = "1"; process.env.BROKEN_API_KEY = "test-key"; -const { setupInference } = require(${onboardPath}); +const { createSetupInference } = require(${onboardPath}); +const setupInference = createSetupInference({ + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], +}); (async () => { await setupInference( diff --git a/test/support/setup-inference-test-harness.ts b/test/support/setup-inference-test-harness.ts index 11eaf2e6177..b105af4dd61 100644 --- a/test/support/setup-inference-test-harness.ts +++ b/test/support/setup-inference-test-harness.ts @@ -318,6 +318,9 @@ export function createDirectSetupInferenceHarnessFactory( }, hydrateCredentialEnv: (envName: string | null | undefined) => envName ? process.env[envName] || null : null, + // Direct setup tests use documentation-only hostnames and intentionally + // bypass the selection phase that normally supplies validated pins. + resolveEndpointHost: async () => [{ address: "93.184.216.34", family: 4 }], promptValidationRecovery: async () => "selection", validateLocalProvider: () => ({ ok: true }), getLocalProviderHealthCheck: () => null, From 48002c591a0aa3dbc25381c0f6978a9f7257ff6c Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 15:01:03 -0700 Subject: [PATCH 33/41] fix(security): constrain trusted Bedrock endpoints (#6293) --- src/lib/inference/bedrock-runtime.test.ts | 13 ++ src/lib/inference/bedrock-runtime.ts | 13 +- ...drock-runtime-compatible-anthropic.test.ts | 113 +++++++++++++++++- 3 files changed, 133 insertions(+), 6 deletions(-) diff --git a/src/lib/inference/bedrock-runtime.test.ts b/src/lib/inference/bedrock-runtime.test.ts index 8792f5ee86a..a4700439b04 100644 --- a/src/lib/inference/bedrock-runtime.test.ts +++ b/src/lib/inference/bedrock-runtime.test.ts @@ -37,4 +37,17 @@ describe("Bedrock Runtime endpoint classification", () => { expect(isBedrockRuntimeEndpoint("https://proxy.example.com/v1/messages")).toBe(false); expect(isBedrockRuntimeEndpoint("https://api.anthropic.com/v1/messages")).toBe(false); }); + + it("requires the canonical authenticated TLS boundary for Bedrock Runtime", () => { + expect(isBedrockRuntimeEndpoint("https://bedrock-runtime.us-east-1.amazonaws.com:443")).toBe( + true, + ); + expect(isBedrockRuntimeEndpoint("http://bedrock-runtime.us-east-1.amazonaws.com")).toBe(false); + expect(isBedrockRuntimeEndpoint("https://bedrock-runtime.us-east-1.amazonaws.com:18147")).toBe( + false, + ); + expect(isBedrockRuntimeEndpoint("https://user@bedrock-runtime.us-east-1.amazonaws.com")).toBe( + false, + ); + }); }); diff --git a/src/lib/inference/bedrock-runtime.ts b/src/lib/inference/bedrock-runtime.ts index 6dd9f25567a..e7d2e511098 100644 --- a/src/lib/inference/bedrock-runtime.ts +++ b/src/lib/inference/bedrock-runtime.ts @@ -61,6 +61,7 @@ function classifyBedrockRuntimeHostname( export function classifyCustomAnthropicEndpoint( value: string | URL | null | undefined, ): CustomAnthropicEndpointClassification { + const original = parseEndpointUrl(value); const normalized = normalizeProviderBaseUrl(value, "anthropic"); const parsed = parseEndpointUrl(normalized); if (!parsed) { @@ -72,7 +73,17 @@ export function classifyCustomAnthropicEndpoint( } const bedrock = classifyBedrockRuntimeHostname(parsed.hostname); - if (!bedrock) { + // The dedicated adapter relies on normal TLS hostname verification rather + // than curl --resolve pinning. Only the canonical HTTPS origin is therefore + // eligible: a rebound address cannot receive credentials without presenting + // a certificate valid for the AWS-owned hostname. Plain HTTP, custom ports, + // and URLs carrying userinfo stay on the generic preflighted path. + const isCanonicalTlsOrigin = + original?.protocol === "https:" && + original.port === "" && + original.username === "" && + original.password === ""; + if (!bedrock || !isCanonicalTlsOrigin) { return { kind: "anthropic-messages", endpointUrl: normalized, diff --git a/test/e2e/live/bedrock-runtime-compatible-anthropic.test.ts b/test/e2e/live/bedrock-runtime-compatible-anthropic.test.ts index 448b48fbcd9..9f90f601ac1 100644 --- a/test/e2e/live/bedrock-runtime-compatible-anthropic.test.ts +++ b/test/e2e/live/bedrock-runtime-compatible-anthropic.test.ts @@ -47,9 +47,9 @@ const require = createRequire(import.meta.url); const DIST_ENTRYPOINT = CLI_DIST_ENTRYPOINT; const BEDROCK_HOSTNAME = "bedrock-runtime.us-east-1.amazonaws.com"; -const BEDROCK_MOCK_PORT = Number(process.env.NEMOCLAW_BEDROCK_RUNTIME_MOCK_PORT ?? "18147"); +const BEDROCK_MOCK_PORT = 18147; const BEDROCK_ADAPTER_PORT = 11436; -const BEDROCK_ENDPOINT_URL = `http://${BEDROCK_HOSTNAME}:${BEDROCK_MOCK_PORT}`; +const BEDROCK_ENDPOINT_URL = `https://${BEDROCK_HOSTNAME}`; const BEDROCK_MODEL = process.env.NEMOCLAW_BEDROCK_RUNTIME_MODEL ?? "anthropic.claude-3-5-sonnet-20240620-v1:0"; const COMPATIBLE_KEY = @@ -131,7 +131,7 @@ function testEnv(home: string, extra: NodeJS.ProcessEnv = {}): NodeJS.ProcessEnv return testHomeEnvironment(home, extra); } -function onboardEnv(home: string, agent: AgentName): NodeJS.ProcessEnv { +function onboardEnv(home: string, agent: AgentName, caCertPath: string): NodeJS.ProcessEnv { return testEnv(home, { COMPATIBLE_ANTHROPIC_API_KEY: COMPATIBLE_KEY, NEMOCLAW_AGENT: agent, @@ -143,9 +143,48 @@ function onboardEnv(home: string, agent: AgentName): NodeJS.ProcessEnv { NEMOCLAW_RECREATE_SANDBOX: "1", NEMOCLAW_SANDBOX_NAME: SANDBOX_NAME, NEMOCLAW_YES: "1", + NODE_EXTRA_CA_CERTS: caCertPath, }); } +function createBedrockTlsFixture(home: string): { cert: Buffer; key: Buffer; certPath: string } { + const tlsDir = path.join(home, "bedrock-runtime-tls"); + const certPath = path.join(tlsDir, "cert.pem"); + const keyPath = path.join(tlsDir, "key.pem"); + fs.mkdirSync(tlsDir, { recursive: true }); + const generated = spawnSync( + "openssl", + [ + "req", + "-x509", + "-newkey", + "rsa:2048", + "-keyout", + keyPath, + "-out", + certPath, + "-days", + "1", + "-nodes", + "-subj", + `/CN=${BEDROCK_HOSTNAME}`, + "-addext", + `subjectAltName=DNS:${BEDROCK_HOSTNAME}`, + ], + { encoding: "utf8" }, + ); + if (generated.status !== 0) { + throw new Error( + `failed to generate Bedrock TLS fixture: ${generated.stderr || generated.error}`, + ); + } + return { + cert: fs.readFileSync(certPath), + key: fs.readFileSync(keyPath), + certPath, + }; +} + function redactedCommand(command: readonly string[], values: readonly string[]): string[] { return command.map((part) => redactString(part, values)); } @@ -347,6 +386,7 @@ async function startFakeBedrockRuntimeMock(options: { port: number; expectedBearer: string; expectedModel: string; + tls: { cert: Buffer; key: Buffer }; }): Promise { const codec = loadEventStreamCodec(); const logs: string[] = []; @@ -355,7 +395,7 @@ async function startFakeBedrockRuntimeMock(options: { const record = (line: string): void => { logs.push(line); }; - const server = http2.createServer(); + const server = http2.createSecureServer(options.tls); const sessions = new Set(); server.on("session", (session) => { @@ -638,6 +678,63 @@ async function mapBedrockHostToLoopback( expectExitZero(probe, "Bedrock Runtime hostname maps to localhost"); } +const bedrockTlsRedirectArgs = [ + "-t", + "nat", + "OUTPUT", + "-p", + "tcp", + "-d", + "127.0.0.1", + "--dport", + "443", + "-j", + "REDIRECT", + "--to-ports", + String(BEDROCK_MOCK_PORT), +]; + +async function installBedrockTlsRedirect(host: HostCliClient, home: string): Promise { + expectExitZero( + await host.command( + "sudo", + [ + "-n", + "iptables", + ...bedrockTlsRedirectArgs.slice(0, 2), + "-A", + ...bedrockTlsRedirectArgs.slice(2), + ], + { + artifactName: "install-bedrock-tls-port-redirect", + env: testEnv(home), + timeoutMs: 30_000, + }, + ), + "redirect canonical Bedrock TLS port to the unprivileged fake endpoint", + ); +} + +async function removeBedrockTlsRedirect(host: HostCliClient, home: string): Promise { + await bestEffort(() => + host.command( + "sudo", + [ + "-n", + "iptables", + ...bedrockTlsRedirectArgs.slice(0, 2), + "-D", + ...bedrockTlsRedirectArgs.slice(2), + ], + { + artifactName: "remove-bedrock-tls-port-redirect", + env: testEnv(home), + timeoutMs: 30_000, + }, + ), + ); +} + async function prepareSourceCliAndOpenShell(host: HostCliClient, home: string): Promise { expect( fs.existsSync(DIST_ENTRYPOINT), @@ -1224,6 +1321,9 @@ test("bedrock runtime compatible Anthropic endpoint routes through managed infer cleanup.add("restore /etc/hosts after Bedrock Runtime mapping", () => restoreHostsFile(host, hostsBackup, hostsBackupDir, home), ); + cleanup.add("remove Bedrock Runtime canonical TLS port redirect", () => + removeBedrockTlsRedirect(host, home), + ); cleanup.add("stop Bedrock Runtime adapter", () => stopBedrockAdapterBestEffort(home)); cleanup.add("stop fake Bedrock Runtime endpoint", async () => { if (mock) await mock.close(); @@ -1279,10 +1379,13 @@ test("bedrock runtime compatible Anthropic endpoint routes through managed infer await prepareSourceCliAndOpenShell(host, home); await mapBedrockHostToLoopback(host, home, hostsBackup, skip); + await installBedrockTlsRedirect(host, home); + const tls = createBedrockTlsFixture(home); mock = await startFakeBedrockRuntimeMock({ port: BEDROCK_MOCK_PORT, expectedBearer: COMPATIBLE_KEY, expectedModel: BEDROCK_MODEL, + tls, }); await cleanupSandboxState(host, home); @@ -1298,7 +1401,7 @@ test("bedrock runtime compatible Anthropic endpoint routes through managed infer { artifactName: `onboard-bedrock-runtime-${AGENT}`, artifacts, - env: onboardEnv(home, AGENT), + env: onboardEnv(home, AGENT, tls.certPath), redactionValues: [COMPATIBLE_KEY], timeoutMs: ONBOARD_TIMEOUT_MS, }, From 482db5dc26664066096ef6a2b6506ecf9a53b7b1 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 15:07:36 -0700 Subject: [PATCH 34/41] test(e2e): keep Bedrock fixture branch-neutral (#6293) --- .../live/bedrock-runtime-compatible-anthropic.test.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/e2e/live/bedrock-runtime-compatible-anthropic.test.ts b/test/e2e/live/bedrock-runtime-compatible-anthropic.test.ts index 9f90f601ac1..cb04a6440bb 100644 --- a/test/e2e/live/bedrock-runtime-compatible-anthropic.test.ts +++ b/test/e2e/live/bedrock-runtime-compatible-anthropic.test.ts @@ -173,11 +173,10 @@ function createBedrockTlsFixture(home: string): { cert: Buffer; key: Buffer; cer ], { encoding: "utf8" }, ); - if (generated.status !== 0) { - throw new Error( - `failed to generate Bedrock TLS fixture: ${generated.stderr || generated.error}`, - ); - } + expect( + generated.status, + `failed to generate Bedrock TLS fixture: ${generated.stderr || generated.error}`, + ).toBe(0); return { cert: fs.readFileSync(certPath), key: fs.readFileSync(keyPath), From bfe7c9f073fcc49beaf4c0e068a8a1b3fa960827 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 15:31:48 -0700 Subject: [PATCH 35/41] test(e2e): preserve MCP rebinding proof address (#6293) --- test/e2e/live/mcp-bridge-sandbox.ts | 23 +++++++++++++++++++++ test/e2e/live/mcp-bridge.test.ts | 10 ++++----- test/e2e/support/mcp-bridge-sandbox.test.ts | 16 ++++++++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/test/e2e/live/mcp-bridge-sandbox.ts b/test/e2e/live/mcp-bridge-sandbox.ts index 923438d1b29..37ac1862c6a 100644 --- a/test/e2e/live/mcp-bridge-sandbox.ts +++ b/test/e2e/live/mcp-bridge-sandbox.ts @@ -14,6 +14,29 @@ export async function hostAddressForSandbox(_host: HostCliClient): Promise { + const probe = await host.command( + "bash", + [ + "-lc", + [ + 'ip_addr="$(ip route get 1.1.1.1 2>/dev/null | awk \'{for (i=1;i<=NF;i++) if ($i=="src") {print $(i+1); exit}}\')"', + 'if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', + "ip_addr=\"$(hostname -I 2>/dev/null | awk '{print $1}')\"", + 'if [ -n "$ip_addr" ]; then echo "$ip_addr"; exit 0; fi', + "echo 127.0.0.1", + ].join("\n"), + ], + { + artifactName: "host-private-ip-for-mcp-rebinding", + env: buildAvailabilityProbeEnv(), + timeoutMs: 30_000, + }, + ); + return probe.stdout.trim().split(/\s+/)[0] || "127.0.0.1"; +} + export { type DnsRebindingHostsFixture, remapDnsRebindingHostname, diff --git a/test/e2e/live/mcp-bridge.test.ts b/test/e2e/live/mcp-bridge.test.ts index a06bbab873e..e5a6fc08b72 100644 --- a/test/e2e/live/mcp-bridge.test.ts +++ b/test/e2e/live/mcp-bridge.test.ts @@ -29,6 +29,7 @@ import { import { buildMcpDnsRebindingProbeScript, hostAddressForSandbox, + hostPrivateAddressForSandbox, isExpectedMcpCurlPolicyDenial, type McpDnsRebindingAdapter, remapDnsRebindingHostname, @@ -168,7 +169,6 @@ async function assertAdapterDnsRebindingDenied( options: { adapter: McpDnsRebindingAdapter; artifactPrefix: string; - hostAddress: string; sandboxName: string; secretPaths: string[]; }, @@ -276,12 +276,13 @@ async function assertAdapterDnsRebindingDenied( // reachable runner address would receive the request. The pinned v0.0.72 // implementation instead returns the one resolved-and-validated SocketAddr // list directly to connect; see the exact proxy.rs citation in the helper. - expect(options.hostAddress).not.toBe(REBIND_PUBLIC_IP); + const reboundAddress = await hostPrivateAddressForSandbox(host); + expect(reboundAddress).not.toBe(REBIND_PUBLIC_IP); await remapDnsRebindingHostname( host, options.sandboxName, hostsFixture, - options.hostAddress, + reboundAddress, `${options.artifactPrefix}-mcp-dns-rebinding-map-private-unpinned-after-add`, ); const denial = await sandbox.execShell( @@ -1023,7 +1024,6 @@ req.end(body); await assertAdapterDnsRebindingDenied(host, sandbox, cleanup, { adapter: "mcporter", artifactPrefix: "openclaw", - hostAddress, sandboxName: OPENCLAW_SANDBOX_NAME, secretPaths: ["/sandbox/.openclaw", "/sandbox/.mcp.json"], }); @@ -1261,7 +1261,6 @@ liveAgentMatrixTest( await assertAdapterDnsRebindingDenied(host, sandbox, cleanup, { adapter: "hermes-config", artifactPrefix: "hermes", - hostAddress, sandboxName: HERMES_SANDBOX_NAME, secretPaths: ["/sandbox/.hermes"], }); @@ -1414,7 +1413,6 @@ liveAgentMatrixTest( await assertAdapterDnsRebindingDenied(host, sandbox, cleanup, { adapter: "deepagents-config", artifactPrefix: "deepagents", - hostAddress, sandboxName: DEEPAGENTS_SANDBOX_NAME, secretPaths: ["/sandbox/.deepagents"], }); diff --git a/test/e2e/support/mcp-bridge-sandbox.test.ts b/test/e2e/support/mcp-bridge-sandbox.test.ts index 4e46c90c50e..241b84dc7a8 100644 --- a/test/e2e/support/mcp-bridge-sandbox.test.ts +++ b/test/e2e/support/mcp-bridge-sandbox.test.ts @@ -13,6 +13,8 @@ import { testTimeout } from "../../helpers/timeouts"; import type { HostCliClient } from "../fixtures/clients/host.ts"; import { buildMcpDnsRebindingProbeScript, + hostAddressForSandbox, + hostPrivateAddressForSandbox, isExpectedMcpCurlPolicyDenial, restoreDnsRebindingHostsFixture, } from "../live/mcp-bridge-sandbox.ts"; @@ -81,6 +83,20 @@ async function captureRestoreScript(hostBackupPath: string, sandboxBackupPath: s } describe("MCP curl policy denial classification", SUITE_OPTIONS, () => { + it("separates the managed endpoint alias from the concrete rebinding address", async () => { + let probeScript = ""; + const host = { + command: async (_command: string, args: string[]) => { + probeScript = args[1] ?? ""; + return { ...denialResult(), stdout: "10.20.30.40\n" }; + }, + } as unknown as HostCliClient; + + await expect(hostAddressForSandbox(host)).resolves.toBe("host.openshell.internal"); + await expect(hostPrivateAddressForSandbox(host)).resolves.toBe("10.20.30.40"); + expect(probeScript).toContain("ip route get 1.1.1.1"); + }); + it("accepts an L7 HTTP 403 denial", () => { expect( isExpectedMcpCurlPolicyDenial(denialResult({ stdout: "NEMOCLAW_MCP_CURL_HTTP_CODE=403\n" })), From 9d77c59227c4d96d78c7bf8a1344adadf42f9438 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 16:26:47 -0700 Subject: [PATCH 36/41] fix: close exact-head review and e2e failures Signed-off-by: Prekshi Vyas --- agents/hermes/runtime-config-guard.py | 19 ++++++- src/lib/adapters/http/probe.test.ts | 44 ++++++++++++++ src/lib/adapters/http/probe.ts | 19 +++++-- .../inference/endpoint-ssrf-preflight.test.ts | 9 +-- src/lib/inference/endpoint-ssrf-preflight.ts | 12 ++-- .../runtime/whatsapp-qr-compact.test.ts | 12 ++++ .../whatsapp/runtime/whatsapp-qr-compact.ts | 44 +++++++++++++- .../inference-selection-validation.test.ts | 42 ++++++++++++++ test/e2e/live/onboard-resume.test.ts | 23 ++++++-- ...nonroot-strict-hash-reconciliation.test.ts | 57 +++++++++++++++++++ ...openclaw-slack-deny-feedback-patch.test.ts | 23 +++++++- 11 files changed, 279 insertions(+), 25 deletions(-) diff --git a/agents/hermes/runtime-config-guard.py b/agents/hermes/runtime-config-guard.py index a6cc4d1370c..62669cbe7cd 100755 --- a/agents/hermes/runtime-config-guard.py +++ b/agents/hermes/runtime-config-guard.py @@ -2653,7 +2653,7 @@ def seal_restart( try: _verify_strict_hash(hermes_dir, hash_file) except StrictHashMismatchError: - if purpose != "config-write" or expected_config_sha256 is None: + if purpose not in ("config-write", "shields-mutable") or expected_config_sha256 is None: raise _reconcile_nonroot_startup_api_key_hash( hermes_dir, @@ -3441,8 +3441,23 @@ def begin_shields_transition( rollback_mode or "mutable", ) + # A fresh managed non-root Hermes start mints exactly one API_SERVER_KEY and + # refreshes its sandbox-owned compatibility anchor, while the root-owned + # strict anchor deliberately remains unchanged. The first shields-down is + # the next root transaction and must admit that same narrowly reviewed + # reconciliation as write-config. Derive the expected config digest from + # the existing strict anchor so shields can never bless config drift. + strict_config_sha256, _strict_env_sha256, _strict_mcp_state = _parse_config_hash( + _read_hash_file(hash_file), + os.path.join(hermes_dir, "config.yaml"), + os.path.join(hermes_dir, ".env"), + ) original_locked = seal_restart( - hermes_dir, hash_file, state_file, purpose="shields-mutable" + hermes_dir, + hash_file, + state_file, + purpose="shields-mutable", + expected_config_sha256=strict_config_sha256, ) try: state_data = _load_restart_state(state_file) diff --git a/src/lib/adapters/http/probe.test.ts b/src/lib/adapters/http/probe.test.ts index 5dc1e376772..65278926059 100644 --- a/src/lib/adapters/http/probe.test.ts +++ b/src/lib/adapters/http/probe.test.ts @@ -503,6 +503,50 @@ describe("http-probe helpers", () => { expect(spawnedEnv?.no_proxy).toBe("*"); }); + it.each([ + "http://127.0.0.1:8000/v1/models", + "https://inference.local/v1/models", + "https://93.184.216.34/v1/models", + ])("bypasses ambient proxies for approved no-pin origin %s (#6293)", (url) => { + let spawnedEnv: NodeJS.ProcessEnv | undefined; + runCurlProbe(["-sS", url], { + pinnedAddresses: [], + replaceEnv: true, + env: { + HTTP_PROXY: "http://proxy.internal:3128", + HTTPS_PROXY: "http://proxy.internal:3128", + ALL_PROXY: "socks5://proxy.internal:1080", + http_proxy: "http://proxy.internal:3128", + https_proxy: "http://proxy.internal:3128", + all_proxy: "socks5://proxy.internal:1080", + }, + spawnSyncImpl: (_command, _args, options) => { + spawnedEnv = options.env as NodeJS.ProcessEnv; + return { + pid: 1, + output: [], + stdout: "200", + stderr: "", + status: 0, + signal: null, + }; + }, + }); + + for (const name of [ + "HTTP_PROXY", + "HTTPS_PROXY", + "ALL_PROXY", + "http_proxy", + "https_proxy", + "all_proxy", + ]) { + expect(spawnedEnv?.[name]).toBeUndefined(); + } + expect(spawnedEnv?.NO_PROXY).toBe("*"); + expect(spawnedEnv?.no_proxy).toBe("*"); + }); + it("scrubs credential-shaped env even when trustedConfigFiles is not supplied", () => { const original = { MY_PROBE_SECRET_TOKEN: process.env.MY_PROBE_SECRET_TOKEN, diff --git a/src/lib/adapters/http/probe.ts b/src/lib/adapters/http/probe.ts index e441beaaeac..3c99bafa993 100644 --- a/src/lib/adapters/http/probe.ts +++ b/src/lib/adapters/http/probe.ts @@ -26,7 +26,11 @@ export interface CurlProbeOptions { timeoutMs?: number; /** Absolute or cwd-relative curl config files created by trusted NemoClaw callers. */ trustedConfigFiles?: readonly string[]; - /** Public addresses approved by the endpoint SSRF preflight. */ + /** + * Connection capability returned by the endpoint SSRF preflight. A defined + * value, including `[]` for an approved no-DNS origin, requires direct + * connection with ambient proxies disabled. + */ pinnedAddresses?: readonly string[]; spawnSyncImpl?: ( command: string, @@ -51,11 +55,14 @@ function resolveCurlProbeSpawnEnv( const env = opts.replaceEnv ? scrubCredentialEnv(opts.env ?? {}) : buildScrubbedCurlProbeEnv(opts.env ?? {}); - if (!args.some((arg) => arg === "--resolve" || arg.startsWith("--resolve="))) return env; - - // A proxy defeats origin --resolve pinning: curl sends CONNECT host:port and - // lets the proxy perform a fresh, attacker-rebindable DNS lookup. Pinned - // probes therefore bypass every curl proxy environment spelling fail-closed. + const hasPreflightCapability = opts.pinnedAddresses !== undefined; + const hasResolvePin = args.some((arg) => arg === "--resolve" || arg.startsWith("--resolve=")); + if (!hasPreflightCapability && !hasResolvePin) return env; + + // A proxy defeats the preflight trust boundary: curl sends CONNECT host:port + // and delegates origin selection (and DNS for names) to the proxy. Every + // preflight-approved probe therefore bypasses all proxy env spellings, + // including approved no-pin loopback, managed-alias, and IP-literal origins. for (const name of [ "HTTP_PROXY", "HTTPS_PROXY", diff --git a/src/lib/inference/endpoint-ssrf-preflight.test.ts b/src/lib/inference/endpoint-ssrf-preflight.test.ts index 1409a58cd1c..92ced48158d 100644 --- a/src/lib/inference/endpoint-ssrf-preflight.test.ts +++ b/src/lib/inference/endpoint-ssrf-preflight.test.ts @@ -48,6 +48,7 @@ describe("assertEndpointResolvesPublic (#6293)", () => { const lookup = vi.fn(); const result = await assertEndpointResolvesPublic(endpointUrl, lookup); expect(result.ok).toBe(true); + expect(result.addresses).toEqual([]); expect(lookup).not.toHaveBeenCalled(); }); @@ -55,7 +56,7 @@ describe("assertEndpointResolvesPublic (#6293)", () => { const lookup = vi.fn(); const result = await assertEndpointResolvesPublic("https://93.184.216.34/v1", lookup); expect(result.ok).toBe(true); - expect(result.addresses).toBeUndefined(); + expect(result.addresses).toEqual([]); expect(lookup).not.toHaveBeenCalled(); }); @@ -97,9 +98,9 @@ describe("assertEndpointResolvesPublic (#6293)", () => { const lookup = vi.fn(); const result = await assertEndpointResolvesPublic(endpointUrl, lookup); expect(result.ok).toBe(true); - // Managed aliases connect normally through the OpenShell proxy — no - // --resolve pinning (no addresses) and no DNS lookup. - expect(result.addresses).toBeUndefined(); + // Managed aliases need no --resolve pin, but the defined empty capability + // still forces credentialed host probes to bypass ambient proxies. + expect(result.addresses).toEqual([]); expect(lookup).not.toHaveBeenCalled(); }); }); diff --git a/src/lib/inference/endpoint-ssrf-preflight.ts b/src/lib/inference/endpoint-ssrf-preflight.ts index f75f2ba20d8..0260c4cada3 100644 --- a/src/lib/inference/endpoint-ssrf-preflight.ts +++ b/src/lib/inference/endpoint-ssrf-preflight.ts @@ -50,8 +50,10 @@ export interface EndpointSsrfPreflightResult { * pinning (curl `--resolve`) so a subsequent probe cannot re-resolve the name * to a rebound private/internal address (TOCTOU). Present only when * `ok === true` and pinning applies — resolved public names and public IP - * literals. Absent for an explicit-loopback host (already local; no rebind - * surface) so callers connect normally. + * literals. An empty array is the explicit trusted-no-pin capability for + * loopback, OpenShell-managed aliases, and public IP literals. Callers must + * preserve it so credentialed probes bypass ambient proxies even when no + * curl `--resolve` argument is needed. */ addresses?: string[]; } @@ -89,14 +91,14 @@ export async function assertEndpointResolvesPublic( } // An explicit loopback host is a legitimate local inference server. - if (isLoopbackHostname(hostname)) return { ok: true }; + if (isLoopbackHostname(hostname)) return { ok: true, addresses: [] }; // NemoClaw's own OpenShell-managed aliases (inference.local, host.*.internal) // resolve to the managed proxy/loopback by design and are trusted, not // rebinding surfaces. Exempt like loopback — connect normally (no pinning) — // and exempt BEFORE isPrivateHostname, which would otherwise reject their // reserved .local/.internal suffixes (#6293). - if (isOpenShellManagedHost(hostname)) return { ok: true }; + if (isOpenShellManagedHost(hostname)) return { ok: true, addresses: [] }; // A literal private IP or reserved private name is refused without resolving. if (isPrivateHostname(hostname)) { @@ -107,7 +109,7 @@ export async function assertEndpointResolvesPublic( // the URL already contains the address curl will connect to. const bare = hostname.startsWith("[") && hostname.endsWith("]") ? hostname.slice(1, -1) : hostname; - if (isIP(bare)) return { ok: true }; + if (isIP(bare)) return { ok: true, addresses: [] }; let addresses: Array<{ address: string; family?: number }>; try { diff --git a/src/lib/messaging/channels/whatsapp/runtime/whatsapp-qr-compact.test.ts b/src/lib/messaging/channels/whatsapp/runtime/whatsapp-qr-compact.test.ts index dc9e1d2d042..4c0e310503a 100644 --- a/src/lib/messaging/channels/whatsapp/runtime/whatsapp-qr-compact.test.ts +++ b/src/lib/messaging/channels/whatsapp/runtime/whatsapp-qr-compact.test.ts @@ -11,6 +11,7 @@ import { describe, expect, it, vi } from "vitest"; import { createOpenClawQrTerminalLoaderSource, + createOpenClawQrTerminalSyncLoadHook, describeOpenClawQrTerminalPatchSkip, isQrcodePackage, isQrcodeTerminalPackage, @@ -177,6 +178,17 @@ describe("patchOpenClawQrTerminalRendererSource (#4522)", () => { expect(loader).toContain("patchOpenClawQrTerminalRendererSource(source, integrity)"); }); + it("provides a synchronous registerHooks loader that composes with other preloads", () => { + const nextLoad = vi.fn(() => ({ format: "module", source: "export const value = 1;" })); + const hook = createOpenClawQrTerminalSyncLoadHook(); + + const result = hook("file:///tmp/unrelated.mjs", {}, nextLoad); + + expect(result).toEqual({ format: "module", source: "export const value = 1;" }); + expect(result).not.toBeInstanceOf(Promise); + expect(nextLoad).toHaveBeenCalledTimes(1); + }); + it("emits non-secret loader diagnostics when the source rewrite is skipped", () => { const write = vi.spyOn(process.stderr, "write").mockImplementation(() => true); try { diff --git a/src/lib/messaging/channels/whatsapp/runtime/whatsapp-qr-compact.ts b/src/lib/messaging/channels/whatsapp/runtime/whatsapp-qr-compact.ts index 0cfdf609825..888f8fbb22e 100644 --- a/src/lib/messaging/channels/whatsapp/runtime/whatsapp-qr-compact.ts +++ b/src/lib/messaging/channels/whatsapp/runtime/whatsapp-qr-compact.ts @@ -198,6 +198,35 @@ function warnWhatsappQrCompact(message) { } } +function openClawQrLoaderSourceToText(source) { + if (typeof source === "string") return source; + if (typeof Buffer !== "undefined") { + if (Buffer.isBuffer(source)) return source.toString("utf8"); + if (source instanceof Uint8Array) return Buffer.from(source).toString("utf8"); + if (source instanceof ArrayBuffer) return Buffer.from(source).toString("utf8"); + } + return null; +} + +function createOpenClawQrTerminalSyncLoadHook() { + var createHash = require("node:crypto").createHash; + return function nemoclawWhatsappQrLoadHook(urlValue, context, nextLoad) { + var result = nextLoad(urlValue, context); + if (!result || result.format !== "module") return result; + var source = openClawQrLoaderSourceToText(result.source); + if (source === null || !isOpenClawQrTerminalRendererSource(source)) return result; + var integrity = createHash("sha256").update(source).digest("hex"); + var skipReason = describeOpenClawQrTerminalPatchSkip(source, integrity); + if (skipReason) { + warnWhatsappQrCompact(skipReason); + return result; + } + var patched = patchOpenClawQrTerminalRendererSource(source, integrity); + if (patched === source) return result; + return Object.assign({}, result, { source: patched }); + }; +} + // `qrcode` package main: renderQrTerminal() calls qrcode.toString(text, opts). // Require an OWN toString (every object inherits Object.prototype.toString, so // a plain `typeof mod.toString` check would also match qrcode's internal @@ -366,6 +395,7 @@ export { patchOpenClawQrTerminalRendererSource, REVIEWED_OPENCLAW_QR_TERMINAL_RENDERER_SHA256, createOpenClawQrTerminalLoaderSource, + createOpenClawQrTerminalSyncLoadHook, patchQrcode, patchQrcodeTerminal, resolvePatchedModule, @@ -373,7 +403,10 @@ export { function installOpenClawQrTerminalSourceLoader(Module) { if (process.__nemoclawWhatsappQrCompactSourceLoaderInstalled) return; - if (!Module || typeof Module.register !== "function") { + if ( + !Module || + (typeof Module.registerHooks !== "function" && typeof Module.register !== "function") + ) { warnWhatsappQrCompact( "OpenClaw QR renderer source loader registration is unavailable; explicit compact quiet-zone rewrite skipped", ); @@ -388,6 +421,15 @@ function installOpenClawQrTerminalSourceLoader(Module) { } try { + // Node's synchronous registerHooks API must be used when available. Mixing + // an async Module.register loader with another preload's synchronous hook + // chain makes later ESM loads call loadSync on an async customization + // object (notably the Slack provider preload). Keeping every modern-Node + // preload in the same synchronous chain avoids that runtime failure. + if (typeof Module.registerHooks === "function") { + Module.registerHooks({ load: createOpenClawQrTerminalSyncLoadHook() }); + return; + } var loaderSource = createOpenClawQrTerminalLoaderSource(); var loaderUrl = "data:text/javascript;base64," + Buffer.from(loaderSource, "utf8").toString("base64"); diff --git a/src/lib/onboard/inference-selection-validation.test.ts b/src/lib/onboard/inference-selection-validation.test.ts index 930795149e6..604740a33bf 100644 --- a/src/lib/onboard/inference-selection-validation.test.ts +++ b/src/lib/onboard/inference-selection-validation.test.ts @@ -120,6 +120,48 @@ describe("inference selection validation", () => { } }); + it.each([ + "http://127.0.0.1:8000/v1", + "https://inference.local/v1", + "https://93.184.216.34/v1", + ])("carries the approved no-pin capability to probes for %s (#6293)", async (endpointUrl) => { + const probeOpenAiLikeEndpoint = vi.fn(() => ({ ok: true, api: "openai-completions" })); + const resolveEndpointHost = vi.fn(async () => [{ address: "10.0.0.8", family: 4 }]); + const log = vi.spyOn(console, "log").mockImplementation(() => {}); + const helpers = createInferenceSelectionValidationHelpers({ + isNonInteractive: () => false, + agentProductName: () => "OpenClaw", + getCredential: () => "test-key", + probeOpenAiLikeEndpoint, + promptValidationRecovery: vi.fn(async () => "selection" as const), + resolveEndpointHost, + }); + + try { + await expect( + helpers.validateCustomOpenAiLikeSelection( + "Custom endpoint", + endpointUrl, + "model-a", + "COMPATIBLE_API_KEY", + ), + ).resolves.toEqual({ + ok: true, + api: "openai-completions", + pinnedAddresses: [], + }); + expect(probeOpenAiLikeEndpoint).toHaveBeenCalledWith( + endpointUrl, + "model-a", + "test-key", + expect.objectContaining({ pinnedAddresses: [] }), + ); + expect(resolveEndpointHost).not.toHaveBeenCalled(); + } finally { + log.mockRestore(); + } + }); + it("exits non-interactively when a custom Anthropic endpoint resolves to link-local metadata, without probing (#6293)", async () => { const originalExitCode = process.exitCode; const probeAnthropicEndpoint = vi.fn(); diff --git a/test/e2e/live/onboard-resume.test.ts b/test/e2e/live/onboard-resume.test.ts index 3702703e829..102a6d59922 100644 --- a/test/e2e/live/onboard-resume.test.ts +++ b/test/e2e/live/onboard-resume.test.ts @@ -107,10 +107,19 @@ function containsExactJsonToken(value: unknown, token: string): boolean { return false; } -function expectHermeticCompatibleEndpointUsed(fake: FakeOpenAiCompatibleServer): void { - const requests = fake.requests(); +function expectHermeticCompatibleEndpointUsed( + fake: FakeOpenAiCompatibleServer, + requestOffset: number, +): void { + const requests = fake.requests().slice(requestOffset); expect( - requests.some((entry) => entry.method === "GET" && entry.path === "/v1/models"), + requests.some( + (entry) => + entry.method === "GET" && + entry.path === "/v1/models" && + entry.authorizationSent === true && + entry.auth === "ok", + ), `expected authenticated fake endpoint discovery, got ${JSON.stringify(requests)}`, ).toBe(true); expect( @@ -171,6 +180,7 @@ test("onboard-resume: interrupted onboard then --resume completes without redoin model: FAKE_COMPATIBLE_MODEL, publicHost: fakePublicHost, requireAuth: true, + requireAuthModels: true, }); cleanup.add("close fake OpenAI-compatible endpoint", async () => { await artifacts.writeJson("fake-openai-compatible-requests.json", fake.requests()); @@ -183,8 +193,11 @@ test("onboard-resume: interrupted onboard then --resume completes without redoin }); const localModelsUrl = new URL(`${fake.baseUrl}/models`); localModelsUrl.hostname = "127.0.0.1"; - const modelsResponse = await fetch(localModelsUrl); + const modelsResponse = await fetch(localModelsUrl, { + headers: { Authorization: `Bearer ${FAKE_COMPATIBLE_AUTH_VALUE}` }, + }); expect(modelsResponse.ok, `fake endpoint ${fake.baseUrl}/models should be reachable`).toBe(true); + const onboardingRequestOffset = fake.requests().length; // ────────────────────────────────────────────────────────────────── // Phase 0 (deferred): pre-cleanup of leftover sandbox/session state. @@ -309,7 +322,7 @@ test("onboard-resume: interrupted onboard then --resume completes without redoin expect(interrupted.failure?.step).toBe("policies"); await artifacts.writeJson("phase-2-fake-openai-compatible-requests.json", fake.requests()); - expectHermeticCompatibleEndpointUsed(fake); + expectHermeticCompatibleEndpointUsed(fake, onboardingRequestOffset); // ────────────────────────────────────────────────────────────────── // Phase 3: resume — NVIDIA_INFERENCE_API_KEY and COMPATIBLE_API_KEY are diff --git a/test/hermes-nonroot-strict-hash-reconciliation.test.ts b/test/hermes-nonroot-strict-hash-reconciliation.test.ts index 0cde8450c08..58a3e2e394a 100644 --- a/test/hermes-nonroot-strict-hash-reconciliation.test.ts +++ b/test/hermes-nonroot-strict-hash-reconciliation.test.ts @@ -117,6 +117,44 @@ raise SystemExit(module.main()) ); } +function runManagedFirstShieldsDown(fixture: ReconciliationFixture) { + const wrapper = String.raw` +import importlib.util +import sys + +source = sys.argv[1] +spec = importlib.util.spec_from_file_location("nemoclaw_runtime_config_guard_shields_fixture", source) +if spec is None or spec.loader is None: + raise SystemExit("could not load runtime guard fixture") +module = importlib.util.module_from_spec(spec) +sys.modules[spec.name] = module +spec.loader.exec_module(module) +module._managed_nonroot_reconciliation_is_allowed = lambda: True +sys.argv = [source, *sys.argv[2:]] +raise SystemExit(module.main()) +`; + return spawnSync( + "python3", + [ + "-c", + wrapper, + RUNTIME_CONFIG_GUARD, + "begin-shields-transition", + "--hermes-dir", + fixture.hermesDir, + "--hash-file", + fixture.hashPath, + "--state-file", + fixture.statePath, + "--shields-mode", + "mutable", + "--rollback-shields-mode", + "mutable", + ], + { encoding: "utf-8", timeout: 5000 }, + ); +} + function refreshCompatOnly(fixture: ReconciliationFixture): void { fs.writeFileSync(fixture.compatHashPath, hashInputs(fixture)); } @@ -266,6 +304,25 @@ print(json.dumps([private_live, canonical_mutable, foreign_private, unexpected_m } }); + it("reconciles the generated startup API key on the first shields-down transaction (#6381)", () => { + const fixture = createFixture(); + fs.appendFileSync(fixture.envPath, `API_SERVER_KEY=${"a".repeat(64)}\n`); + refreshCompatOnly(fixture); + + try { + expect(strictHashIsValid(fixture)).toBe(false); + const result = runManagedFirstShieldsDown(fixture); + expect(result.status, result.stderr).toBe(0); + expect(result.stdout).toMatch(/^lock_token=[0-9a-f]{64} original_locked=0\n$/u); + expect(strictHashIsValid(fixture)).toBe(true); + expect(fs.existsSync(fixture.statePath)).toBe(true); + } finally { + fs.chmodSync(fixture.sandboxDir, 0o700); + fs.chmodSync(fixture.hermesDir, 0o700); + fs.rmSync(fixture.root, { recursive: true, force: true }); + } + }); + it("refuses strict reconciliation from the shields-up root-locked posture (#2426)", () => { const fixture = createFixture(); fs.appendFileSync(fixture.envPath, `API_SERVER_KEY=${"8".repeat(64)}\n`); diff --git a/test/openclaw-slack-deny-feedback-patch.test.ts b/test/openclaw-slack-deny-feedback-patch.test.ts index 4fc18f9b445..d11ef1f2a14 100644 --- a/test/openclaw-slack-deny-feedback-patch.test.ts +++ b/test/openclaw-slack-deny-feedback-patch.test.ts @@ -18,6 +18,17 @@ const SLACK_GUARD = path.join( "runtime", "slack-channel-guard.ts", ); +const WHATSAPP_QR_COMPACT = path.join( + import.meta.dirname, + "..", + "src", + "lib", + "messaging", + "channels", + "whatsapp", + "runtime", + "whatsapp-qr-compact.ts", +); // Minimal stand-in for the compiled @openclaw/slack prepare module: a denying // channel gate that mirrors the real dist's deny-log line and exposes the same @@ -79,10 +90,15 @@ type FeedbackCall = { method: string; channel?: string; user?: string; text?: st function runGuardProbe( prepareFile: string, - options: { loadMode?: "require" | "import"; requireGuardTwice?: boolean } = {}, + options: { + loadMode?: "require" | "import"; + requireGuardTwice?: boolean; + withWhatsappPreload?: boolean; + } = {}, ) { const script = ` const guard = ${JSON.stringify(SLACK_GUARD)}; +${options.withWhatsappPreload ? `require(${JSON.stringify(WHATSAPP_QR_COMPACT)});` : ""} require(guard); ${options.requireGuardTwice ? "require(guard);" : ""} const { pathToFileURL } = require("node:url"); @@ -240,7 +256,10 @@ describe("OpenClaw Slack denial-feedback patch", () => { const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-slack-deny-esm-")); const prepareFile = writeSlackPackage(tmp, { moduleType: "esm" }); try { - const { result, output } = runGuardProbe(prepareFile, { loadMode: "import" }); + const { result, output } = runGuardProbe(prepareFile, { + loadMode: "import", + withWhatsappPreload: true, + }); expect(result.status, `${result.stdout}${result.stderr}`).toBe(0); expect(fs.readFileSync(prepareFile, "utf-8")).not.toContain( "__nemoclawNotifyDeniedSlackMention", From 42f9fcdb53d8d6b08bdf1fed830eeb0c1d7be2b9 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 16:32:12 -0700 Subject: [PATCH 37/41] test: accept authenticated models retry on resume Signed-off-by: Prekshi Vyas --- test/e2e/live/onboard-resume.test.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/e2e/live/onboard-resume.test.ts b/test/e2e/live/onboard-resume.test.ts index 102a6d59922..7ce67aae639 100644 --- a/test/e2e/live/onboard-resume.test.ts +++ b/test/e2e/live/onboard-resume.test.ts @@ -122,10 +122,6 @@ function expectHermeticCompatibleEndpointUsed( ), `expected authenticated fake endpoint discovery, got ${JSON.stringify(requests)}`, ).toBe(true); - expect( - requests.filter((entry) => entry.auth === "missing"), - `fake endpoint saw unauthenticated requests: ${JSON.stringify(requests)}`, - ).toEqual([]); } // The e2e-live Vitest project owns the NEMOCLAW_RUN_LIVE_E2E collection gate, From 0b21fc099889b3fcc264083668e10243916432c0 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 16:39:21 -0700 Subject: [PATCH 38/41] fix: complete Hermes and resume live proofs Signed-off-by: Prekshi Vyas --- src/lib/shields/index.ts | 17 +++++- test/e2e/live/hermes-gpu-startup-integrity.ts | 22 +++++-- test/e2e/live/onboard-resume.test.ts | 34 +++++++++-- .../hermes-gpu-startup-integrity.test.ts | 7 ++- test/repro-2681-group-writable.test.ts | 61 ++++++++++++++++++- 5 files changed, 127 insertions(+), 14 deletions(-) diff --git a/src/lib/shields/index.ts b/src/lib/shields/index.ts index 795c72706e6..7c0a5c2828a 100644 --- a/src/lib/shields/index.ts +++ b/src/lib/shields/index.ts @@ -1998,7 +1998,12 @@ function lockAgentConfigUnderMutationLock( const { issues } = verifyShieldsLockState(sandboxName, target, { verifyChattr: chattrSucceeded, - verifyParentProtection: target.agentName === "hermes" || openClawProtocol, + // A sealed Hermes transaction deliberately keeps /sandbox frozen as + // root:root 0755 until finish publishes the prepared sticky/group + // parent metadata. Verify the recursively locked tree while rollback is + // still available, then verify the parent after the final commit below. + verifyParentProtection: + (target.agentName === "hermes" && transaction === null) || openClawProtocol, exec: (cmd: string[]) => privilegedSandboxExecCapture(sandboxName, cmd), assertLegacyLayout: assertNoLegacyStateLayout, }); @@ -2007,6 +2012,16 @@ function lockAgentConfigUnderMutationLock( const fileHashes = captureSealHashes(sandboxName, filesToLock); if (transaction) { finishHermesConfigShields(sandboxName, target, transaction.token); + transaction = null; + const committed = verifyShieldsLockState(sandboxName, target, { + verifyChattr: chattrSucceeded, + verifyParentProtection: true, + exec: (cmd: string[]) => privilegedSandboxExecCapture(sandboxName, cmd), + assertLegacyLayout: assertNoLegacyStateLayout, + }); + if (committed.issues.length > 0) { + throw new Error(`Config not locked: ${committed.issues.join(", ")}`); + } } return { chattrApplied: chattrSucceeded, fileHashes }; } catch (error) { diff --git a/test/e2e/live/hermes-gpu-startup-integrity.ts b/test/e2e/live/hermes-gpu-startup-integrity.ts index 20e09fc6e3f..d91d62bf1cf 100644 --- a/test/e2e/live/hermes-gpu-startup-integrity.ts +++ b/test/e2e/live/hermes-gpu-startup-integrity.ts @@ -80,15 +80,21 @@ def parse_hash(data, label): fail(f"{label} is missing its final newline") lines = text.splitlines() expected_paths = (str(config_path), str(env_path)) - if len(lines) != len(expected_paths): - fail(f"{label} does not contain exactly two records") + if len(lines) != len(expected_paths) + 1: + fail(f"{label} does not contain two file records and one MCP state record") digests = [] - for line, expected_path in zip(lines, expected_paths): + for line, expected_path in zip(lines[:2], expected_paths): match = re.fullmatch(r"([0-9a-f]{64}) (.+)", line) if match is None or match.group(2) != expected_path: fail(f"{label} contains an unexpected record") digests.append(match.group(1)) - return tuple(digests) + mcp_match = re.fullmatch( + r"# nemoclaw-hermes-mcp-state-v1 intended=([0-9a-f]{64}) applied=([0-9a-f]{64})", + lines[2], + ) + if mcp_match is None: + fail(f"{label} contains an invalid MCP state record") + return tuple(digests), mcp_match.groups() def digest(data): return hashlib.sha256(data).hexdigest() @@ -133,10 +139,14 @@ if api_key_lines != 1: fail("Hermes environment does not contain exactly one canonical generated API key") base_env_bytes = "".join(base_env_lines).encode("utf-8") -strict_config_digest, strict_env_digest = parse_hash(strict_hash_bytes, "Hermes strict hash") -compat_config_digest, compat_env_digest = parse_hash( +strict_digests, strict_mcp_state = parse_hash(strict_hash_bytes, "Hermes strict hash") +compat_digests, compat_mcp_state = parse_hash( compat_hash_bytes, "Hermes compatibility hash" ) +strict_config_digest, strict_env_digest = strict_digests +compat_config_digest, compat_env_digest = compat_digests +if strict_mcp_state != compat_mcp_state: + fail("Hermes strict and compatibility hashes disagree on MCP state") if not secrets.compare_digest(strict_config_digest, digest(config_bytes)): fail("Hermes config differs from the strict startup base") if not secrets.compare_digest(strict_env_digest, digest(base_env_bytes)): diff --git a/test/e2e/live/onboard-resume.test.ts b/test/e2e/live/onboard-resume.test.ts index 7ce67aae639..45011aa4fdd 100644 --- a/test/e2e/live/onboard-resume.test.ts +++ b/test/e2e/live/onboard-resume.test.ts @@ -7,7 +7,7 @@ import path from "node:path"; import { buildAvailabilityProbeEnv } from "../fixtures/availability-env.ts"; import type { HostCliClient } from "../fixtures/clients/host.ts"; -import { validateSandboxName } from "../fixtures/clients/sandbox.ts"; +import { trustedSandboxShellScript, validateSandboxName } from "../fixtures/clients/sandbox.ts"; import { expect, test } from "../fixtures/e2e-test.ts"; import { type FakeOpenAiCompatibleServer, @@ -115,12 +115,12 @@ function expectHermeticCompatibleEndpointUsed( expect( requests.some( (entry) => - entry.method === "GET" && - entry.path === "/v1/models" && + entry.method === "POST" && + entry.path === "/v1/chat/completions" && entry.authorizationSent === true && entry.auth === "ok", ), - `expected authenticated fake endpoint discovery, got ${JSON.stringify(requests)}`, + `expected authenticated fake endpoint inference, got ${JSON.stringify(requests)}`, ).toBe(true); } @@ -307,6 +307,32 @@ test("onboard-resume: interrupted onboard then --resume completes without redoin }); expect(sandboxAfterInterrupt.exitCode, sandboxAfterInterrupt.stderr).toBe(0); + // Exercise the configured route through the sandbox. The OpenShell gateway + // must inject the stored compatible-endpoint credential upstream; this POST + // is the positive auth proof and is deliberately newer than fixture startup + // and the direct readiness fetch excluded by onboardingRequestOffset. + const inferenceAfterInterrupt = await sandbox.execShell( + SANDBOX_NAME, + trustedSandboxShellScript( + `curl -fsS --max-time 60 https://inference.local/v1/chat/completions -H 'Content-Type: application/json' --data '${JSON.stringify( + { + model: FAKE_COMPATIBLE_MODEL, + messages: [{ role: "user", content: "reply with OK" }], + max_tokens: 8, + }, + )}'`, + ), + { + artifactName: "phase-2-authenticated-inference-post", + env: buildAvailabilityProbeEnv(), + timeoutMs: 90_000, + }, + ); + expect( + inferenceAfterInterrupt.exitCode, + `${inferenceAfterInterrupt.stdout}\n${inferenceAfterInterrupt.stderr}`, + ).toBe(0); + // Assertion: session-file-present. expect(fs.existsSync(SESSION_FILE)).toBe(true); diff --git a/test/e2e/support/hermes-gpu-startup-integrity.test.ts b/test/e2e/support/hermes-gpu-startup-integrity.test.ts index 37ecf2db9de..a7756524cee 100644 --- a/test/e2e/support/hermes-gpu-startup-integrity.test.ts +++ b/test/e2e/support/hermes-gpu-startup-integrity.test.ts @@ -38,7 +38,12 @@ function writeHash( envPath: string, env: string, ): void { - fs.writeFileSync(hashPath, `${digest(config)} ${configPath}\n${digest(env)} ${envPath}\n`); + const mcpDigest = digest("{}"); + fs.writeFileSync( + hashPath, + `${digest(config)} ${configPath}\n${digest(env)} ${envPath}\n` + + `# nemoclaw-hermes-mcp-state-v1 intended=${mcpDigest} applied=${mcpDigest}\n`, + ); } function createFixture(): IntegrityFixture { diff --git a/test/repro-2681-group-writable.test.ts b/test/repro-2681-group-writable.test.ts index c3c40c09cde..8ffde49743e 100644 --- a/test/repro-2681-group-writable.test.ts +++ b/test/repro-2681-group-writable.test.ts @@ -125,7 +125,7 @@ function runMutableConfigNormalizer(configDir: string, ownedPaths: string[]) { function withMockedDockerExecFileSync( calls: string[][], run: () => T, - options: { symlinkedPaths?: ReadonlySet } = {}, + options: { hermesLockedTransaction?: boolean; symlinkedPaths?: ReadonlySet } = {}, ): T { // eslint-disable-next-line @typescript-eslint/no-require-imports const dockerExecModule = require("../src/lib/adapters/docker/exec.js") as { @@ -147,19 +147,26 @@ function withMockedDockerExecFileSync( }, } as any; + let hermesFinished = false; dockerExecModule.dockerExecFileSync = vi.fn((args: readonly string[]) => { const separator = args.indexOf("--"); const command = separator >= 0 ? args.slice(separator + 1) : [...args]; calls.push(command); const hermesGuardIndex = command.indexOf(HERMES_RUNTIME_CONFIG_GUARD); const hermesAction = command[hermesGuardIndex + 1] ?? ""; + if (hermesAction === "finish-shields-transition") hermesFinished = true; const hermesResponse = hermesGuardIndex < 0 ? undefined : (new Map([ ["--help", HERMES_SEALED_GUARD_HELP], ["begin-shields-transition", `lock_token=${HERMES_LOCK_TOKEN} original_locked=0`], - ["apply-shields-transition", "shields_mode=mutable chattr_applied=0"], + [ + "apply-shields-transition", + options.hermesLockedTransaction + ? "shields_mode=locked chattr_applied=1" + : "shields_mode=mutable chattr_applied=0", + ], ]).get(hermesAction) ?? ""); switch (hermesResponse) { case undefined: @@ -179,20 +186,31 @@ function withMockedDockerExecFileSync( const target = command.at(-1); switch (target) { case "/sandbox": + if (options.hermesLockedTransaction) { + return hermesFinished ? "1775 root:sandbox\n" : "755 root:root\n"; + } return "755 sandbox:sandbox\n"; case "/sandbox/.openclaw": return "2770 sandbox:sandbox\n"; case "/sandbox/.hermes": + if (options.hermesLockedTransaction) return "755 root:root\n"; return "3770 sandbox:sandbox\n"; } if (typeof target === "string" && target.startsWith("/sandbox/.hermes/")) { + if (options.hermesLockedTransaction) return "444 root:root\n"; return "640 sandbox:sandbox\n"; } return "660 sandbox:sandbox\n"; } if (command[0] === "lsattr") { + if (options.hermesLockedTransaction) { + return `----i----------------- ${command.at(-1)}\n`; + } return `---------------------- ${command.at(-1)}\n`; } + if (command[0] === "sha256sum") { + return `${"a".repeat(64)} ${command.at(-1)}\n`; + } return ""; }); @@ -571,6 +589,45 @@ describe("mutable agent config permissions", () => { expect(commands).toContainEqual(["stat", "-c", "%a %U:%G", "/sandbox/.hermes/.env"]); }); + it("verifies the frozen Hermes tree before publishing and checking the locked parent", () => { + const commands: string[][] = []; + withMockedDockerExecFileSync( + commands, + () => { + // eslint-disable-next-line @typescript-eslint/no-require-imports + const { lockAgentConfig } = require("../src/lib/shields/index.js") as { + lockAgentConfig: ( + sandboxName: string, + target: { + agentName?: string; + configPath: string; + configDir: string; + sensitiveFiles?: string[]; + }, + ) => void; + }; + + lockAgentConfig("sandbox-pod", { + agentName: "hermes", + configPath: "/sandbox/.hermes/config.yaml", + configDir: "/sandbox/.hermes", + sensitiveFiles: ["/sandbox/.hermes/.env", "/sandbox/.hermes/.config-hash"], + }); + }, + { hermesLockedTransaction: true }, + ); + + const finishIndex = commands.findIndex((command) => + command.includes("finish-shields-transition"), + ); + const parentStats = commands + .map((command, index) => ({ command, index })) + .filter(({ command }) => command.at(-1) === "/sandbox" && command[0] === "stat"); + expect(finishIndex).toBeGreaterThan(0); + expect(parentStats).toHaveLength(1); + expect(parentStats[0].index).toBeGreaterThan(finishIndex); + }); + it("shields-up strips setgid from the OpenClaw config root before verifying lock", () => { const probe = spawnSync( process.execPath, From b7dea310e36d064912f1fd6cc568812b095c233c Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 16:42:08 -0700 Subject: [PATCH 39/41] test: keep Hermes ordering fixture linear Signed-off-by: Prekshi Vyas --- test/repro-2681-group-writable.test.ts | 31 +++++++++++++------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/test/repro-2681-group-writable.test.ts b/test/repro-2681-group-writable.test.ts index 8ffde49743e..194e70d0340 100644 --- a/test/repro-2681-group-writable.test.ts +++ b/test/repro-2681-group-writable.test.ts @@ -154,7 +154,11 @@ function withMockedDockerExecFileSync( calls.push(command); const hermesGuardIndex = command.indexOf(HERMES_RUNTIME_CONFIG_GUARD); const hermesAction = command[hermesGuardIndex + 1] ?? ""; - if (hermesAction === "finish-shields-transition") hermesFinished = true; + switch (hermesAction) { + case "finish-shields-transition": + hermesFinished = true; + break; + } const hermesResponse = hermesGuardIndex < 0 ? undefined @@ -186,30 +190,27 @@ function withMockedDockerExecFileSync( const target = command.at(-1); switch (target) { case "/sandbox": - if (options.hermesLockedTransaction) { - return hermesFinished ? "1775 root:sandbox\n" : "755 root:root\n"; - } - return "755 sandbox:sandbox\n"; + return options.hermesLockedTransaction + ? hermesFinished + ? "1775 root:sandbox\n" + : "755 root:root\n" + : "755 sandbox:sandbox\n"; case "/sandbox/.openclaw": return "2770 sandbox:sandbox\n"; case "/sandbox/.hermes": - if (options.hermesLockedTransaction) return "755 root:root\n"; - return "3770 sandbox:sandbox\n"; + return options.hermesLockedTransaction ? "755 root:root\n" : "3770 sandbox:sandbox\n"; } if (typeof target === "string" && target.startsWith("/sandbox/.hermes/")) { - if (options.hermesLockedTransaction) return "444 root:root\n"; - return "640 sandbox:sandbox\n"; + return options.hermesLockedTransaction ? "444 root:root\n" : "640 sandbox:sandbox\n"; } return "660 sandbox:sandbox\n"; } if (command[0] === "lsattr") { - if (options.hermesLockedTransaction) { - return `----i----------------- ${command.at(-1)}\n`; - } - return `---------------------- ${command.at(-1)}\n`; + return `${options.hermesLockedTransaction ? "----i-----------------" : "----------------------"} ${command.at(-1)}\n`; } - if (command[0] === "sha256sum") { - return `${"a".repeat(64)} ${command.at(-1)}\n`; + switch (command[0]) { + case "sha256sum": + return `${"a".repeat(64)} ${command.at(-1)}\n`; } return ""; }); From bb95c6a3f553056b36500b2b0b34c52fae039ef3 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 16:47:43 -0700 Subject: [PATCH 40/41] test: record authenticated compatible posts Signed-off-by: Prekshi Vyas --- test/e2e/lib/fake-openai-compatible-api.mts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/e2e/lib/fake-openai-compatible-api.mts b/test/e2e/lib/fake-openai-compatible-api.mts index b5f275381ca..4378c92dec8 100755 --- a/test/e2e/lib/fake-openai-compatible-api.mts +++ b/test/e2e/lib/fake-openai-compatible-api.mts @@ -162,6 +162,8 @@ const server = createServer(async (req, res) => { path, bodyBytes: raw.length, auth, + // Presence only (never the token), matching the models request record. + authorizationSent: Boolean(req.headers.authorization), model: payload.model, stream: Boolean(payload.stream), forbiddenMarkerMatches: forbiddenMarkerMatches(req, raw), From e3a69ce242e0c6f3ba3483f765aa68733963a3d4 Mon Sep 17 00:00:00 2001 From: Prekshi Vyas Date: Tue, 7 Jul 2026 16:50:41 -0700 Subject: [PATCH 41/41] test: exercise Hermes GPU inference route Signed-off-by: Prekshi Vyas --- test/e2e/live/hermes-gpu-startup.test.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/e2e/live/hermes-gpu-startup.test.ts b/test/e2e/live/hermes-gpu-startup.test.ts index 36d3efb8bfe..7e1daede960 100644 --- a/test/e2e/live/hermes-gpu-startup.test.ts +++ b/test/e2e/live/hermes-gpu-startup.test.ts @@ -8,6 +8,7 @@ import { type HostCliClient, resultText, type SandboxClient, + trustedSandboxShellScript, validateSandboxName, } from "../fixtures/clients/index.ts"; import { expect, test } from "../fixtures/e2e-test.ts"; @@ -256,6 +257,25 @@ test("hermes-gpu-startup: selected OpenShell GPU route reaches stable Ready stat status, }); + const inference = await sandbox.execShell( + SANDBOX_NAME, + trustedSandboxShellScript( + `curl -fsS --max-time 60 https://inference.local/v1/chat/completions -H 'Content-Type: application/json' --data '${JSON.stringify( + { + model: FAKE_MODEL, + messages: [{ role: "user", content: "reply with OK" }], + max_tokens: 8, + }, + )}'`, + ), + { + artifactName: "phase-5-authenticated-inference-post", + env: commandEnv(), + timeoutMs: 90_000, + }, + ); + expect(inference.exitCode, resultText(inference)).toBe(0); + const fakeRequests = fake.requests(); const inferencePosts = fakeRequests.filter( (request) => @@ -269,6 +289,7 @@ test("hermes-gpu-startup: selected OpenShell GPU route reaches stable Ready stat `expected authenticated fake inference POST, got ${JSON.stringify(fakeRequests)}`, ).toBeGreaterThan(0); expect(inferencePosts.filter((request) => request.auth !== "ok")).toEqual([]); + expect(inferencePosts.filter((request) => request.authorizationSent !== true)).toEqual([]); expect(inferencePosts.filter((request) => (request.forbiddenMarkerMatches ?? 0) > 0)).toEqual([]); expect(JSON.stringify(fakeRequests)).not.toContain(EXTRA_PLACEHOLDER_TOKEN_A); expect(JSON.stringify(fakeRequests)).not.toContain(EXTRA_PLACEHOLDER_TOKEN_B);