Skip to content

Commit 5713885

Browse files
committed
fixes & tests
1 parent 260f1a1 commit 5713885

File tree

4 files changed

+33
-45
lines changed

4 files changed

+33
-45
lines changed

packages/remix/test/utils/getIpAddress.test.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

packages/remix/test/utils/normalizeRemixRequest.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ describe('normalizeRemixRequest', () => {
8383
hostname: 'example.com',
8484
href: 'https://example.com/api/json?id=123',
8585
insecureHTTPParser: undefined,
86-
ip: null,
8786
method: 'GET',
8887
originalUrl: 'https://example.com/api/json?id=123',
8988
path: '/api/json?id=123',

packages/utils/src/vendor/getIpAddress.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ function parseForwardedHeader(value: string | null | undefined): string | null {
104104
* 255.255.255.255
105105
* 10.1.1.1
106106
* 0.0.0.0
107+
* 2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5
107108
*
108109
* Rejects:
109110
* 1.1.1.01
@@ -117,7 +118,6 @@ function parseForwardedHeader(value: string | null | undefined): string | null {
117118
*/
118119
function isIP(str: string): boolean {
119120
const regex =
120-
/^((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/;
121-
121+
/(?:^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$)|(?:^(?:(?:[a-fA-F\d]{1,4}:){7}(?:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,2}|:)|(?:[a-fA-F\d]{1,4}:){4}(?:(?::[a-fA-F\d]{1,4}){0,1}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,3}|:)|(?:[a-fA-F\d]{1,4}:){3}(?:(?::[a-fA-F\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,4}|:)|(?:[a-fA-F\d]{1,4}:){2}(?:(?::[a-fA-F\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,5}|:)|(?:[a-fA-F\d]{1,4}:){1}(?:(?::[a-fA-F\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,6}|:)|(?::(?:(?::[a-fA-F\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,7}|:)))(?:%[0-9a-zA-Z]{1,})?$)/;
122122
return regex.test(str);
123123
}

packages/utils/test/requestdata.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type * as net from 'net';
22
import type { Event, PolymorphicRequest, TransactionSource, User } from '@sentry/types';
33
import { addRequestDataToEvent, extractPathForTransaction, extractRequestData } from '@sentry/utils';
4+
import { getClientIPAddress } from '../src/vendor/getIpAddress';
45

56
describe('addRequestDataToEvent', () => {
67
let mockEvent: Event;
@@ -787,3 +788,33 @@ describe('extractPathForTransaction', () => {
787788
expect(source).toEqual('route');
788789
});
789790
});
791+
792+
describe('getClientIPAddress', () => {
793+
it.each([
794+
[
795+
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5,2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5, 141.101.69.35',
796+
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5',
797+
],
798+
[
799+
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5, 2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5, 141.101.69.35',
800+
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5',
801+
],
802+
[
803+
'2a01:cb19:8350:ed00:d0dd:INVALID_IP_ADDR:8be5,141.101.69.35,2a01:cb19:8350:ed00:d0dd:fa5b:de31:8be5',
804+
'141.101.69.35',
805+
],
806+
[
807+
'2b01:cb19:8350:ed00:d0dd:fa5b:nope:8be5, 2b01:cb19:NOPE:ed00:d0dd:fa5b:de31:8be5, 141.101.69.35 ',
808+
'141.101.69.35',
809+
],
810+
['2b01:cb19:8350:ed00:d0 dd:fa5b:de31:8be5, 141.101.69.35', '141.101.69.35'],
811+
])('should parse the IP from the X-Forwarded-For header %s', (headerValue, expectedIP) => {
812+
const headers = {
813+
'X-Forwarded-For': headerValue,
814+
};
815+
816+
const ip = getClientIPAddress(headers);
817+
818+
expect(ip).toEqual(expectedIP);
819+
});
820+
});

0 commit comments

Comments
 (0)