Skip to content

Commit

Permalink
sanitize x-forwarded-for header (#7122)
Browse files Browse the repository at this point in the history
Many proxies (NGINX included) will generate a list of IPs in v4 and
v6 formats.  The forwarded-for library is a well-maintained library for
express that sanitizes, checks, and validates trusted proxy IPs.  This helps
eliminate XSS or other attempts to inject invalid material through the
x-forwarded-for header.

Co-authored-by: Sulka Haro <[email protected]>
  • Loading branch information
bewest and sulkaharo authored Oct 24, 2021
1 parent bc8e319 commit 6250612
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 13 deletions.
4 changes: 3 additions & 1 deletion lib/api/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

function configure (app, wares, env, ctx) {
var express = require('express'),
forwarded = require('forwarded-for'),
api = express.Router( )
;

Expand All @@ -21,7 +22,8 @@ function configure (app, wares, env, ctx) {
var authToken = req.query.token || req.query.secret || '';

function getRemoteIP (req) {
return req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const address = forwarded(req, req.headers);
return address.ip;
}

var date = new Date();
Expand Down
4 changes: 3 additions & 1 deletion lib/api3/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const apiConst = require('./const.json')
, _ = require('lodash')
, shiroTrie = require('shiro-trie')
, opTools = require('./shared/operationTools')
, forwarded = require('forwarded-for')
;


function getRemoteIP (req) {
return req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const address = forwarded(req, req.headers);
return address.ip;
}


Expand Down
6 changes: 4 additions & 2 deletions lib/api3/storageSocket.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const apiConst = require('./const');
const forwarded = require('forwarded-for');

/**
* Socket.IO broadcaster of any storage change
Expand Down Expand Up @@ -28,7 +29,8 @@ function StorageSocket (app, env, ctx) {
self.namespace = io.of(NAMESPACE);
self.namespace.on('connection', function onConnected (socket) {

const remoteIP = socket.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
const address = forwarded(socket.request, socket.request.headers);
const remoteIP = address.ip;
console.log(LOG + 'Connection from client ID: ', socket.client.id, ' IP: ', remoteIP);

socket.on('disconnect', function onDisconnect () {
Expand Down Expand Up @@ -142,4 +144,4 @@ function StorageSocket (app, env, ctx) {
}
}

module.exports = StorageSocket;
module.exports = StorageSocket;
4 changes: 3 additions & 1 deletion lib/authorization/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const shiroTrie = require('shiro-trie');

const consts = require('./../constants');
const sleep = require('util').promisify(setTimeout);
const forwarded = require('forwarded-for');

function getRemoteIP (req) {
return req.headers['x-forwarded-for'] || req.connection.remoteAddress;
const address = forwarded(req, req.headers);
return address.ip;
}

function init (env, ctx) {
Expand Down
4 changes: 3 additions & 1 deletion lib/server/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var times = require('../times');
var calcData = require('../data/calcdelta');
var ObjectID = require('mongodb').ObjectID;
const forwarded = require('forwarded-for');

function init (env, ctx, server) {

Expand Down Expand Up @@ -127,7 +128,8 @@ function init (env, ctx, server) {
var timeDiff;
var history;

var remoteIP = socket.request.headers['x-forwarded-for'] || socket.request.connection.remoteAddress;
const address = forwarded(socket.request, socket.request.headers);
const remoteIP = address.ip;
console.log(LOG_WS + 'Connection from client ID: ', socket.client.id, ' IP: ', remoteIP);

io.emit('clients', ++watchers);
Expand Down
19 changes: 12 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"fast-password-entropy": "^1.1.1",
"file-loader": "^6.2.0",
"flot": "^0.8.3",
"forwarded-for": "^1.1.0",
"helmet": "^4.0.0",
"jquery": "^3.5.1",
"jquery-ui-bundle": "^1.12.1-migrate",
Expand Down

0 comments on commit 6250612

Please sign in to comment.