From 0c8a99f6bc7101f04da966e7058643e1633d7a91 Mon Sep 17 00:00:00 2001 From: itowlson Date: Thu, 16 Sep 2021 08:58:05 +1200 Subject: [PATCH] Fix trying to use localhost on bad Hippo URL (#69) --- ts/generators/app/providers/hippo.ts | 2 ++ ts/generators/app/utils/url.ts | 15 +++++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 ts/generators/app/utils/url.ts diff --git a/ts/generators/app/providers/hippo.ts b/ts/generators/app/providers/hippo.ts index 16ac884..e459d25 100644 --- a/ts/generators/app/providers/hippo.ts +++ b/ts/generators/app/providers/hippo.ts @@ -5,6 +5,7 @@ import { HippoClient } from "hippo-js"; import { Registry } from "./registry"; import { Formatter } from '../formatter'; +import { validateUrl } from '../utils/url'; export const hippo: Registry = { prompts() { @@ -113,6 +114,7 @@ export const hippo: Registry = { try { const { hippoUrl, hippoUsername, hippoPassword } = answers; + validateUrl(hippoUrl); const agent = new https.Agent({ rejectUnauthorized: false }); const client = await withErrorContext("Login failed", () => HippoClient.new(hippoUrl, hippoUsername, hippoPassword, agent) diff --git a/ts/generators/app/utils/url.ts b/ts/generators/app/utils/url.ts new file mode 100644 index 0000000..33a0841 --- /dev/null +++ b/ts/generators/app/utils/url.ts @@ -0,0 +1,15 @@ +import { URL } from 'url'; + +export function validateUrl(candidate: string) { + try { + const url = new URL(candidate); + if (url.protocol === 'http:' || url.protocol === 'https:') { + return; + } + } catch (e) { + if (e instanceof TypeError) { + throw new Error(e.message); + } + } + throw new Error(`Invalid URL ${candidate}: protocol must be http: or https:`); +}