Skip to content
Open
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
65 changes: 54 additions & 11 deletions src/main/ipc/sshIpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,49 @@ export function registerSshIpc() {
config: SshConfig & { password?: string; passphrase?: string }
): Promise<ConnectionTestResult> => {
try {
// When renderer sends only connection id (e.g. SshConnectionTestButton), resolve from DB and keytar
const isIdOnly =
config.id &&
(typeof config.host !== 'string' || !config.host.trim()) &&
(typeof config.username !== 'string' || !config.username.trim());

let resolvedConfig: SshConfig & { password?: string; passphrase?: string } = config;
if (isIdOnly && config.id) {
Comment on lines +165 to +171
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant config.id guard in the if condition

isIdOnly already short-circuits to a falsy value whenever config.id is falsy (it's the first operand in the && chain). The outer if (isIdOnly && config.id) therefore re-checks the same thing twice and can never be true unless config.id is already truthy.

The extra guard does help TypeScript narrow config.id to string inside the block, but that could be achieved more explicitly with a type assertion or by restructuring the guard. As written it looks like a logical mistake rather than an intentional narrowing hint.

Suggested change
const isIdOnly =
config.id &&
(typeof config.host !== 'string' || !config.host.trim()) &&
(typeof config.username !== 'string' || !config.username.trim());
let resolvedConfig: SshConfig & { password?: string; passphrase?: string } = config;
if (isIdOnly && config.id) {
if (isIdOnly) {
const id = config.id!;
const { db } = await getDrizzleClient();
const rows = await db
.select({
id: sshConnectionsTable.id,
name: sshConnectionsTable.name,
host: sshConnectionsTable.host,
port: sshConnectionsTable.port,
username: sshConnectionsTable.username,
authType: sshConnectionsTable.authType,
privateKeyPath: sshConnectionsTable.privateKeyPath,
useAgent: sshConnectionsTable.useAgent,
})
.from(sshConnectionsTable)
.where(eq(sshConnectionsTable.id, id))
.limit(1);

const { db } = await getDrizzleClient();
const rows = await db
.select({
id: sshConnectionsTable.id,
name: sshConnectionsTable.name,
host: sshConnectionsTable.host,
port: sshConnectionsTable.port,
username: sshConnectionsTable.username,
authType: sshConnectionsTable.authType,
privateKeyPath: sshConnectionsTable.privateKeyPath,
useAgent: sshConnectionsTable.useAgent,
})
.from(sshConnectionsTable)
.where(eq(sshConnectionsTable.id, config.id))
.limit(1);

const row = rows[0];
if (!row) {
return { success: false, error: `Connection not found: ${config.id}` };
}

resolvedConfig = mapRowToConfig(row) as SshConfig & {
password?: string;
passphrase?: string;
};
if (resolvedConfig.authType === 'password') {
resolvedConfig.password =
(await credentialService.getPassword(resolvedConfig.id)) ?? undefined;
}
if (resolvedConfig.authType === 'key') {
resolvedConfig.passphrase =
(await credentialService.getPassphrase(resolvedConfig.id)) ?? undefined;
}
}

const { Client } = await import('ssh2');
const debugLogs: string[] = [];
const testClient = new Client();
Expand Down Expand Up @@ -199,30 +242,30 @@ export function registerSshIpc() {
agent?: string;
debug?: (info: string) => void;
} = {
host: config.host,
port: config.port,
username: config.username,
host: resolvedConfig.host,
port: resolvedConfig.port,
username: resolvedConfig.username,
readyTimeout: 10000,
debug: (info: string) => debugLogs.push(info),
};

if (config.authType === 'password') {
connectConfig.password = config.password;
} else if (config.authType === 'key' && config.privateKeyPath) {
if (resolvedConfig.authType === 'password') {
connectConfig.password = resolvedConfig.password;
} else if (resolvedConfig.authType === 'key' && resolvedConfig.privateKeyPath) {
const fs = require('fs');
const os = require('os');
try {
// Expand ~ to home directory
let keyPath = config.privateKeyPath;
let keyPath = resolvedConfig.privateKeyPath;
if (keyPath.startsWith('~/')) {
keyPath = keyPath.replace('~', os.homedir());
} else if (keyPath === '~') {
keyPath = os.homedir();
}

connectConfig.privateKey = fs.readFileSync(keyPath);
if (config.passphrase) {
connectConfig.passphrase = config.passphrase;
if (resolvedConfig.passphrase) {
connectConfig.passphrase = resolvedConfig.passphrase;
}
} catch (err: any) {
resolve({
Expand All @@ -232,8 +275,8 @@ export function registerSshIpc() {
});
return;
}
} else if (config.authType === 'agent') {
const identityAgent = await resolveIdentityAgent(config.host);
} else if (resolvedConfig.authType === 'agent') {
const identityAgent = await resolveIdentityAgent(resolvedConfig.host);
connectConfig.agent = identityAgent || process.env.SSH_AUTH_SOCK;
}

Expand Down
4 changes: 1 addition & 3 deletions src/renderer/components/ssh/SshConnectionTestButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export const SshConnectionTestButton: React.FC<Props> = ({
setCopied(false);

try {
// For testing, we need the full config - this would need to be fetched or passed in
// For now, we'll call the test with just the ID and let the main process handle it
// TODO: Fetch connection details or update IPC to accept just ID
// Main process accepts ID-only: it loads the saved connection from DB and hydrates credentials from keytar.
const testResult = await window.electronAPI.sshTestConnection({
id: connectionId,
name: '',
Expand Down