Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle errors where the SSH configuration file is corrupt or malformed #4644

Merged
merged 3 commits into from
Apr 11, 2023
Merged
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
24 changes: 16 additions & 8 deletions src/env/browser/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { parse as parseConfig } from 'ssh-config';
import Logger from '../../common/logger';

const SSH_URL_RE = /^(?:([^@:]+)@)?([^:/]+):?(.+)$/;
const URL_SCHEME_RE = /^([a-z-+]+):\/\//;
Expand Down Expand Up @@ -56,7 +57,7 @@ export const sshParse = (url: string): Config | undefined => {
return config && resolveConfig(config);
};

export function baseResolver(config: Config) {
export function baseResolver(config: Config): Config {
return {
...config,
Hostname: config.Host,
Expand All @@ -83,13 +84,20 @@ export type ConfigResolver = (config: Config) => Config;
export function chainResolvers(...chain: (ConfigResolver | undefined)[]): ConfigResolver {
const resolvers = chain.filter(x => !!x) as ConfigResolver[];
return (config: Config) =>
resolvers.reduce(
(resolved, next) => ({
...resolved,
...next(resolved),
}),
config,
);
resolvers.reduce((resolved, next) => {
try {
return {
...resolved,
...next(resolved),
};
} catch (err) {
// We cannot trust that some resolvers are not going to throw (i.e user has malformed .ssh/config file).
// Since we can't guarantee that ssh-config package won't throw and we're reducing over the entire chain of resolvers,
// we'll skip erroneous resolvers for now and log. Potentially can validate
Logger.warn(`Failed to parse config for '${config.Host}, this can occur when the extension configurations is invalid or system ssh config files are malformed. Skipping erroneous resolver for now.'`);
return resolved;
}
}, config);
}

export function resolverFromConfig(text: string): ConfigResolver {
Expand Down