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

fix: mobile perf issue #4871

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
104 changes: 104 additions & 0 deletions packages/phishing-controller/src/PhishingController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ import {
CLIENT_SIDE_DETECION_BASE_URL,
C2_DOMAIN_BLOCKLIST_ENDPOINT,
} from './PhishingController';
import type { DappScanResponse } from './PhishingDetector';
import {
DAPP_SCAN_API_BASE_URL,
DAPP_SCAN_ENDPOINT,
RecommendedAction,
} from './PhishingDetector';
import { formatHostnameToUrl } from './tests/utils';
import { PhishingDetectorResultType } from './types';
import { getHostnameFromUrl } from './utils';
Expand Down Expand Up @@ -2370,4 +2376,102 @@ describe('PhishingController', () => {
expect(controller.state.whitelist).toHaveLength(1);
});
});
describe('PhishingController - scanDomain', () => {
afterEach(() => {
nock.cleanAll();
});

it('should return safe if domain is in the allowlist', async () => {
const allowlistedDomain = 'example.com';

const controller = getPhishingController();

const result = await controller.scanDomain(allowlistedDomain);
expect(result).toMatchObject({
result: false,
type: PhishingDetectorResultType.RealTimeDappScan,
});
});

it('should return false if the URL is in the whitelist', async () => {
const whitelistedHostname = 'example.com';

const controller = getPhishingController();
controller.bypass(formatHostnameToUrl(whitelistedHostname));
const result = await controller.scanDomain(whitelistedHostname);
expect(result).toMatchObject({
result: false,
type: PhishingDetectorResultType.RealTimeDappScan,
});
});

it('should return malicious if the dApp scan API recommends BLOCK', async () => {
const domainToScan = 'malicious.com';

nock(DAPP_SCAN_API_BASE_URL)
.get(`${DAPP_SCAN_ENDPOINT}?url=${domainToScan}`)
.reply(200, {
domainName: domainToScan,
recommendedAction: RecommendedAction.Block,
riskFactors: [
{
type: 'DRAINER',
severity: 'CRITICAL',
message: 'Domain identified as a wallet drainer.',
},
],
verified: false,
status: 'COMPLETE',
} as DappScanResponse);

const controller = getPhishingController();

const result = await controller.scanDomain(domainToScan);
expect(result).toStrictEqual({
result: true,
type: PhishingDetectorResultType.RealTimeDappScan,
name: 'DappScan',
version: '1',
match: domainToScan,
});
});

it('should return safe if the dApp scan API recommends NONE', async () => {
const domainToScan = 'none.com';

nock(DAPP_SCAN_API_BASE_URL)
.get(`${DAPP_SCAN_ENDPOINT}?url=${domainToScan}`)
.reply(200, {
domainName: domainToScan,
recommendedAction: RecommendedAction.None,
riskFactors: [],
verified: true,
status: 'COMPLETE',
} as DappScanResponse);

const controller = getPhishingController();

const result = await controller.scanDomain(domainToScan);
expect(result).toMatchObject({
result: false,
type: PhishingDetectorResultType.RealTimeDappScan,
});
});

it('should return safe if the dApp scan API request fails', async () => {
const domainToScan = 'unreachable.com';

nock(DAPP_SCAN_API_BASE_URL)
.get(`${DAPP_SCAN_ENDPOINT}?url=${domainToScan}`)
.replyWithError('Network error');

const controller = getPhishingController();

const result = await controller.scanDomain(domainToScan);
expect(result).toMatchObject({
result: false,
type: PhishingDetectorResultType.RealTimeDappScan,
});
});
});
});
20 changes: 20 additions & 0 deletions packages/phishing-controller/src/PhishingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,26 @@ export class PhishingController extends BaseController<
return this.#detector.isMaliciousC2Domain(punycodeOrigin);
}

/**
* Determines if a given origin is unapproved or malicious.
*
* @param origin - Domain origin of a website.
* @returns A promise that resolves to the phishing detection result.
*/
async scanDomain(origin: string): Promise<PhishingDetectorResult> {
const punycodeOrigin = toASCII(origin);
const hostname = getHostnameFromUrl(punycodeOrigin);

if (this.state.whitelist.includes(hostname || punycodeOrigin)) {
return {
result: false,
type: PhishingDetectorResultType.RealTimeDappScan,
};
}

return this.#detector.scanDomain(punycodeOrigin);
}

/**
* Temporarily marks a given origin as approved.
*
Expand Down
232 changes: 232 additions & 0 deletions packages/phishing-controller/src/PhishingDetector.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import nock from 'nock';

import {
DAPP_SCAN_API_BASE_URL,
DAPP_SCAN_ENDPOINT,
PhishingDetector,
type PhishingDetectorOptions,
} from './PhishingDetector';
Expand Down Expand Up @@ -1782,6 +1786,234 @@ describe('PhishingDetector', () => {
},
);
});

describe('scanDomain', () => {
it('should return allowlist result if domain is in the allowlist', async () => {
const allowlistedDomain = 'example.com';

await withPhishingDetector(
[
{
allowlist: ['example.com'],
blocklist: [],
fuzzylist: [],
name: 'allowlist-config',
version: 1,
tolerance: 2,
},
],
async ({ detector }) => {
const result = await detector.scanDomain(allowlistedDomain);
expect(result).toStrictEqual({
match: 'example.com',
name: 'allowlist-config',
result: false,
type: PhishingDetectorResultType.Allowlist,
version: '1',
});
},
);
});

it('should return malicious result if dApp scan API recommends BLOCK', async () => {
const maliciousDomain = 'malicious.com';

nock(DAPP_SCAN_API_BASE_URL)
.get(`${DAPP_SCAN_ENDPOINT}?url=${maliciousDomain}`)
.reply(200, {
domainName: maliciousDomain,
recommendedAction: 'BLOCK',
riskFactors: [
{
type: 'DRAINER',
severity: 'CRITICAL',
message: 'Domain identified as a wallet drainer.',
},
],
verified: false,
status: 'COMPLETE',
});

await withPhishingDetector(
[
{
allowlist: [],
blocklist: [],
fuzzylist: [],
name: 'api-block-config',
version: 1,
tolerance: 2,
},
],
async ({ detector }) => {
const result = await detector.scanDomain(maliciousDomain);
expect(result).toStrictEqual({
result: true,
type: PhishingDetectorResultType.RealTimeDappScan,
name: 'DappScan',
version: '1',
match: maliciousDomain,
});
},
);

expect(nock.isDone()).toBe(true);
});

it('should return safe result if dApp scan API does not recommend BLOCK', async () => {
const safeDomain = 'safe.com';

nock(DAPP_SCAN_API_BASE_URL)
.get(`${DAPP_SCAN_ENDPOINT}?url=${safeDomain}`)
.reply(200, {
domainName: safeDomain,
recommendedAction: 'WARN',
riskFactors: [
{
type: 'SUSPICIOUS',
severity: 'MEDIUM',
message: 'Domain has suspicious activity.',
},
],
verified: true,
status: 'COMPLETE',
});

await withPhishingDetector(
[
{
allowlist: [],
blocklist: [],
fuzzylist: [],
name: 'api-warn-config',
version: 1,
tolerance: 2,
},
],
async ({ detector }) => {
const result = await detector.scanDomain(safeDomain);
expect(result).toStrictEqual({
result: false,
type: PhishingDetectorResultType.RealTimeDappScan,
});
},
);

expect(nock.isDone()).toBe(true);
});

it('should return safe result if dApp scan API responds with non-OK status', async () => {
const domain = 'unknown.com';

nock(DAPP_SCAN_API_BASE_URL)
.get(`${DAPP_SCAN_ENDPOINT}?url=${domain}`)
.reply(500, 'Internal Server Error');

const consoleErrorSpy = jest.spyOn(console, 'error');

await withPhishingDetector(
[
{
allowlist: [],
blocklist: [],
fuzzylist: [],
name: 'api-error-config',
version: 1,
tolerance: 2,
},
],
async ({ detector }) => {
const result = await detector.scanDomain(domain);
expect(result).toStrictEqual({
result: false,
type: PhishingDetectorResultType.RealTimeDappScan,
});
},
);

expect(consoleErrorSpy).toHaveBeenCalledWith(
'dApp Scan API error: 500 Internal Server Error',
);

consoleErrorSpy.mockRestore();

expect(nock.isDone()).toBe(true);
});

it('should return safe result if dApp scan API request fails due to network error', async () => {
const domain = 'network-error.com';

nock(DAPP_SCAN_API_BASE_URL)
.get(`${DAPP_SCAN_ENDPOINT}?url=${domain}`)
.replyWithError('Network Failure');

const consoleErrorSpy = jest.spyOn(console, 'error');

await withPhishingDetector(
[
{
allowlist: [],
blocklist: [],
fuzzylist: [],
name: 'api-network-error-config',
version: 1,
tolerance: 2,
},
],
async ({ detector }) => {
const result = await detector.scanDomain(domain);
expect(result).toStrictEqual({
result: false,
type: PhishingDetectorResultType.RealTimeDappScan,
});
},
);

expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringContaining('dApp Scan fetch error:'),
);

consoleErrorSpy.mockRestore();

expect(nock.isDone()).toBe(true);
});

it('should return safe result if domain is not in allowlist and API does not recommend BLOCK', async () => {
const domain = 'neutral.com';

nock(DAPP_SCAN_API_BASE_URL)
.get(`${DAPP_SCAN_ENDPOINT}?url=${domain}`)
.reply(200, {
domainName: domain,
recommendedAction: 'NONE',
riskFactors: [],
verified: true,
status: 'COMPLETE',
});

await withPhishingDetector(
[
{
allowlist: [],
blocklist: [],
fuzzylist: [],
name: 'api-none-config',
version: 1,
tolerance: 2,
},
],
async ({ detector }) => {
const result = await detector.scanDomain(domain);
expect(result).toStrictEqual({
result: false,
type: PhishingDetectorResultType.RealTimeDappScan,
});
},
);

expect(nock.isDone()).toBe(true);
});
});
});

type WithPhishingDetectorCallback<ReturnValue> = ({
Expand Down
Loading
Loading