Skip to content

Node.js based forum software built for the modern web

refactor: one client ip helper for both transports (#14844)

palmoni554ae3cc5f87e
diff --git a/src/api/helpers.js b/src/api/helpers.js
index 109549e7e8..c496fb5d47 100644
--- a/src/api/helpers.js
+++ b/src/api/helpers.js
@@ -10,11 +10,12 @@ const utils = require('../utils');
const socketHelpers = require('../socket.io/helpers');
const websockets = require('../socket.io');
const events = require('../events');
+const clientIp = require('../ip');
exports.setDefaultPostData = function (req, data) {
data.uid = req.uid;
data.req = exports.buildReqObject(req, { ...data });
- data.ip = req.ip;
+ data.ip = clientIp.getClientIp(req);
data.timestamp = Date.now();
data.fromQueue = false;
};
diff --git a/src/controllers/write/intents.js b/src/controllers/write/intents.js
index a5f36158e2..ab4a8de694 100644
--- a/src/controllers/write/intents.js
+++ b/src/controllers/write/intents.js
@@ -8,6 +8,7 @@ const activitypub = require('../../activitypub');
const meta = require('../../meta');
const helpers = require('../helpers');
+const clientIp = require('../../ip');
const RATE_LIMIT_WINDOW = 5000; // 5 seconds
@@ -26,7 +27,7 @@ Intents.query = async (req, res, next) => {
return next();
}
- const ip = req.ip || req.connection.remoteAddress;
+ const ip = clientIp.getClientIp(req);
await checkRateLimit(ip);
let { handle } = req.params;
diff --git a/src/socket.io/utils/ip.js b/src/ip.js
similarity index 100%
rename from src/socket.io/utils/ip.js
rename to src/ip.js
diff --git a/src/routes/feeds.js b/src/routes/feeds.js
index 224ed26a9b..9e0f927522 100644
--- a/src/routes/feeds.js
+++ b/src/routes/feeds.js
@@ -13,6 +13,7 @@ const privileges = require('../privileges');
const db = require('../database');
const utils = require('../utils');
const controllers404 = require('../controllers/404');
+const clientIp = require('../ip');
const routeHelpers = require('./helpers');
const { terms } = controllerHelpers;
@@ -432,8 +433,7 @@ async function generateForTag(req, res) {
}
function getRssTokenRateLimitKey(req) {
- const ip = req.ip || req.connection.remoteAddress;
- return `rss:token:fail:${ip}`;
+ return `rss:token:fail:${clientIp.getClientIp(req)}`;
}
async function getUidFromToken(req, res) {
diff --git a/src/socket.io/index.js b/src/socket.io/index.js
index 8173423082..df8aefd490 100644
--- a/src/socket.io/index.js
+++ b/src/socket.io/index.js
@@ -14,7 +14,7 @@ const ratelimit = require('../middleware/ratelimit');
const blacklist = require('../meta/blacklist');
const als = require('../als');
const apiHelpers = require('../api/helpers');
-const socketIp = require('./utils/ip');
+const socketIp = require('../ip');
const Namespaces = Object.create(null);
diff --git a/test/socket.io.ip.js b/test/ip.js
similarity index 63%
rename from test/socket.io.ip.js
rename to test/ip.js
index 50a9e8987a..2005dbaacb 100644
--- a/test/socket.io.ip.js
+++ b/test/ip.js
@@ -6,9 +6,9 @@ const nconf = require('nconf');
const db = require('./mocks/databasemock');
-const socketIp = require('../src/socket.io/utils/ip');
+const clientIp = require('../src/ip');
-describe('socket.io ip utils', () => {
+describe('client ip', () => {
let originalTrustProxy;
before(() => {
@@ -21,7 +21,7 @@ describe('socket.io ip utils', () => {
it('should use remoteAddress when trust_proxy is false', () => {
nconf.set('trust_proxy', false);
- const ip = socketIp.getClientIp({
+ const ip = clientIp.getClientIp({
headers: {
'x-forwarded-for': '203.0.113.1, 198.51.100.1',
},
@@ -35,7 +35,7 @@ describe('socket.io ip utils', () => {
it('should use forwarded client address when trust_proxy is true', () => {
nconf.set('trust_proxy', true);
- const ip = socketIp.getClientIp({
+ const ip = clientIp.getClientIp({
headers: {
'x-forwarded-for': '203.0.113.1, 198.51.100.1',
},
@@ -49,7 +49,7 @@ describe('socket.io ip utils', () => {
it('should honor numeric trust proxy hop counts', () => {
nconf.set('trust_proxy', 1);
- const ip = socketIp.getClientIp({
+ const ip = clientIp.getClientIp({
headers: {
'x-forwarded-for': '203.0.113.1, 198.51.100.1',
},
@@ -63,7 +63,7 @@ describe('socket.io ip utils', () => {
it('should fall back to remoteAddress when forwarded chain is invalid', () => {
nconf.set('trust_proxy', true);
- const ip = socketIp.getClientIp({
+ const ip = clientIp.getClientIp({
headers: {
'x-forwarded-for': '<script>alert("xss")</script>',
},
@@ -77,6 +77,30 @@ describe('socket.io ip utils', () => {
it('should return empty string without request', () => {
nconf.set('trust_proxy', false);
- assert.strictEqual(socketIp.getClientIp(null), '');
+ assert.strictEqual(clientIp.getClientIp(null), '');
+ });
+
+ it('should normalize IPv4-mapped IPv6 addresses', () => {
+ nconf.set('trust_proxy', false);
+ const ip = clientIp.getClientIp({
+ headers: {},
+ socket: { remoteAddress: '::ffff:192.0.2.1' },
+ });
+
+ assert.strictEqual(ip, '192.0.2.1');
+ });
+
+ it('should record a normalized ip on write api payloads', () => {
+ const apiHelpers = require('../src/api/helpers');
+ nconf.set('trust_proxy', false);
+ const data = {};
+ apiHelpers.setDefaultPostData({
+ uid: 1,
+ headers: {},
+ ip: '::ffff:192.0.2.1',
+ socket: { remoteAddress: '::ffff:192.0.2.1' },
+ }, data);
+
+ assert.strictEqual(data.ip, '192.0.2.1');
});
});