import { describe, it, expect } from "vitest"; import { generateSSHCommand, isValidHost, isValidUsername, isValidPort } from "../src/utils/ssh.js"; import { VPS } from "../src/types/vps.js"; describe("SSH Utility", () => { const baseVPS: VPS = { id: "vps-test", name: "test-node", provider: "Vultr", region: "Singapore", environment: "Production", cpu: 4, ramGB: 8, diskGB: 160, }; describe("Validation functions", () => { it("should correctly validate IPv4 and hostnames", () => { expect(isValidHost("203.0.113.10")).toBe(true); expect(isValidHost("192.168.1.1")).toBe(true); expect(isValidHost("vps.example.com")).toBe(true); // Malicious or invalid hosts expect(isValidHost("203.0.113.10; rm -rf /")).toBe(false); expect(isValidHost("`whoami`.example.com")).toBe(false); expect(isValidHost("-oProxyCommand=calc.exe")).toBe(false); expect(isValidHost("256.300.1.1")).toBe(false); }); it("should correctly validate SSH usernames", () => { expect(isValidUsername("root")).toBe(true); expect(isValidUsername("deploy")).toBe(true); expect(isValidUsername("admin_user-01")).toBe(true); // Malicious usernames expect(isValidUsername("deploy; ls")).toBe(false); expect(isValidUsername("user$(whoami)")).toBe(false); expect(isValidUsername("user`id`")).toBe(false); expect(isValidUsername("user with spaces")).toBe(false); }); it("should correctly validate ports", () => { expect(isValidPort(22)).toBe(true); expect(isValidPort(2222)).toBe(true); expect(isValidPort(65535)).toBe(true); expect(isValidPort(0)).toBe(false); expect(isValidPort(-1)).toBe(false); expect(isValidPort(70000)).toBe(false); expect(isValidPort(22.5)).toBe(false); }); }); describe("generateSSHCommand", () => { it("should fail gracefully if IP is missing", () => { const vpsWithoutIP: VPS = { ...baseVPS, sshUser: "deploy" }; const result = generateSSHCommand(vpsWithoutIP); expect(result.success).toBe(false); expect(result.error).toBe("SSH connection information has not been configured for this VPS."); expect(result.command).toBeUndefined(); }); it("should fail gracefully if sshUser is missing", () => { const vpsWithoutUser: VPS = { ...baseVPS, ip: "203.0.113.10" }; const result = generateSSHCommand(vpsWithoutUser); expect(result.success).toBe(false); expect(result.error).toBe("SSH connection information has not been configured for this VPS."); }); it("should default to port 22 when sshPort is not specified", () => { const vps: VPS = { ...baseVPS, ip: "203.0.113.10", sshUser: "deploy", }; const result = generateSSHCommand(vps); expect(result.success).toBe(true); expect(result.command).toBe("ssh -p 22 deploy@203.0.113.10"); }); it("should use custom port when sshPort is specified", () => { const vps: VPS = { ...baseVPS, ip: "198.51.100.25", sshUser: "ubuntu", sshPort: 22022, }; const result = generateSSHCommand(vps); expect(result.success).toBe(true); expect(result.command).toBe("ssh -p 22022 ubuntu@198.51.100.25"); }); it("should reject malicious IP input", () => { const vps: VPS = { ...baseVPS, ip: "203.0.113.10; id", sshUser: "deploy", }; const result = generateSSHCommand(vps); expect(result.success).toBe(false); expect(result.error).toContain("invalid or unsafe"); }); it("should reject malicious username input", () => { const vps: VPS = { ...baseVPS, ip: "203.0.113.10", sshUser: "deploy&&reboot", }; const result = generateSSHCommand(vps); expect(result.success).toBe(false); expect(result.error).toContain("invalid or unsafe"); }); }); });