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

Hotfix: Prevent SSRF #3410

Merged
merged 6 commits into from Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 28 additions & 10 deletions lib/adapters/http.js
Expand Up @@ -16,6 +16,33 @@ var enhanceError = require('../core/enhanceError');

var isHttps = /https:?/;

/**
*
* @param {http.ClientRequestArgs} options
* @param {AxiosProxyConfig} proxy
* @param {string} location
* @returns {http.ClientRequestArgs}
timemachine3030 marked this conversation as resolved.
Show resolved Hide resolved
*/
function setProxy(options, proxy, location) {
options.hostname = proxy.host;
options.host = proxy.host;
options.port = proxy.port;
options.path = location;

// Basic proxy authorization
if (proxy.auth) {
var base64 = Buffer.from(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');
options.headers['Proxy-Authorization'] = 'Basic ' + base64;
}

// If a proxy is used any redirects must also pass through the proxy
timemachine3030 marked this conversation as resolved.
Show resolved Hide resolved
options.beforeRedirect = function beforeRedirect(redirection) {
redirection.headers.host = redirection.host;
Object.assign(redirection, setProxy(redirection, proxy, redirection.href));
timemachine3030 marked this conversation as resolved.
Show resolved Hide resolved
};
return options;
}

/*eslint consistent-return:0*/
module.exports = function httpAdapter(config) {
return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
Expand Down Expand Up @@ -145,17 +172,8 @@ module.exports = function httpAdapter(config) {
}

if (proxy) {
options.hostname = proxy.host;
options.host = proxy.host;
options.headers.host = parsed.hostname + (parsed.port ? ':' + parsed.port : '');
jasonsaayman marked this conversation as resolved.
Show resolved Hide resolved
options.port = proxy.port;
options.path = protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path;

// Basic proxy authorization
if (proxy.auth) {
var base64 = Buffer.from(proxy.auth.username + ':' + proxy.auth.password, 'utf8').toString('base64');
options.headers['Proxy-Authorization'] = 'Basic ' + base64;
}
options = setProxy(options, proxy, protocol + '//' + parsed.hostname + (parsed.port ? ':' + parsed.port : '') + options.path);
}

var transport;
Expand Down
61 changes: 61 additions & 0 deletions test/unit/regression/SNYK-JS-AXIOS-1038255.js
@@ -0,0 +1,61 @@
// https://snyk.io/vuln/SNYK-JS-AXIOS-1038255
// https://github.com/axios/axios/issues/3407
// https://github.com/axios/axios/issues/3369
timemachine3030 marked this conversation as resolved.
Show resolved Hide resolved

const axios = require('../../../index');
const http = require('http');
const assert = require('assert');

const PROXY_PORT = 4777;
const EVIL_PORT = 4666;


describe('Server-Side Request Forgery (SSRF)', () => {
let fail = false;
let proxy;
let server;
let location;
beforeEach(() => {
server = http.createServer(function (req, res) {
fail = true;
res.end('rm -rf /');
}).listen(EVIL_PORT);
proxy = http.createServer(function (req, res) {
if (req.url === 'http://localhost:' + EVIL_PORT + '/') {
return res.end(JSON.stringify({
msg: 'Protected',
headers: req.headers,
}));
}
res.writeHead(302, { location })
res.end()
}).listen(PROXY_PORT);
});
afterEach(() => {
server.close();
proxy.close();
});
it('obeys proxy settings when following redirects', async () => {
location = 'http://localhost:' + EVIL_PORT;
let response = await axios({
method: "get",
url: "http://www.google.com/",
proxy: {
host: "localhost",
port: PROXY_PORT,
auth: {
username: 'sam',
password: 'password',
}
},
});

assert.strictEqual(fail, false);
assert.strictEqual(response.data.msg, 'Protected');
assert.strictEqual(response.data.headers.host, 'localhost:' + EVIL_PORT);
assert.strictEqual(response.data.headers['proxy-authorization'], 'Basic ' + Buffer.from('sam:password').toString('base64'));

return response;

});
});