Skip to content

Commit

Permalink
Merge pull request #2094 from transloadit/fix-http
Browse files Browse the repository at this point in the history
companion: return the right httpAgent when protocol value contains ":"
  • Loading branch information
ifedapoolarewaju committed Feb 27, 2020
2 parents c575036 + 9b17cf2 commit 7525440
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/@uppy/companion/src/companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions packages/@uppy/companion/src/server/helpers/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions packages/@uppy/companion/test/__tests__/http-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 7525440

Please sign in to comment.