Bläddra i källkod

Fix IP address filtering when behind a proxy

Gaël Métais 7 år sedan
förälder
incheckning
95659839b2
1 ändrade filer med 7 tillägg och 5 borttagningar
  1. 7 5
      lib/server/middlewares/apiLimitsMiddleware.js

+ 7 - 5
lib/server/middlewares/apiLimitsMiddleware.js

@@ -6,25 +6,27 @@ var debug = require('debug')('apiLimitsMiddleware');
 var apiLimitsMiddleware = function(req, res, next) {
     'use strict';
 
-    debug('Entering API Limits Middleware with IP address %s', req.connection.remoteAddress);
+    var ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
+
+    debug('Entering API Limits Middleware with IP address %s', ipAddress);
 
     if (req.path.indexOf('/api/') === 0 && !res.locals.hasApiKey) {
         
         
         if (req.path === '/api/runs') {
             
-            if (!runsTable.accepts(req.connection.remoteAddress)) {
+            if (!runsTable.accepts(ipAddress)) {
                 // Sorry :/
-                debug('Too many tests launched from IP address %s', req.connection.remoteAddress);
+                debug('Too many tests launched from IP address %s', ipAddress);
                 res.status(429).send('Too many requests');
                 return;
             }
 
         }
 
-        if (!callsTable.accepts(req.connection.remoteAddress)) {
+        if (!callsTable.accepts(ipAddress)) {
             // Sorry :/
-            debug('Too many API requests from IP address %s', req.connection.remoteAddress);
+            debug('Too many API requests from IP address %s', ipAddress);
             res.status(429).send('Too many requests');
             return;
         }