From 9b17cf203711ac89984832f2efe921beb7513419 Mon Sep 17 00:00:00 2001 From: ifedapoolarewaju Date: Thu, 27 Feb 2020 12:58:48 +0100 Subject: [PATCH] companion: return the right httpAgent when protocol value contains ":" --- packages/@uppy/companion/src/companion.js | 1 + .../companion/src/server/helpers/request.js | 6 ++-- .../companion/test/__tests__/http-agent.js | 28 +++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/@uppy/companion/src/companion.js b/packages/@uppy/companion/src/companion.js index 6814aca2c3..789a9292b3 100644 --- a/packages/@uppy/companion/src/companion.js +++ b/packages/@uppy/companion/src/companion.js @@ -251,6 +251,7 @@ const getOptionsMiddleware = (options) => { buildURL: getURLBuilder(options) } + logger.info(`uppy client version ${req.companion.clientVersion}`, 'companion.client.version') // @todo remove req.uppy in next major release req.uppy = req.companion next() diff --git a/packages/@uppy/companion/src/server/helpers/request.js b/packages/@uppy/companion/src/server/helpers/request.js index 241f753391..890a0a6bc5 100644 --- a/packages/@uppy/companion/src/server/helpers/request.js +++ b/packages/@uppy/companion/src/server/helpers/request.js @@ -77,15 +77,15 @@ module.exports.FORBIDDEN_IP_ADDRESS = FORBIDDEN_IP_ADDRESS /** * Returns http Agent that will prevent requests to private IPs (to preven SSRF) - * @param {string} protocol http or https protocol needed for the request + * @param {string} protocol http or http: or https: or https protocol needed for the request * @param {boolean} blockPrivateIPs if set to false, this protection will be disabled */ module.exports.getProtectedHttpAgent = (protocol, blockPrivateIPs) => { if (blockPrivateIPs) { - return protocol === 'https' ? HttpsAgent : HttpAgent + return protocol.startsWith('https') ? HttpsAgent : HttpAgent } - return protocol === 'https' ? https.Agent : http.Agent + return protocol.startsWith('https') ? https.Agent : http.Agent } function dnsLookup (hostname, options, callback) { diff --git a/packages/@uppy/companion/test/__tests__/http-agent.js b/packages/@uppy/companion/test/__tests__/http-agent.js index 4fa4e30280..f7e2ca42ba 100644 --- a/packages/@uppy/companion/test/__tests__/http-agent.js +++ b/packages/@uppy/companion/test/__tests__/http-agent.js @@ -2,6 +2,34 @@ const { getProtectedHttpAgent, FORBIDDEN_IP_ADDRESS } = require('../../src/server/helpers/request') const request = require('request') +const http = require('http') +const https = require('https') + +describe('test getProtectedHttpAgent', () => { + test('setting "https:" as protocol', (done) => { + const Agent = getProtectedHttpAgent('https:') + expect(Agent).toEqual(https.Agent) + done() + }) + + test('setting "https" as protocol', (done) => { + const Agent = getProtectedHttpAgent('https') + expect(Agent).toEqual(https.Agent) + done() + }) + + test('setting "http:" as protocol', (done) => { + const Agent = getProtectedHttpAgent('http:') + expect(Agent).toEqual(http.Agent) + done() + }) + + test('setting "http" as protocol', (done) => { + const Agent = getProtectedHttpAgent('http') + expect(Agent).toEqual(http.Agent) + done() + }) +}) describe('test protected request Agent', () => { test('allows URLs without IP addresses', (done) => {