diff --git a/lib/proxy.js b/lib/proxy.js index 92e3df4..4b6b3e2 100644 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -22,6 +22,8 @@ exports.opts = {}; // Port or socket path of internal MITM server. var mitmAddress; +// Random header to prevent sending requests in a cycle +var cycleCheckHeader = 'x-npm-proxy-cache-' + Math.round(Math.random() * 10000) exports.powerup = function(opts) { @@ -86,12 +88,20 @@ exports.httpHandler = function(req, res) { schema = req.client.pair || req.connection.encrypted ? 'https' : 'http', dest = schema + '://' + req.headers['host'] + path; + if (req.headers[cycleCheckHeader]) { + res.writeHead(502) + res.end('Sending requests to myself. Stopping to prevent cycles.') + return + } + var params = { headers: {}, rejectUnauthorized: false, url: dest }; + params.headers[cycleCheckHeader] = 1 + // Carry following headers down to dest npm repository. var carryHeaders = ['authorization', 'version', 'referer', 'npm-session', 'user-agent']; carryHeaders.forEach(function(name) {