From 9b9ff70b752a831b76793bb83949a7309c4d171f Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sat, 25 Mar 2023 16:01:10 +0100 Subject: [PATCH] util: fix isValidURL Signed-off-by: CrazyMax --- __tests__/util.test.ts | 5 ++++- src/util.ts | 7 ++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index 0b7812b..bda42d7 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -198,9 +198,12 @@ describe('isValidUrl', () => { test.each([ ['https://github.com/docker/buildx.git', true], ['https://github.com/docker/buildx.git#refs/pull/648/head', true], + ['git@github.com:moby/buildkit.git', false], + ['git://github.com/user/repo.git', false], + ['github.com/moby/buildkit.git#main', false], ['v0.4.1', false] ])('given %p', async (url, expected) => { - expect(Util.isValidUrl(url)).toEqual(expected); + expect(Util.isValidURL(url)).toEqual(expected); }); }); diff --git a/src/util.ts b/src/util.ts index 86c57cb..1c4766b 100644 --- a/src/util.ts +++ b/src/util.ts @@ -65,13 +65,14 @@ export class Util { } } - public static isValidUrl(url: string): boolean { + public static isValidURL(urlStr: string): boolean { + let url; try { - new URL(url); + url = new URL(urlStr); } catch (e) { return false; } - return true; + return url.protocol === 'http:' || url.protocol === 'https:'; } public static async powershellCommand(script: string, params?: Record) {