Skip to content
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
10 changes: 10 additions & 0 deletions src/base-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ export function setupBaseMiddleware(ctx: AppContext, expressApp: Express) {
req.id = requestId;
res.setHeader(requestIdHeaderName, requestId);

const expressClientIpHeaderName = ctx.config.expressClientIpHeaderName?.toLowerCase();
if (expressClientIpHeaderName) {
const clientIp = req.headers[expressClientIpHeaderName] as string | undefined;
Copy link
Copy Markdown
Contributor

@resure resure Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Should probably add a few tests for this
  2. Wouldn't it be overridden by standard logic that fills req.ip based on the content of x-forwarded-for?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be overridden by standard logic that fills req.ip based on the content of x-forwarded-for?

express uses getter, we redefine it
I checked it works
https://github.com/expressjs/express/blob/e5099198b292a565f8583d70caf12d7afed3607f/lib/request.js#L340

I'll add some tests

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added tests

Object.defineProperty(req, 'ip', {
value: clientIp,
writable: true,
configurable: true,
});
}

req.routeInfo = {};

const startTime = Date.now();
Expand Down
122 changes: 122 additions & 0 deletions src/tests/client-ip-header.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {ExpressKit, Request, Response} from '..';
import {AppConfig, NodeKit} from '@gravity-ui/nodekit';
import request from 'supertest';

const APP_NAME = 'app';

const setupApp = ({config}: {config?: AppConfig} = {}) => {
const logger = {
write: () => {},
};

const nodekit = new NodeKit({
config: {
appLoggingDestination: logger,
appName: APP_NAME,
...config,
},
});

const routes = {
'GET /ip': {
handler: (req: Request, res: Response) => {
res.status(200);
res.send({ip: req.ip});
},
},
};

const app = new ExpressKit(nodekit, routes);

return {app, logger};
};

describe('expressClientIpHeaderName', () => {
it('should use default req.ip from X-Forwarded-For when expressClientIpHeaderName is not set', async () => {
const {app} = setupApp();

const forwardedIp = '198.51.100.25';

const agent = request.agent(app.express);

const response = await agent.get('/ip').set('X-Forwarded-For', forwardedIp);

expect(response.status).toBe(200);
expect(response.body.ip).toBe(forwardedIp);
});

it('should override req.ip with value from custom header when expressClientIpHeaderName is set', async () => {
const customIpHeader = 'X-Real-IP';
const customIp = '203.0.113.42';

const {app} = setupApp({
config: {
expressClientIpHeaderName: customIpHeader,
},
});

const agent = request.agent(app.express);

const response = await agent.get('/ip').set(customIpHeader, customIp);

expect(response.status).toBe(200);
expect(response.body.ip).toBe(customIp);
});

it('should be case-insensitive for header names', async () => {
const customIpHeader = 'X-Real-IP';
const customIp = '203.0.113.99';

const {app} = setupApp({
config: {
expressClientIpHeaderName: customIpHeader,
},
});

const agent = request.agent(app.express);

const response = await agent.get('/ip').set('x-real-ip', customIp);

expect(response.status).toBe(200);
expect(response.body.ip).toBe(customIp);
});

it('should set req.ip to undefined when custom header is not present in request', async () => {
const customIpHeader = 'X-Real-IP';

const {app} = setupApp({
config: {
expressClientIpHeaderName: customIpHeader,
},
});

const agent = request.agent(app.express);

const response = await agent.get('/ip');

expect(response.status).toBe(200);
expect(response.body.ip).toBeUndefined();
});

it('should use configured header when both x-forwarded-for and x-real-ip are present', async () => {
const customIpHeader = 'X-Real-IP';
const realIp = '203.0.113.50';
const forwardedIp = '198.51.100.25';

const {app} = setupApp({
config: {
expressClientIpHeaderName: customIpHeader,
},
});

const agent = request.agent(app.express);

const response = await agent
.get('/ip')
.set('X-Real-IP', realIp)
.set('X-Forwarded-For', forwardedIp);

expect(response.status).toBe(200);
expect(response.body.ip).toBe(realIp);
});
});
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ declare module '@gravity-ui/nodekit' {
expressTrustProxyNumber?: number | boolean;
expressCookieSecret?: string | string[];
expressRequestIdHeaderName?: string;
expressClientIpHeaderName?: string;

expressDisableBodyParserJSON?: boolean;
expressBodyParserJSONConfig?: bodyParser.OptionsJson;
Expand Down
Loading