Better port validation + fill actual config with default SSH port (fix #266)

pull/285/head
Kelvin Schoofs 3 years ago
parent f86e33a7de
commit a41c435e49

@ -8,7 +8,7 @@ import { getConfig, getFlagBoolean } from './config';
import type { FileSystemConfig } from './fileSystemConfig'; import type { FileSystemConfig } from './fileSystemConfig';
import { censorConfig, Logging } from './logging'; import { censorConfig, Logging } from './logging';
import type { PuttySession } from './putty'; import type { PuttySession } from './putty';
import { toPromise } from './utils'; import { toPromise, validatePort } from './utils';
// tslint:disable-next-line:variable-name // tslint:disable-next-line:variable-name
const SFTPWrapper = require('ssh2/lib/SFTPWrapper') as (new (stream: SFTPStream) => SFTPWrapperReal); const SFTPWrapper = require('ssh2/lib/SFTPWrapper') as (new (stream: SFTPStream) => SFTPWrapperReal);
@ -66,7 +66,7 @@ export async function calculateActualConfig(config: FileSystemConfig): Promise<F
if (config.username !== '$USER') config.username = replaceVariables(config.username); if (config.username !== '$USER') config.username = replaceVariables(config.username);
config.host = replaceVariables(config.host); config.host = replaceVariables(config.host);
const port = replaceVariables((config.port || '') + ''); const port = replaceVariables((config.port || '') + '');
if (port) config.port = Number(port); config.port = port ? validatePort(port) : 22;
config.agent = replaceVariables(config.agent); config.agent = replaceVariables(config.agent);
config.privateKeyPath = replaceVariables(config.privateKeyPath); config.privateKeyPath = replaceVariables(config.privateKeyPath);
logging.info(`Calculating actual config`); logging.info(`Calculating actual config`);

@ -4,7 +4,7 @@ import { request } from 'http';
import { SocksClient } from 'socks'; import { SocksClient } from 'socks';
import type { FileSystemConfig } from './fileSystemConfig'; import type { FileSystemConfig } from './fileSystemConfig';
import { Logging } from './logging'; import { Logging } from './logging';
import { toPromise } from './utils'; import { toPromise, validatePort } from './utils';
async function resolveHostname(hostname: string): Promise<string> { async function resolveHostname(hostname: string): Promise<string> {
return toPromise<string>(cb => dns.lookup(hostname, cb)).then((ip) => { return toPromise<string>(cb => dns.lookup(hostname, cb)).then((ip) => {
@ -15,9 +15,10 @@ async function resolveHostname(hostname: string): Promise<string> {
function validateConfig(config: FileSystemConfig) { function validateConfig(config: FileSystemConfig) {
if (!config.proxy) throw new Error(`Missing field 'config.proxy'`); if (!config.proxy) throw new Error(`Missing field 'config.proxy'`);
if (!config.proxy.type) throw new Error(`Missing field 'config.proxy.type'`);
if (!config.proxy.host) throw new Error(`Missing field 'config.proxy.host'`); if (!config.proxy.host) throw new Error(`Missing field 'config.proxy.host'`);
if (!config.proxy.port) throw new Error(`Missing field 'config.proxy.port'`); if (!config.proxy.port) throw new Error(`Missing field 'config.proxy.port'`);
if (!config.proxy.type) throw new Error(`Missing field 'config.proxy.type'`); config.proxy.port = validatePort(config.proxy.port);
} }
export async function socks(config: FileSystemConfig): Promise<NodeJS.ReadableStream> { export async function socks(config: FileSystemConfig): Promise<NodeJS.ReadableStream> {

@ -27,6 +27,14 @@ export async function catchingPromise<T>(executor: (resolve: (value?: T | Promis
}); });
} }
/** Converts the given number/string to a port number. Throws an error for invalid strings or ports outside the 1-65565 range */
export function validatePort(port: string | number): number {
const p = Number(port);
if (!Number.isInteger(p)) throw new Error(`Wanting to use non-int '${port}' as port`);
if (p < 0 || p > 65565) throw new Error(`Wanting to use port ${p} outside the 1-65565 range`);
return p;
}
const CLEAN_BASH_VALUE_REGEX = /^[\w-/\\]+$/; const CLEAN_BASH_VALUE_REGEX = /^[\w-/\\]+$/;
/** Based on way 1 in https://stackoverflow.com/a/20053121 */ /** Based on way 1 in https://stackoverflow.com/a/20053121 */
function escapeBashValue(value: string) { function escapeBashValue(value: string) {

Loading…
Cancel
Save