diff --git a/src/backend/tests/utilitiesTests/networking.test.ts b/src/backend/tests/utilitiesTests/networking.test.ts index 3b8ad26d..f7702bda 100644 --- a/src/backend/tests/utilitiesTests/networking.test.ts +++ b/src/backend/tests/utilitiesTests/networking.test.ts @@ -139,6 +139,10 @@ describe('URL Parsing', function () { expect(normalizeWebAddress(`https://${domain}`).port).to.eq(443); }); + it('Should normalize a domain without TLD to http', function () { + expect(normalizeWebAddress('myDomain').url.protocol).to.eq('http:'); + }); + }); describe('With Port', function () { @@ -158,6 +162,12 @@ describe('URL Parsing', function () { expect(normalizeWebAddress(`https://${domain}:1055`).url.protocol).to.eq('https:'); }); + it('Should normalize a domain without TLD but with port to that port', function () { + const normal = normalizeWebAddress('myDomain:881'); + expect(normal.url.protocol).to.eq('http:'); + expect(normal.port).to.eq(881); + }); + }); }); diff --git a/src/backend/utils/NetworkUtils.ts b/src/backend/utils/NetworkUtils.ts index d02854c0..5c64d7d9 100644 --- a/src/backend/utils/NetworkUtils.ts +++ b/src/backend/utils/NetworkUtils.ts @@ -90,6 +90,8 @@ export const isPortReachableConnect = async (port: number, opts: PortReachableOp } const QUOTES_UNWRAP_REGEX: RegExp = new RegExp(/^"(.*)"$/); +const DOMAIN_AND_PORT: RegExp = new RegExp(/^([^:]+):(\d+)$/); +const commonProtocols = ['http','https','ws','wss']; export const normalizeWebAddress = (val: string, options: {defaultPath?: string, removeTrailingSlash?: boolean} = {}): URLData => { let cleanUserUrl = val.trim(); @@ -101,6 +103,18 @@ export const normalizeWebAddress = (val: string, options: {defaultPath?: string, const {defaultPath, removeTrailingSlash = true} = options; let normal = normalizeUrl(cleanUserUrl, {removeTrailingSlash}); + if(normal === cleanUserUrl) { + // checking to see if input was DOMAIN:PORT + // in which case we also check DOMAIN isn't mistakenly a protocol + // and if it isn't then we force a protocol based on port + // so that we get a full URL out of this function + const res = parseRegexSingle(DOMAIN_AND_PORT, cleanUserUrl); + if(res !== undefined && !commonProtocols.includes(res.groups[0])) { + const protocol = Number.parseInt(res.groups[1]) === 443 ? 'https:' : 'http:'; + cleanUserUrl = `${protocol}//${cleanUserUrl}`; + normal = normalizeUrl(cleanUserUrl, {removeTrailingSlash}); + } + } const u = new URL(normal); let port: number;