From e07d6d0b389eb8dcd553778aea570c6a082843f9 Mon Sep 17 00:00:00 2001 From: Joel Griffith Date: Wed, 18 Nov 2020 08:19:41 -0800 Subject: [PATCH] Derive the port from the protocol in cases where it's not explicitly stated (#83583) --- .../chromium/driver/chromium_driver.ts | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/reporting/server/browsers/chromium/driver/chromium_driver.ts b/x-pack/plugins/reporting/server/browsers/chromium/driver/chromium_driver.ts index 500185cd7e14f..5a1cdfe867590 100644 --- a/x-pack/plugins/reporting/server/browsers/chromium/driver/chromium_driver.ts +++ b/x-pack/plugins/reporting/server/browsers/chromium/driver/chromium_driver.ts @@ -333,13 +333,32 @@ export class HeadlessChromiumDriver { private _shouldUseCustomHeaders(conditions: ConditionalHeadersConditions, url: string) { const { hostname, protocol, port, pathname } = parseUrl(url); - if (port === null) throw new Error(`URL missing port: ${url}`); + // `port` is null in URLs that don't explicitly state it, + // however we can derive the port from the protocol (http/https) + // IE: https://feeds-staging.elastic.co/kibana/v8.0.0.json + const derivedPort = (() => { + if (port) { + return port; + } + + if (protocol === 'http:') { + return '80'; + } + + if (protocol === 'https:') { + return '443'; + } + + return null; + })(); + + if (derivedPort === null) throw new Error(`URL missing port: ${url}`); if (pathname === null) throw new Error(`URL missing pathname: ${url}`); return ( hostname === conditions.hostname && protocol === `${conditions.protocol}:` && - this._shouldUseCustomHeadersForPort(conditions, port) && + this._shouldUseCustomHeadersForPort(conditions, derivedPort) && pathname.startsWith(`${conditions.basePath}/`) ); }