Skip to content

Commit e7059b7

Browse files
jprusikdiffersthecat
authored andcommitted
[PM-24936] Prevent inline menu inheritance of potentially dangerous opacity from host body and above (#16063)
* prevent inline menu inheritance of dangerous opacity from host body and above * cleanup and update tests * check page opacity after html/body attribute mutations * update tests * cleanup
1 parent f157d8c commit e7059b7

File tree

2 files changed

+98
-3
lines changed

2 files changed

+98
-3
lines changed

apps/browser/src/autofill/overlay/inline-menu/content/autofill-inline-menu-content.service.spec.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe("AutofillInlineMenuContentService", () => {
2929
autofillInit = new AutofillInit(
3030
domQueryService,
3131
domElementVisibilityService,
32-
null,
32+
undefined,
3333
autofillInlineMenuContentService,
3434
);
3535
autofillInit.init();
@@ -319,6 +319,8 @@ describe("AutofillInlineMenuContentService", () => {
319319

320320
describe("handleContainerElementMutationObserverUpdate", () => {
321321
let mockMutationRecord: MockProxy<MutationRecord>;
322+
let mockBodyMutationRecord: MockProxy<MutationRecord>;
323+
let mockHTMLMutationRecord: MockProxy<MutationRecord>;
322324
let buttonElement: HTMLElement;
323325
let listElement: HTMLElement;
324326
let isInlineMenuListVisibleSpy: jest.SpyInstance;
@@ -329,6 +331,16 @@ describe("AutofillInlineMenuContentService", () => {
329331
<div class="overlay-list"></div>
330332
`;
331333
mockMutationRecord = mock<MutationRecord>({ target: globalThis.document.body } as any);
334+
mockHTMLMutationRecord = mock<MutationRecord>({
335+
target: globalThis.document.body.parentElement,
336+
attributeName: "style",
337+
type: "attributes",
338+
} as any);
339+
mockBodyMutationRecord = mock<MutationRecord>({
340+
target: globalThis.document.body,
341+
attributeName: "style",
342+
type: "attributes",
343+
} as any);
332344
buttonElement = document.querySelector(".overlay-button") as HTMLElement;
333345
listElement = document.querySelector(".overlay-list") as HTMLElement;
334346
autofillInlineMenuContentService["buttonElement"] = buttonElement;
@@ -343,6 +355,7 @@ describe("AutofillInlineMenuContentService", () => {
343355
"isTriggeringExcessiveMutationObserverIterations",
344356
)
345357
.mockReturnValue(false);
358+
jest.spyOn(autofillInlineMenuContentService as any, "closeInlineMenu");
346359
});
347360

348361
it("skips handling the mutation if the overlay elements are not present in the DOM", async () => {
@@ -373,6 +386,33 @@ describe("AutofillInlineMenuContentService", () => {
373386
expect(globalThis.document.body.insertBefore).not.toHaveBeenCalled();
374387
});
375388

389+
it("closes the inline menu if the page body is not sufficiently opaque", async () => {
390+
document.querySelector("html").style.opacity = "0.9";
391+
document.body.style.opacity = "0";
392+
autofillInlineMenuContentService["handlePageMutations"]([mockBodyMutationRecord]);
393+
394+
expect(autofillInlineMenuContentService["pageIsOpaque"]).toBe(false);
395+
expect(autofillInlineMenuContentService["closeInlineMenu"]).toHaveBeenCalled();
396+
});
397+
398+
it("closes the inline menu if the page html is not sufficiently opaque", async () => {
399+
document.querySelector("html").style.opacity = "0.3";
400+
document.body.style.opacity = "0.7";
401+
autofillInlineMenuContentService["handlePageMutations"]([mockHTMLMutationRecord]);
402+
403+
expect(autofillInlineMenuContentService["pageIsOpaque"]).toBe(false);
404+
expect(autofillInlineMenuContentService["closeInlineMenu"]).toHaveBeenCalled();
405+
});
406+
407+
it("does not close the inline menu if the page html and body is sufficiently opaque", async () => {
408+
document.querySelector("html").style.opacity = "0.9";
409+
document.body.style.opacity = "1";
410+
autofillInlineMenuContentService["handlePageMutations"]([mockBodyMutationRecord]);
411+
412+
expect(autofillInlineMenuContentService["pageIsOpaque"]).toBe(true);
413+
expect(autofillInlineMenuContentService["closeInlineMenu"]).not.toHaveBeenCalled();
414+
});
415+
376416
it("skips re-arranging the DOM elements if the last child of the body is non-existent", async () => {
377417
document.body.innerHTML = "";
378418

apps/browser/src/autofill/overlay/inline-menu/content/autofill-inline-menu-content.service.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
2929
private isFirefoxBrowser =
3030
globalThis.navigator.userAgent.indexOf(" Firefox/") !== -1 ||
3131
globalThis.navigator.userAgent.indexOf(" Gecko/") !== -1;
32-
private buttonElement: HTMLElement;
33-
private listElement: HTMLElement;
32+
private buttonElement?: HTMLElement;
33+
private listElement?: HTMLElement;
34+
private htmlMutationObserver: MutationObserver;
35+
private bodyMutationObserver: MutationObserver;
36+
private pageIsOpaque = true;
3437
private inlineMenuElementsMutationObserver: MutationObserver;
3538
private containerElementMutationObserver: MutationObserver;
3639
private mutationObserverIterations = 0;
@@ -49,6 +52,7 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
4952
};
5053

5154
constructor() {
55+
this.checkPageOpacity();
5256
this.setupMutationObserver();
5357
}
5458

@@ -281,6 +285,9 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
281285
* that the inline menu elements are always present at the bottom of the menu container.
282286
*/
283287
private setupMutationObserver = () => {
288+
this.htmlMutationObserver = new MutationObserver(this.handlePageMutations);
289+
this.bodyMutationObserver = new MutationObserver(this.handlePageMutations);
290+
284291
this.inlineMenuElementsMutationObserver = new MutationObserver(
285292
this.handleInlineMenuElementMutationObserverUpdate,
286293
);
@@ -295,6 +302,9 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
295302
* elements are not modified by the website.
296303
*/
297304
private observeCustomElements() {
305+
this.htmlMutationObserver?.observe(document.querySelector("html"), { attributes: true });
306+
this.bodyMutationObserver?.observe(document.body, { attributes: true });
307+
298308
if (this.buttonElement) {
299309
this.inlineMenuElementsMutationObserver?.observe(this.buttonElement, {
300310
attributes: true,
@@ -395,11 +405,56 @@ export class AutofillInlineMenuContentService implements AutofillInlineMenuConte
395405
});
396406
};
397407

408+
private checkPageOpacity = () => {
409+
this.pageIsOpaque = this.getPageIsOpaque();
410+
411+
if (!this.pageIsOpaque) {
412+
this.closeInlineMenu();
413+
}
414+
};
415+
416+
private handlePageMutations = (mutations: MutationRecord[]) => {
417+
for (const mutation of mutations) {
418+
if (mutation.type === "attributes") {
419+
this.checkPageOpacity();
420+
}
421+
}
422+
};
423+
424+
/**
425+
* Checks the opacity of the page body and body parent, since the inline menu experience
426+
* will inherit the opacity, despite being otherwise encapsulated from styling changes
427+
* of parents below the body. Assumes the target element will be a direct child of the page
428+
* `body` (enforced elsewhere).
429+
*/
430+
private getPageIsOpaque() {
431+
// These are computed style values, so we don't need to worry about non-float values
432+
// for `opacity`, here
433+
const htmlOpacity = globalThis.window.getComputedStyle(
434+
globalThis.document.querySelector("html"),
435+
).opacity;
436+
const bodyOpacity = globalThis.window.getComputedStyle(
437+
globalThis.document.querySelector("body"),
438+
).opacity;
439+
440+
// Any value above this is considered "opaque" for our purposes
441+
const opacityThreshold = 0.6;
442+
443+
return parseFloat(htmlOpacity) > opacityThreshold && parseFloat(bodyOpacity) > opacityThreshold;
444+
}
445+
398446
/**
399447
* Processes the mutation of the element that contains the inline menu. Will trigger when an
400448
* idle moment in the execution of the main thread is detected.
401449
*/
402450
private processContainerElementMutation = async (containerElement: HTMLElement) => {
451+
// If the computed opacity of the body and parent is not sufficiently opaque, tear
452+
// down and prevent building the inline menu experience.
453+
this.checkPageOpacity();
454+
if (!this.pageIsOpaque) {
455+
return;
456+
}
457+
403458
const lastChild = containerElement.lastElementChild;
404459
const secondToLastChild = lastChild?.previousElementSibling;
405460
const lastChildIsInlineMenuList = lastChild === this.listElement;

0 commit comments

Comments
 (0)