From 225a89c2afbbe53cf39ffa7ea0cd485095a1d5f5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 21 Dec 2017 09:56:59 +0100 Subject: Sanitize url to not end with implicit ports --- server/helpers/core-utils.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'server/helpers/core-utils.ts') diff --git a/server/helpers/core-utils.ts b/server/helpers/core-utils.ts index 443115336..0c6c36d11 100644 --- a/server/helpers/core-utils.ts +++ b/server/helpers/core-utils.ts @@ -11,6 +11,26 @@ import * as mkdirp from 'mkdirp' import { join } from 'path' import * as pem from 'pem' import * as rimraf from 'rimraf' +import { URL } from 'url' + +function sanitizeUrl (url: string) { + const urlObject = new URL(url) + + if (urlObject.protocol === 'https:' && urlObject.port === '443') { + urlObject.port = '' + } else if (urlObject.protocol === 'http:' && urlObject.port === '80') { + urlObject.port = '' + } + + return urlObject.href.replace(/\/$/, '') +} + +// Don't import remote scheme from constants because we are in core utils +function sanitizeHost (host: string, remoteScheme: string) { + let toRemove = remoteScheme === 'https' ? 443 : 80 + + return host.replace(new RegExp(`:${toRemove}$`), '') +} function isTestInstance () { return process.env.NODE_ENV === 'test' @@ -114,6 +134,8 @@ export { root, escapeHTML, pageToStartAndCount, + sanitizeUrl, + sanitizeHost, promisify0, promisify1, -- cgit v1.2.3