-
Notifications
You must be signed in to change notification settings - Fork 3.1k
test(security): add regression tests for deploy() instance name validation (#575) #1815
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
Closed
Closed
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -144,3 +144,103 @@ describe("Brev status helpers", () => { | |
| ).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("executeDeploy — instance name validation (#575)", () => { | ||
| // Helper: build a minimal DeployExecutionOptions that tracks calls | ||
| function makeMockOpts(instanceName: string) { | ||
| const calls: string[] = []; | ||
| let exitCode: number | undefined; | ||
| let exitError: Error | undefined; | ||
|
|
||
| return { | ||
| opts: { | ||
| instanceName, | ||
| env: { NEMOCLAW_GPU: "a2-highgpu-1g:nvidia-tesla-a100:1" }, | ||
| rootDir: "/fake/root", | ||
| getCredential: () => null, | ||
| validateName: (name: string, label: string) => { | ||
| if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(name)) { | ||
| throw new Error( | ||
| `Invalid ${label}: '${name}'. Must be lowercase alphanumeric with optional internal hyphens.`, | ||
| ); | ||
| } | ||
| return name; | ||
| }, | ||
| shellQuote: (value: string) => `'${value.replace(/'/g, "'\''")}'`, | ||
| run: (command: string) => { | ||
| calls.push(`run:${command}`); | ||
| }, | ||
| runInteractive: (command: string) => { | ||
| calls.push(`runInteractive:${command}`); | ||
| }, | ||
| execFileSync: (file: string, args: string[], _opts?: Record<string, unknown>) => { | ||
| calls.push(`execFileSync:${file} ${args.join(" ")}`); | ||
| return ""; | ||
| }, | ||
| spawnSync: (file: string, args: string[], _opts?: Record<string, unknown>) => { | ||
| calls.push(`spawnSync:${file} ${args.join(" ")}`); | ||
| }, | ||
| log: () => {}, | ||
| error: () => {}, | ||
| stdoutWrite: () => {}, | ||
| exit: ((code: number) => { | ||
| exitCode = code; | ||
| exitError = new Error(`exit(${code})`); | ||
| throw exitError; | ||
| }) as (code: number) => never, | ||
| }, | ||
| calls, | ||
| getExitCode: () => exitCode, | ||
| getExitError: () => exitError, | ||
| }; | ||
| } | ||
|
|
||
| const maliciousNames = [ | ||
| "foo;rm -rf /", | ||
| "foo|cat /etc/passwd", | ||
| "$(whoami)", | ||
| "`whoami`", | ||
| "foo && echo pwned", | ||
| "foo'inject", | ||
| 'foo"inject', | ||
| "../traversal", | ||
| "UPPERCASE", | ||
| "has spaces", | ||
|
|
||
| ]; | ||
|
|
||
| for (const name of maliciousNames) { | ||
| it(`rejects malicious instance name: ${JSON.stringify(name)}`, async () => { | ||
| const { executeDeploy } = await import("../../dist/lib/deploy"); | ||
| const { opts, calls } = makeMockOpts(name); | ||
|
|
||
| await expect(executeDeploy(opts)).rejects.toThrow(/Invalid instance name|instance name is required/i); | ||
|
|
||
| // No shell commands should have been executed | ||
| expect(calls).toHaveLength(0); | ||
| }); | ||
| } | ||
|
|
||
| it("accepts a valid instance name", async () => { | ||
| const { executeDeploy } = await import("../../dist/lib/deploy"); | ||
| const { opts, getExitCode } = makeMockOpts("my-valid-instance"); | ||
|
|
||
| // This will fail at the provider detection step (no credentials), | ||
| // but it should NOT fail at validation — proving the name was accepted. | ||
| // Catch any thrown error and explicitly assert it wasn't a name-validation | ||
| // failure, so a future regression can't silently pass this test. | ||
| const caught = await executeDeploy(opts) | ||
| .then(() => null) | ||
| .catch((error: unknown) => error); | ||
| if (caught) { | ||
| expect(String(caught)).not.toMatch( | ||
| /Invalid instance name|instance name is required/i, | ||
| ); | ||
| } | ||
|
|
||
| // If it exited, it should be because of missing provider/brev, not name validation | ||
| if (getExitCode() !== undefined) { | ||
| expect(getExitCode()).toBe(1); | ||
| } | ||
|
Comment on lines
+226
to
+244
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The valid-name test still has a vacuous success path. If Suggested patch- const { opts, getExitCode } = makeMockOpts("my-valid-instance");
+ const { opts, calls, getExitCode } = makeMockOpts("my-valid-instance");
@@
- // If it exited, it should be because of missing provider/brev, not name validation
+ // Ensure it progressed beyond validation (either exited downstream or attempted a command path)
+ expect(getExitCode() !== undefined || calls.length > 0).toBe(true);
+ // If it exited, it should be because of missing provider/brev, not name validation
if (getExitCode() !== undefined) {
expect(getExitCode()).toBe(1);
}🤖 Prompt for AI Agents |
||
| }); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mock
validateNamediverges from production rules and can mask regressions.At Line 162, the test helper allows names starting with digits and omits the max-length guard, while
src/lib/runner.tsrequires a leading letter and enforces 63-char max. This weakens regression fidelity.Suggested patch
validateName: (name: string, label: string) => { - if (!/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/.test(name)) { + if (!name) { + throw new Error(`${label} is required`); + } + if (name.length > 63) { + throw new Error(`${label} too long (max 63 chars): '${name.slice(0, 20)}...'`); + } + if (!/^[a-z]([a-z0-9-]*[a-z0-9])?$/.test(name)) { throw new Error( - `Invalid ${label}: '${name}'. Must be lowercase alphanumeric with optional internal hyphens.`, + `Invalid ${label}: '${name}'. Must start with a letter and contain only lowercase alphanumerics with optional internal hyphens.`, ); } return name; },📝 Committable suggestion
🤖 Prompt for AI Agents