Skip to content

Commit

Permalink
fixes & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Aug 8, 2024
1 parent 260f1a1 commit 5713885
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 45 deletions.
42 changes: 0 additions & 42 deletions packages/remix/test/utils/getIpAddress.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/remix/test/utils/normalizeRemixRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ describe('normalizeRemixRequest', () => {
hostname: 'example.com',
href: 'https://example.com/api/json?id=123',
insecureHTTPParser: undefined,
ip: null,
method: 'GET',
originalUrl: 'https://example.com/api/json?id=123',
path: '/api/json?id=123',
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/vendor/getIpAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ function parseForwardedHeader(value: string | null | undefined): string | null {
* 255.255.255.255
* 10.1.1.1
* 0.0.0.0
* 2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5
*
* Rejects:
* 1.1.1.01
Expand All @@ -117,7 +118,6 @@ function parseForwardedHeader(value: string | null | undefined): string | null {
*/
function isIP(str: string): boolean {
const regex =
/^((?:[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])$/;

/(?:^(?: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,})?$)/;
return regex.test(str);
}
31 changes: 31 additions & 0 deletions packages/utils/test/requestdata.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type * as net from 'net';
import type { Event, PolymorphicRequest, TransactionSource, User } from '@sentry/types';
import { addRequestDataToEvent, extractPathForTransaction, extractRequestData } from '@sentry/utils';
import { getClientIPAddress } from '../src/vendor/getIpAddress';

describe('addRequestDataToEvent', () => {
let mockEvent: Event;
Expand Down Expand Up @@ -787,3 +788,33 @@ describe('extractPathForTransaction', () => {
expect(source).toEqual('route');
});
});

describe('getClientIPAddress', () => {
it.each([
[
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5,2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5, 141.101.69.35',
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5',
],
[
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5, 2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5, 141.101.69.35',
'2b01:cb19:8350:ed00:d0dd:fa5b:de31:8be5',
],
[
'2a01:cb19:8350:ed00:d0dd:INVALID_IP_ADDR:8be5,141.101.69.35,2a01:cb19:8350:ed00:d0dd:fa5b:de31:8be5',
'141.101.69.35',
],
[
'2b01:cb19:8350:ed00:d0dd:fa5b:nope:8be5, 2b01:cb19:NOPE:ed00:d0dd:fa5b:de31:8be5, 141.101.69.35 ',
'141.101.69.35',
],
['2b01:cb19:8350:ed00:d0 dd:fa5b:de31:8be5, 141.101.69.35', '141.101.69.35'],
])('should parse the IP from the X-Forwarded-For header %s', (headerValue, expectedIP) => {
const headers = {
'X-Forwarded-For': headerValue,
};

const ip = getClientIPAddress(headers);

expect(ip).toEqual(expectedIP);
});
});

0 comments on commit 5713885

Please sign in to comment.