Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sanitize x-forwarded-for header #7122

Merged
merged 2 commits into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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