Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

CodeMirror styling in custom expression fails in Embed JS as the CSP nonce is missing (#61959)

Phoomparin Mano2e884c4ce682
diff --git a/e2e/test/scenarios/embedding/sdk-iframe-embedding/sdk-iframe-embedding.cy.spec.ts b/e2e/test/scenarios/embedding/sdk-iframe-embedding/sdk-iframe-embedding.cy.spec.ts
index 8460bdde03c..cffeef476e4 100644
--- a/e2e/test/scenarios/embedding/sdk-iframe-embedding/sdk-iframe-embedding.cy.spec.ts
+++ b/e2e/test/scenarios/embedding/sdk-iframe-embedding/sdk-iframe-embedding.cy.spec.ts
@@ -230,6 +230,40 @@ describe("scenarios > embedding > sdk iframe embedding", () => {
cy.log("3. dashboard title should now be visible");
getIframeWindow().findByText("Orders in a dashboard").should("be.visible");
});
+
+ it("CSP nonces are set for custom expression styles (EMB-707)", () => {
+ const frame = H.loadSdkIframeEmbedTestPage({
+ element: "metabase-question",
+ attributes: {
+ questionId: "new",
+ },
+ });
+
+ frame.within(() => {
+ cy.findByText("Orders").should("be.visible");
+
+ H.popover().within(() => {
+ cy.findByText("Orders").click();
+ });
+
+ cy.log("csp nonces should be set");
+ cy.get("style[nonce]")
+ .should("have.length.greaterThan", 0)
+ .first()
+ .should("have.attr", "nonce")
+ .and("have.length.greaterThan", 4);
+
+ cy.findByRole("button", { name: "Custom column" }).click();
+
+ cy.log("injected codemirror styles should be set");
+ cy.findByTestId("custom-expression-query-editor")
+ .should("be.visible")
+ .find(".cm-editor .cm-placeholder")
+ .and("have.css", "color", "rgb(136, 136, 136)");
+
+ cy.findByRole("button", { name: "Cancel" }).click();
+ });
+ });
});
const getIframeWindow = () =>
diff --git a/frontend/src/metabase/app-embed-sdk.tsx b/frontend/src/metabase/app-embed-sdk.tsx
index 809a13678e6..eeee333dc9f 100644
--- a/frontend/src/metabase/app-embed-sdk.tsx
+++ b/frontend/src/metabase/app-embed-sdk.tsx
@@ -1,5 +1,8 @@
import { createRoot } from "react-dom/client";
+// Setup CSP nonce for CodeMirror dynamic styles
+import "metabase/lib/csp";
+
import {
EMBEDDING_SDK_CONFIG,
EMBEDDING_SDK_IFRAME_EMBEDDING_CONFIG,