From eb7158cbd8c1d161f058bcea7cab05640141713e Mon Sep 17 00:00:00 2001 From: Jimmy Vo Date: Mon, 14 Apr 2025 12:59:21 -0400 Subject: [PATCH] [PM-19814] Add types to the handlers. --- .../background/phishing-detection.service.ts | 66 +++++++++++++------ 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/apps/browser/src/phishing-detection/background/phishing-detection.service.ts b/apps/browser/src/phishing-detection/background/phishing-detection.service.ts index 9748282e5d5..adfbe1c6876 100644 --- a/apps/browser/src/phishing-detection/background/phishing-detection.service.ts +++ b/apps/browser/src/phishing-detection/background/phishing-detection.service.ts @@ -9,6 +9,16 @@ import { TaskSchedulerService } from "@bitwarden/common/platform/scheduling/task import { PhishingDetectionCommands } from "../../phishing-detection/phishing-detection.enum"; import { BrowserApi } from "../../platform/browser/browser-api"; +export type RedirectMessage = { + command: string; + phishingHost: string; +}; + +export type CheckUrlMessage = { + command: string; + activeUrl: string; +}; + export class PhishingDetectionService { private static knownPhishingDomains = new Set(); private static lastUpdateTime: number = 0; @@ -215,35 +225,49 @@ export class PhishingDetectionService { This listener will check the URL when the tab has finished loading. */ static setupCheckUrlListener(): void { - BrowserApi.addListener(chrome.runtime.onMessage, async (message, sender, sendResponse) => { - if (message.command === PhishingDetectionCommands.CheckUrl) { - const { activeUrl } = message; + BrowserApi.addListener( + chrome.runtime.onMessage, + async ( + message: CheckUrlMessage, + _: chrome.runtime.MessageSender, + sendResponse: (response?: any) => void, + ) => { + if (message.command === PhishingDetectionCommands.CheckUrl) { + const { activeUrl } = message; - const result = { isPhishingDomain: PhishingDetectionService.checkUrl(activeUrl) }; + const result = { isPhishingDomain: PhishingDetectionService.checkUrl(activeUrl) }; - PhishingDetectionService.logService.debug("CheckUrl handler", { result, message }); - sendResponse(result); - } - }); + PhishingDetectionService.logService.debug("CheckUrl handler", { result, message }); + sendResponse(result); + } + }, + ); } static setupRedirectToWarningPageListener(): void { - BrowserApi.addListener(chrome.runtime.onMessage, async (message, sender, sendResponse) => { - if (message.command === PhishingDetectionCommands.RedirectToWarningPage) { - const phishingWarningPage = chrome.runtime.getURL( - "popup/index.html#/security/phishing-warning", - ); + BrowserApi.addListener( + chrome.runtime.onMessage, + async ( + message: RedirectMessage, + sender: chrome.runtime.MessageSender, + _: (response?: any) => void, + ) => { + if (message.command === PhishingDetectionCommands.RedirectToWarningPage) { + const phishingWarningPage = chrome.runtime.getURL( + "popup/index.html#/security/phishing-warning", + ); - const pageWithViewData = `${phishingWarningPage}?phishingHost=${message.phishingHost}`; + const pageWithViewData = `${phishingWarningPage}?phishingHost=${message.phishingHost}`; - PhishingDetectionService.logService.debug("RedirectToWarningPage handler", { - message, - phishingWarning: pageWithViewData, - }); + PhishingDetectionService.logService.debug("RedirectToWarningPage handler", { + message, + phishingWarning: pageWithViewData, + }); - await chrome.tabs.update(sender.tab.id, { url: pageWithViewData }); - } - }); + await chrome.tabs.update(sender.tab.id, { url: pageWithViewData }); + } + }, + ); } static setupListeners(): void {