Repository navigation
fix(security): validate blueprint sandbox and provider names #7238
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
cv
merged 7 commits into
NVIDIA:main
from
1PoPTRoN:fix/blueprint-sandbox-name-validation
Jul 26, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4922fce
fix(security): validate blueprint sandbox and provider names
1PoPTRoN 15601c1
fix(security): harden blueprint name validation
cv 4486d17
merge(main): refresh blueprint name validation
cv b0e0c26
docs(architecture): document blueprint name constraints
cv ad316cb
Merge branch 'main' into fix/blueprint-sandbox-name-validation
prekshivyas ca556e6
Merge branch 'main' into fix/blueprint-sandbox-name-validation
prekshivyas f2020df
Merge branch 'main' into fix/blueprint-sandbox-name-validation
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
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,225 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Integration tests for the runner's fail-closed name validation. Kept in a | ||
| // focused file so runner.test.ts stays under the test-file-size budget. The | ||
| // mocks mirror runner.test.ts so these exercise the real apply/rollback paths. | ||
|
|
||
| import type fs from "node:fs"; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| interface FsEntry { | ||
| type: "file" | "dir"; | ||
| content?: string; | ||
| } | ||
|
|
||
| /** Throw from an expression position so the fake fs stays branch-free. */ | ||
| function raise(message: string): never { | ||
| throw new Error(message); | ||
| } | ||
|
|
||
| const store = new Map<string, FsEntry>(); | ||
|
|
||
| function addFile(p: string, content: string): void { | ||
| store.set(p, { type: "file", content }); | ||
| } | ||
|
|
||
| function addDir(p: string): void { | ||
| store.set(p, { type: "dir" }); | ||
| } | ||
|
|
||
| const FAKE_HOME = "/fakehome"; | ||
|
|
||
| vi.mock("node:os", () => ({ | ||
| homedir: () => FAKE_HOME, | ||
| })); | ||
|
|
||
| vi.mock("node:crypto", () => ({ | ||
| randomUUID: () => "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", | ||
| })); | ||
|
|
||
| vi.mock("node:fs", async (importOriginal) => { | ||
| const original = await importOriginal<typeof fs>(); | ||
| return { | ||
| ...original, | ||
| existsSync: (p: string) => store.has(p), | ||
| mkdirSync: vi.fn((p: string) => { | ||
| addDir(p); | ||
| }), | ||
| readFileSync: (p: string) => { | ||
| const entry = store.get(p); | ||
| return entry?.type === "file" ? (entry.content ?? "") : raise(`ENOENT: ${p}`); | ||
| }, | ||
| writeFileSync: vi.fn((p: string, data: string) => { | ||
| store.set(p, { type: "file", content: data }); | ||
| }), | ||
| readdirSync: (p: string) => { | ||
| const prefix = p.endsWith("/") ? p : `${p}/`; | ||
| const entries = new Set( | ||
| [...store.keys()] | ||
| .filter((k) => k.startsWith(prefix)) | ||
| .map((k) => k.slice(prefix.length).split("/")[0]) | ||
| .filter((first): first is string => Boolean(first)), | ||
| ); | ||
| return entries.size === 0 && !store.has(p) ? raise(`ENOENT: ${p}`) : [...entries].sort(); | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| const mockExeca = vi.fn(); | ||
| vi.mock("execa", () => ({ | ||
| execa: (...args: unknown[]) => mockExeca(...args), | ||
| })); | ||
|
|
||
| vi.mock("./ssrf.js", async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import("./ssrf.js")>(); | ||
| return { | ||
| ...actual, | ||
| validateEndpointUrl: vi.fn(async (url: string) => ({ | ||
| url, | ||
| pinnedUrl: url, | ||
| protocol: url.startsWith("http:") ? "http:" : "https:", | ||
| hostname: new URL(url).hostname, | ||
| dnsResolved: false, | ||
| })), | ||
| }; | ||
| }); | ||
|
|
||
| const { actionApply, actionRollback } = await import("./runner.js"); | ||
|
|
||
| const stdoutChunks: string[] = []; | ||
|
|
||
| function captureStdout(): void { | ||
| vi.spyOn(process.stdout, "write").mockImplementation((chunk: string | Uint8Array) => { | ||
| stdoutChunks.push(String(chunk)); | ||
| return true; | ||
| }); | ||
| } | ||
|
|
||
| function minimalBlueprint(): Record<string, unknown> { | ||
| return { | ||
| version: "1.0", | ||
| components: { | ||
| inference: { | ||
| profiles: { | ||
| default: { | ||
| provider_type: "openai", | ||
| provider_name: "my-provider", | ||
| endpoint: "https://api.example.com/v1", | ||
| model: "gpt-4", | ||
| credential_env: "MY_API_KEY", | ||
| }, | ||
| }, | ||
| }, | ||
| sandbox: { | ||
| image: "openclaw", | ||
| name: "test-sandbox", | ||
| forward_ports: [18789], | ||
| }, | ||
| policy: { additions: {} }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function createCalls(): unknown[] { | ||
| return mockExeca.mock.calls.filter( | ||
| (c) => Array.isArray(c[1]) && c[1][0] === "sandbox" && c[1][1] === "create", | ||
| ); | ||
| } | ||
|
|
||
| type BlueprintComponents = { | ||
| sandbox: { name: string }; | ||
| inference: { profiles: { default: { provider_name: string } } }; | ||
| }; | ||
|
|
||
| /** Each case corrupts one identifier so the table stays branch-free. */ | ||
| const INVALID_NAME_CASES: ReadonlyArray< | ||
| readonly [string, (components: BlueprintComponents) => void, RegExp] | ||
| > = [ | ||
| [ | ||
| "sandbox name", | ||
| // "--help" would be consumed as a flag by `openshell sandbox create`. | ||
| (components) => { | ||
| components.sandbox.name = "--help"; | ||
| }, | ||
| /Invalid sandbox name/, | ||
| ], | ||
| [ | ||
| "provider name", | ||
| (components) => { | ||
| components.inference.profiles.default.provider_name = "$(id)"; | ||
| }, | ||
| /Invalid provider name/, | ||
| ], | ||
| [ | ||
| "provider name containing a line feed", | ||
| (components) => { | ||
| components.inference.profiles.default.provider_name = "provider\n::error::forged"; | ||
| }, | ||
| /Invalid provider name/, | ||
| ], | ||
| [ | ||
| "over-length provider name", | ||
| (components) => { | ||
| components.inference.profiles.default.provider_name = `a${"b".repeat(128)}`; | ||
| }, | ||
| /Invalid provider name/, | ||
| ], | ||
| ]; | ||
|
|
||
| const VALID_PROVIDER_NAMES = ["Provider_1.prod", `a${"b".repeat(127)}`] as const; | ||
|
|
||
| describe("blueprint name validation (fail-closed integration)", () => { | ||
| const RUNS_DIR = `${FAKE_HOME}/.nemoclaw/state/runs`; | ||
|
|
||
| beforeEach(() => { | ||
| store.clear(); | ||
| stdoutChunks.length = 0; | ||
| vi.clearAllMocks(); | ||
| captureStdout(); | ||
| mockExeca.mockResolvedValue({ exitCode: 0, stdout: "", stderr: "" }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it.each( | ||
| INVALID_NAME_CASES, | ||
| )("apply rejects a malformed %s and creates no sandbox", async (_label, corrupt, expected) => { | ||
| const bp = minimalBlueprint(); | ||
| corrupt(bp.components as BlueprintComponents); | ||
|
|
||
| await expect(actionApply("default", bp)).rejects.toThrow(expected); | ||
| // Validation runs before any command, so no provider or sandbox command | ||
| // may have executed — not merely no sandbox create. | ||
| expect(mockExeca).not.toHaveBeenCalled(); | ||
| expect(createCalls()).toEqual([]); | ||
|
1PoPTRoN marked this conversation as resolved.
Resolved
|
||
| }); | ||
|
|
||
| it.each( | ||
| VALID_PROVIDER_NAMES, | ||
| )("apply accepts the supported provider name '%s'", async (providerName) => { | ||
| const bp = minimalBlueprint(); | ||
| const components = bp.components as BlueprintComponents; | ||
| components.inference.profiles.default.provider_name = providerName; | ||
|
|
||
| await expect(actionApply("default", bp)).resolves.toBeUndefined(); | ||
| expect(mockExeca).toHaveBeenCalledWith( | ||
| "openshell", | ||
| expect.arrayContaining(["provider", "create", "--name", providerName]), | ||
| expect.any(Object), | ||
| ); | ||
| }); | ||
|
|
||
| it("rollback rejects a plan whose sandbox_name is not an RFC 1035 label", async () => { | ||
| const runDir = `${RUNS_DIR}/nc-run-1`; | ||
| addDir(runDir); | ||
| // "--rm" would be consumed as a flag by `openshell sandbox stop/remove`. | ||
| addFile(`${runDir}/plan.json`, JSON.stringify({ sandbox_name: "--rm" })); | ||
|
|
||
| await expect(actionRollback("nc-run-1")).rejects.toThrow(/Invalid sandbox name/); | ||
| expect(mockExeca).not.toHaveBeenCalled(); | ||
| expect(store.has(`${runDir}/rolled_back`)).toBe(false); | ||
| }); | ||
| }); | ||
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
Oops, something went wrong.
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.