Skip to content

feat: get scan from cache #6023

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions packages/phishing-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Added `getScanResultFromCache` which allows getting scan results from the cache directly. ([#6023](https://github.com/MetaMask/core/pull/6023))

### Changed

- **BREAKING**`scanUrl` hits the v2 endpoint now. Returns `hostname` instead of `domainName` now. ([#5981](https://github.com/MetaMask/core/pull/5981))
Expand Down
32 changes: 32 additions & 0 deletions packages/phishing-controller/src/PhishingController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3025,6 +3025,38 @@ describe('PhishingController', () => {
expect(nock.pendingMocks()).toHaveLength(0);
});
});

describe('getScanResultFromCache', () => {
it('returns the cached result for URLs that are in the cache', async () => {
const controller = getPhishingController();
const result = controller.getScanResultFromCache('https://example.com');
expect(result).toBeUndefined();
nock(PHISHING_DETECTION_BASE_URL)
.get(
`/${PHISHING_DETECTION_SCAN_ENDPOINT}?url=${encodeURIComponent('example.com')}`,
)
.reply(200, {
recommendedAction: RecommendedAction.None,
});

await controller.scanUrl('https://example.com');

const result2 = controller.getScanResultFromCache('https://example.com');
expect(result2).toBeDefined();
});

it('returns undefined for invalid URLs', async () => {
const controller = getPhishingController();
const result = controller.getScanResultFromCache('not-a-url');
expect(result).toBeUndefined();
});

it('returns undefined for URLs that are not in the cache', async () => {
const controller = getPhishingController();
const result = controller.getScanResultFromCache('https://example.com');
expect(result).toBeUndefined();
});
});
});

describe('URL Scan Cache', () => {
Expand Down
16 changes: 16 additions & 0 deletions packages/phishing-controller/src/PhishingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,22 @@ export class PhishingController extends BaseController<
return combinedResponse;
};

/**
* Get a cached result if it exists and is not expired
*
* @param url - The URL to get the cached result for. You can pass in a full URL or just the hostname.
* @returns The cached scan result or undefined if not found or expired
*/
getScanResultFromCache = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not need this method at all... any caller to the phishing controller API can just rely on the scanUrl method that is already checking the internal cache before doing any network request to the real time dapp scanning API?

url: string,
): PhishingDetectionScanResult | undefined => {
const [hostname, ok] = getHostnameFromWebUrl(url);
if (!ok) {
return undefined;
}
return this.#urlScanCache.get(hostname);
};

/**
* Process a batch of URLs (up to 50) for phishing detection.
*
Expand Down
Loading