Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

companion: return the right httpAgent when protocol value contains ":" #2094

Merged
merged 1 commit into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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