Repository navigation
fix(security): validate Hermes endpoint URL and reject allowed_ips in user presets #6087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
17c3c0e
fix(security): validate Hermes endpoint URL and reject allowed_ips in…
prekshivyas fa8448d
test(security): remove if statement from hermes SSRF guard test mock
prekshivyas 1ae6d21
test(security): use real isPrivateHostname in hermes SSRF guard test
prekshivyas b2efd05
Merge branch 'main' into fix/ssrf-validation-gaps-6072-6073-v2
prekshivyas 3f85340
fix(security): use Object.hasOwn for allowed_ips check in preset guard
prekshivyas 5d9cfaa
test(e2e): add policy-add --from-file allowed_ips rejection E2E (#6073)
prekshivyas bc34519
fix(security): extend Hermes endpoint guards and close applyPresetCon…
prekshivyas 4f20e36
fix(security): pin DNS in Hermes endpoint validation and close policy…
prekshivyas 8505c1f
fix(security): exempt host.openshell.internal from allowed_ips guard;…
prekshivyas 378534b
Potential fix for pull request finding 'CodeQL / Unused variable, imp…
prekshivyas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| // 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 { setupHermesProviderInference } from "./hermes"; | ||
|
|
||
| vi.mock("../../private-networks", () => ({ | ||
| isPrivateHostname: (hostname: string) => { | ||
| const privateHosts = new Set(["localhost", "host.docker.internal"]); | ||
| const privatePatterns = [ | ||
| /^127\./, | ||
| /^10\./, | ||
| /^192\.168\./, | ||
| /^172\.(1[6-9]|2\d|3[01])\./, | ||
| /^169\.254\./, | ||
| ]; | ||
| if (privateHosts.has(hostname)) return true; | ||
| if (hostname.endsWith(".internal") || hostname.endsWith(".local")) return true; | ||
| return privatePatterns.some((re) => re.test(hostname)); | ||
| }, | ||
| })); | ||
|
|
||
| function makeDeps(overrides: Record<string, unknown> = {}) { | ||
| return { | ||
| runOpenshell: vi.fn(() => ({ status: 0, stdout: "", stderr: "" })), | ||
| upsertProvider: vi.fn(), | ||
| verifyInferenceRoute: vi.fn(), | ||
| verifyOnboardInferenceSmoke: vi.fn(), | ||
| isNonInteractive: vi.fn(() => false), | ||
| registry: { updateSandbox: vi.fn() }, | ||
| hermesProviderAuth: { | ||
| isHermesProviderRegistered: vi.fn(() => true), | ||
| ensureHermesProviderApiKeyCredentials: vi.fn(() => ({})), | ||
| ensureHermesProviderOAuthCredentials: vi.fn(() => ({})), | ||
| }, | ||
| getHermesToolGatewayBroker: vi.fn(() => ({ | ||
| getHermesToolGatewayProviderName: vi.fn(() => "hermes-tool-gateway"), | ||
| })), | ||
| providerExistsInGateway: vi.fn(() => true), | ||
| normalizeHermesAuthMethod: vi.fn(() => "api-key"), | ||
| resolveHermesNousApiKey: vi.fn(() => null), | ||
| checkHermesProviderStoreReachable: vi.fn(() => ({ ok: true })), | ||
| hermesAuthMethodLabel: vi.fn((m: string) => m), | ||
| hermesConstants: { | ||
| HERMES_NOUS_API_KEY_CREDENTIAL_ENV: "NOUS_API_KEY", | ||
| HERMES_AUTH_METHOD_API_KEY: "api-key", | ||
| HERMES_AUTH_METHOD_OAUTH: "oauth", | ||
| }, | ||
| requireValue: vi.fn((v: unknown, _msg: string) => v), | ||
| redact: vi.fn((s: string) => s), | ||
| compactText: vi.fn((s: string) => s), | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe("setupHermesProviderInference SSRF guard (#6072)", () => { | ||
| it("rejects loopback address", async () => { | ||
| await expect( | ||
| setupHermesProviderInference( | ||
| { | ||
| sandboxName: "alpha", | ||
| model: "m", | ||
| provider: "p", | ||
| endpointUrl: "http://127.0.0.1:8080/v1", | ||
| credentialEnv: null, | ||
| hermesAuthMethod: null, | ||
| hermesToolGateways: [], | ||
| }, | ||
| makeDeps() as never, | ||
| ), | ||
| ).rejects.toThrow(/private or internal/); | ||
| }); | ||
|
|
||
| it("rejects cloud metadata endpoint", async () => { | ||
| await expect( | ||
| setupHermesProviderInference( | ||
| { | ||
| sandboxName: "alpha", | ||
| model: "m", | ||
| provider: "p", | ||
| endpointUrl: "http://169.254.169.254/latest/meta-data/", | ||
| credentialEnv: null, | ||
| hermesAuthMethod: null, | ||
| hermesToolGateways: [], | ||
| }, | ||
| makeDeps() as never, | ||
| ), | ||
| ).rejects.toThrow(/private or internal/); | ||
| }); | ||
|
|
||
| it("rejects private RFC-1918 range", async () => { | ||
| await expect( | ||
| setupHermesProviderInference( | ||
| { | ||
| sandboxName: "alpha", | ||
| model: "m", | ||
| provider: "p", | ||
| endpointUrl: "http://10.0.0.1/v1", | ||
| credentialEnv: null, | ||
| hermesAuthMethod: null, | ||
| hermesToolGateways: [], | ||
| }, | ||
| makeDeps() as never, | ||
| ), | ||
| ).rejects.toThrow(/private or internal/); | ||
| }); | ||
|
|
||
| it("rejects localhost hostname", async () => { | ||
| await expect( | ||
| setupHermesProviderInference( | ||
| { | ||
| sandboxName: "alpha", | ||
| model: "m", | ||
| provider: "p", | ||
| endpointUrl: "http://localhost:11434/v1", | ||
| credentialEnv: null, | ||
| hermesAuthMethod: null, | ||
| hermesToolGateways: [], | ||
| }, | ||
| makeDeps() as never, | ||
| ), | ||
| ).rejects.toThrow(/private or internal/); | ||
| }); | ||
|
|
||
| it("rejects .internal TLD", async () => { | ||
| await expect( | ||
| setupHermesProviderInference( | ||
| { | ||
| sandboxName: "alpha", | ||
| model: "m", | ||
| provider: "p", | ||
| endpointUrl: "http://my-service.internal/v1", | ||
| credentialEnv: null, | ||
| hermesAuthMethod: null, | ||
| hermesToolGateways: [], | ||
| }, | ||
| makeDeps() as never, | ||
| ), | ||
| ).rejects.toThrow(/private or internal/); | ||
| }); | ||
|
|
||
| it("throws on malformed URL", async () => { | ||
| await expect( | ||
| setupHermesProviderInference( | ||
| { | ||
| sandboxName: "alpha", | ||
| model: "m", | ||
| provider: "p", | ||
| endpointUrl: "not-a-url", | ||
| credentialEnv: null, | ||
| hermesAuthMethod: null, | ||
| hermesToolGateways: [], | ||
| }, | ||
| makeDeps() as never, | ||
| ), | ||
| ).rejects.toThrow(/Invalid inference endpoint URL/); | ||
| }); | ||
|
|
||
| it("accepts a public HTTPS endpoint", async () => { | ||
| const deps = makeDeps(); | ||
| await setupHermesProviderInference( | ||
| { | ||
| sandboxName: "alpha", | ||
| model: "m", | ||
| provider: "p", | ||
| endpointUrl: "https://integrate.api.nvidia.com/v1", | ||
| credentialEnv: null, | ||
| hermesAuthMethod: null, | ||
| hermesToolGateways: [], | ||
| }, | ||
| deps as never, | ||
| ); | ||
| expect(deps.runOpenshell).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("skips SSRF check when endpointUrl is null", async () => { | ||
| const deps = makeDeps(); | ||
| await setupHermesProviderInference( | ||
| { | ||
| sandboxName: "alpha", | ||
| model: "m", | ||
| provider: "p", | ||
| endpointUrl: null, | ||
| credentialEnv: null, | ||
| hermesAuthMethod: null, | ||
| hermesToolGateways: [], | ||
| }, | ||
| deps as never, | ||
| ); | ||
| expect(deps.runOpenshell).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import * as fs from "node:fs"; | ||
| import * as os from "node:os"; | ||
| import * as path from "node:path"; | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it } from "vitest"; | ||
|
|
||
| import { loadPresetFromFile } from "."; | ||
|
|
||
| let tempDir: string; | ||
|
|
||
| function writePreset(name: string, body: string): string { | ||
| const file = path.join(tempDir, `${name}.yaml`); | ||
| fs.writeFileSync(file, body); | ||
| return file; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "preset-ssrf-test-")); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| fs.rmSync(tempDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe("loadPresetFromFile allowed_ips guard (#6073)", () => { | ||
| it("rejects a preset whose endpoint declares allowed_ips", () => { | ||
| const file = writePreset( | ||
| "evil-preset", | ||
| `\ | ||
| preset: | ||
| name: evil-preset | ||
| description: sneaky | ||
| network_policies: | ||
| evil: | ||
| endpoints: | ||
| - host: 10.200.0.2 | ||
| port: 18789 | ||
| allowed_ips: | ||
| - 10.0.0.0/8 | ||
| `, | ||
| ); | ||
| expect(loadPresetFromFile(file)).toBeNull(); | ||
| }); | ||
|
|
||
| it("rejects when allowed_ips appears in a second policy entry", () => { | ||
| const file = writePreset( | ||
| "evil-preset-2", | ||
| `\ | ||
| preset: | ||
| name: evil-preset-2 | ||
| description: sneaky second policy | ||
| network_policies: | ||
| legit: | ||
| endpoints: | ||
| - host: api.example.com | ||
| port: 443 | ||
| evil: | ||
| endpoints: | ||
| - host: 192.168.1.1 | ||
| port: 8080 | ||
| allowed_ips: | ||
| - 192.168.0.0/16 | ||
| `, | ||
| ); | ||
| expect(loadPresetFromFile(file)).toBeNull(); | ||
| }); | ||
|
|
||
| it("accepts a valid preset with no allowed_ips", () => { | ||
| const file = writePreset( | ||
| "good-preset", | ||
| `\ | ||
| preset: | ||
| name: good-preset | ||
| description: clean | ||
| network_policies: | ||
| api: | ||
| endpoints: | ||
| - host: api.example.com | ||
| port: 443 | ||
| `, | ||
| ); | ||
| expect(loadPresetFromFile(file)).toMatchObject({ presetName: "good-preset" }); | ||
| }); | ||
|
|
||
| it("accepts endpoints that omit allowed_ips entirely", () => { | ||
| const file = writePreset( | ||
| "no-ips-preset", | ||
| `\ | ||
| preset: | ||
| name: no-ips-preset | ||
| description: plain endpoints only | ||
| network_policies: | ||
| cdn: | ||
| endpoints: | ||
| - host: cdn.example.com | ||
| port: 443 | ||
| - host: assets.example.com | ||
| port: 443 | ||
| `, | ||
| ); | ||
| expect(loadPresetFromFile(file)).toMatchObject({ presetName: "no-ips-preset" }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.