Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#8219: don't let frames control sidebar panel via setPanels #8225

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/contentScript/contentScriptCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { markDocumentAsFocusableByUser } from "@/utils/focusTracker";
import contentScriptPlatform from "@/contentScript/contentScriptPlatform";
import axios from "axios";
import { initDeferredLoginController } from "@/contentScript/integrations/deferredLoginController";
import { isLoadedInIframe } from "@/utils/iframeUtils";

setPlatform(contentScriptPlatform);

Expand Down Expand Up @@ -101,8 +102,11 @@ export async function init(): Promise<void> {
initSidebarFocusEvents();
void initSidebarActivation();

// Update `sidePanel`
void renderPanelsIfVisible();
if (!isLoadedInIframe()) {
// FIXME: do we want to do this? It will cause the panels to be reset on non-SPA navigation/reload
// Update `sidePanel`
void renderPanelsIfVisible();
}

// Let the partner page know
initPartnerIntegrations();
Expand Down
10 changes: 10 additions & 0 deletions src/contentScript/sidebarController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ export async function updateSidebar(
export async function renderPanelsIfVisible(): Promise<void> {
expectContext("contentScript");

if (isLoadedInIframe()) {
console.warn("renderPanelsIfVisible should not be called from a frame");
return;
}

if (await isSidePanelOpen()) {
void sidebarInThisTab.renderPanels(getTimedSequence(), panels);
} else {
Expand Down Expand Up @@ -298,6 +303,11 @@ export function removeExtensions(extensionIds: UUID[]): void {

console.debug("sidebarController:removeExtensions", { extensionIds });

// Avoid unnecessary messaging. Also, prevent issues if this method is called as cleanup from a frame
if (extensionIds.length === 0) {
return;
}

// `panels` is const, so replace the contents
const current = panels.splice(0);
panels.push(...current.filter((x) => !extensionIds.includes(x.extensionId)));
Expand Down
5 changes: 4 additions & 1 deletion src/utils/iframeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
* Returns true if this function is called within an IFrame
*/

export const isLoadedInIframe = () => {
export const isLoadedInIframe = (): boolean => {
// Using a try/catch block because in some cases trying to
// access window.top from an iframe can result in a SecurityError

// If updating this method to use the messenger/frameId, remember that frameId === 0 only for the active top-level
// frame: see https://developer.chrome.com/blog/extension-instantnav
try {
return window.self !== window.top;
} catch {
Expand Down
Loading