Skip to content

Commit 5ee765f

Browse files
authored
Merge pull request #49 from nknapp/pr-request-forward-loop
Prevent cycles when forwarding requests
2 parents cb480c5 + b6be592 commit 5ee765f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

lib/proxy.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ exports.opts = {};
2222
// Port or socket path of internal MITM server.
2323
var mitmAddress;
2424

25+
// Random header to prevent sending requests in a cycle
26+
var cycleCheckHeader = 'x-npm-proxy-cache-' + Math.round(Math.random() * 10000)
2527

2628
exports.powerup = function(opts) {
2729

@@ -86,12 +88,20 @@ exports.httpHandler = function(req, res) {
8688
schema = req.client.pair || req.connection.encrypted ? 'https' : 'http',
8789
dest = schema + '://' + req.headers['host'] + path;
8890

91+
if (req.headers[cycleCheckHeader]) {
92+
res.writeHead(502)
93+
res.end('Sending requests to myself. Stopping to prevent cycles.')
94+
return
95+
}
96+
8997
var params = {
9098
headers: {},
9199
rejectUnauthorized: false,
92100
url: dest
93101
};
94102

103+
params.headers[cycleCheckHeader] = 1
104+
95105
// Carry following headers down to dest npm repository.
96106
var carryHeaders = ['authorization', 'version', 'referer', 'npm-session', 'user-agent'];
97107
carryHeaders.forEach(function(name) {

0 commit comments

Comments
 (0)