Skip to content

Commit 06289b3

Browse files
steipeteYLChen-007
andcommitted
fix(security): harden SSH target handling (#4001)
Thanks @YLChen-007. Co-authored-by: Edward-x <[email protected]>
1 parent 4b5514a commit 06289b3

File tree

8 files changed

+82
-5
lines changed

8 files changed

+82
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Docs: https://docs.molt.bot
66
Status: beta.
77

88
### Changes
9+
- Security: harden SSH tunnel target parsing to prevent option injection/DoS. (#4001) Thanks @YLChen-007.
910
- Rebrand: rename the npm package/CLI to `moltbot`, add a `moltbot` compatibility shim, and move extensions to the `@moltbot/*` scope.
1011
- Commands: group /help and /commands output with Telegram paging. (#2504) Thanks @hougangdev.
1112
- macOS: limit project-local `node_modules/.bin` PATH preference to debug builds (reduce PATH hijacking risk).

src/commands/gateway-status.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,45 @@ describe("gateway-status command", () => {
192192
expect(targets.some((t) => t.kind === "sshTunnel")).toBe(true);
193193
});
194194

195+
it("skips invalid ssh-auto discovery targets", async () => {
196+
const runtimeLogs: string[] = [];
197+
const runtime = {
198+
log: (msg: string) => runtimeLogs.push(msg),
199+
error: (_msg: string) => {},
200+
exit: (code: number) => {
201+
throw new Error(`__exit__:${code}`);
202+
},
203+
};
204+
205+
const originalUser = process.env.USER;
206+
try {
207+
process.env.USER = "steipete";
208+
loadConfig.mockReturnValueOnce({
209+
gateway: {
210+
mode: "remote",
211+
remote: {},
212+
},
213+
});
214+
discoverGatewayBeacons.mockResolvedValueOnce([
215+
{ tailnetDns: "-V" },
216+
{ tailnetDns: "goodhost" },
217+
]);
218+
219+
startSshPortForward.mockClear();
220+
const { gatewayStatusCommand } = await import("./gateway-status.js");
221+
await gatewayStatusCommand(
222+
{ timeout: "1000", json: true, sshAuto: true },
223+
runtime as unknown as import("../runtime.js").RuntimeEnv,
224+
);
225+
226+
expect(startSshPortForward).toHaveBeenCalledTimes(1);
227+
const call = startSshPortForward.mock.calls[0]?.[0] as { target: string };
228+
expect(call.target).toBe("steipete@goodhost");
229+
} finally {
230+
process.env.USER = originalUser;
231+
}
232+
});
233+
195234
it("infers SSH target from gateway.remote.url and ssh config", async () => {
196235
const runtimeLogs: string[] = [];
197236
const runtime = {

src/commands/gateway-status.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ export async function gatewayStatusCommand(
107107
const base = user ? `${user}@${host.trim()}` : host.trim();
108108
return sshPort !== 22 ? `${base}:${sshPort}` : base;
109109
})
110-
.filter((x): x is string => Boolean(x));
110+
.filter((candidate): candidate is string =>
111+
Boolean(candidate && parseSshTarget(candidate)),
112+
);
111113
if (candidates.length > 0) sshTarget = candidates[0] ?? null;
112114
}
113115

src/infra/ssh-config.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ describe("ssh-config", () => {
5454
expect(config?.host).toBe("peters-mac-studio-1.sheep-coho.ts.net");
5555
expect(config?.port).toBe(2222);
5656
expect(config?.identityFiles).toEqual(["/tmp/id_ed25519"]);
57+
const args = spawnMock.mock.calls[0]?.[1] as string[] | undefined;
58+
expect(args?.slice(-2)).toEqual(["--", "me@alias"]);
5759
});
5860

5961
it("returns null when ssh -G fails", async () => {

src/infra/ssh-config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ export async function resolveSshConfig(
5858
args.push("-i", opts.identity.trim());
5959
}
6060
const userHost = target.user ? `${target.user}@${target.host}` : target.host;
61-
args.push(userHost);
61+
// Use "--" so userHost can't be parsed as an ssh option.
62+
args.push("--", userHost);
6263

6364
return await new Promise<SshResolvedConfig | null>((resolve) => {
6465
const child = spawn(sshPath, args, {

src/infra/ssh-tunnel.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { parseSshTarget } from "./ssh-tunnel.js";
4+
5+
describe("parseSshTarget", () => {
6+
it("parses user@host:port targets", () => {
7+
expect(parseSshTarget("[email protected]:2222")).toEqual({
8+
user: "me",
9+
host: "example.com",
10+
port: 2222,
11+
});
12+
});
13+
14+
it("parses host-only targets with default port", () => {
15+
expect(parseSshTarget("example.com")).toEqual({
16+
user: undefined,
17+
host: "example.com",
18+
port: 22,
19+
});
20+
});
21+
22+
it("rejects hostnames that start with '-'", () => {
23+
expect(parseSshTarget("-V")).toBeNull();
24+
expect(parseSshTarget("me@-badhost")).toBeNull();
25+
expect(parseSshTarget("-oProxyCommand=echo")).toBeNull();
26+
});
27+
});

src/infra/ssh-tunnel.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ export function parseSshTarget(raw: string): SshParsedTarget | null {
4141
const portRaw = hostPart.slice(colonIdx + 1).trim();
4242
const port = Number.parseInt(portRaw, 10);
4343
if (!host || !Number.isFinite(port) || port <= 0) return null;
44+
// Security: Reject hostnames starting with '-' to prevent argument injection
45+
if (host.startsWith("-")) return null;
4446
return { user: userPart, host, port };
4547
}
4648

4749
if (!hostPart) return null;
50+
// Security: Reject hostnames starting with '-' to prevent argument injection
51+
if (hostPart.startsWith("-")) return null;
4852
return { user: userPart, host: hostPart, port: 22 };
4953
}
5054

@@ -134,7 +138,8 @@ export async function startSshPortForward(opts: {
134138
if (opts.identity?.trim()) {
135139
args.push("-i", opts.identity.trim());
136140
}
137-
args.push(userHost);
141+
// Security: Use '--' to prevent userHost from being interpreted as an option
142+
args.push("--", userHost);
138143

139144
const stderr: string[] = [];
140145
const child = spawn("/usr/bin/ssh", args, {

src/plugins/config-state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export function applyTestPluginDefaults(
9999
...plugins,
100100
slots: {
101101
...plugins?.slots,
102-
memory: null,
102+
memory: "none",
103103
},
104104
},
105105
};
@@ -112,7 +112,7 @@ export function applyTestPluginDefaults(
112112
enabled: false,
113113
slots: {
114114
...plugins?.slots,
115-
memory: null,
115+
memory: "none",
116116
},
117117
},
118118
};

0 commit comments

Comments
 (0)