Skip to content

Commit

Permalink
Merge pull request #49 from nknapp/pr-request-forward-loop
Browse files Browse the repository at this point in the history
Prevent cycles when forwarding requests
  • Loading branch information
runk authored Mar 31, 2017
2 parents cb480c5 + b6be592 commit 5ee765f
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5ee765f

Please sign in to comment.