Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

fix(extension): dismiss autofill popup on shadow-host outside clicks

AmarBegob0d7b974ca61
diff --git a/apps/browser-extension/src/entrypoints/contentScript/Popup.ts b/apps/browser-extension/src/entrypoints/contentScript/Popup.ts
index 92de8f747..78ebb763f 100644
--- a/apps/browser-extension/src/entrypoints/contentScript/Popup.ts
+++ b/apps/browser-extension/src/entrypoints/contentScript/Popup.ts
@@ -105,6 +105,22 @@ const createSuggestedNameSpan = (name: string): HTMLElement => {
return span;
};
+/**
+ * Check if an outside-click event originated from AliasVault UI controls.
+ * Shadow DOM events retarget to the host, so use the composed path instead of
+ * only checking event.target.
+ */
+function isClickInsidePopupUi(event: MouseEvent, popup: Element, input: HTMLInputElement): boolean {
+ const eventPath = event.composedPath();
+
+ return eventPath.includes(popup) ||
+ eventPath.includes(input) ||
+ eventPath.some((pathTarget) => (
+ pathTarget instanceof Element &&
+ pathTarget.closest('.av-input-icon') !== null
+ ));
+}
+
/**
* Open (or refresh) the autofill popup including check if vault is locked.
* @param input - The input element that triggered the popup
@@ -380,19 +396,14 @@ async function createTotpPopup(input: HTMLInputElement, items: Item[] | undefine
*/
const handleClickOutside = (event: MouseEvent) : void => {
const popupElement = rootContainer.querySelector('#aliasvault-credential-popup');
- const target = event.target as Node;
- const targetElement = event.target as HTMLElement;
// If popup doesn't exist, remove the listener
if (!popupElement) {
document.removeEventListener('mousedown', handleClickOutside);
return;
}
- // Check if the click is on the AliasVault icon
- const isIconClick = targetElement.closest('.av-input-icon') !== null;
-
- // Check if the click is outside the popup and outside the shadow UI
- if (popupElement && !popupElement.contains(target) && !input.contains(target) && !isIconClick && targetElement.tagName !== 'ALIASVAULT-UI') {
+ // Check if the click is outside the popup and outside the input/icon UI.
+ if (!isClickInsidePopupUi(event, popupElement, input)) {
removeExistingPopup(rootContainer);
}
};
@@ -1112,19 +1123,14 @@ export async function createAutofillPopup(input: HTMLInputElement, items: Item[]
*/
const handleClickOutside = (event: MouseEvent) : void => {
const popup = rootContainer.querySelector('#aliasvault-credential-popup');
- const target = event.target as Node;
- const targetElement = event.target as HTMLElement;
// If popup doesn't exist, remove the listener
if (!popup) {
document.removeEventListener('mousedown', handleClickOutside);
return;
}
- // Check if the click is on the AliasVault icon
- const isIconClick = targetElement.closest('.av-input-icon') !== null;
-
- // Check if the click is outside the popup and outside the shadow UI
- if (popup && !popup.contains(target) && !input.contains(target) && !isIconClick && targetElement.tagName !== 'ALIASVAULT-UI') {
+ // Check if the click is outside the popup and outside the input/icon UI.
+ if (!isClickInsidePopupUi(event, popup, input)) {
removeExistingPopup(rootContainer);
}
};
@@ -1208,14 +1214,8 @@ export async function createVaultLockedPopup(input: HTMLInputElement, rootContai
* Add event listener to document to close popup when clicking outside.
*/
const handleClickOutside = (event: MouseEvent): void => {
- const target = event.target as Node;
- const targetElement = event.target as HTMLElement;
-
- // Check if the click is on the AliasVault icon
- const isIconClick = targetElement.closest('.av-input-icon') !== null;
-
- // Check if the click is outside the popup and outside the shadow UI
- if (popup && !popup.contains(target) && !input.contains(target) && !isIconClick && targetElement.tagName !== 'ALIASVAULT-UI') {
+ // Check if the click is outside the popup and outside the input/icon UI.
+ if (!isClickInsidePopupUi(event, popup, input)) {
removeExistingPopup(rootContainer);
document.removeEventListener('mousedown', handleClickOutside);
}
@@ -2635,14 +2635,8 @@ export async function createUpgradeRequiredPopup(input: HTMLInputElement, rootCo
* Add event listener to document to close popup when clicking outside.
*/
const handleClickOutside = (event: MouseEvent): void => {
- const target = event.target as Node;
- const targetElement = event.target as HTMLElement;
-
- // Check if the click is on the AliasVault icon
- const isIconClick = targetElement.closest('.av-input-icon') !== null;
-
- // Check if the click is outside the popup and outside the shadow UI
- if (popup && !popup.contains(target) && !input.contains(target) && !isIconClick && targetElement.tagName !== 'ALIASVAULT-UI') {
+ // Check if the click is outside the popup and outside the input/icon UI.
+ if (!isClickInsidePopupUi(event, popup, input)) {
removeExistingPopup(rootContainer);
document.removeEventListener('mousedown', handleClickOutside);
}