Skip to content

Commit a833ee1

Browse files
committed
Check if ssh config file is a file before read it
1 parent c6eb5fc commit a833ee1

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/common/files.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ export async function exists(path: string) {
1717
}
1818
}
1919

20+
export async function isFile(path: string) {
21+
try {
22+
const s = await fs.promises.stat(path);
23+
return s.isFile();
24+
} catch {
25+
return false;
26+
}
27+
}
28+
2029
export function untildify(path: string){
2130
return path.replace(/^~(?=$|\/|\\)/, homeDir);
2231
}

src/ssh/sshConfig.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as fs from 'fs';
88
import * as path from 'path';
99
import SSHConfig, { Line, Section } from 'ssh-config';
1010
import * as vscode from 'vscode';
11-
import { exists as fileExists } from '../common/files';
11+
import { isFile } from '../common/files';
1212
import { isWindows } from '../common/platform';
1313

1414
const systemSSHConfig = isWindows ? path.resolve(process.env.ALLUSERSPROFILE || 'C:\\ProgramData', 'ssh\\ssh_config') : '/etc/ssh/ssh_config';
@@ -28,12 +28,12 @@ export default class SSHConfiguration {
2828
static async loadFromFS(): Promise<SSHConfiguration> {
2929
const sshConfigPath = getSSHConfigPath();
3030
let content = '';
31-
if (await fileExists(sshConfigPath)) {
31+
if (await isFile(sshConfigPath)) {
3232
content = (await fs.promises.readFile(sshConfigPath, 'utf8')).trim();
3333
}
3434
const config = SSHConfig.parse(content);
3535

36-
if (await fileExists(systemSSHConfig)) {
36+
if (await isFile(systemSSHConfig)) {
3737
content = (await fs.promises.readFile(systemSSHConfig, 'utf8')).trim();
3838
config.push(...SSHConfig.parse(content));
3939
}

0 commit comments

Comments
 (0)