The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Move CORS settings to OSS and show Security tab in Embedding (#71648)
Phoomparin Mano05ec8451c5ac
diff --git a/e2e/support/helpers/embedding-sdk-testing/embedding-sdk-helpers.ts b/e2e/support/helpers/embedding-sdk-testing/embedding-sdk-helpers.tsindex 72870161881..bd6f1b4910b 100644--- a/e2e/support/helpers/embedding-sdk-testing/embedding-sdk-helpers.ts+++ b/e2e/support/helpers/embedding-sdk-testing/embedding-sdk-helpers.ts@@ -79,6 +79,12 @@ export function signInAsAdminAndEnableEmbeddingSdk() {enableJwtAuth();cy.request("PUT", "/api/setting", {"enable-embedding-sdk": true,++ // Needed so CORS headers (e.g. Access-Control-Expose-Headers) are sent in responses,+ // and we can assert them in tests.+ // Component tests don't send an Origin header (same-origin), so+ // the middleware skips CORS headers entirely.+ "embedding-app-origins-sdk": "http://localhost:*",});}diff --git a/e2e/test/scenarios/embedding/embedding-admin-settings-oss.cy.spec.ts b/e2e/test/scenarios/embedding/embedding-admin-settings-oss.cy.spec.tsindex 54135c27703..bb6b6121340 100644--- a/e2e/test/scenarios/embedding/embedding-admin-settings-oss.cy.spec.ts+++ b/e2e/test/scenarios/embedding/embedding-admin-settings-oss.cy.spec.ts@@ -29,10 +29,10 @@ describe(.findByRole("link", { name: /Guest embeds/ }).should("not.exist");- cy.log("Verify sidebar does not contain security settings link");+ cy.log("Verify sidebar contains security settings link");cy.findByTestId("admin-layout-sidebar").findByRole("link", { name: /Security/ })- .should("not.exist");+ .should("exist");});it("should show embedding upsell on oss", () => {@@ -55,13 +55,11 @@ describe(});});- it("should not show CORS setting", () => {- cy.visit("/admin/embedding");+ it("should show CORS setting on security page", () => {+ cy.visit("/admin/embedding/security");cy.findByTestId("admin-layout-content").within(() => {- cy.findByTestId("embedding-app-origins-sdk-setting").should(- "not.exist",- );+ cy.findByText("Cross-Origin Resource Sharing (CORS)").should("exist");});});},diff --git a/e2e/test/scenarios/embedding/embedding-admin-settings-starter.cy.spec.ts b/e2e/test/scenarios/embedding/embedding-admin-settings-starter.cy.spec.tsindex d917bacaa34..6e2d0caa4d7 100644--- a/e2e/test/scenarios/embedding/embedding-admin-settings-starter.cy.spec.ts+++ b/e2e/test/scenarios/embedding/embedding-admin-settings-starter.cy.spec.ts@@ -28,10 +28,10 @@ describe("scenarios > embedding > admin settings > starter", () => {.findByRole("link", { name: /Guest embeds/ }).should("not.exist");- cy.log("Verify sidebar does not contain security settings link");+ cy.log("Verify sidebar contains security settings link");cy.findByTestId("admin-layout-sidebar").findByRole("link", { name: /Security/ })- .should("not.exist");+ .should("exist");});it("should show embedding upsell on oss", () => {@@ -47,11 +47,11 @@ describe("scenarios > embedding > admin settings > starter", () => {});});- it("should not show CORS setting", () => {- cy.visit("/admin/embedding");+ it("should show CORS setting on security page", () => {+ cy.visit("/admin/embedding/security");cy.findByTestId("admin-layout-content").within(() => {- cy.findByTestId("embedding-app-origins-sdk-setting").should("not.exist");+ cy.findByText("Cross-Origin Resource Sharing (CORS)").should("exist");});});});diff --git a/frontend/src/metabase/admin/embedding/components/EmbeddingNav.tsx b/frontend/src/metabase/admin/embedding/components/EmbeddingNav.tsxindex 14fbd36385b..36596117377 100644--- a/frontend/src/metabase/admin/embedding/components/EmbeddingNav.tsx+++ b/frontend/src/metabase/admin/embedding/components/EmbeddingNav.tsx@@ -40,20 +40,18 @@ export function EmbeddingNav() {{/* EE with non-starter plan has embedding settings on different pages */}{hasSimpleEmbedding && (- <>- <EmbeddingNavItem- path="/admin/embedding/guest"- label={t`Guest embeds`}- icon="ghost"- />-- <EmbeddingNavItem- path="/admin/embedding/security"- label={t`Security`}- icon="shield_outline"- />- </>+ <EmbeddingNavItem+ path="/admin/embedding/guest"+ label={t`Guest embeds`}+ icon="ghost"+ />)}++ <EmbeddingNavItem+ path="/admin/embedding/security"+ label={t`Security`}+ icon="shield_outline"+ /></Stack></AdminNavWrapper>);diff --git a/frontend/src/metabase/admin/routes.tsx b/frontend/src/metabase/admin/routes.tsxindex 7de40372355..04da703a5f2 100644--- a/frontend/src/metabase/admin/routes.tsx+++ b/frontend/src/metabase/admin/routes.tsx@@ -200,22 +200,16 @@ export const getRoutes = ({/* EE with non-starter plan has embedding settings on different pages */}{hasSimpleEmbedding && (- <>- <Route path="guest" component={GuestEmbedsSettings} />-- <Route path="security" component={EmbeddingSecuritySettings} />- </>+ <Route path="guest" component={GuestEmbedsSettings} />)}++ <Route path="security" component={EmbeddingSecuritySettings} /></Route></Route>{/* OSS/Starter has all embedding settings on the same page */}{!hasSimpleEmbedding && (- <>- <Redirect from="/admin/embedding/guest" to="/admin/embedding" />-- <Redirect from="/admin/embedding/security" to="/admin/embedding" />- </>+ <Redirect from="/admin/embedding/guest" to="/admin/embedding" />)}{/* Backwards compatibility for embedding settings */}diff --git a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/CorsInputWidget.tsx b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/CorsInputWidget.tsxindex 01f9e970232..fbc13d8ea1b 100644--- a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/CorsInputWidget.tsx+++ b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/CorsInputWidget.tsx@@ -2,30 +2,22 @@ import { jt, t } from "ttag";import { AdminSettingInput } from "metabase/admin/settings/components/widgets/AdminSettingInput";import { useSetting } from "metabase/common/hooks";-import { PLUGIN_EMBEDDING_SDK } from "metabase/plugins";import { Box, Group, HoverCard, Icon, Text } from "metabase/ui";export const CorsInputWidget = () => {- const isReactSdkEnabled = useSetting("enable-embedding-sdk");- const isReactSdkFeatureAvailable = PLUGIN_EMBEDDING_SDK.isEnabled();const isLocalhostCorsDisabled = useSetting("disable-cors-on-localhost");- const isSimpleEmbedEnabled = useSetting("enable-embedding-simple");-const corsHintText = isLocalhostCorsDisabled? t`Separate values with a space. Localhost is not allowed. Changes will take effect within one minute.`: t`Separate values with a space. Localhost is automatically included. Changes will take effect within one minute.`;- const canEditSdkOrigins =- (isReactSdkFeatureAvailable && isReactSdkEnabled) || isSimpleEmbedEnabled;-return (<AdminSettingInputtitle={t`Cross-Origin Resource Sharing (CORS)`}description={<Group align="center" gap="sm"><Text c="text-secondary" fz="md">- {jt`Add the website domains where you want to allow modular embedding (with or without SDK). API keys can only be used on localhost. ${(+ {jt`Add the website domains where you want to allow embedding. ${(<HoverCard key="embedding-cors-hint" position="bottom"><HoverCard.Target><Icon@@ -52,7 +44,6 @@ export const CorsInputWidget = () => {name="embedding-app-origins-sdk"placeholder="https://*.example.com"inputType="text"- disabled={!canEditSdkOrigins}/>);};diff --git a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/index.ts b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/index.tsindex fb8eb49c3cf..0187c2bc70b 100644--- a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/index.ts+++ b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/index.ts@@ -1 +1,3 @@export { EmbeddingSecuritySettings } from "./EmbeddingSecuritySettings";+export { CorsInputWidget } from "./CorsInputWidget";+export { SameSiteSelectWidget } from "./SameSiteSelectWidget";diff --git a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/tests/common.unit.spec.tsx b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/tests/common.unit.spec.tsxindex f19010cb03f..47f5f135973 100644--- a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/tests/common.unit.spec.tsx+++ b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/tests/common.unit.spec.tsx@@ -18,6 +18,25 @@ const setup = async () => {};describe("EmbeddingSecuritySettings => common", () => {+ it("should allow users to update CORS settings", async () => {+ await setup();++ expect(+ await screen.findByText("Cross-Origin Resource Sharing (CORS)"),+ ).toBeInTheDocument();++ const input = await screen.findByPlaceholderText("https://*.example.com");+ await userEvent.type(input, "https://my-app.example.com");+ await userEvent.tab();++ const puts = await findRequests("PUT");+ expect(puts).toHaveLength(1);++ const [{ url, body }] = puts;+ expect(url).toContain("/setting/embedding-app-origins-sdk");+ expect(body).toEqual({ value: "https://my-app.example.com" });+ });+it("should allow changing samesite cookie setting", async () => {await setup();diff --git a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/tests/enterprise.unit.spec.tsx b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/tests/enterprise.unit.spec.tsxdeleted file mode 100644index df574c3e520..00000000000--- a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/tests/enterprise.unit.spec.tsx+++ /dev/null@@ -1,57 +0,0 @@-import userEvent from "@testing-library/user-event";--import { findRequests } from "__support__/server-mocks";-import { fireEvent, renderWithProviders, screen, within } from "__support__/ui";--import { setup as baseSetup } from "../../tests/setup";-import { EmbeddingSecuritySettings } from "../EmbeddingSecuritySettings";--const setup = async ({- showSdkEmbedTerms,- isEmbeddingSdkEnabled,-}: {- showSdkEmbedTerms?: boolean;- isEmbeddingSdkEnabled?: boolean;-} = {}) => {- await baseSetup({- renderCallback: ({ state }) =>- renderWithProviders(<EmbeddingSecuritySettings />, {- storeInitialState: state,- }),- showSdkEmbedTerms,- isEmbeddingSdkEnabled,- tokenFeatures: {- embedding_sdk: isEmbeddingSdkEnabled,- },- enterprisePlugins: isEmbeddingSdkEnabled ? ["embedding-sdk"] : [],- });-- expect(await screen.findByText("Security")).toBeInTheDocument();-};--describe("EmbeddingSecuritySettings => enterprise", () => {- it("should allow users to update CORS settings", async () => {- await setup({- isEmbeddingSdkEnabled: true,- showSdkEmbedTerms: false,- });-- const input = within(- await screen.findByTestId("embedding-app-origins-sdk-setting"),- ).getByRole("textbox");-- expect(input).toBeEnabled();- await userEvent.clear(input);- await userEvent.type(input, "fast.limos");- fireEvent.blur(input);- await screen.findByDisplayValue("fast.limos");-- const puts = await findRequests("PUT");- expect(puts).toHaveLength(1);- const [{ url, body }] = puts;- expect(url).toContain("api/setting/embedding-app-origins-sdk");- expect(body).toEqual({- value: "fast.limos",- });- });-});diff --git a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/tests/simple-embedding.unit.spec.tsx b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/tests/simple-embedding.unit.spec.tsxdeleted file mode 100644index ad25f8fefc8..00000000000--- a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/EmbeddingSecuritySettings/tests/simple-embedding.unit.spec.tsx+++ /dev/null@@ -1,85 +0,0 @@-import { renderWithProviders, screen } from "__support__/ui";--import { setup as baseSetup } from "../../tests/setup";-import { EmbeddingSecuritySettings } from "../EmbeddingSecuritySettings";--const setup = async ({- showSdkEmbedTerms,- isEmbeddingSdkEnabled,- isEmbeddingSimpleEnabled,-}: {- showSdkEmbedTerms?: boolean;- isEmbeddingSdkEnabled?: boolean;- isEmbeddingSimpleEnabled?: boolean;-} = {}) => {- await baseSetup({- renderCallback: ({ state }) =>- renderWithProviders(<EmbeddingSecuritySettings />, {- storeInitialState: state,- }),- showSdkEmbedTerms,- isEmbeddingSdkEnabled,- isEmbeddingSimpleEnabled,- tokenFeatures: {- embedding_sdk: true,- embedding_simple: true,- },- enterprisePlugins: ["embedding-sdk"],- });-- expect(await screen.findByText("Security")).toBeInTheDocument();-};--describe("EmbeddingSecuritySettings", () => {- describe("Authorized Origins input field", () => {- it("should be disabled when both SDK and simple embedding are disabled", async () => {- await setup({- isEmbeddingSdkEnabled: false,- isEmbeddingSimpleEnabled: false,- showSdkEmbedTerms: false,- });-- expect(- screen.getByText("Cross-Origin Resource Sharing (CORS)"),- ).toBeInTheDocument();-- const originInput = screen.getByPlaceholderText("https://*.example.com");- expect(originInput).toBeDisabled();- });-- it.each([- {- description:- "should be enabled when SDK is enabled and simple embedding is disabled",- isEmbeddingSdkEnabled: true,- isEmbeddingSimpleEnabled: false,- },- {- description:- "should be enabled when simple embedding is enabled and SDK is disabled",- isEmbeddingSdkEnabled: false,- isEmbeddingSimpleEnabled: true,- },- {- description:- "should be enabled when both SDK and simple embedding are enabled",- isEmbeddingSdkEnabled: true,- isEmbeddingSimpleEnabled: true,- },- ])(- "$description",- async ({ isEmbeddingSdkEnabled, isEmbeddingSimpleEnabled }) => {- await setup({- isEmbeddingSdkEnabled,- isEmbeddingSimpleEnabled,- showSdkEmbedTerms: false,- });-- const originInput = screen.getByPlaceholderText(- "https://*.example.com",- );- expect(originInput).toBeEnabled();- },- );- });-});diff --git a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/SharedCombinedEmbeddingSettings/SharedCombinedEmbeddingSettings.tsx b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/SharedCombinedEmbeddingSettings/SharedCombinedEmbeddingSettings.tsxindex e1fa281b3bd..2a88c914531 100644--- a/frontend/src/metabase/admin/settings/components/EmbeddingSettings/SharedCombinedEmbeddingSettings/SharedCombinedEmbeddingSettings.tsx+++ b/frontend/src/metabase/admin/settings/components/EmbeddingSettings/SharedCombinedEmbeddingSettings/SharedCombinedEmbeddingSettings.tsx@@ -17,16 +17,13 @@ import { Box, Text } from "metabase/ui";import { SettingTitle } from "../../SettingHeader";import { EmbeddedResources } from "../../widgets/PublicLinksListing/EmbeddedResources";import { EmbeddingSecretKeyWidget } from "../EmbeddingSecretKeyWidget";-import { CorsInputWidget } from "../EmbeddingSecuritySettings/CorsInputWidget";type Props = {showEmbeddingSdkSettings?: boolean;- showCorsSettings?: boolean;showContentTranslationSettings?: boolean;};export function SharedCombinedEmbeddingSettings({- showCorsSettings,showContentTranslationSettings,}: Props) {const isSimpleEmbedFeatureAvailable =@@ -87,12 +84,6 @@ export function SharedCombinedEmbeddingSettings({</SettingsSection>)}- {showCorsSettings && (- <SettingsSection>- <CorsInputWidget />- </SettingsSection>- )}-{showContentTranslationSettings && (<PLUGIN_CONTENT_TRANSLATION.ContentTranslationConfiguration />)}diff --git a/src/metabase/embedding/settings.clj b/src/metabase/embedding/settings.cljindex b15ac7f4551..ae8981a380f 100644--- a/src/metabase/embedding/settings.clj+++ b/src/metabase/embedding/settings.clj@@ -182,7 +182,6 @@:type :string:export? false:visibility :public- :feature :embedding-sdk:default "":encryption :no:audit :getterdiff --git a/src/metabase/server/middleware/security.clj b/src/metabase/server/middleware/security.cljindex 1b97992c7ad..aba4e9e5540 100644--- a/src/metabase/server/middleware/security.clj+++ b/src/metabase/server/middleware/security.clj@@ -260,9 +260,9 @@(defn access-control-headers"Returns headers for CORS requests"- [origin enabled? approved-origins]+ [origin approved-origins](let [localhost-allowed? (and (localhost-origin? origin) (not (server.settings/disable-cors-on-localhost)))]- (when (or enabled? localhost-allowed?)+ (when (or (seq approved-origins) localhost-allowed?)(merge(when (approved-origin? origin approved-origins){"Access-Control-Allow-Origin" origin@@ -281,11 +281,7 @@(if allow-cache? cache-far-future-headers (cache-prevention-headers))strict-transport-security-header(content-security-policy-header-with-frame-ancestors allow-iframes? nonce)- (access-control-headers origin- (or- (setting/get-value-of-type :boolean :enable-embedding-sdk)- (setting/get-value-of-type :boolean :enable-embedding-simple))- (embedding.settings/embedding-app-origins-sdk))+ (access-control-headers origin (embedding.settings/embedding-app-origins-sdk))(when-not allow-iframes?;; Tell browsers not to render our site as an iframe (prevent clickjacking){"X-Frame-Options" (if-let [eao (and (setting/get-value-of-type :boolean :enable-embedding-interactive)diff --git a/test/metabase/server/middleware/security_test.clj b/test/metabase/server/middleware/security_test.cljindex c4f20a9d9da..8b61a74c6a1 100644--- a/test/metabase/server/middleware/security_test.clj+++ b/test/metabase/server/middleware/security_test.clj@@ -5,7 +5,6 @@[clojure.test :refer :all][environ.core :as env][metabase.config.core :as config]- [metabase.embedding.settings :as embed.settings][metabase.server.instance :as server.instance][metabase.server.middleware.security :as mw.security][metabase.server.settings :as server.settings]@@ -228,90 +227,67 @@(deftest test-disable-cors-on-localhost-access-control-headers(testing "Should allow CORS headers for localhost when disable-cors-on-localhost is false"- (mt/with-temporary-setting-values [disable-cors-on-localhost false- enable-embedding-sdk false]+ (mt/with-temporary-setting-values [disable-cors-on-localhost false](is (= "http://localhost:8080" (get (mw.security/access-control-headers"http://localhost:8080"- false"")"Access-Control-Allow-Origin")))))- (testing "Should block CORS headers for localhost when disable-cors-on-localhost is true and embedding disabled"- (mt/with-temporary-setting-values [disable-cors-on-localhost true- enable-embedding-sdk false]+ (testing "Should block CORS headers for localhost when disable-cors-on-localhost is true and no origins configured"+ (mt/with-temporary-setting-values [disable-cors-on-localhost true](is (= nil (get (mw.security/access-control-headers"http://localhost:8080"- false"")"Access-Control-Allow-Origin"))))))(deftest test-access-control-headers- (mt/with-premium-features #{:embedding-sdk}- (testing "Should allow localhost even when enable-embedding-sdk is disabled"- (mt/with-temporary-setting-values [enable-embedding-sdk false- enable-embedding-simple false]- (is (= "http://localhost:8080" (get (mw.security/access-control-headers- "http://localhost:8080"- (embed.settings/enable-embedding-sdk)- (embed.settings/embedding-app-origins-sdk))- "Access-Control-Allow-Origin"))- "Localhost should always be permitted even when `enable-embedding-sdk` is `false`."))- (testing "Non-localhost origins should still be blocked when embedding is disabled"- (is (= nil (get (mw.security/access-control-headers- "http://1.2.3.4:5555"- false- "localhost:*")- "Access-Control-Allow-Origin")))))- (testing "Should work with embedding-app-origin"- (mt/with-premium-features #{:embedding-sdk}- (mt/with-temporary-setting-values [enable-embedding-sdk true- embedding-app-origins-sdk "https://example.com"]- (is (= "https://example.com"- (get (mw.security/access-control-headers "https://example.com"- (embed.settings/enable-embedding-sdk)- (embed.settings/embedding-app-origins-sdk))- "Access-Control-Allow-Origin"))))))- (testing "Should set Access-Control-Max-Age to 60"- (mt/with-temporary-setting-values [enable-embedding-sdk true- embedding-app-origins-sdk "https://example.com"]- (let [headers (mw.security/access-control-headers- "https://example.com"- (embed.settings/enable-embedding-sdk)- (embed.settings/embedding-app-origins-sdk))]- (is (= "60" (get headers "Access-Control-Max-Age"))- "Expected Access-Control-Max-Age header to be set to 60")))))- (testing "CORS should be enabled when enable-embedding-simple is true"- (mt/with-temporary-setting-values [enable-embedding-simple true+ (testing "Should allow localhost even when no origins are configured"+ (is (= "http://localhost:8080" (get (mw.security/access-control-headers+ "http://localhost:8080"+ "")+ "Access-Control-Allow-Origin"))+ "Localhost should always be permitted even with no configured origins."))+ (testing "Non-localhost origins should be blocked when not in approved origins"+ (is (= nil (get (mw.security/access-control-headers+ "http://1.2.3.4:5555"+ "localhost:*")+ "Access-Control-Allow-Origin"))))+ (testing "Should work with configured origins"+ (is (= "https://example.com"+ (get (mw.security/access-control-headers "https://example.com"+ "https://example.com")+ "Access-Control-Allow-Origin"))))+ (testing "Should set Access-Control-Max-Age to 60"+ (let [headers (mw.security/access-control-headers "https://example.com"+ "https://example.com")]+ (is (= "60" (get headers "Access-Control-Max-Age"))+ "Expected Access-Control-Max-Age header to be set to 60")))+ (testing "CORS should be enabled when origins are configured regardless of embedding flags"+ (mt/with-temporary-setting-values [enable-embedding-simple falseenable-embedding-sdk false](let [headers (mw.security/access-control-headers "https://example.com"- (or (embed.settings/enable-embedding-sdk)- (embed.settings/enable-embedding-simple))"https://example.com")](is (= "https://example.com"(get headers "Access-Control-Allow-Origin"))- "CORS should work when enable-embedding-simple is true even if enable-embedding-sdk is false"))))- (testing "CORS should be enabled when both enable-embedding-simple and enable-embedding-sdk are true"- (mt/with-premium-features #{:embedding-sdk}- (mt/with-temporary-setting-values [enable-embedding-simple true- enable-embedding-sdk true- embedding-app-origins-sdk "https://example.com"]- (let [headers (mw.security/access-control-headers "https://example.com"- (or (embed.settings/enable-embedding-sdk)- (embed.settings/enable-embedding-simple))- (embed.settings/embedding-app-origins-sdk))]- (is (= "https://example.com"- (get headers "Access-Control-Allow-Origin"))- "CORS should work when both embedding options are enabled")))))- (testing "CORS should be disabled when both enable-embedding-simple and enable-embedding-sdk are false"- (mt/with-temporary-setting-values [enable-embedding-simple false- enable-embedding-sdk false+ "CORS is driven by configured origins, not embedding flags"))))+ (testing "CORS should return nil when no origins configured and origin is not localhost"+ (let [headers (mw.security/access-control-headers "https://example.com"+ "")]+ (is (= nil (get headers "Access-Control-Allow-Origin"))))))++(deftest test-cors-enabled-when-origins-configured-without-embedding-features+ (testing "CORS headers should be sent when origins are configured even if embedding features are disabled"+ (mt/with-temporary-setting-values [enable-embedding-sdk false+ enable-embedding-simple falseembedding-app-origins-sdk "https://example.com"]- (let [headers (mw.security/access-control-headers "https://example.com"- (or (embed.settings/enable-embedding-sdk)- (embed.settings/enable-embedding-simple))- (embed.settings/embedding-app-origins-sdk))]- (is (= nil- (get headers "Access-Control-Allow-Origin"))- "CORS should be disabled when both embedding options are disabled")))))+ (let [wrapped-handler (mw.security/add-security-headers+ (fn [_request respond _raise]+ (respond {:status 200 :headers {} :body "ok"})))+ response (wrapped-handler {:headers {"origin" "https://example.com"}+ :uri "/api/dashboard/1"}+ identity identity)]+ (is (= "https://example.com"+ (get-in response [:headers "Access-Control-Allow-Origin"]))+ "CORS should be enabled when origins are configured, even without embedding features")))))(deftest ^:parallel allowed-iframe-hosts-test(testing "The allowed iframe hosts parse in the expected way."@@ -376,13 +352,11 @@(is (= @#'mw.security/always-allowed-iframe-hosts(mw.security/parse-allowed-iframe-hosts "asdf/wasd/:8000 */localhost:*")))))))-(defn- run-add-security-headers-mw-test! [enable-embedding-sdk-value- embedding-app-origins-sdk-value+(defn- run-add-security-headers-mw-test! [embedding-app-origins-sdk-valuerequest-origin-headerrequest-uriaccess-control-allow-origin-header-value]- (mt/with-temporary-setting-values [embedding-app-origins-sdk embedding-app-origins-sdk-value- enable-embedding-sdk enable-embedding-sdk-value]+ (mt/with-temporary-setting-values [embedding-app-origins-sdk embedding-app-origins-sdk-value](let [wrapped-handler (mw.security/add-security-headers(fn [_request respond _raise](respond {:status 200 :headers {"response" "ok"} :body "ok"})))@@ -393,50 +367,32 @@(-> response :headers (get "Access-Control-Allow-Origin")))))))(deftest add-security-headers-mw-test- (doseq [[idx [enable-embedding-sdk embedding-app-origins-sdk request-origin request-uri access-control-allow-origin-header-value]]- [[1 [true "" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]- [2 [true "" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]- [3 [true "" "http://my-site.com" "http://public.metabase.com" nil]]- [4 [true "" "http://my-site.com" "http://www.a-site.com" nil]]- [5 [true "localhost:1234" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]- [6 [true "localhost:1234" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]- [7 [true "localhost:1234" "http://my-site.com" "http://public.metabase.com" nil]]- [8 [true "localhost:1234" "http://my-site.com" "http://www.a-site.com" nil]]- [9 [true "http://my-site.com" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]- [10 [true "http://my-site.com" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]- [11 [true "http://my-site.com" "http://my-site.com" "http://public.metabase.com" nil]]- [12 [true "http://my-site.com" "http://my-site.com" "http://www.a-site.com" nil]]- [13 [true "http://my-site.com:80" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]- [14 [true "http://my-site.com:80" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]- [15 [true "http://my-site.com:80" "http://my-site.com" "http://public.metabase.com" nil]]- [16 [true "http://my-site.com:80" "http://my-site.com" "http://www.a-site.com" nil]]- [17 [true "https://my-site-1:1234 http://my-other-site:8080" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]- [18 [true "https://my-site-1:1234 http://my-other-site:8080" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]- [19 [true "https://my-site-1:1234 http://my-other-site:8080" "http://my-site.com" "http://public.metabase.com" nil]]- [20 [true "https://my-site-1:1234 http://my-other-site:8080" "http://my-site.com" "http://www.a-site.com" nil]]- [21 [false "" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]- [22 [false "" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]- [23 [false "" "http://my-site.com" "http://public.metabase.com" nil]]- [24 [false "" "http://my-site.com" "http://www.a-site.com" nil]]- [25 [false "localhost:1234" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]- [26 [false "localhost:1234" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]- [27 [false "localhost:1234" "http://my-site.com" "http://public.metabase.com" nil]]- [28 [false "localhost:1234" "http://my-site.com" "http://www.a-site.com" nil]]- [29 [false "http://my-site.com" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]- [30 [false "http://my-site.com" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]- [31 [false "http://my-site.com" "http://my-site.com" "http://public.metabase.com" nil]]- [32 [false "http://my-site.com" "http://my-site.com" "http://www.a-site.com" nil]]- [33 [false "http://my-site.com:80" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]- [34 [false "http://my-site.com:80" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]- [35 [false "http://my-site.com:80" "http://my-site.com" "http://public.metabase.com" nil]]- [36 [false "http://my-site.com:80" "http://my-site.com" "http://www.a-site.com" nil]]- [37 [false "https://my-site-1:1234 http://my-other-site:8080" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]- [38 [false "https://my-site-1:1234 http://my-other-site:8080" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]- [39 [false "https://my-site-1:1234 http://my-other-site:8080" "http://my-site.com" "http://public.metabase.com" nil]]- [40 [false "https://my-site-1:1234 http://my-other-site:8080" "http://my-site.com" "http://www.a-site.com" nil]]]]+ (doseq [[idx [embedding-app-origins-sdk request-origin request-uri access-control-allow-origin-header-value]]+ [;; No CORS origins configured = only localhost passthrough+ [1 ["" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]+ [2 ["" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]+ [3 ["" "http://my-site.com" "http://public.metabase.com" nil]]+ [4 ["" "http://my-site.com" "http://www.a-site.com" nil]]++ ;; CORS origins configured = CORS enabled via origins+ [5 ["localhost:1234" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]+ [6 ["localhost:1234" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]+ [7 ["localhost:1234" "http://my-site.com" "http://public.metabase.com" nil]]+ [8 ["localhost:1234" "http://my-site.com" "http://www.a-site.com" nil]]+ [9 ["http://my-site.com" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]+ [10 ["http://my-site.com" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]+ [11 ["http://my-site.com" "http://my-site.com" "http://public.metabase.com" "http://my-site.com"]]+ [12 ["http://my-site.com" "http://my-site.com" "http://www.a-site.com" "http://my-site.com"]]+ [13 ["http://my-site.com:80" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]+ [14 ["http://my-site.com:80" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]+ [15 ["http://my-site.com:80" "http://my-site.com" "http://public.metabase.com" nil]]+ [16 ["http://my-site.com:80" "http://my-site.com" "http://www.a-site.com" nil]]+ [17 ["https://my-site-1:1234 http://my-other-site:8080" "http://localhost:1234" "http://public.metabase.com" "http://localhost:1234"]]+ [18 ["https://my-site-1:1234 http://my-other-site:8080" "http://localhost:1234" "http://www.a-site.com" "http://localhost:1234"]]+ [19 ["https://my-site-1:1234 http://my-other-site:8080" "http://my-site.com" "http://public.metabase.com" nil]]+ [20 ["https://my-site-1:1234 http://my-other-site:8080" "http://my-site.com" "http://www.a-site.com" nil]]]](testing (str "add security headers mw test, index: " idx)(run-add-security-headers-mw-test!- enable-embedding-sdkembedding-app-origins-sdkrequest-originrequest-uri