Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore ip address flag #29

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const defaultConfig = {
remoteConfigFetchEndpoint: '/config',
telemetryEndpoint: '/telemetry',
allowLocalUrls: false,
allowIpAddresses: false,
logRequestHeaders: true,
logRequestBody: true,
logResponseHeaders: true,
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const Supergood = () => {
const interceptorOpts = {
ignoredDomains: supergoodConfig.ignoredDomains,
allowLocalUrls: supergoodConfig.allowLocalUrls,
allowIpAddresses: supergoodConfig.allowIpAddresses,
baseUrl
};

Expand Down
3 changes: 2 additions & 1 deletion src/interceptor/FetchInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class FetchInterceptor extends Interceptor {
url: new URL(request.url),
ignoredDomains: this.options.ignoredDomains ?? [],
baseUrl: this.options.baseUrl ?? '',
allowLocalUrls: this.options.allowLocalUrls ?? false
allowLocalUrls: this.options.allowLocalUrls ?? false,
allowIpAddresses: this.options.allowIpAddresses ?? false,
});

if (_isInterceptable) {
Expand Down
1 change: 1 addition & 0 deletions src/interceptor/Interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EventEmitter } from 'events';
export interface NodeRequestInterceptorOptions {
ignoredDomains?: string[];
allowLocalUrls?: boolean;
allowIpAddresses?: boolean;
baseUrl?: string;
}

Expand Down
4 changes: 3 additions & 1 deletion src/interceptor/NodeClientRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type NodeClientOptions = {
allowLocalUrls: boolean;
baseUrl?: string;
ignoredDomains?: string[];
allowIpAddresses: boolean;
};

export type Protocol = 'http' | 'https';
Expand Down Expand Up @@ -49,7 +50,8 @@ export class NodeClientRequest extends ClientRequest {
url: this.url,
ignoredDomains: options.ignoredDomains ?? [],
baseUrl: options.baseUrl ?? '',
allowLocalUrls: options.allowLocalUrls ?? false
allowLocalUrls: options.allowLocalUrls ?? false,
allowIpAddresses: options.allowIpAddresses ?? false
});
}

Expand Down
1 change: 1 addition & 0 deletions src/interceptor/NodeRequestInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class NodeRequestInterceptor extends Interceptor {
emitter: this.emitter,
ignoredDomains: this.options.ignoredDomains,
allowLocalUrls: this.options.allowLocalUrls,
allowIpAddresses: this.options.allowIpAddresses,
baseUrl: this.options.baseUrl
};

Expand Down
25 changes: 19 additions & 6 deletions src/interceptor/utils/isInterceptable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ describe('isInterceptable', () => {
const ignoredDomains: string[] = [];
const baseUrl = 'https://api.supergood.ai';
const allowLocalUrls = false;
const allowIpAddresses = false;

const result = isInterceptable({
url,
ignoredDomains,
baseUrl,
allowLocalUrls
allowLocalUrls,
allowIpAddresses,
});

expect(result).toBe(false);
Expand All @@ -22,12 +24,15 @@ describe('isInterceptable', () => {
const ignoredDomains: string[] = [];
const baseUrl = 'https://api.supergood.ai';
const allowLocalUrls = false;
const allowIpAddresses = false;


const result = isInterceptable({
url,
ignoredDomains,
baseUrl,
allowLocalUrls
allowLocalUrls,
allowIpAddresses,
});

expect(result).toBe(false);
Expand All @@ -38,12 +43,14 @@ describe('isInterceptable', () => {
const ignoredDomains: string[] = [];
const baseUrl = 'https://api.supergood.ai';
const allowLocalUrls = true;
const allowIpAddresses = false;

const result = isInterceptable({
url,
ignoredDomains,
baseUrl,
allowLocalUrls
allowLocalUrls,
allowIpAddresses
});

expect(result).toBe(true);
Expand All @@ -54,12 +61,14 @@ describe('isInterceptable', () => {
const ignoredDomains: string[] = [];
const baseUrl = 'https://api.supergood.ai';
const allowLocalUrls = false;
const allowIpAddresses = false;

const result = isInterceptable({
url,
ignoredDomains,
baseUrl,
allowLocalUrls
allowLocalUrls,
allowIpAddresses
});

expect(result).toBe(false);
Expand All @@ -70,12 +79,14 @@ describe('isInterceptable', () => {
const ignoredDomains: string[] = [];
const baseUrl = 'https://api.supergood.ai';
const allowLocalUrls = true;
const allowIpAddresses = false;

const result = isInterceptable({
url,
ignoredDomains,
baseUrl,
allowLocalUrls
allowLocalUrls,
allowIpAddresses
});

expect(result).toBe(true);
Expand All @@ -86,12 +97,14 @@ describe('isInterceptable', () => {
const ignoredDomains: string[] = ['somedomain.com'];
const baseUrl = 'https://api.supergood.ai';
const allowLocalUrls = false;
const allowIpAddresses = false;

const result = isInterceptable({
url,
ignoredDomains,
baseUrl,
allowLocalUrls
allowLocalUrls,
allowIpAddresses
});

expect(result).toBe(false);
Expand Down
10 changes: 8 additions & 2 deletions src/interceptor/utils/isInterceptable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export function isInterceptable({
url,
ignoredDomains,
baseUrl,
allowLocalUrls
allowLocalUrls,
allowIpAddresses
}: {
url: URL;
ignoredDomains: string[];
baseUrl: string;
allowLocalUrls: boolean;
allowIpAddresses: boolean;
}): boolean {
const { origin: baseOrigin } = new URL(baseUrl);
const hostname = url.hostname;
Expand All @@ -24,7 +26,11 @@ export function isInterceptable({
return false;
}

if (!hostname && !allowLocalUrls) {
if (!allowIpAddresses && /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(hostname)) {
return false;
}

if (!allowLocalUrls && !hostname) {
return false;
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface ConfigType {
remoteConfigFetchInterval: number;
ignoredDomains: string[];
allowLocalUrls: boolean;
allowIpAddresses: boolean;
cacheTtl: number;
keysToHash: string[];
remoteConfigFetchEndpoint: string; // Defaults to {baseUrl}/config if not provided
Expand Down
69 changes: 17 additions & 52 deletions test/e2e/core.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,97 +118,62 @@ describe('core functionality', () => {
});
});

xdescribe('config specifications', () => {
test('hashing', async () => {
describe('config specifications', () => {
it('should ignore requests to ignored domains', async () => {
await Supergood.init(
{
config: {
keysToHash: ['response.body'],
ignoredDomains: ['supergood-testbed.herokuapp.com'],
allowLocalUrls: true
},
clientId: SUPERGOOD_CLIENT_ID,
clientSecret: SUPERGOOD_CLIENT_SECRET
},
SUPERGOOD_SERVER
);
await axios.get(`${MOCK_DATA_SERVER}/posts`);
await Supergood.close();

checkPostedEvents(postEventsMock, 1, {
response: expect.objectContaining({
body: expect.arrayContaining([expect.stringMatching(BASE64_REGEX)])
})
});
});

it('should not hash anything', async () => {
await Supergood.init(
{
config: { keysToHash: [], allowLocalUrls: true },
clientId: SUPERGOOD_CLIENT_ID,
clientSecret: SUPERGOOD_CLIENT_SECRET
},
SUPERGOOD_SERVER
);
const response = await axios.get(`${MOCK_DATA_SERVER}/posts`);
await axios.get('https://supergood-testbed.herokuapp.com/200');
await Supergood.close();
expect(postEventsMock).not.toHaveBeenCalled();
}, 10000);

checkPostedEvents(postEventsMock, 1, {
response: expect.objectContaining({
body: response.data
})
});
});

it('should not hash if provided keys do not exist', async () => {
it('should operate normally when ignored domains is empty', async () => {
await Supergood.init(
{
config: {
keysToHash: ['thisKeyDoesNotExist', 'response.thisKeyDoesNotExist'],
allowLocalUrls: true
},
config: { ignoredDomains: [], allowLocalUrls: true },
clientId: SUPERGOOD_CLIENT_ID,
clientSecret: SUPERGOOD_CLIENT_SECRET
},
SUPERGOOD_SERVER
);
const response = await axios.get(`${MOCK_DATA_SERVER}/posts`);
await axios.get('https://supergood-testbed.herokuapp.com/200');
await Supergood.close();
expect(postEventsMock).toHaveBeenCalled();
}, 10000);

checkPostedEvents(postEventsMock, 1, {
response: expect.objectContaining({
body: response.data
})
});
});

it('should ignore requests to ignored domains', async () => {
it('should ignore IP addresses by default', async () => {
await Supergood.init(
{
config: {
ignoredDomains: ['supergood-testbed.herokuapp.com'],
allowLocalUrls: true
},
config: { ignoredDomains: [], allowLocalUrls: true },
clientId: SUPERGOOD_CLIENT_ID,
clientSecret: SUPERGOOD_CLIENT_SECRET
},
SUPERGOOD_SERVER
);
await axios.get('https://supergood-testbed.herokuapp.com/200');
const response = await fetch('http://13.107.4.52/');
await Supergood.close();
expect(postEventsMock).not.toHaveBeenCalled();
}, 10000);

it('should operate normally when ignored domains is empty', async () => {
it('should not ignore IP addresses if specified', async () => {
await Supergood.init(
{
config: { ignoredDomains: [], allowLocalUrls: true },
config: { ignoredDomains: [], allowLocalUrls: true, allowIpAddresses: true },
clientId: SUPERGOOD_CLIENT_ID,
clientSecret: SUPERGOOD_CLIENT_SECRET
},
SUPERGOOD_SERVER
);
await axios.get('https://supergood-testbed.herokuapp.com/200');
const response = await fetch('http://13.107.4.52/');
await Supergood.close();
expect(postEventsMock).toHaveBeenCalled();
}, 10000);
Expand Down
Loading