diff --git a/lib/config.js b/lib/config.js index 5d389cd77..128fb927f 100644 --- a/lib/config.js +++ b/lib/config.js @@ -104,6 +104,11 @@ function Config(config) { } }).bind(this)) + // unique identifier of this server (or a cluster), used to avoid loops + if (!this.server_id) { + this.server_id = crypto.pseudoRandomBytes(6).toString('hex') + } + return this } diff --git a/lib/index.js b/lib/index.js index 0bc63fd24..2c8171efe 100644 --- a/lib/index.js +++ b/lib/index.js @@ -76,6 +76,7 @@ module.exports = function(config_hash) { // TODO: npm DO NOT support compression :( app.use(express.compress()) + app.use(Middleware.anti_loop(config)) app.param('package', validate_name) app.param('filename', validate_name) diff --git a/lib/middleware.js b/lib/middleware.js index 4d08b70d6..a715ad728 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -84,6 +84,24 @@ module.exports.basic_auth = function basic_auth(callback) { } } +module.exports.anti_loop = function(config) { + return function(req, res, next) { + if (req.headers.via != null) { + var arr = req.headers.via.split(',') + for (var i=0; i