Skip to content

Commit

Permalink
Merge branch 'w/8.1/bugfix/ARSN-453-fix-ipchecks' into tmp/octopus/w/…
Browse files Browse the repository at this point in the history
…8.2/bugfix/ARSN-453-fix-ipchecks
  • Loading branch information
bert-e committed Dec 26, 2024
2 parents c49fe0d + a0927dc commit 81ee00a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
13 changes: 6 additions & 7 deletions lib/policyEvaluator/requestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ export interface S3Config {
}
}

// TODO
// I'm not sure about this behavior.
// Should it returns string | string[] | undefined or string ?
/**
* getClientIp - Gets the client IP from the request
* @param request - http request object
Expand All @@ -20,8 +17,7 @@ export interface S3Config {
export function getClientIp(request: IncomingMessage, s3config?: S3Config): string {
const requestConfig = s3config?.requests;
const remoteAddress = request.socket.remoteAddress;
// TODO What to do if clientIp === undefined ?
const clientIp = (requestConfig ? remoteAddress : request.headers['x-forwarded-for'] || remoteAddress)?.toString() ?? '';
const clientIp = remoteAddress?.toString() ?? '';
if (requestConfig) {
const { trustedProxyCIDRs, extractClientIPFromHeader } = requestConfig;
/**
Expand All @@ -30,11 +26,14 @@ export function getClientIp(request: IncomingMessage, s3config?: S3Config): stri
* which header to be used to extract client IP
*/
if (ipCheck.ipMatchCidrList(trustedProxyCIDRs, clientIp)) {
const ipFromHeader = request.headers[extractClientIPFromHeader]?.toString();
// Request headers in nodejs are lower-cased, so we should not
// be case-sentive when looking for the header, as http headers
// are case-insensitive.
const ipFromHeader = request.headers[extractClientIPFromHeader.toLowerCase()]?.toString();
if (ipFromHeader && ipFromHeader.trim().length) {
return ipFromHeader.split(',')[0].trim();
}
}
}
return clientIp?.toString() ?? '';
return clientIp;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"engines": {
"node": ">=16"
},
"version": "8.1.143",
"version": "8.1.144",
"description": "Common utilities for the S3 project components",
"main": "build/index.js",
"repository": {
Expand Down
27 changes: 24 additions & 3 deletions tests/unit/policyEvaluator/requestUtils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ describe('requestUtils.getClientIp', () => {
assert.strictEqual(result, testClientIp1);
});

it('should return client Ip address in the proxy case when the header has uppercases', () => {
const request = new DummyRequest({
headers: {
'x-forwarded-for': [testClientIp1, testProxyIp].join(','),
},
url: '/',
parsedHost: 'localhost',
socket: {
remoteAddress: testProxyIp,
},
});
const result = requestUtils.getClientIp(request, {
requests: {
viaProxy: true,
trustedProxyCIDRs: ['192.168.100.0/22'],
extractClientIPFromHeader: 'X-Forwarded-For',
},
});
assert.strictEqual(result, testClientIp1);
});

it('should return client Ip address from socket info if the request is not forwarded from proxies', () => {
const request = new DummyRequest({
headers: {},
Expand Down Expand Up @@ -56,8 +77,8 @@ describe('requestUtils.getClientIp', () => {
assert.strictEqual(result, testClientIp2);
});

it('should return client Ip address from header if the request comes via proxies and ' +
'no request config is available', () => {
it('should not return client Ip address from header if the request comes via proxies and ' +
'no request config is available as the proxy is not trusted', () => {
const request = new DummyRequest({
headers: {
'x-forwarded-for': testClientIp1,
Expand All @@ -69,7 +90,7 @@ describe('requestUtils.getClientIp', () => {
},
});
const result = requestUtils.getClientIp(request, configWithoutProxy);
assert.strictEqual(result, testClientIp1);
assert.strictEqual(result, testProxyIp);
});

it('should return client Ip address from socket info if the request comes via proxies and ' +
Expand Down

0 comments on commit 81ee00a

Please sign in to comment.