Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions packages/cli/src/__tests__/ssh-cov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import * as net from "node:net";
// Suppress stderr during tests β€” restored in afterAll to avoid contamination
let stderrSpy: ReturnType<typeof spyOn>;

const { spawnInteractive, startSshTunnel, waitForSsh, SSH_BASE_OPTS, SSH_INTERACTIVE_OPTS } = await import(
"../shared/ssh.js"
);
const { spawnInteractive, startSshTunnel, waitForSsh, validateRemotePath, SSH_BASE_OPTS, SSH_INTERACTIVE_OPTS } =
await import("../shared/ssh.js");

/** Create a fake socket (EventEmitter) that satisfies net.Socket interface for testing. */
function createFakeSocket(): net.Socket {
Expand Down Expand Up @@ -274,6 +273,40 @@ describe("waitForSsh", () => {
});
});

// ── validateRemotePath ───────────────────────────────────────────────

describe("validateRemotePath", () => {
it("accepts valid Linux paths with forward slashes", () => {
expect(validateRemotePath("/home/user/config.json")).toBe("/home/user/config.json");
expect(validateRemotePath("/root/.spawn-tarball")).toBe("/root/.spawn-tarball");
expect(validateRemotePath("$HOME/.config/spawn")).toBe("$HOME/.config/spawn");
});

it("normalizes using POSIX rules (no backslashes)", () => {
// normalize should collapse double slashes but never introduce backslashes
const result = validateRemotePath("/home//user///file.txt");
expect(result).toBe("/home/user/file.txt");
expect(result).not.toContain("\\");
});

it("rejects path traversal", () => {
expect(() => validateRemotePath("/home/../etc/passwd")).toThrow("path traversal");
expect(() => validateRemotePath("../etc/shadow")).toThrow("path traversal");
});

it("rejects empty path", () => {
expect(() => validateRemotePath("")).toThrow("must not be empty");
});

it("rejects argument injection", () => {
expect(() => validateRemotePath("/-evil")).toThrow('must not start with "-"');
});

it("rejects unsafe characters", () => {
expect(() => validateRemotePath("/home/user;rm -rf")).toThrow("unsafe characters");
});
});

// Final cleanup
afterAll(() => {
stderrSpy.mockRestore();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/shared/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { spawnSync as nodeSpawnSync } from "node:child_process";
import { connect } from "node:net";
import { normalize } from "node:path";
import { normalize } from "node:path/posix";
import { asyncTryCatch, tryCatch } from "./result.js";
import { logError, logInfo, logStep, logStepDone, logStepInline } from "./ui.js";

Expand Down
Loading