Skip to content

feat: get req.ip from custom header#113

Merged
resure merged 5 commits intomainfrom
feat/get-req-ip-from-custom-header
Apr 7, 2026
Merged

feat: get req.ip from custom header#113
resure merged 5 commits intomainfrom
feat/get-req-ip-from-custom-header

Conversation

@stepanenkoxx
Copy link
Copy Markdown
Contributor

No description provided.

@stepanenkoxx stepanenkoxx marked this pull request as ready for review April 2, 2026 09:56

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

Comment on lines +101 to +118
it('should handle multiple IPs in X-Forwarded-For header', async () => {
const customIpHeader = 'X-Forwarded-For';
const multipleIps = '203.0.113.1, 198.51.100.2, 192.0.2.3';

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

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

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

expect(response.status).toBe(200);
expect(response.body.ip).toBe(multipleIps);
});
});
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.

don't know maybe in this case we need to split value and take 0 element
or maybe instead of header name we could pass a getter function

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.

it's clientIpHeader, so i think we must pass it as is actually
i'll remove this test case

@stepanenkoxx stepanenkoxx requested review from DakEnviy and resure April 7, 2026 12:50
@resure resure merged commit 0c8ce51 into main Apr 7, 2026
5 checks passed
@resure resure deleted the feat/get-req-ip-from-custom-header branch April 7, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants