Compare commits
3 Commits
developmen
...
bugfix/S3C
Author | SHA1 | Date |
---|---|---|
Rahul Padigela | 125a57247b | |
Ilke | 4914a85f68 | |
Ilke | c76b36b7ef |
|
@ -9,23 +9,18 @@ const ipCheck = require('../ipCheck');
|
||||||
function getClientIp(request, s3config) {
|
function getClientIp(request, s3config) {
|
||||||
const clientIp = request.socket.remoteAddress;
|
const clientIp = request.socket.remoteAddress;
|
||||||
const requestConfig = s3config ? s3config.requests : {};
|
const requestConfig = s3config ? s3config.requests : {};
|
||||||
if (requestConfig && requestConfig.viaProxy) {
|
const { trustedProxyCIDRs, extractClientIPFromHeader } = requestConfig;
|
||||||
/**
|
/**
|
||||||
* if requests are configured to come via proxy,
|
* if requests are configured to come via proxy,
|
||||||
* check from config which proxies are to be trusted and
|
* check from config which proxies are to be trusted and
|
||||||
* which header to be used to extract client IP
|
* which header to be used to extract client IP
|
||||||
*/
|
*/
|
||||||
if (ipCheck.ipMatchCidrList(requestConfig.trustedProxyCIDRs,
|
if (ipCheck.ipMatchCidrList(trustedProxyCIDRs, clientIp)) {
|
||||||
clientIp)) {
|
const ipFromHeader = request.headers[extractClientIPFromHeader];
|
||||||
const ipFromHeader
|
if (ipFromHeader && ipFromHeader.trim().length) {
|
||||||
// eslint-disable-next-line operator-linebreak
|
return ipFromHeader.split(',')[0].trim();
|
||||||
= request.headers[requestConfig.extractClientIPFromHeader];
|
|
||||||
if (ipFromHeader && ipFromHeader.trim().length) {
|
|
||||||
return ipFromHeader.split(',')[0].trim();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return clientIp;
|
return clientIp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,9 +28,9 @@ describe('requestUtils.getClientIp', () => {
|
||||||
assert.strictEqual(result, testClientIp1);
|
assert.strictEqual(result, testClientIp1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not return client Ip address from header ' +
|
it('should extract client Ip address from x-forwarded-for header ' +
|
||||||
'if the request is not forwarded from proxies or ' +
|
'when the header is present and has valid ip address(es) if the ' +
|
||||||
'fails ip check', () => {
|
'request is not forwarded via proxy', () => {
|
||||||
const request = new DummyRequest({
|
const request = new DummyRequest({
|
||||||
headers: {
|
headers: {
|
||||||
'x-forwarded-for': [testClientIp1, testProxyIp].join(','),
|
'x-forwarded-for': [testClientIp1, testProxyIp].join(','),
|
||||||
|
@ -42,11 +42,11 @@ describe('requestUtils.getClientIp', () => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const result = requestUtils.getClientIp(request, configWithoutProxy);
|
const result = requestUtils.getClientIp(request, configWithoutProxy);
|
||||||
assert.strictEqual(result, testClientIp2);
|
assert.strictEqual(result, testClientIp1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not return client Ip address from header ' +
|
it('should not return client Ip address from header ' +
|
||||||
'if the request is forwarded from proxies, but the request' +
|
'if the request is forwarded from proxies, but the request ' +
|
||||||
'has no expected header or the header value is empty', () => {
|
'has no expected header or the header value is empty', () => {
|
||||||
const request = new DummyRequest({
|
const request = new DummyRequest({
|
||||||
headers: {
|
headers: {
|
||||||
|
|
Loading…
Reference in New Issue