Skip to content

Commit

Permalink
#8227: error if showing temporary panel from frame (#8231)
Browse files Browse the repository at this point in the history
Co-authored-by: Eduardo Fungairino <[email protected]>
  • Loading branch information
twschiller and fungairino authored Apr 12, 2024
1 parent 6c3e02a commit d6a1f25
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/bricks/transformers/temporaryInfo/DisplayTemporaryInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,21 @@ import { contextAsPlainObject } from "@/runtime/extendModVariableContext";
import { unary } from "lodash";
import { toExpression } from "@/utils/expressionUtils";
import { showModal } from "@/contentScript/modalDom";
import { isLoadedInIframe } from "@/utils/iframeUtils";

jest.mock("@/contentScript/modalDom");
jest.mock("@/contentScript/sidebarController");
jest.mock("@/platform/panels/panelController");

jest.mock("@/utils/iframeUtils");

const displayTemporaryInfoBlock = new DisplayTemporaryInfo();
const renderer = new DocumentRenderer();

describe("DisplayTemporaryInfo", () => {
beforeEach(() => {
jest.mocked(isLoadedInIframe).mockReturnValue(false);

blockRegistry.clear();
blockRegistry.register([
echoBrick,
Expand Down Expand Up @@ -197,6 +202,34 @@ describe("DisplayTemporaryInfo", () => {
});
});

test("it errors from frame", async () => {
jest.mocked(isLoadedInIframe).mockReturnValue(true);

const config = getExampleBrickConfig(renderer.id);
const pipeline = {
id: displayTemporaryInfoBlock.id,
config: {
title: "Test Temp Panel",
body: toExpression("pipeline", [{ id: renderer.id, config }]),
location: "panel",
isRootAware: true,
},
};

const extensionId = uuidv4();

const options = {
...testOptions("v3"),
logger: new ConsoleLogger({
extensionId,
}),
};

await expect(
reducePipeline(pipeline, simpleInput({}), options),
).rejects.toThrow("Cannot show sidebar in a frame");
});

test("requires target for popover", async () => {
const config = getExampleBrickConfig(renderer.id);
const pipeline = {
Expand Down
8 changes: 8 additions & 0 deletions src/contentScript/ephemeralPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type { Location } from "@/types/starterBrickTypes";
import { getThisFrame } from "webext-messenger";
import { expectContext } from "@/utils/expectContext";
import { showModal } from "@/contentScript/modalDom";
import { isLoadedInIframe } from "@/utils/iframeUtils";

export async function createFrameSource(
nonce: string,
Expand Down Expand Up @@ -82,6 +83,13 @@ export async function ephemeralPanel({
}: TemporaryPanelDefinition): Promise<JsonObject> {
expectContext("contentScript");

if (location === "panel" && isLoadedInIframe()) {
// Validate before registerEmptyTemporaryPanel to avoid an uncaught promise rejection
throw new BusinessError(
"Cannot show sidebar in a frame. To use the sidebar, set the target to Top-level Frame",
);
}

const nonce = uuidv4();
let onReady: (() => void) | undefined;

Expand Down

0 comments on commit d6a1f25

Please sign in to comment.