From 92b562ca0ee0cb2040f78b99944a34bea6fc2c17 Mon Sep 17 00:00:00 2001 From: Jimmy Vo Date: Mon, 17 Mar 2025 15:42:19 -0400 Subject: [PATCH] Add communication between the content script and background service. --- .../browser/src/background/main.background.ts | 3 ++ .../phishing-detection-browser.service.ts | 18 --------- .../phishing-detection.enum.ts | 3 ++ ...ger-phishing-detection-script-injection.ts | 16 ++++++-- .../services/phishing-detection.service.ts | 39 ++++++------------- 5 files changed, 31 insertions(+), 48 deletions(-) create mode 100644 apps/browser/src/phishing-detection/phishing-detection.enum.ts diff --git a/apps/browser/src/background/main.background.ts b/apps/browser/src/background/main.background.ts index cd65220936e..a3758953134 100644 --- a/apps/browser/src/background/main.background.ts +++ b/apps/browser/src/background/main.background.ts @@ -272,6 +272,7 @@ import BrowserMemoryStorageService from "../platform/services/browser-memory-sto import { BrowserScriptInjectorService } from "../platform/services/browser-script-injector.service"; import I18nService from "../platform/services/i18n.service"; import { LocalBackedSessionStorageService } from "../platform/services/local-backed-session-storage.service"; +import { PhishingDetectionService } from "../platform/services/phishing-detection.service"; import { BackgroundPlatformUtilsService } from "../platform/services/platform-utils/background-platform-utils.service"; import { BrowserPlatformUtilsService } from "../platform/services/platform-utils/browser-platform-utils.service"; import { PopupViewCacheBackgroundService } from "../platform/services/popup-view-cache-background.service"; @@ -431,6 +432,8 @@ export default class MainBackground { } }; + PhishingDetectionService.setupCheckUrlListener(); + const logoutCallback = async (logoutReason: LogoutReason, userId?: UserId) => await this.logout(logoutReason, userId); diff --git a/apps/browser/src/phishing-detection/content/phishing-detection-browser.service.ts b/apps/browser/src/phishing-detection/content/phishing-detection-browser.service.ts index 9f4f9374a62..11822c5f33f 100644 --- a/apps/browser/src/phishing-detection/content/phishing-detection-browser.service.ts +++ b/apps/browser/src/phishing-detection/content/phishing-detection-browser.service.ts @@ -1,13 +1,6 @@ -import { Utils } from "@bitwarden/common/platform/misc/utils"; - export class PhishingDetectionBrowserService { private static knownPhishingDomains = new Set(); - static checkUrl(url: string): boolean { - const domain = Utils.getDomain(url); - return PhishingDetectionBrowserService.knownPhishingDomains.has(domain); - } - static notifyUser(url: string) { const phishingDivId = "phishing-notification-bar"; const message = `${url} is a known phishing site`; @@ -52,15 +45,4 @@ export class PhishingDetectionBrowserService { static getActiveUrl() { return window?.location?.href; } - - // @TODO: This can be remove once we implement the real code. - static loadMockedData() { - PhishingDetectionBrowserService.knownPhishingDomains.add("google.com"); - PhishingDetectionBrowserService.knownPhishingDomains.add("atlassian.net"); - PhishingDetectionBrowserService.knownPhishingDomains.add("example.com"); - PhishingDetectionBrowserService.knownPhishingDomains.add("w3schools.com"); - } } - -// Initializing the data for local development -PhishingDetectionBrowserService.loadMockedData(); diff --git a/apps/browser/src/phishing-detection/phishing-detection.enum.ts b/apps/browser/src/phishing-detection/phishing-detection.enum.ts new file mode 100644 index 00000000000..0bea27af8b9 --- /dev/null +++ b/apps/browser/src/phishing-detection/phishing-detection.enum.ts @@ -0,0 +1,3 @@ +export enum PhishingDetectionCommands { + CheckUrl = "CheckUrl", +} diff --git a/apps/browser/src/phishing-detection/trigger-phishing-detection-script-injection.ts b/apps/browser/src/phishing-detection/trigger-phishing-detection-script-injection.ts index 37ea922c880..e585657297a 100644 --- a/apps/browser/src/phishing-detection/trigger-phishing-detection-script-injection.ts +++ b/apps/browser/src/phishing-detection/trigger-phishing-detection-script-injection.ts @@ -1,5 +1,8 @@ -/* eslint-disable no-console */ + +import { Utils } from "@bitwarden/common/platform/misc/utils"; + import { PhishingDetectionBrowserService } from "./content/phishing-detection-browser.service"; +import { PhishingDetectionCommands } from "./phishing-detection.enum"; if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", loadPhishingDetectionContent); @@ -9,9 +12,16 @@ if (document.readyState === "loading") { async function loadPhishingDetectionContent() { const activeUrl = PhishingDetectionBrowserService.getActiveUrl(); - const isPhishingDomain = PhishingDetectionBrowserService.checkUrl(activeUrl); + + const { isPhishingDomain } = await chrome.runtime.sendMessage({ + command: PhishingDetectionCommands.CheckUrl, + activeUrl, + }); + if (isPhishingDomain) { - PhishingDetectionBrowserService.notifyUser(activeUrl); + const domain = Utils.getDomain(activeUrl); + + PhishingDetectionBrowserService.notifyUser(domain); } } diff --git a/apps/browser/src/platform/services/phishing-detection.service.ts b/apps/browser/src/platform/services/phishing-detection.service.ts index d4f7ade0fbe..b7de8c8125b 100644 --- a/apps/browser/src/platform/services/phishing-detection.service.ts +++ b/apps/browser/src/platform/services/phishing-detection.service.ts @@ -1,6 +1,7 @@ -/* eslint-disable no-console */ + import { Utils } from "@bitwarden/common/platform/misc/utils"; +import { PhishingDetectionCommands } from "../../phishing-detection/phishing-detection.enum"; import { BrowserApi } from "../browser/browser-api"; export class PhishingDetectionService { @@ -19,35 +20,19 @@ export class PhishingDetectionService { static loadMockedData() { PhishingDetectionService.knownPhishingDomains.add("google.com"); PhishingDetectionService.knownPhishingDomains.add("atlassian.net"); + PhishingDetectionService.knownPhishingDomains.add("example.com"); + PhishingDetectionService.knownPhishingDomains.add("w3schools.com"); } - static async getActiveUrl(): Promise { - const win = await BrowserApi.getCurrentWindow(); - const currentWindow = await BrowserApi.tabsQuery({ windowId: win.id, active: true }); + static setupCheckUrlListener(): void { + BrowserApi.addListener(chrome.runtime.onMessage, async (message, sender, sendResponse) => { + console.log("Jimmy addListener ", { message }); + if (message.command === PhishingDetectionCommands.CheckUrl) { + const { activeUrl } = message; - // @TODO: Account for cases with no active windows. - return currentWindow[0].url; - } - - // @TODO: WIP. We can have a pop-up or send a notification to other services. - static notifyUser(url: string) {} - - /* - This listener will check the URL when the tab has finished loading. - */ - setupTabEventListeners(): void { - BrowserApi.addListener(chrome.tabs.onUpdated, async (tabId, changeInfo, tab) => { - if (changeInfo.status === "complete") { - const activeUrl = await PhishingDetectionService.getActiveUrl(); - - // Debugging - console.log("Tab changed:", { tab, changeInfo, tabId }); - - const isPhishingDomain = PhishingDetectionService.checkUrl(activeUrl); - - if (isPhishingDomain) { - PhishingDetectionService.notifyUser(activeUrl); - } + const result = { isPhishingDomain: PhishingDetectionService.checkUrl(activeUrl) }; + console.log("Jimmy", result); + sendResponse(result); } }); }