From 3a2d89c9488976a2f07f97c2dbdb21e8837efe27 Mon Sep 17 00:00:00 2001 From: Jonathan Prusik Date: Tue, 15 Aug 2023 09:28:05 -0400 Subject: [PATCH] [PM-2597] Do not show the notification banner on the configured bitwarden vault domain (#5863) * ignore TLD when checking for no banner display on a vault page * do not show the notification banner on the configured bitwarden vault domain * add types --- .../src/autofill/content/notification-bar.ts | 110 ++++++++++-------- apps/browser/src/autofill/types/index.ts | 41 +++++++ 2 files changed, 100 insertions(+), 51 deletions(-) create mode 100644 apps/browser/src/autofill/types/index.ts diff --git a/apps/browser/src/autofill/content/notification-bar.ts b/apps/browser/src/autofill/content/notification-bar.ts index 41d8f14560..5bccd1d2a2 100644 --- a/apps/browser/src/autofill/content/notification-bar.ts +++ b/apps/browser/src/autofill/content/notification-bar.ts @@ -3,6 +3,7 @@ import ChangePasswordRuntimeMessage from "../../background/models/changePassword import AutofillField from "../models/autofill-field"; import { WatchedForm } from "../models/watched-form"; import { FormData } from "../services/abstractions/autofill.service"; +import { UserSettings } from "../types"; interface HTMLElementWithFormOpId extends HTMLElement { formOpId: string; @@ -26,10 +27,64 @@ interface HTMLElementWithFormOpId extends HTMLElement { * and async scripts to finish loading. * https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event */ -document.addEventListener("DOMContentLoaded", (event) => { - // Do not show the notification bar on the Bitwarden vault - // because they can add logins and change passwords there - if (window.location.hostname.endsWith("vault.bitwarden.com")) { +document.addEventListener("DOMContentLoaded", async (event) => { + // These are preferences for whether to show the notification bar based on the user's settings + // and they are set in the Settings > Options page in the browser extension. + let disabledAddLoginNotification = false; + let disabledChangedPasswordNotification = false; + let showNotificationBar = true; + + // Look up the active user id from storage + const activeUserIdKey = "activeUserId"; + let activeUserId: string; + await chrome.storage.local.get(activeUserIdKey, (obj: any) => { + if (obj == null || obj[activeUserIdKey] == null) { + return; + } + activeUserId = obj[activeUserIdKey]; + }); + + // Look up the user's settings from storage + await chrome.storage.local.get(activeUserId, (obj: any) => { + if (obj?.[activeUserId] == null) { + return; + } + + const userSettings: UserSettings = obj[activeUserId].settings; + + // Do not show the notification bar on the Bitwarden vault + // because they can add logins and change passwords there + if (window.location.origin === userSettings.serverConfig.environment.vault) { + showNotificationBar = false; + + return; + } + + // NeverDomains is a dictionary of domains that the user has chosen to never + // show the notification bar on (for login detail collection or password change). + // It is managed in the Settings > Excluded Domains page in the browser extension. + // Example: '{"bitwarden.com":null}' + const excludedDomainsDict = userSettings.neverDomains; + + if ( + excludedDomainsDict != null && + // eslint-disable-next-line + excludedDomainsDict.hasOwnProperty(window.location.hostname) + ) { + return; + } + + // Set local disabled preferences + disabledAddLoginNotification = userSettings.disableAddLoginNotification; + disabledChangedPasswordNotification = userSettings.disableChangedPasswordNotification; + + if (!disabledAddLoginNotification || !disabledChangedPasswordNotification) { + // If the user has not disabled both notifications, then handle the initial page change (null -> actual page) + handlePageChange(); + } + }); + + if (!showNotificationBar) { return; } @@ -77,53 +132,6 @@ document.addEventListener("DOMContentLoaded", (event) => { ]); const changePasswordButtonContainsNames = new Set(["pass", "change", "contras", "senha"]); - // These are preferences for whether to show the notification bar based on the user's settings - // and they are set in the Settings > Options page in the browser extension. - let disabledAddLoginNotification = false; - let disabledChangedPasswordNotification = false; - - // Look up the active user id from storage - const activeUserIdKey = "activeUserId"; - let activeUserId: string; - chrome.storage.local.get(activeUserIdKey, (obj: any) => { - if (obj == null || obj[activeUserIdKey] == null) { - return; - } - activeUserId = obj[activeUserIdKey]; - }); - - // Look up the user's settings from storage - chrome.storage.local.get(activeUserId, (obj: any) => { - if (obj?.[activeUserId] == null) { - return; - } - - const userSettings = obj[activeUserId].settings; - - // NeverDomains is a dictionary of domains that the user has chosen to never - // show the notification bar on (for login detail collection or password change). - // It is managed in the Settings > Excluded Domains page in the browser extension. - // Example: '{"bitwarden.com":null}' - const excludedDomainsDict = userSettings.neverDomains; - - if ( - excludedDomainsDict != null && - // eslint-disable-next-line - excludedDomainsDict.hasOwnProperty(window.location.hostname) - ) { - return; - } - - // Set local disabled preferences - disabledAddLoginNotification = userSettings.disableAddLoginNotification; - disabledChangedPasswordNotification = userSettings.disableChangedPasswordNotification; - - if (!disabledAddLoginNotification || !disabledChangedPasswordNotification) { - // If the user has not disabled both notifications, then handle the initial page change (null -> actual page) - handlePageChange(); - } - }); - // Message Processing // Listen for messages from the background script diff --git a/apps/browser/src/autofill/types/index.ts b/apps/browser/src/autofill/types/index.ts new file mode 100644 index 0000000000..d689132535 --- /dev/null +++ b/apps/browser/src/autofill/types/index.ts @@ -0,0 +1,41 @@ +import { Region } from "@bitwarden/common/platform/abstractions/environment.service"; +import { VaultTimeoutAction } from "@bitwarden/common/src/enums/vault-timeout-action.enum"; + +export type UserSettings = { + avatarColor: string | null; + environmentUrls: { + api: string | null; + base: string | null; + events: string | null; + icons: string | null; + identity: string | null; + keyConnector: string | null; + notifications: string | null; + webVault: string | null; + }; + pinProtected: { [key: string]: any }; + region: Region; + serverConfig: { + environment: { + api: string | null; + cloudRegion: string | null; + identity: string | null; + notifications: string | null; + sso: string | null; + vault: string | null; + }; + featureStates: { [key: string]: any }; + gitHash: string; + server: { [key: string]: any }; + utcDate: string; + version: string; + }; + settings: { + equivalentDomains: string[][]; + }; + neverDomains?: { [key: string]: any }; + disableAddLoginNotification?: boolean; + disableChangedPasswordNotification?: boolean; + vaultTimeout: number; + vaultTimeoutAction: VaultTimeoutAction; +};