Skip to content

Commit

Permalink
Fix trying to use localhost on bad Hippo URL (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
itowlson authored Sep 15, 2021
1 parent 2e5ba6b commit 0c8a99f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ts/generators/app/providers/hippo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions ts/generators/app/utils/url.ts
Original file line number Diff line number Diff line change
@@ -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:`);
}

0 comments on commit 0c8a99f

Please sign in to comment.