mirror of
https://github.com/bitwarden/browser
synced 2026-02-10 05:30:01 +00:00
Add communication between the content script and background service.
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export enum PhishingDetectionCommands {
|
||||
CheckUrl = "CheckUrl",
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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<string> {
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user