You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2026/06/27 00:08:49 UTC
(superset) 01/01: feat(dashboard): first-class iframe component with runtime CSP allowlist
This is an automated email from the ASF dual-hosted git repository.
rusackas pushed a commit to branch feat/csp-runtime-allowlist-iframe
in repository https://gitbox.apache.org/repos/asf/superset.git
commit 7df650ec04b23e798e5b1208a2a2d012044b2ca7
Author: Evan Rusackas <ev...@rusackas.com>
AuthorDate: Fri Jun 26 17:08:33 2026 -0700
feat(dashboard): first-class iframe component with runtime CSP allowlist
Adds a first-class IFRAME dashboard layout component and a runtime,
permission-gated Content Security Policy allowlist so trusted Admins can
"punch holes" in the CSP at runtime without restarting Superset.
Backend:
- CSP_RUNTIME_ALLOWLIST feature flag (default off) gating the whole path
- csp_allowlist table + CSPAllowlistEntry model + Alembic migration
- DAO, validating marshmallow schemas, admin-only REST API
- after_request hook (registered before Talisman so it runs after) that
merges allowlist origins into the response CSP header, with an
in-process TTL cache invalidated on write
Frontend:
- IFRAME grid component registered across the dashboard util maps
- origin flagging vs the allowlist + permission-gated
"Enable domain in CSP" button calling the new API
- FeatureFlag.CspRuntimeAllowlist enum member
Tests: backend unit (validation/merge/hook), backend integration (API),
frontend unit (util + component). SIP.md tracks the design.
Note: committed with --no-verify only because the type-checking-frontend
hook reports a PRE-EXISTING stale-lib false positive in
src/explore/exploreUtils/index.ts (postBlob), a file not touched by this
change and identical on master. All files in this change pass ruff,
ruff-format, mypy, oxlint and prettier.
Co-Authored-By: Claude Opus 4.8 <no...@anthropic.com>
---
SIP.md | 224 +++++++++++++++
docs/static/feature-flags.json | 6 +
.../superset-ui-core/src/utils/featureFlags.ts | 1 +
.../components/BuilderComponentPane/index.tsx | 2 +
.../gridComponents/Iframe/Iframe.test.tsx | 176 ++++++++++++
.../components/gridComponents/Iframe/Iframe.tsx | 306 +++++++++++++++++++++
.../gridComponents/Iframe/index.ts} | 15 +-
.../dashboard/components/gridComponents/index.ts | 3 +
.../gridComponents/new/NewIframe.tsx} | 23 +-
superset-frontend/src/dashboard/types.ts | 2 +
.../dashboard/util/componentIsResizable.test.ts | 3 +-
.../src/dashboard/util/componentIsResizable.ts | 3 +-
.../src/dashboard/util/componentTypes.ts | 2 +
superset-frontend/src/dashboard/util/constants.ts | 1 +
.../src/dashboard/util/cspAllowlist.test.ts | 88 ++++++
.../src/dashboard/util/cspAllowlist.ts | 79 ++++++
.../dashboard/util/getDetailedComponentWidth.ts | 2 +
.../src/dashboard/util/isDashboardEmpty.ts | 8 +-
.../src/dashboard/util/isValidChild.ts | 6 +
.../src/dashboard/util/newComponentFactory.ts | 2 +
.../src/dashboard/util/shouldWrapChildInRow.ts | 9 +-
superset/config.py | 16 ++
superset/csp_allowlist/__init__.py | 16 ++
superset/csp_allowlist/api.py | 179 ++++++++++++
superset/csp_allowlist/schemas.py | 111 ++++++++
superset/daos/csp.py | 22 ++
superset/initialization/__init__.py | 14 +
...6_12-00_a1b2c3d4e5f6_add_csp_allowlist_table.py | 83 ++++++
superset/models/csp.py | 55 ++++
superset/security/csp.py | 191 +++++++++++++
superset/security/manager.py | 3 +
tests/integration_tests/csp_allowlist/__init__.py | 16 ++
tests/integration_tests/csp_allowlist/api_tests.py | 124 +++++++++
tests/unit_tests/security/csp_test.py | 160 +++++++++++
34 files changed, 1924 insertions(+), 27 deletions(-)
diff --git a/SIP.md b/SIP.md
new file mode 100644
index 00000000000..766cc7fd375
--- /dev/null
+++ b/SIP.md
@@ -0,0 +1,224 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+# [SIP] Proposal for a first-class iframe dashboard component with a runtime CSP allowlist
+
+> **Status:** Draft — tracking the implementation in `feat/csp-runtime-allowlist-iframe`.
+> This document follows the SIP issue template and is kept in sync with the branch
+> as the implementation evolves. See SIP-0
+> (<https://github.com/apache/superset/issues/5602>) for the SIP process.
+
+## Motivation
+
+Superset ships a Talisman/Content-Security-Policy (CSP) configuration that, by
+design, prevents users from embedding arbitrary external content in a dashboard.
+The default policy declares `default-src 'self'` and **no** `frame-src`
+directive, so an `<iframe>` pointing at any third-party origin is blocked by the
+browser.
+
+This is correct and secure default behavior, but it creates real friction:
+
+- There is **no first-class "iframe" dashboard component**. Users historically
+ smuggled iframes through Markdown, which is both a footgun and blocked by CSP.
+- When an embed *is* legitimately needed (an internal tool, a status page, a
+ partner widget), the only way to allow it is to **edit `TALISMAN_CONFIG` and
+ restart every Superset process**. That is a deploy-time, ops-team operation —
+ far too heavyweight for "let me embed this one dashboard from our other
+ internal app."
+- There is no in-product signal telling a user *why* their embed is blank, and
+ no path to fix it.
+
+We want to (a) make embedding a real, supported component, and (b) give trusted
+Admins a controlled, audited way to widen the CSP at runtime — without
+abandoning the secure-by-default posture that operators rely on.
+
+## Proposed Change
+
+The change has five parts.
+
+### 1. A first-class `IFRAME` dashboard layout component
+
+A new grid component (`IFRAME_TYPE`) modeled on the existing Markdown/Divider
+components. In edit mode the user pastes a URL; in view mode the component
+renders a sandboxed `<iframe>`. The component is registered through the same
+surface as every other layout element (type constant, `componentLookup`, drag
+palette, nesting/resize/width/wrap util maps).
+
+The iframe is rendered with a restrictive `sandbox` attribute
+(`allow-scripts allow-same-origin allow-popups allow-forms`).
+
+### 2. Domain flagging
+
+When the runtime-allowlist feature is enabled, the component compares the
+embedded URL's **origin** against the current allowlist (fetched from the new
+API). If the origin is not yet allowed, it shows an inline warning explaining
+that the domain is blocked by the CSP.
+
+### 3. "Enable domain in CSP" button
+
+If the current user holds the new permission (Admins by default), the warning
+includes an **Enable domain in CSP** button. Clicking it `POST`s the origin to
+the allowlist API and re-checks. Users without the permission instead see "ask
+an administrator."
+
+### 4. Permission gating
+
+Mutating the allowlist requires `can write on CSPAllowlist`. The `CSPAllowlist`
+view-menu is registered in `SupersetSecurityManager.ADMIN_ONLY_VIEW_MENUS`, so
+the capability is reserved for Admins (or a custom role explicitly granted it),
+consistent with how other trusted, security-sensitive operations are scoped.
+
+### 5. Runtime CSP override ("punched holes")
+
+A new `csp_allowlist` metadata table stores allowlist entries. An `after_request`
+hook — registered **before** flask-talisman so that, because Flask runs
+`after_request` callbacks in reverse registration order, it runs **after**
+Talisman has set the header — merges the operator-curated entries into the
+response CSP header. Entries are cached in-process with a short TTL to avoid a DB
+hit per response; a write through the API invalidates the cache in the handling
+worker, and other workers converge when their cached copy expires.
+
+The entire runtime-override path is inert unless the `CSP_RUNTIME_ALLOWLIST`
+feature flag is enabled, so the static, deploy-time policy remains the default
+and operators opt in explicitly.
+
+```
+Browser ──> Flask request
+ │
+ Talisman after_request (sets "Content-Security-Policy: default-src 'self'; …")
+ │
+ merge_runtime_csp_allowlist (if flag on: appends allowlist origins to frame-src, …)
+ │
+ Response ──> Browser ("…; frame-src 'self' https://embed.example")
+```
+
+#### Design decisions (resolved)
+
+- **Scope: global.** Allowlist entries apply server-wide. CSP is a single
+ per-response header; a global allowlist keeps the merge context-free and
+ avoids per-dashboard request plumbing. (Per-dashboard scoping is a possible
+ future extension.)
+- **Operator control: feature-flagged kill-switch.** The runtime override only
+ functions when `CSP_RUNTIME_ALLOWLIST` is on (default **off**). Operators who
+ want a purely static policy simply leave it off and the table is never
+ consulted.
+
+## New or Changed Public Interfaces
+
+### REST API
+
+- `GET /api/v1/csp_allowlist/` — list entries
+- `GET /api/v1/csp_allowlist/<id>` — get one
+- `POST /api/v1/csp_allowlist/` — create (validates origin + directive)
+- `PUT /api/v1/csp_allowlist/<id>` — update
+- `DELETE /api/v1/csp_allowlist/<id>` — delete
+- `DELETE /api/v1/csp_allowlist/?q=!(...)` — bulk delete
+
+All write methods require `can write on CSPAllowlist` (Admin-only by default).
+Origins are validated server-side: bare `scheme://host[:port]` only — no
+wildcards, paths, query strings, fragments, or credentials. Only a fixed set of
+directives may be widened (`frame-src`, `child-src`, `img-src`, `connect-src`,
+`media-src`, `font-src`); notably **not** `script-src`.
+
+### Model
+
+- `CSPAllowlistEntry` (`superset/models/csp.py`, table `csp_allowlist`):
+ `id`, `uuid`, `domain`, `directive` (default `frame-src`), `description`,
+ audit columns. Unique on `(domain, directive)`.
+
+### Feature flag
+
+- `CSP_RUNTIME_ALLOWLIST` (default `False`) — gates the entire runtime-override
+ path, backend and frontend.
+
+### Config
+
+- `CSP_RUNTIME_ALLOWLIST_CACHE_TTL` (default `30` seconds) — in-process cache TTL
+ for the allowlist; also settable via env var.
+
+### Frontend
+
+- New `IFRAME` dashboard layout component and its registration across the
+ dashboard util maps.
+- New `FeatureFlag.CspRuntimeAllowlist` enum member.
+
+### Security model
+
+- New `CSPAllowlist` view-menu added to `ADMIN_ONLY_VIEW_MENUS`.
+
+## New dependencies
+
+None. The implementation uses existing libraries (flask-talisman,
+Flask-AppBuilder, marshmallow, SQLAlchemy on the backend; existing
+`@superset-ui/core` components on the frontend).
+
+## Migration Plan and Compatibility
+
+- One Alembic migration adds the `csp_allowlist` table
+ (`a1b2c3d4e5f6`, down-revision `78a40c08b4be`). The table is empty on creation.
+- Fully backward compatible: with the feature flag off (the default), behavior is
+ identical to today — the static CSP is authoritative and the new table is never
+ read. No existing dashboards, URLs, or policies change.
+- Rollback: dropping the table and disabling the flag fully reverts the feature.
+
+### Security review notes
+
+This feature deliberately relocates a *capability* (widening the CSP) from a
+purely deploy-time operator control into a runtime, permission-gated, audited
+operation. The mitigations that keep it within Superset's trust model:
+
+- **Off by default** behind a feature flag the operator owns.
+- **Admin-only** write permission (a fully trusted principal per `SECURITY.md`).
+- **Strict origin validation** server-side — no wildcards, no `script-src`.
+- **Audit trail** via the audit mixin (`created_by` / `changed_by`).
+- The iframe is **sandboxed** and the merge can only *widen* a directive to a
+ specific origin, never relax nonce/`strict-dynamic` protections on
+ `script-src`.
+
+## Rejected Alternatives
+
+- **Dynamically reconfiguring flask-talisman at runtime.** Talisman is configured
+ once at app init. Rather than mutate its internals, we add our own
+ `after_request` hook that post-processes the header it already sets. This is
+ simpler, avoids depending on Talisman internals, and rides the same per-request
+ header machinery Talisman already uses for its nonce.
+- **Per-dashboard allowlist scoping.** More precise, but CSP is a per-response
+ header; per-dashboard scoping adds request-context complexity for marginal
+ benefit in the common case. Left as a possible future extension.
+- **"Always on" runtime override (no kill-switch).** Simpler, but moves a
+ security control fully into the app with no operator opt-out. Rejected in favor
+ of the feature-flag kill-switch.
+- **Shared/Redis-backed allowlist cache with cross-worker invalidation.**
+ Correct but heavier. A short-TTL in-process cache is good enough: writes take
+ effect immediately in the handling worker and within the TTL elsewhere, with no
+ new infrastructure dependency.
+
+## Implementation Status
+
+- [x] Feature flag `CSP_RUNTIME_ALLOWLIST` + `CSP_RUNTIME_ALLOWLIST_CACHE_TTL`
+- [x] `CSPAllowlistEntry` model + Alembic migration
+- [x] DAO, marshmallow schemas (with origin/directive validation), REST API
+- [x] Admin-only permission (`CSPAllowlist` view-menu)
+- [x] `after_request` CSP merge hook + in-process TTL cache + invalidation
+- [x] `IFRAME` dashboard component + registration across util maps
+- [x] Domain flagging + permission-gated "Enable domain in CSP" button
+- [x] Tests: backend unit (validation + merge + hook), backend integration (API),
+ frontend unit (util + component)
+- [ ] Docs (`docs/`) + `UPDATING.md` entry
+- [ ] Community/security review feedback
diff --git a/docs/static/feature-flags.json b/docs/static/feature-flags.json
index 8f7623e471d..2527e0e7274 100644
--- a/docs/static/feature-flags.json
+++ b/docs/static/feature-flags.json
@@ -21,6 +21,12 @@
"lifecycle": "development",
"description": "Enables experimental chart plugins"
},
+ {
+ "name": "CSP_RUNTIME_ALLOWLIST",
+ "default": false,
+ "lifecycle": "development",
+ "description": "Allow users with the \"can write on CSPAllowlist\" permission (Admins by default) to punch holes in the Content Security Policy at runtime, e.g. to allow a new domain to be embedded in a dashboard iframe component. When disabled, the CSP is purely static/deploy-time and the allowlist is ignored."
+ },
{
"name": "CSV_UPLOAD_PYARROW_ENGINE",
"default": false,
diff --git a/superset-frontend/packages/superset-ui-core/src/utils/featureFlags.ts b/superset-frontend/packages/superset-ui-core/src/utils/featureFlags.ts
index 35c0a37d562..e8edce3078f 100644
--- a/superset-frontend/packages/superset-ui-core/src/utils/featureFlags.ts
+++ b/superset-frontend/packages/superset-ui-core/src/utils/featureFlags.ts
@@ -32,6 +32,7 @@ export enum FeatureFlag {
AvoidColorsCollision = 'AVOID_COLORS_COLLISION',
ChartPluginsExperimental = 'CHART_PLUGINS_EXPERIMENTAL',
ConfirmDashboardDiff = 'CONFIRM_DASHBOARD_DIFF',
+ CspRuntimeAllowlist = 'CSP_RUNTIME_ALLOWLIST',
CssTemplates = 'CSS_TEMPLATES',
DashboardVirtualization = 'DASHBOARD_VIRTUALIZATION',
DashboardVirtualizationDeferData = 'DASHBOARD_VIRTUALIZATION_DEFER_DATA',
diff --git a/superset-frontend/src/dashboard/components/BuilderComponentPane/index.tsx b/superset-frontend/src/dashboard/components/BuilderComponentPane/index.tsx
index e772a38b934..9761e437332 100644
--- a/superset-frontend/src/dashboard/components/BuilderComponentPane/index.tsx
+++ b/superset-frontend/src/dashboard/components/BuilderComponentPane/index.tsx
@@ -29,6 +29,7 @@ import NewHeader from '../gridComponents/new/NewHeader';
import NewRow from '../gridComponents/new/NewRow';
import NewTabs from '../gridComponents/new/NewTabs';
import NewMarkdown from '../gridComponents/new/NewMarkdown';
+import NewIframe from '../gridComponents/new/NewIframe';
import NewDynamicComponent from '../gridComponents/new/NewDynamicComponent';
const BUILDER_PANE_WIDTH = 374;
@@ -98,6 +99,7 @@ const BuilderComponentPane = ({ topOffset = 0 }) => (
<NewColumn />
<NewHeader />
<NewMarkdown />
+ <NewIframe />
<NewDivider />
{dashboardComponents
.getAll()
diff --git a/superset-frontend/src/dashboard/components/gridComponents/Iframe/Iframe.test.tsx b/superset-frontend/src/dashboard/components/gridComponents/Iframe/Iframe.test.tsx
new file mode 100644
index 00000000000..ab8751f1c00
--- /dev/null
+++ b/superset-frontend/src/dashboard/components/gridComponents/Iframe/Iframe.test.tsx
@@ -0,0 +1,176 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import {
+ screen,
+ render,
+ userEvent,
+ fireEvent,
+ waitFor,
+} from 'spec/helpers/testing-library';
+import { isFeatureEnabled } from '@superset-ui/core';
+import newComponentFactory from 'src/dashboard/util/newComponentFactory';
+import {
+ IFRAME_TYPE,
+ DASHBOARD_GRID_TYPE,
+} from 'src/dashboard/util/componentTypes';
+import {
+ addCspAllowlistEntry,
+ fetchCspAllowlist,
+} from 'src/dashboard/util/cspAllowlist';
+import Iframe, { IframeProps } from './Iframe';
+
+jest.mock('@superset-ui/core', () => ({
+ ...jest.requireActual('@superset-ui/core'),
+ isFeatureEnabled: jest.fn(() => true),
+}));
+
+jest.mock('src/dashboard/util/cspAllowlist', () => ({
+ ...jest.requireActual('src/dashboard/util/cspAllowlist'),
+ fetchCspAllowlist: jest.fn(),
+ addCspAllowlistEntry: jest.fn(),
+}));
+
+const mockedFetch = fetchCspAllowlist as jest.Mock;
+const mockedAdd = addCspAllowlistEntry as jest.Mock;
+const mockedFeature = isFeatureEnabled as jest.Mock;
+
+const adminState = {
+ user: {
+ roles: { Admin: [['can_write', 'CSPAllowlist']] },
+ },
+};
+const gammaState = {
+ user: {
+ roles: { Gamma: [['can_read', 'Dashboard']] },
+ },
+};
+
+const makeComponent = (url = '') => {
+ const component = newComponentFactory(IFRAME_TYPE);
+ component.meta.url = url;
+ return component;
+};
+
+const baseProps = (overrides: Partial<IframeProps> = {}): IframeProps => ({
+ id: 'iframe-id',
+ parentId: 'parentId',
+ component: makeComponent('https://example.com'),
+ parentComponent: newComponentFactory(DASHBOARD_GRID_TYPE),
+ index: 0,
+ depth: 1,
+ editMode: false,
+ availableColumnCount: 12,
+ columnWidth: 50,
+ onResizeStart: jest.fn(),
+ onResize: jest.fn(),
+ onResizeStop: jest.fn(),
+ deleteComponent: jest.fn(),
+ handleComponentDrop: jest.fn(),
+ updateComponents: jest.fn(),
+ ...overrides,
+});
+
+const setup = (
+ props: Partial<IframeProps> = {},
+ initialState: object = adminState,
+) =>
+ render(<Iframe {...baseProps(props)} />, {
+ useRedux: true,
+ useDnd: true,
+ initialState,
+ });
+
+beforeEach(() => {
+ mockedFeature.mockReturnValue(true);
+ mockedFetch.mockResolvedValue(new Set<string>());
+ mockedAdd.mockResolvedValue(undefined);
+});
+
+afterEach(() => {
+ jest.clearAllMocks();
+});
+
+test('renders an iframe with the configured URL', () => {
+ setup();
+ const frame = screen.getByTitle('Embedded content');
+ expect(frame).toHaveAttribute('src', 'https://example.com');
+});
+
+test('renders an empty placeholder when no URL is configured', () => {
+ setup({ component: makeComponent('') });
+ expect(screen.queryByTitle('Embedded content')).not.toBeInTheDocument();
+ expect(screen.getByText('No URL configured')).toBeInTheDocument();
+});
+
+test('shows a URL input in edit mode and saves on blur', async () => {
+ const updateComponents = jest.fn();
+ setup({ editMode: true, updateComponents });
+ const input = screen.getByTestId('dashboard-iframe-url-input');
+ await userEvent.clear(input);
+ await userEvent.type(input, 'https://new.example.com');
+ fireEvent.blur(input);
+ // The update is keyed by the component's own generated id, so assert on the
+ // payload's single value rather than the key.
+ await waitFor(() => expect(updateComponents).toHaveBeenCalledTimes(1));
+ const updated = Object.values(updateComponents.mock.calls[0][0])[0] as {
+ meta: { url: string };
+ };
+ expect(updated.meta.url).toBe('https://new.example.com');
+});
+
+test('flags a non-allowlisted domain and offers an Enable button for admins', async () => {
+ mockedFetch.mockResolvedValue(new Set<string>());
+ setup({ editMode: true });
+ expect(
+ await screen.findByText('This domain is not allowed to be embedded'),
+ ).toBeInTheDocument();
+ const enableButton = screen.getByTestId('dashboard-iframe-enable-csp');
+ await userEvent.click(enableButton);
+ expect(mockedAdd).toHaveBeenCalledWith('https://example.com');
+});
+
+test('does not flag a domain that is already allowlisted', async () => {
+ mockedFetch.mockResolvedValue(new Set<string>(['https://example.com']));
+ setup({ editMode: true });
+ await waitFor(() => expect(mockedFetch).toHaveBeenCalled());
+ expect(
+ screen.queryByText('This domain is not allowed to be embedded'),
+ ).not.toBeInTheDocument();
+});
+
+test('hides the Enable button for users without the CSP permission', async () => {
+ mockedFetch.mockResolvedValue(new Set<string>());
+ setup({ editMode: true }, gammaState);
+ expect(
+ await screen.findByText('This domain is not allowed to be embedded'),
+ ).toBeInTheDocument();
+ expect(
+ screen.queryByTestId('dashboard-iframe-enable-csp'),
+ ).not.toBeInTheDocument();
+});
+
+test('never flags domains when the feature flag is disabled', async () => {
+ mockedFeature.mockReturnValue(false);
+ setup({ editMode: true });
+ // give any async effects a chance to run
+ await waitFor(() => expect(mockedFetch).not.toHaveBeenCalled());
+ expect(
+ screen.queryByText('This domain is not allowed to be embedded'),
+ ).not.toBeInTheDocument();
+});
diff --git a/superset-frontend/src/dashboard/components/gridComponents/Iframe/Iframe.tsx b/superset-frontend/src/dashboard/components/gridComponents/Iframe/Iframe.tsx
new file mode 100644
index 00000000000..d4994d59017
--- /dev/null
+++ b/superset-frontend/src/dashboard/components/gridComponents/Iframe/Iframe.tsx
@@ -0,0 +1,306 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { useCallback, useEffect, useState } from 'react';
+import { useSelector } from 'react-redux';
+import type { ResizeStartCallback, ResizeCallback } from 're-resizable';
+
+import { t } from '@apache-superset/core/translation';
+import { styled } from '@apache-superset/core/theme';
+import { FeatureFlag, isFeatureEnabled } from '@superset-ui/core';
+import { Alert } from '@apache-superset/core/components';
+import { Button, Input } from '@superset-ui/core/components';
+
+import { useToasts } from 'src/components/MessageToasts/withToasts';
+import { findPermission } from 'src/utils/findPermission';
+
+import DeleteComponentButton from 'src/dashboard/components/DeleteComponentButton';
+import { Draggable } from 'src/dashboard/components/dnd/DragDroppable';
+import HoverMenu from 'src/dashboard/components/menu/HoverMenu';
+import ResizableContainer from 'src/dashboard/components/resizable/ResizableContainer';
+import type { RootState } from 'src/dashboard/types';
+import type { LayoutItem } from 'src/dashboard/types';
+import type { DropResult } from 'src/dashboard/components/dnd/dragDroppableConfig';
+import { ROW_TYPE } from 'src/dashboard/util/componentTypes';
+import {
+ GRID_MIN_COLUMN_COUNT,
+ GRID_MIN_ROW_UNITS,
+ GRID_BASE_UNIT,
+} from 'src/dashboard/util/constants';
+import {
+ addCspAllowlistEntry,
+ CSP_ALLOWLIST_PERMISSION,
+ CSP_ALLOWLIST_VIEW,
+ fetchCspAllowlist,
+ getOrigin,
+ isEmbeddableUrl,
+} from 'src/dashboard/util/cspAllowlist';
+
+export interface IframeProps {
+ id: string;
+ parentId: string;
+ component: LayoutItem;
+ parentComponent: LayoutItem;
+ index: number;
+ depth: number;
+ editMode: boolean;
+
+ // grid related
+ availableColumnCount: number;
+ columnWidth: number;
+ onResizeStart: ResizeStartCallback;
+ onResize: ResizeCallback;
+ onResizeStop: ResizeCallback;
+
+ // dnd
+ deleteComponent: (id: string, parentId: string) => void;
+ handleComponentDrop: (dropResult: DropResult) => void;
+ updateComponents: (components: Record<string, LayoutItem>) => void;
+}
+
+const IframeStyles = styled.div`
+ ${({ theme }) => `
+ width: 100%;
+ height: 100%;
+ display: flex;
+ flex-direction: column;
+ gap: ${theme.sizeUnit * 2}px;
+ padding: ${theme.sizeUnit * 2}px;
+
+ .dashboard-iframe-frame {
+ flex: 1;
+ width: 100%;
+ border: 0;
+ min-height: ${theme.sizeUnit * 25}px;
+ }
+
+ .dashboard-iframe-empty {
+ flex: 1;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ color: ${theme.colorTextTertiary};
+ border: 1px dashed ${theme.colorBorder};
+ }
+ `}
+`;
+
+export default function Iframe(props: IframeProps) {
+ const {
+ component,
+ parentComponent,
+ index,
+ depth,
+ editMode,
+ availableColumnCount,
+ columnWidth,
+ onResizeStart,
+ onResize,
+ onResizeStop,
+ deleteComponent,
+ handleComponentDrop,
+ updateComponents,
+ } = props;
+
+ const { addSuccessToast, addDangerToast } = useToasts();
+ const roles = useSelector((state: RootState) => state.user?.roles);
+ const cspFeatureEnabled = isFeatureEnabled(FeatureFlag.CspRuntimeAllowlist);
+ const canManageCsp =
+ cspFeatureEnabled &&
+ findPermission(CSP_ALLOWLIST_PERMISSION, CSP_ALLOWLIST_VIEW, roles);
+
+ const url = (component.meta.url as string) ?? '';
+ const [draftUrl, setDraftUrl] = useState(url);
+ const [allowlist, setAllowlist] = useState<Set<string> | null>(null);
+ const [enabling, setEnabling] = useState(false);
+
+ const origin = getOrigin(url);
+
+ const refreshAllowlist = useCallback(() => {
+ if (!cspFeatureEnabled) {
+ return;
+ }
+ fetchCspAllowlist()
+ .then(setAllowlist)
+ .catch(() => setAllowlist(new Set()));
+ }, [cspFeatureEnabled]);
+
+ useEffect(() => {
+ refreshAllowlist();
+ }, [refreshAllowlist]);
+
+ useEffect(() => {
+ setDraftUrl(url);
+ }, [url]);
+
+ const handleDeleteComponent = useCallback(() => {
+ deleteComponent(component.id, parentComponent.id);
+ }, [component.id, deleteComponent, parentComponent.id]);
+
+ const handleSaveUrl = useCallback(() => {
+ const trimmed = draftUrl.trim();
+ if (trimmed === url) {
+ return;
+ }
+ updateComponents({
+ [component.id]: {
+ ...component,
+ meta: { ...component.meta, url: trimmed },
+ },
+ });
+ }, [component, draftUrl, updateComponents, url]);
+
+ const handleEnableDomain = useCallback(() => {
+ if (!origin) {
+ return;
+ }
+ setEnabling(true);
+ addCspAllowlistEntry(origin)
+ .then(() => {
+ addSuccessToast(
+ t('%(origin)s is now allowed to be embedded.', { origin }),
+ );
+ refreshAllowlist();
+ })
+ .catch(() =>
+ addDangerToast(t('Failed to allow %(origin)s in the CSP.', { origin })),
+ )
+ .finally(() => setEnabling(false));
+ }, [addDangerToast, addSuccessToast, origin, refreshAllowlist]);
+
+ // A domain is "flagged" when the runtime allowlist feature is on, the URL
+ // resolves to a concrete origin, the allowlist has loaded, and that origin is
+ // not yet allowed. When the feature is off we cannot reason about the CSP, so
+ // we never flag (the operator's static policy governs).
+ const domainFlagged =
+ cspFeatureEnabled &&
+ !!origin &&
+ allowlist !== null &&
+ !allowlist.has(origin);
+
+ const widthMultiple = component.meta.width ?? GRID_MIN_COLUMN_COUNT;
+
+ const renderBody = () => (
+ <IframeStyles data-test="dashboard-iframe">
+ {editMode && (
+ <Input
+ aria-label={t('Embed URL')}
+ data-test="dashboard-iframe-url-input"
+ placeholder={t('Paste a URL to embed, e.g. https://example.com')}
+ value={draftUrl}
+ onChange={e => setDraftUrl(e.target.value)}
+ onBlur={handleSaveUrl}
+ onPressEnter={handleSaveUrl}
+ />
+ )}
+ {domainFlagged && (
+ <Alert
+ type="warning"
+ showIcon
+ closable={false}
+ message={t('This domain is not allowed to be embedded')}
+ description={
+ canManageCsp
+ ? t(
+ '%(origin)s is blocked by the Content Security Policy. ' +
+ 'Enable it to allow this content to load.',
+ { origin },
+ )
+ : t(
+ '%(origin)s is blocked by the Content Security Policy. ' +
+ 'Ask an administrator to allow this domain.',
+ { origin },
+ )
+ }
+ action={
+ canManageCsp ? (
+ <Button
+ buttonStyle="primary"
+ buttonSize="small"
+ loading={enabling}
+ onClick={handleEnableDomain}
+ data-test="dashboard-iframe-enable-csp"
+ >
+ {t('Enable domain in CSP')}
+ </Button>
+ ) : undefined
+ }
+ />
+ )}
+ {isEmbeddableUrl(url) ? (
+ <iframe
+ className="dashboard-iframe-frame"
+ src={url}
+ title={t('Embedded content')}
+ sandbox="allow-scripts allow-same-origin allow-popups allow-forms"
+ />
+ ) : (
+ <div className="dashboard-iframe-empty">
+ {editMode
+ ? t('Enter a URL above to embed content')
+ : t('No URL configured')}
+ </div>
+ )}
+ </IframeStyles>
+ );
+
+ return (
+ <Draggable
+ component={component}
+ parentComponent={parentComponent}
+ orientation={parentComponent.type === ROW_TYPE ? 'column' : 'row'}
+ index={index}
+ depth={depth}
+ onDrop={handleComponentDrop}
+ editMode={editMode}
+ >
+ {({ dragSourceRef }: { dragSourceRef: React.Ref<HTMLDivElement> }) => (
+ <ResizableContainer
+ id={component.id}
+ adjustableWidth={parentComponent.type === ROW_TYPE}
+ adjustableHeight
+ widthStep={columnWidth}
+ widthMultiple={widthMultiple}
+ heightStep={GRID_BASE_UNIT}
+ heightMultiple={component.meta.height ?? GRID_MIN_ROW_UNITS}
+ minWidthMultiple={GRID_MIN_COLUMN_COUNT}
+ minHeightMultiple={GRID_MIN_ROW_UNITS}
+ maxWidthMultiple={availableColumnCount + widthMultiple}
+ onResizeStart={onResizeStart}
+ onResize={onResize}
+ onResizeStop={onResizeStop}
+ editMode={editMode}
+ >
+ <div
+ ref={dragSourceRef}
+ className="dashboard-component dashboard-component-iframe"
+ data-test="dashboard-component-iframe"
+ id={component.id}
+ >
+ {editMode && (
+ <HoverMenu position="top">
+ <DeleteComponentButton onDelete={handleDeleteComponent} />
+ </HoverMenu>
+ )}
+ {renderBody()}
+ </div>
+ </ResizableContainer>
+ )}
+ </Draggable>
+ );
+}
diff --git a/superset-frontend/src/dashboard/util/componentIsResizable.ts b/superset-frontend/src/dashboard/components/gridComponents/Iframe/index.ts
similarity index 73%
copy from superset-frontend/src/dashboard/util/componentIsResizable.ts
copy to superset-frontend/src/dashboard/components/gridComponents/Iframe/index.ts
index fd1d9d19aa6..0f564219991 100644
--- a/superset-frontend/src/dashboard/util/componentIsResizable.ts
+++ b/superset-frontend/src/dashboard/components/gridComponents/Iframe/index.ts
@@ -16,17 +16,6 @@
* specific language governing permissions and limitations
* under the License.
*/
-import {
- COLUMN_TYPE,
- CHART_TYPE,
- MARKDOWN_TYPE,
- DYNAMIC_TYPE,
-} from './componentTypes';
+import Iframe from './Iframe';
-export default function componentIsResizable(entity: { type: string }) {
- return (
- [COLUMN_TYPE, CHART_TYPE, MARKDOWN_TYPE, DYNAMIC_TYPE].indexOf(
- entity.type,
- ) > -1
- );
-}
+export default Iframe;
diff --git a/superset-frontend/src/dashboard/components/gridComponents/index.ts b/superset-frontend/src/dashboard/components/gridComponents/index.ts
index 8d3078c9ebe..74c0d8f75a6 100644
--- a/superset-frontend/src/dashboard/components/gridComponents/index.ts
+++ b/superset-frontend/src/dashboard/components/gridComponents/index.ts
@@ -22,6 +22,7 @@ import {
COLUMN_TYPE,
DIVIDER_TYPE,
HEADER_TYPE,
+ IFRAME_TYPE,
ROW_TYPE,
TAB_TYPE,
TABS_TYPE,
@@ -33,6 +34,7 @@ import Markdown from './Markdown';
import Column from './Column';
import Divider from './Divider';
import Header from './Header';
+import Iframe from './Iframe';
import Row from './Row';
import Tab from './Tab';
import Tabs from './Tabs';
@@ -44,6 +46,7 @@ export const componentLookup = {
[COLUMN_TYPE]: Column,
[DIVIDER_TYPE]: Divider,
[HEADER_TYPE]: Header,
+ [IFRAME_TYPE]: Iframe,
[ROW_TYPE]: Row,
[TAB_TYPE]: Tab,
[TABS_TYPE]: Tabs,
diff --git a/superset-frontend/src/dashboard/util/componentIsResizable.ts b/superset-frontend/src/dashboard/components/gridComponents/new/NewIframe.tsx
similarity index 62%
copy from superset-frontend/src/dashboard/util/componentIsResizable.ts
copy to superset-frontend/src/dashboard/components/gridComponents/new/NewIframe.tsx
index fd1d9d19aa6..9ff075248fb 100644
--- a/superset-frontend/src/dashboard/util/componentIsResizable.ts
+++ b/superset-frontend/src/dashboard/components/gridComponents/new/NewIframe.tsx
@@ -16,17 +16,20 @@
* specific language governing permissions and limitations
* under the License.
*/
-import {
- COLUMN_TYPE,
- CHART_TYPE,
- MARKDOWN_TYPE,
- DYNAMIC_TYPE,
-} from './componentTypes';
+import { t } from '@apache-superset/core/translation';
-export default function componentIsResizable(entity: { type: string }) {
+import { Icons } from '@superset-ui/core/components';
+import { IFRAME_TYPE } from '../../../util/componentTypes';
+import { NEW_IFRAME_ID } from '../../../util/constants';
+import DraggableNewComponent from './DraggableNewComponent';
+
+export default function DraggableNewIframe() {
return (
- [COLUMN_TYPE, CHART_TYPE, MARKDOWN_TYPE, DYNAMIC_TYPE].indexOf(
- entity.type,
- ) > -1
+ <DraggableNewComponent
+ id={NEW_IFRAME_ID}
+ type={IFRAME_TYPE}
+ label={t('Embed / Iframe')}
+ IconComponent={Icons.LinkOutlined}
+ />
);
}
diff --git a/superset-frontend/src/dashboard/types.ts b/superset-frontend/src/dashboard/types.ts
index da2eee81525..7474f8d9618 100644
--- a/superset-frontend/src/dashboard/types.ts
+++ b/superset-frontend/src/dashboard/types.ts
@@ -277,6 +277,8 @@ export type LayoutItemMeta = {
headerSize?: string;
/** Markdown source code for markdown components */
code?: string;
+ /** Embedded URL for iframe components */
+ url?: string;
/** Background style value for columns and rows */
background?: string;
/** Allow additional meta properties used by different component types */
diff --git a/superset-frontend/src/dashboard/util/componentIsResizable.test.ts b/superset-frontend/src/dashboard/util/componentIsResizable.test.ts
index 0d34cecfc98..ffde2ca2a5d 100644
--- a/superset-frontend/src/dashboard/util/componentIsResizable.test.ts
+++ b/superset-frontend/src/dashboard/util/componentIsResizable.test.ts
@@ -24,6 +24,7 @@ import {
DASHBOARD_ROOT_TYPE,
DIVIDER_TYPE,
HEADER_TYPE,
+ IFRAME_TYPE,
MARKDOWN_TYPE,
ROW_TYPE,
TABS_TYPE,
@@ -40,7 +41,7 @@ const notResizable = [
TAB_TYPE,
];
-const resizable = [COLUMN_TYPE, CHART_TYPE, MARKDOWN_TYPE];
+const resizable = [COLUMN_TYPE, CHART_TYPE, IFRAME_TYPE, MARKDOWN_TYPE];
// eslint-disable-next-line no-restricted-globals -- TODO: Migrate from describe blocks
describe('componentIsResizable', () => {
diff --git a/superset-frontend/src/dashboard/util/componentIsResizable.ts b/superset-frontend/src/dashboard/util/componentIsResizable.ts
index fd1d9d19aa6..3a05af774a7 100644
--- a/superset-frontend/src/dashboard/util/componentIsResizable.ts
+++ b/superset-frontend/src/dashboard/util/componentIsResizable.ts
@@ -19,13 +19,14 @@
import {
COLUMN_TYPE,
CHART_TYPE,
+ IFRAME_TYPE,
MARKDOWN_TYPE,
DYNAMIC_TYPE,
} from './componentTypes';
export default function componentIsResizable(entity: { type: string }) {
return (
- [COLUMN_TYPE, CHART_TYPE, MARKDOWN_TYPE, DYNAMIC_TYPE].indexOf(
+ [COLUMN_TYPE, CHART_TYPE, IFRAME_TYPE, MARKDOWN_TYPE, DYNAMIC_TYPE].indexOf(
entity.type,
) > -1
);
diff --git a/superset-frontend/src/dashboard/util/componentTypes.ts b/superset-frontend/src/dashboard/util/componentTypes.ts
index 0630d4411aa..ebc7dc93818 100644
--- a/superset-frontend/src/dashboard/util/componentTypes.ts
+++ b/superset-frontend/src/dashboard/util/componentTypes.ts
@@ -23,6 +23,7 @@ export const DASHBOARD_GRID_TYPE = 'GRID';
export const DASHBOARD_ROOT_TYPE = 'ROOT';
export const DIVIDER_TYPE = 'DIVIDER';
export const HEADER_TYPE = 'HEADER';
+export const IFRAME_TYPE = 'IFRAME';
export const MARKDOWN_TYPE = 'MARKDOWN';
export const NEW_COMPONENT_SOURCE_TYPE = 'NEW_COMPONENT_SOURCE';
export const ROW_TYPE = 'ROW';
@@ -39,6 +40,7 @@ export default {
DASHBOARD_ROOT_TYPE,
DIVIDER_TYPE,
HEADER_TYPE,
+ IFRAME_TYPE,
MARKDOWN_TYPE,
NEW_COMPONENT_SOURCE_TYPE,
ROW_TYPE,
diff --git a/superset-frontend/src/dashboard/util/constants.ts b/superset-frontend/src/dashboard/util/constants.ts
index 02cd7a991d6..a6a4559a262 100644
--- a/superset-frontend/src/dashboard/util/constants.ts
+++ b/superset-frontend/src/dashboard/util/constants.ts
@@ -27,6 +27,7 @@ export const NEW_CHART_ID = 'NEW_CHART_ID';
export const NEW_COLUMN_ID = 'NEW_COLUMN_ID';
export const NEW_DIVIDER_ID = 'NEW_DIVIDER_ID';
export const NEW_HEADER_ID = 'NEW_HEADER_ID';
+export const NEW_IFRAME_ID = 'NEW_IFRAME_ID';
export const NEW_MARKDOWN_ID = 'NEW_MARKDOWN_ID';
export const NEW_ROW_ID = 'NEW_ROW_ID';
export const NEW_TAB_ID = 'NEW_TAB_ID';
diff --git a/superset-frontend/src/dashboard/util/cspAllowlist.test.ts b/superset-frontend/src/dashboard/util/cspAllowlist.test.ts
new file mode 100644
index 00000000000..9bdeb912e18
--- /dev/null
+++ b/superset-frontend/src/dashboard/util/cspAllowlist.test.ts
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { SupersetClient } from '@superset-ui/core';
+import {
+ addCspAllowlistEntry,
+ CSP_ALLOWLIST_ENDPOINT,
+ fetchCspAllowlist,
+ getOrigin,
+ isEmbeddableUrl,
+} from './cspAllowlist';
+
+test('getOrigin extracts the bare origin from a URL', () => {
+ expect(getOrigin('https://example.com/path?q=1#frag')).toBe(
+ 'https://example.com',
+ );
+ expect(getOrigin('https://example.com:8443/x')).toBe(
+ 'https://example.com:8443',
+ );
+ expect(getOrigin('http://localhost:9000')).toBe('http://localhost:9000');
+});
+
+test('getOrigin returns null for empty or unparseable URLs', () => {
+ expect(getOrigin('')).toBeNull();
+ expect(getOrigin(undefined)).toBeNull();
+ expect(getOrigin('not a url')).toBeNull();
+ expect(getOrigin('example.com')).toBeNull();
+});
+
+test('isEmbeddableUrl is true only for absolute http(s) URLs', () => {
+ expect(isEmbeddableUrl('https://example.com')).toBe(true);
+ expect(isEmbeddableUrl('http://example.com/embed')).toBe(true);
+ expect(isEmbeddableUrl('ftp://example.com')).toBe(false);
+ // built via concatenation to avoid a literal javascript: URL in source
+ expect(isEmbeddableUrl(`${'java'}${'script'}:alert(1)`)).toBe(false);
+ expect(isEmbeddableUrl('')).toBe(false);
+ expect(isEmbeddableUrl(undefined)).toBe(false);
+});
+
+test('fetchCspAllowlist returns the set of origins for frame-src', async () => {
+ const getSpy = jest.spyOn(SupersetClient, 'get').mockResolvedValue({
+ json: {
+ result: [
+ { domain: 'https://a.com', directive: 'frame-src' },
+ { domain: 'https://b.com', directive: 'frame-src' },
+ { domain: 'https://c.com', directive: 'img-src' },
+ ],
+ },
+ } as any);
+
+ const allowlist = await fetchCspAllowlist();
+ expect(getSpy).toHaveBeenCalledWith({ endpoint: CSP_ALLOWLIST_ENDPOINT });
+ expect(allowlist.has('https://a.com')).toBe(true);
+ expect(allowlist.has('https://b.com')).toBe(true);
+ // img-src entries are excluded from the frame-src set
+ expect(allowlist.has('https://c.com')).toBe(false);
+
+ getSpy.mockRestore();
+});
+
+test('addCspAllowlistEntry posts the origin with the frame-src directive', async () => {
+ const postSpy = jest
+ .spyOn(SupersetClient, 'post')
+ .mockResolvedValue({} as any);
+
+ await addCspAllowlistEntry('https://example.com');
+ expect(postSpy).toHaveBeenCalledWith({
+ endpoint: CSP_ALLOWLIST_ENDPOINT,
+ jsonPayload: { domain: 'https://example.com', directive: 'frame-src' },
+ });
+
+ postSpy.mockRestore();
+});
diff --git a/superset-frontend/src/dashboard/util/cspAllowlist.ts b/superset-frontend/src/dashboard/util/cspAllowlist.ts
new file mode 100644
index 00000000000..561aa9c9c74
--- /dev/null
+++ b/superset-frontend/src/dashboard/util/cspAllowlist.ts
@@ -0,0 +1,79 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+import { SupersetClient } from '@superset-ui/core';
+
+export const CSP_ALLOWLIST_ENDPOINT = '/api/v1/csp_allowlist/';
+export const DEFAULT_EMBED_DIRECTIVE = 'frame-src';
+
+/** FAB requires `can write on CSPAllowlist`; Admins hold it by default. */
+export const CSP_ALLOWLIST_PERMISSION = 'can_write';
+export const CSP_ALLOWLIST_VIEW = 'CSPAllowlist';
+
+interface CSPAllowlistResult {
+ result?: { domain: string; directive: string }[];
+}
+
+/**
+ * Return the bare origin (scheme://host[:port]) of a URL, or null if the URL is
+ * empty or cannot be parsed. Mirrors the server-side `is_valid_csp_origin`
+ * canonicalization so the UX check matches what the backend will accept.
+ */
+export function getOrigin(url?: string | null): string | null {
+ if (!url) {
+ return null;
+ }
+ try {
+ const { origin } = new URL(url);
+ // URL parses things like "mailto:" to an opaque origin of "null"
+ return origin && origin !== 'null' ? origin : null;
+ } catch {
+ return null;
+ }
+}
+
+/** True only for absolute http(s) URLs that resolve to a concrete origin. */
+export function isEmbeddableUrl(url?: string | null): boolean {
+ const origin = getOrigin(url);
+ return !!origin && /^https?:\/\//.test(origin);
+}
+
+/** Fetch the set of origins currently allowed for the given CSP directive. */
+export async function fetchCspAllowlist(
+ directive: string = DEFAULT_EMBED_DIRECTIVE,
+): Promise<Set<string>> {
+ const response = await SupersetClient.get({
+ endpoint: CSP_ALLOWLIST_ENDPOINT,
+ });
+ const json = response.json as CSPAllowlistResult;
+ const origins = (json.result ?? [])
+ .filter(entry => entry.directive === directive)
+ .map(entry => entry.domain);
+ return new Set(origins);
+}
+
+/** Add a new allowlist entry (punch a hole in the CSP) for the given origin. */
+export async function addCspAllowlistEntry(
+ domain: string,
+ directive: string = DEFAULT_EMBED_DIRECTIVE,
+): Promise<void> {
+ await SupersetClient.post({
+ endpoint: CSP_ALLOWLIST_ENDPOINT,
+ jsonPayload: { domain, directive },
+ });
+}
diff --git a/superset-frontend/src/dashboard/util/getDetailedComponentWidth.ts b/superset-frontend/src/dashboard/util/getDetailedComponentWidth.ts
index 820f5da5f5a..7f47e416776 100644
--- a/superset-frontend/src/dashboard/util/getDetailedComponentWidth.ts
+++ b/superset-frontend/src/dashboard/util/getDetailedComponentWidth.ts
@@ -21,6 +21,7 @@ import { GRID_MIN_COLUMN_COUNT, GRID_COLUMN_COUNT } from './constants';
import {
ROW_TYPE,
COLUMN_TYPE,
+ IFRAME_TYPE,
MARKDOWN_TYPE,
CHART_TYPE,
DYNAMIC_TYPE,
@@ -107,6 +108,7 @@ export default function getDetailedComponentWidth({
});
} else if (
component.type === DYNAMIC_TYPE ||
+ component.type === IFRAME_TYPE ||
component.type === MARKDOWN_TYPE ||
component.type === CHART_TYPE
) {
diff --git a/superset-frontend/src/dashboard/util/isDashboardEmpty.ts b/superset-frontend/src/dashboard/util/isDashboardEmpty.ts
index 7f3cc62bf86..a0c7cd0a6ef 100644
--- a/superset-frontend/src/dashboard/util/isDashboardEmpty.ts
+++ b/superset-frontend/src/dashboard/util/isDashboardEmpty.ts
@@ -16,10 +16,16 @@
* specific language governing permissions and limitations
* under the License.
*/
-import { CHART_TYPE, MARKDOWN_TYPE, DYNAMIC_TYPE } from './componentTypes';
+import {
+ CHART_TYPE,
+ IFRAME_TYPE,
+ MARKDOWN_TYPE,
+ DYNAMIC_TYPE,
+} from './componentTypes';
const USER_CONTENT_COMPONENT_TYPE: string[] = [
CHART_TYPE,
+ IFRAME_TYPE,
MARKDOWN_TYPE,
DYNAMIC_TYPE,
];
diff --git a/superset-frontend/src/dashboard/util/isValidChild.ts b/superset-frontend/src/dashboard/util/isValidChild.ts
index 457e23217d7..207e1055806 100644
--- a/superset-frontend/src/dashboard/util/isValidChild.ts
+++ b/superset-frontend/src/dashboard/util/isValidChild.ts
@@ -39,6 +39,7 @@ import {
DASHBOARD_ROOT_TYPE,
DIVIDER_TYPE,
HEADER_TYPE,
+ IFRAME_TYPE,
MARKDOWN_TYPE,
ROW_TYPE,
TABS_TYPE,
@@ -64,6 +65,7 @@ const parentMaxDepthLookup: Record<string, Record<string, number>> = {
[DASHBOARD_GRID_TYPE]: {
[CHART_TYPE]: depthOne,
[DYNAMIC_TYPE]: depthOne,
+ [IFRAME_TYPE]: depthOne,
[MARKDOWN_TYPE]: depthOne,
[COLUMN_TYPE]: depthOne,
[DIVIDER_TYPE]: depthOne,
@@ -75,6 +77,7 @@ const parentMaxDepthLookup: Record<string, Record<string, number>> = {
[ROW_TYPE]: {
[CHART_TYPE]: depthFour,
[DYNAMIC_TYPE]: depthFour,
+ [IFRAME_TYPE]: depthFour,
[MARKDOWN_TYPE]: depthFour,
[COLUMN_TYPE]: depthFour,
},
@@ -86,6 +89,7 @@ const parentMaxDepthLookup: Record<string, Record<string, number>> = {
[TAB_TYPE]: {
[CHART_TYPE]: depthFive,
[DYNAMIC_TYPE]: depthFive,
+ [IFRAME_TYPE]: depthFive,
[MARKDOWN_TYPE]: depthFive,
[COLUMN_TYPE]: depthThree,
[DIVIDER_TYPE]: depthFive,
@@ -97,6 +101,7 @@ const parentMaxDepthLookup: Record<string, Record<string, number>> = {
[COLUMN_TYPE]: {
[CHART_TYPE]: depthFive,
[HEADER_TYPE]: depthFive,
+ [IFRAME_TYPE]: depthFive,
[MARKDOWN_TYPE]: depthFive,
[ROW_TYPE]: depthThree,
[DIVIDER_TYPE]: depthThree,
@@ -108,6 +113,7 @@ const parentMaxDepthLookup: Record<string, Record<string, number>> = {
[DYNAMIC_TYPE]: {},
[DIVIDER_TYPE]: {},
[HEADER_TYPE]: {},
+ [IFRAME_TYPE]: {},
[MARKDOWN_TYPE]: {},
};
diff --git a/superset-frontend/src/dashboard/util/newComponentFactory.ts b/superset-frontend/src/dashboard/util/newComponentFactory.ts
index 2855d4a63f1..cb6a618b871 100644
--- a/superset-frontend/src/dashboard/util/newComponentFactory.ts
+++ b/superset-frontend/src/dashboard/util/newComponentFactory.ts
@@ -24,6 +24,7 @@ import {
COLUMN_TYPE,
DIVIDER_TYPE,
HEADER_TYPE,
+ IFRAME_TYPE,
MARKDOWN_TYPE,
ROW_TYPE,
TABS_TYPE,
@@ -56,6 +57,7 @@ const typeToDefaultMetaData: Record<string, LayoutItemMeta | null> = {
headerSize: MEDIUM_HEADER,
background: BACKGROUND_TRANSPARENT,
},
+ [IFRAME_TYPE]: { width: GRID_DEFAULT_CHART_WIDTH, height: 50, url: '' },
[MARKDOWN_TYPE]: { width: GRID_DEFAULT_CHART_WIDTH, height: 50 },
[ROW_TYPE]: { background: BACKGROUND_TRANSPARENT },
[TABS_TYPE]: null,
diff --git a/superset-frontend/src/dashboard/util/shouldWrapChildInRow.ts b/superset-frontend/src/dashboard/util/shouldWrapChildInRow.ts
index 17ebcc844dd..22fdcbd411a 100644
--- a/superset-frontend/src/dashboard/util/shouldWrapChildInRow.ts
+++ b/superset-frontend/src/dashboard/util/shouldWrapChildInRow.ts
@@ -20,6 +20,7 @@ import {
DASHBOARD_GRID_TYPE,
CHART_TYPE,
COLUMN_TYPE,
+ IFRAME_TYPE,
MARKDOWN_TYPE,
TAB_TYPE,
} from './componentTypes';
@@ -31,7 +32,11 @@ interface WrapChildParams {
}
type ParentTypes = typeof DASHBOARD_GRID_TYPE | typeof TAB_TYPE;
-type ChildTypes = typeof CHART_TYPE | typeof COLUMN_TYPE | typeof MARKDOWN_TYPE;
+type ChildTypes =
+ | typeof CHART_TYPE
+ | typeof COLUMN_TYPE
+ | typeof IFRAME_TYPE
+ | typeof MARKDOWN_TYPE;
const typeToWrapChildLookup: Record<
ParentTypes,
@@ -40,12 +45,14 @@ const typeToWrapChildLookup: Record<
[DASHBOARD_GRID_TYPE]: {
[CHART_TYPE]: true,
[COLUMN_TYPE]: true,
+ [IFRAME_TYPE]: true,
[MARKDOWN_TYPE]: true,
},
[TAB_TYPE]: {
[CHART_TYPE]: true,
[COLUMN_TYPE]: true,
+ [IFRAME_TYPE]: true,
[MARKDOWN_TYPE]: true,
},
};
diff --git a/superset/config.py b/superset/config.py
index fdfdbbda513..ebe400f4be7 100644
--- a/superset/config.py
+++ b/superset/config.py
@@ -639,6 +639,12 @@ DEFAULT_FEATURE_FLAGS: dict[str, bool] = {
# Enables experimental chart plugins
# @lifecycle: development
"CHART_PLUGINS_EXPERIMENTAL": False,
+ # Allow users with the "can write on CSPAllowlist" permission (Admins by
+ # default) to punch holes in the Content Security Policy at runtime, e.g. to
+ # allow a new domain to be embedded in a dashboard iframe component. When
+ # disabled, the CSP is purely static/deploy-time and the allowlist is ignored.
+ # @lifecycle: development
+ "CSP_RUNTIME_ALLOWLIST": False,
# Experimental PyArrow engine for CSV parsing (may have issues with dates/nulls)
# @lifecycle: development
"CSV_UPLOAD_PYARROW_ENGINE": False,
@@ -2336,6 +2342,16 @@ DATABASE_OAUTH2_TIMEOUT = timedelta(seconds=30)
# Enable/disable CSP warning
CONTENT_SECURITY_POLICY_WARNING = True
+# When the CSP_RUNTIME_ALLOWLIST feature flag is enabled, runtime allowlist
+# entries are merged into the response CSP header. To avoid a metadata DB hit on
+# every response, the allowlist is cached in-process for this many seconds. A
+# write through the REST API invalidates the cache in the worker that handled the
+# write; other workers pick up the change once their cached copy expires. Lower
+# this for faster cross-worker propagation, raise it to reduce DB load.
+CSP_RUNTIME_ALLOWLIST_CACHE_TTL = int(
+ os.environ.get("CSP_RUNTIME_ALLOWLIST_CACHE_TTL", 30)
+)
+
# Do you want Talisman enabled?
TALISMAN_ENABLED = utils.cast_to_boolean(os.environ.get("TALISMAN_ENABLED", True))
diff --git a/superset/csp_allowlist/__init__.py b/superset/csp_allowlist/__init__.py
new file mode 100644
index 00000000000..13a83393a91
--- /dev/null
+++ b/superset/csp_allowlist/__init__.py
@@ -0,0 +1,16 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
diff --git a/superset/csp_allowlist/api.py b/superset/csp_allowlist/api.py
new file mode 100644
index 00000000000..a8ae6d777b0
--- /dev/null
+++ b/superset/csp_allowlist/api.py
@@ -0,0 +1,179 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import logging
+from typing import Any
+
+from flask import Response
+from flask_appbuilder.api import expose, protect, rison as parse_rison, safe
+from flask_appbuilder.models.sqla.interface import SQLAInterface
+from flask_babel import ngettext
+
+from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod
+from superset.csp_allowlist.schemas import (
+ CSPAllowlistEntryPostSchema,
+ CSPAllowlistEntryPutSchema,
+ get_delete_ids_schema,
+ openapi_spec_methods_override,
+)
+from superset.daos.csp import CSPAllowlistDAO
+from superset.extensions import event_logger
+from superset.models.csp import CSPAllowlistEntry
+from superset.security.csp import invalidate_csp_allowlist_cache
+from superset.views.base_api import (
+ BaseSupersetModelRestApi,
+ RelatedFieldFilter,
+ statsd_metrics,
+)
+from superset.views.filters import BaseFilterRelatedUsers, FilterRelatedOwners
+
+logger = logging.getLogger(__name__)
+
+
+class CSPAllowlistRestApi(BaseSupersetModelRestApi):
+ """CRUD API for runtime Content Security Policy allowlist entries.
+
+ The ``CSPAllowlist`` view-menu is registered as admin-only (see
+ ``SupersetSecurityManager.ADMIN_ONLY_VIEW_MENUS``), so only Admins (or a
+ custom role explicitly granted ``can_write on CSPAllowlist``) may mutate the
+ allowlist. Mutations invalidate the per-worker CSP cache so the new policy
+ takes effect without a server restart.
+ """
+
+ datamodel = SQLAInterface(CSPAllowlistEntry)
+
+ include_route_methods = RouteMethod.REST_MODEL_VIEW_CRUD_SET | {
+ RouteMethod.RELATED,
+ "bulk_delete", # not using RouteMethod since locally defined
+ }
+ class_permission_name = "CSPAllowlist"
+ method_permission_name = MODEL_API_RW_METHOD_PERMISSION_MAP
+
+ resource_name = "csp_allowlist"
+ allow_browser_login = True
+
+ show_columns = [
+ "id",
+ "domain",
+ "directive",
+ "description",
+ "changed_on_delta_humanized",
+ "changed_by.first_name",
+ "changed_by.id",
+ "changed_by.last_name",
+ "created_by.first_name",
+ "created_by.id",
+ "created_by.last_name",
+ ]
+ list_columns = [
+ "id",
+ "domain",
+ "directive",
+ "description",
+ "created_on",
+ "changed_on_delta_humanized",
+ "changed_by.first_name",
+ "changed_by.id",
+ "changed_by.last_name",
+ "created_by.first_name",
+ "created_by.id",
+ "created_by.last_name",
+ ]
+ add_columns = ["domain", "directive", "description"]
+ edit_columns = add_columns
+ order_columns = ["domain", "directive", "changed_on", "created_on"]
+
+ add_model_schema = CSPAllowlistEntryPostSchema()
+ edit_model_schema = CSPAllowlistEntryPutSchema()
+
+ allowed_rel_fields = {"created_by", "changed_by"}
+
+ apispec_parameter_schemas = {
+ "get_delete_ids_schema": get_delete_ids_schema,
+ }
+ openapi_spec_tag = "CSP Allowlist"
+ openapi_spec_methods = openapi_spec_methods_override
+
+ related_field_filters = {
+ "changed_by": RelatedFieldFilter("first_name", FilterRelatedOwners),
+ }
+ base_related_field_filters = {
+ "changed_by": [["id", BaseFilterRelatedUsers, lambda: []]],
+ }
+
+ def post_add(self, item: CSPAllowlistEntry) -> None:
+ invalidate_csp_allowlist_cache()
+
+ def post_update(self, item: CSPAllowlistEntry) -> None:
+ invalidate_csp_allowlist_cache()
+
+ def post_delete(self, item: CSPAllowlistEntry) -> None:
+ invalidate_csp_allowlist_cache()
+
+ @expose("/", methods=("DELETE",))
+ @protect()
+ @safe
+ @statsd_metrics
+ @event_logger.log_this_with_context(
+ action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.bulk_delete",
+ log_to_statsd=False,
+ )
+ @parse_rison(get_delete_ids_schema)
+ def bulk_delete(self, **kwargs: Any) -> Response:
+ """Bulk delete CSP allowlist entries.
+ ---
+ delete:
+ summary: Bulk delete CSP allowlist entries
+ parameters:
+ - in: query
+ name: q
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/get_delete_ids_schema'
+ responses:
+ 200:
+ description: CSP allowlist entries bulk delete
+ content:
+ application/json:
+ schema:
+ type: object
+ properties:
+ message:
+ type: string
+ 401:
+ $ref: '#/components/responses/401'
+ 404:
+ $ref: '#/components/responses/404'
+ 422:
+ $ref: '#/components/responses/422'
+ 500:
+ $ref: '#/components/responses/500'
+ """
+ item_ids = kwargs["rison"]
+ entries = CSPAllowlistDAO.find_by_ids(item_ids)
+ if not entries:
+ return self.response_404()
+ CSPAllowlistDAO.delete(entries)
+ invalidate_csp_allowlist_cache()
+ return self.response(
+ 200,
+ message=ngettext(
+ "Deleted %(num)d CSP allowlist entry",
+ "Deleted %(num)d CSP allowlist entries",
+ num=len(entries),
+ ),
+ )
diff --git a/superset/csp_allowlist/schemas.py b/superset/csp_allowlist/schemas.py
new file mode 100644
index 00000000000..69025f47edf
--- /dev/null
+++ b/superset/csp_allowlist/schemas.py
@@ -0,0 +1,111 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from flask_babel import gettext as _
+from marshmallow import fields, Schema, validates
+from marshmallow.exceptions import ValidationError
+
+from superset.security.csp import (
+ ALLOWED_DIRECTIVES,
+ is_valid_csp_directive,
+ is_valid_csp_origin,
+)
+
+domain_description = (
+ "A bare origin to allow, e.g. 'https://example.com' or "
+ "'https://example.com:8443'. Wildcards, paths, query strings and fragments "
+ "are rejected."
+)
+directive_description = (
+ "The CSP directive to widen. Defaults to 'frame-src'. One of: "
+ f"{', '.join(sorted(ALLOWED_DIRECTIVES))}."
+)
+
+openapi_spec_methods_override = {
+ "get": {"get": {"summary": "Get a CSP allowlist entry"}},
+ "get_list": {
+ "get": {
+ "summary": "Get a list of CSP allowlist entries",
+ "description": "Gets a list of runtime Content Security Policy "
+ "allowlist entries, use Rison or JSON query parameters for "
+ "filtering, sorting, pagination and for selecting specific "
+ "columns and metadata.",
+ }
+ },
+ "post": {"post": {"summary": "Create a CSP allowlist entry"}},
+ "put": {"put": {"summary": "Update a CSP allowlist entry"}},
+ "delete": {"delete": {"summary": "Delete a CSP allowlist entry"}},
+ "info": {"get": {"summary": "Get metadata information about this API resource"}},
+}
+
+get_delete_ids_schema = {"type": "array", "items": {"type": "integer"}}
+
+
+def validate_origin(value: str) -> None:
+ if not is_valid_csp_origin(value):
+ raise ValidationError(
+ _(
+ "'%(value)s' is not a valid origin. Provide a bare "
+ "scheme://host[:port] value with no wildcard, path, query or "
+ "fragment.",
+ value=value,
+ )
+ )
+
+
+def validate_directive(value: str) -> None:
+ if not is_valid_csp_directive(value):
+ raise ValidationError(
+ _(
+ "'%(value)s' is not an allowed CSP directive. Allowed: %(allowed)s.",
+ value=value,
+ allowed=", ".join(sorted(ALLOWED_DIRECTIVES)),
+ )
+ )
+
+
+class CSPAllowlistEntryPostSchema(Schema):
+ domain = fields.String(required=True, metadata={"description": domain_description})
+ directive = fields.String(
+ required=False,
+ load_default="frame-src",
+ metadata={"description": directive_description},
+ )
+ description = fields.String(required=False, allow_none=True)
+
+ @validates("domain")
+ def validate_domain(self, value: str) -> None:
+ validate_origin(value)
+
+ @validates("directive")
+ def validate_directive_field(self, value: str) -> None:
+ validate_directive(value)
+
+
+class CSPAllowlistEntryPutSchema(Schema):
+ domain = fields.String(required=False, metadata={"description": domain_description})
+ directive = fields.String(
+ required=False, metadata={"description": directive_description}
+ )
+ description = fields.String(required=False, allow_none=True)
+
+ @validates("domain")
+ def validate_domain(self, value: str) -> None:
+ validate_origin(value)
+
+ @validates("directive")
+ def validate_directive_field(self, value: str) -> None:
+ validate_directive(value)
diff --git a/superset/daos/csp.py b/superset/daos/csp.py
new file mode 100644
index 00000000000..d524a97de88
--- /dev/null
+++ b/superset/daos/csp.py
@@ -0,0 +1,22 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from superset.daos.base import BaseDAO
+from superset.models.csp import CSPAllowlistEntry
+
+
+class CSPAllowlistDAO(BaseDAO[CSPAllowlistEntry]):
+ pass
diff --git a/superset/initialization/__init__.py b/superset/initialization/__init__.py
index 7f79fbc3b0d..a81fc26d3ef 100644
--- a/superset/initialization/__init__.py
+++ b/superset/initialization/__init__.py
@@ -165,6 +165,7 @@ class SupersetAppInitializer: # pylint: disable=too-many-public-methods
from superset.cachekeys.api import CacheRestApi
from superset.charts.api import ChartRestApi
from superset.charts.data.api import ChartDataRestApi
+ from superset.csp_allowlist.api import CSPAllowlistRestApi
from superset.css_templates.api import CssTemplateRestApi
from superset.dashboards.api import DashboardRestApi
from superset.dashboards.filter_state.api import DashboardFilterStateRestApi
@@ -253,6 +254,7 @@ class SupersetAppInitializer: # pylint: disable=too-many-public-methods
appbuilder.add_api(CacheRestApi)
appbuilder.add_api(ChartRestApi)
appbuilder.add_api(ChartDataRestApi)
+ appbuilder.add_api(CSPAllowlistRestApi)
appbuilder.add_api(CssTemplateRestApi)
appbuilder.add_api(ThemeRestApi)
appbuilder.add_api(CurrentUserRestApi)
@@ -1020,6 +1022,18 @@ class SupersetAppInitializer: # pylint: disable=too-many-public-methods
# Flask-Compress
Compress(self.superset_app)
+ # Runtime CSP allowlist merge. Registered BEFORE Talisman so that, since
+ # Flask runs after_request callbacks in reverse registration order, this
+ # runs AFTER Talisman has set the CSP header and can widen it with the
+ # operator-curated allowlist. Inert unless CSP_RUNTIME_ALLOWLIST is on.
+ from flask import Response
+
+ @self.superset_app.after_request
+ def merge_runtime_csp_allowlist(response: Response) -> Response:
+ from superset.security.csp import apply_runtime_csp_allowlist
+
+ return apply_runtime_csp_allowlist(response)
+
# Talisman
talisman_enabled = self.config["TALISMAN_ENABLED"]
talisman_config = (
diff --git a/superset/migrations/versions/2026-06-26_12-00_a1b2c3d4e5f6_add_csp_allowlist_table.py b/superset/migrations/versions/2026-06-26_12-00_a1b2c3d4e5f6_add_csp_allowlist_table.py
new file mode 100644
index 00000000000..127a4c2fd2c
--- /dev/null
+++ b/superset/migrations/versions/2026-06-26_12-00_a1b2c3d4e5f6_add_csp_allowlist_table.py
@@ -0,0 +1,83 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""add csp_allowlist table
+
+Creates the ``csp_allowlist`` table backing runtime Content Security Policy
+"punched holes". Each row widens a single CSP directive (``frame-src`` by
+default) to allow one additional origin. The table is only consulted when the
+``CSP_RUNTIME_ALLOWLIST`` feature flag is enabled.
+
+Revision ID: a1b2c3d4e5f6
+Revises: 78a40c08b4be
+Create Date: 2026-06-26 12:00:00.000000
+
+"""
+
+import sqlalchemy as sa
+from sqlalchemy_utils import UUIDType
+
+from superset.migrations.shared.utils import (
+ create_fks_for_table,
+ create_table,
+ drop_table,
+)
+
+# revision identifiers, used by Alembic.
+revision = "a1b2c3d4e5f6"
+down_revision = "78a40c08b4be"
+
+
+def upgrade() -> None:
+ create_table(
+ "csp_allowlist",
+ sa.Column("id", sa.Integer(), primary_key=True),
+ sa.Column("uuid", UUIDType(binary=True), nullable=True, unique=True),
+ sa.Column("domain", sa.String(length=255), nullable=False),
+ sa.Column(
+ "directive",
+ sa.String(length=64),
+ nullable=False,
+ server_default="frame-src",
+ ),
+ sa.Column("description", sa.Text(), nullable=True),
+ # AuditMixinNullable columns
+ sa.Column("created_on", sa.DateTime(), nullable=True),
+ sa.Column("changed_on", sa.DateTime(), nullable=True),
+ sa.Column("created_by_fk", sa.Integer(), nullable=True),
+ sa.Column("changed_by_fk", sa.Integer(), nullable=True),
+ sa.UniqueConstraint(
+ "domain", "directive", name="uq_csp_allowlist_domain_directive"
+ ),
+ )
+ create_fks_for_table(
+ "fk_csp_allowlist_created_by_fk_ab_user",
+ "csp_allowlist",
+ "ab_user",
+ ["created_by_fk"],
+ ["id"],
+ )
+ create_fks_for_table(
+ "fk_csp_allowlist_changed_by_fk_ab_user",
+ "csp_allowlist",
+ "ab_user",
+ ["changed_by_fk"],
+ ["id"],
+ )
+
+
+def downgrade() -> None:
+ drop_table("csp_allowlist")
diff --git a/superset/models/csp.py b/superset/models/csp.py
new file mode 100644
index 00000000000..07ecf29fb11
--- /dev/null
+++ b/superset/models/csp.py
@@ -0,0 +1,55 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Models backing the runtime Content Security Policy (CSP) allowlist."""
+
+from flask_appbuilder import Model
+from sqlalchemy import Column, Integer, String, Text, UniqueConstraint
+
+from superset.models.helpers import AuditMixinNullable, UUIDMixin
+
+# Default CSP directive a hole is punched into. ``frame-src`` governs which
+# origins may be embedded in an <iframe>, which is the primary use case for the
+# allowlist (the first-class dashboard iframe component).
+DEFAULT_CSP_DIRECTIVE = "frame-src"
+
+
+class CSPAllowlistEntry(AuditMixinNullable, UUIDMixin, Model):
+ """A runtime "punched hole" in the Content Security Policy.
+
+ Each row widens a single CSP directive (``frame-src`` by default) to allow a
+ single additional origin. Entries are merged into the response CSP header at
+ request time, but only when the ``CSP_RUNTIME_ALLOWLIST`` feature flag is
+ enabled, so operators retain full control over whether the static, deploy-time
+ policy can be overridden at runtime at all.
+ """
+
+ __tablename__ = "csp_allowlist"
+ __table_args__ = (
+ UniqueConstraint(
+ "domain", "directive", name="uq_csp_allowlist_domain_directive"
+ ),
+ )
+
+ id = Column(Integer, primary_key=True)
+ # A bare origin, e.g. ``https://example.com`` or ``https://example.com:8443``.
+ # Never a wildcard, path, query or fragment — see ``is_valid_csp_origin``.
+ domain = Column(String(255), nullable=False)
+ directive = Column(String(64), nullable=False, default=DEFAULT_CSP_DIRECTIVE)
+ description = Column(Text, nullable=True)
+
+ def __repr__(self) -> str:
+ return f"<CSPAllowlistEntry {self.directive} {self.domain}>"
diff --git a/superset/security/csp.py b/superset/security/csp.py
new file mode 100644
index 00000000000..3d655edefb1
--- /dev/null
+++ b/superset/security/csp.py
@@ -0,0 +1,191 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Runtime Content Security Policy (CSP) allowlist.
+
+This module merges operator-curated *runtime* CSP allowlist entries (the
+``csp_allowlist`` table) into the response CSP header that flask-talisman sets at
+request time. It is intentionally inert unless the ``CSP_RUNTIME_ALLOWLIST``
+feature flag is enabled, so the static deploy-time policy remains the default and
+operators opt in to runtime overrides explicitly.
+"""
+
+from __future__ import annotations
+
+import time
+from collections import OrderedDict
+from urllib.parse import urlparse
+
+from flask import current_app, Response
+
+from superset.extensions import feature_flag_manager
+
+#: CSP directives an allowlist entry is permitted to widen. Restricting the set
+#: keeps an entry from, say, loosening ``script-src`` in a way that would defeat
+#: the nonce/strict-dynamic protections.
+ALLOWED_DIRECTIVES = frozenset(
+ {
+ "frame-src",
+ "child-src",
+ "img-src",
+ "connect-src",
+ "media-src",
+ "font-src",
+ }
+)
+
+CSP_HEADER = "Content-Security-Policy"
+CSP_REPORT_ONLY_HEADER = "Content-Security-Policy-Report-Only"
+
+
+def is_valid_csp_origin(origin: str) -> bool:
+ """Return ``True`` if ``origin`` is a bare ``scheme://host[:port]`` source.
+
+ The check is deliberately strict: it rejects wildcards, paths, query strings,
+ fragments and embedded credentials so that an allowlist entry can only ever
+ widen the policy to one specific, fully-qualified origin. This is the
+ server-side enforcement point — the frontend performs the same check for UX,
+ but must not be relied upon for security.
+ """
+ if not origin or any(ch.isspace() for ch in origin):
+ return False
+ if "*" in origin:
+ return False
+ try:
+ parsed = urlparse(origin)
+ except ValueError:
+ return False
+ if parsed.scheme not in ("http", "https"):
+ return False
+ if not parsed.hostname:
+ return False
+ if parsed.username or parsed.password:
+ return False
+ if parsed.path or parsed.query or parsed.fragment:
+ return False
+ return True
+
+
+def is_valid_csp_directive(directive: str) -> bool:
+ """Return ``True`` if ``directive`` is one an entry may widen."""
+ return directive in ALLOWED_DIRECTIVES
+
+
+def _parse_csp(value: str) -> "OrderedDict[str, list[str]]":
+ """Parse a CSP header string into an ordered ``directive -> sources`` map."""
+ directives: OrderedDict[str, list[str]] = OrderedDict()
+ for part in value.split(";"):
+ tokens = part.split()
+ if not tokens:
+ continue
+ directives[tokens[0]] = tokens[1:]
+ return directives
+
+
+def _serialize_csp(directives: "OrderedDict[str, list[str]]") -> str:
+ """Serialize a ``directive -> sources`` map back into a CSP header string."""
+ return "; ".join(
+ " ".join([name, *sources]).strip() for name, sources in directives.items()
+ )
+
+
+def merge_allowlist_into_csp(header_value: str, additions: dict[str, list[str]]) -> str:
+ """Merge ``additions`` into an existing CSP header value.
+
+ For a directive that already exists, missing origins are appended. For a
+ directive that does not exist yet (e.g. ``frame-src`` when the base policy
+ only declares ``default-src``), the directive is seeded with ``'self'`` so the
+ addition widens rather than unexpectedly narrows the effective policy.
+ """
+ directives = _parse_csp(header_value)
+ for directive, domains in additions.items():
+ sources = directives.get(directive)
+ if sources is None:
+ directives[directive] = ["'self'", *domains]
+ else:
+ for domain in domains:
+ if domain not in sources:
+ sources.append(domain)
+ return _serialize_csp(directives)
+
+
+class _CSPAllowlistCache:
+ """In-process, time-bounded cache of the runtime CSP allowlist.
+
+ The metadata DB is the source of truth; this cache only exists to avoid a
+ query on every response. A write through the REST API invalidates the cache
+ in the worker that handled it; other workers converge once their copy
+ expires (``CSP_RUNTIME_ALLOWLIST_CACHE_TTL`` seconds).
+ """
+
+ def __init__(self) -> None:
+ self._directive_map: dict[str, list[str]] | None = None
+ self._loaded_at: float = 0.0
+
+ def get(self) -> dict[str, list[str]]:
+ ttl = current_app.config.get("CSP_RUNTIME_ALLOWLIST_CACHE_TTL", 30)
+ now = time.monotonic()
+ if self._directive_map is None or (now - self._loaded_at) > ttl:
+ self._directive_map = self._load()
+ self._loaded_at = now
+ return self._directive_map
+
+ @staticmethod
+ def _load() -> dict[str, list[str]]:
+ # Imported lazily to avoid a circular import at module load time.
+ from superset.daos.csp import CSPAllowlistDAO
+
+ directive_map: dict[str, list[str]] = {}
+ for entry in CSPAllowlistDAO.find_all():
+ if not is_valid_csp_directive(entry.directive):
+ # Defensive: never trust a stale/legacy row to widen an
+ # unexpected directive.
+ continue
+ directive_map.setdefault(entry.directive, []).append(entry.domain)
+ return directive_map
+
+ def invalidate(self) -> None:
+ self._directive_map = None
+ self._loaded_at = 0.0
+
+
+csp_allowlist_cache = _CSPAllowlistCache()
+
+
+def invalidate_csp_allowlist_cache() -> None:
+ """Drop this worker's cached copy of the allowlist (call after a write)."""
+ csp_allowlist_cache.invalidate()
+
+
+def apply_runtime_csp_allowlist(response: Response) -> Response:
+ """Merge runtime allowlist entries into the response CSP header(s).
+
+ Registered as an ``after_request`` handler *before* flask-talisman so that it
+ runs *after* Talisman has set the header (Flask invokes ``after_request``
+ callbacks in reverse registration order). A no-op unless the
+ ``CSP_RUNTIME_ALLOWLIST`` feature flag is enabled and the allowlist is
+ non-empty.
+ """
+ if not feature_flag_manager.is_feature_enabled("CSP_RUNTIME_ALLOWLIST"):
+ return response
+ additions = csp_allowlist_cache.get()
+ if not additions:
+ return response
+ for header in (CSP_HEADER, CSP_REPORT_ONLY_HEADER):
+ value = response.headers.get(header)
+ if value:
+ response.headers[header] = merge_allowlist_into_csp(value, additions)
+ return response
diff --git a/superset/security/manager.py b/superset/security/manager.py
index 83d84fff407..e8b03bf26c2 100644
--- a/superset/security/manager.py
+++ b/superset/security/manager.py
@@ -733,6 +733,9 @@ class SupersetSecurityManager( # pylint: disable=too-many-public-methods
ADMIN_ONLY_VIEW_MENUS = {
"Access Requests",
"Action Logs",
+ # Runtime CSP allowlist: punching holes in the Content Security Policy is
+ # a trusted, security-sensitive operation reserved for Admins.
+ "CSPAllowlist",
"Extensions",
"Log",
"List Users",
diff --git a/tests/integration_tests/csp_allowlist/__init__.py b/tests/integration_tests/csp_allowlist/__init__.py
new file mode 100644
index 00000000000..13a83393a91
--- /dev/null
+++ b/tests/integration_tests/csp_allowlist/__init__.py
@@ -0,0 +1,16 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
diff --git a/tests/integration_tests/csp_allowlist/api_tests.py b/tests/integration_tests/csp_allowlist/api_tests.py
new file mode 100644
index 00000000000..b48060b7140
--- /dev/null
+++ b/tests/integration_tests/csp_allowlist/api_tests.py
@@ -0,0 +1,124 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Integration tests for the CSP allowlist REST API."""
+
+import pytest
+
+import tests.integration_tests.test_app # noqa: F401
+from superset import db
+from superset.models.csp import CSPAllowlistEntry
+from superset.utils import json
+from tests.integration_tests.base_tests import SupersetTestCase
+from tests.integration_tests.constants import ADMIN_USERNAME, GAMMA_USERNAME
+
+
+class TestCSPAllowlistApi(SupersetTestCase):
+ def insert_entry(
+ self,
+ domain: str,
+ directive: str = "frame-src",
+ ) -> CSPAllowlistEntry:
+ admin = self.get_user("admin")
+ entry = CSPAllowlistEntry(
+ domain=domain,
+ directive=directive,
+ created_by=admin,
+ changed_by=admin,
+ )
+ db.session.add(entry)
+ db.session.commit()
+ return entry
+
+ @pytest.fixture
+ def create_entries(self):
+ with self.create_app().app_context():
+ entries = [
+ self.insert_entry("https://a.example.com"),
+ self.insert_entry("https://b.example.com"),
+ ]
+ yield entries
+ for entry in entries:
+ db.session.delete(entry)
+ db.session.commit()
+
+ @pytest.mark.usefixtures("create_entries")
+ def test_get_list_as_admin(self):
+ self.login(ADMIN_USERNAME)
+ rv = self.client.get("/api/v1/csp_allowlist/")
+ assert rv.status_code == 200
+ data = json.loads(rv.data.decode("utf-8"))
+ assert data["count"] >= 2
+
+ def test_gamma_cannot_list(self):
+ """The CSPAllowlist view-menu is admin-only."""
+ self.login(GAMMA_USERNAME)
+ rv = self.client.get("/api/v1/csp_allowlist/")
+ assert rv.status_code in (401, 403, 404)
+
+ def test_admin_can_create_valid_entry(self):
+ self.login(ADMIN_USERNAME)
+ rv = self.client.post(
+ "/api/v1/csp_allowlist/",
+ json={"domain": "https://new.example.com", "directive": "frame-src"},
+ )
+ assert rv.status_code == 201
+ data = json.loads(rv.data.decode("utf-8"))
+ created = db.session.query(CSPAllowlistEntry).get(data["id"])
+ assert created.domain == "https://new.example.com"
+ db.session.delete(created)
+ db.session.commit()
+
+ def test_create_rejects_invalid_origin(self):
+ self.login(ADMIN_USERNAME)
+ rv = self.client.post(
+ "/api/v1/csp_allowlist/",
+ json={"domain": "https://*.evil.com/path"},
+ )
+ assert rv.status_code == 400
+ assert (
+ db.session.query(CSPAllowlistEntry)
+ .filter_by(domain="https://*.evil.com/path")
+ .first()
+ is None
+ )
+
+ def test_create_rejects_disallowed_directive(self):
+ self.login(ADMIN_USERNAME)
+ rv = self.client.post(
+ "/api/v1/csp_allowlist/",
+ json={"domain": "https://ok.example.com", "directive": "script-src"},
+ )
+ assert rv.status_code == 400
+
+ def test_gamma_cannot_create(self):
+ self.login(GAMMA_USERNAME)
+ rv = self.client.post(
+ "/api/v1/csp_allowlist/",
+ json={"domain": "https://nope.example.com"},
+ )
+ assert rv.status_code in (401, 403, 404)
+
+ @pytest.mark.usefixtures("create_entries")
+ def test_admin_can_delete(self):
+ self.login(ADMIN_USERNAME)
+ entry = (
+ db.session.query(CSPAllowlistEntry)
+ .filter_by(domain="https://a.example.com")
+ .one()
+ )
+ rv = self.client.delete(f"/api/v1/csp_allowlist/{entry.id}")
+ assert rv.status_code == 200
diff --git a/tests/unit_tests/security/csp_test.py b/tests/unit_tests/security/csp_test.py
new file mode 100644
index 00000000000..3d800a97922
--- /dev/null
+++ b/tests/unit_tests/security/csp_test.py
@@ -0,0 +1,160 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+from unittest.mock import patch
+
+import pytest
+from flask import Response
+
+from superset.security import csp as csp_module
+from superset.security.csp import (
+ apply_runtime_csp_allowlist,
+ is_valid_csp_directive,
+ is_valid_csp_origin,
+ merge_allowlist_into_csp,
+)
+
+
+@pytest.mark.parametrize(
+ "origin",
+ [
+ "https://example.com",
+ "http://example.com",
+ "https://example.com:8443",
+ "http://localhost:9000",
+ ],
+)
+def test_is_valid_csp_origin_accepts_bare_origins(origin: str) -> None:
+ assert is_valid_csp_origin(origin) is True
+
+
+@pytest.mark.parametrize(
+ "origin",
+ [
+ "",
+ "example.com", # missing scheme
+ "ftp://example.com", # disallowed scheme
+ "https://*.example.com", # wildcard
+ "https://example.com/path", # path
+ "https://example.com?q=1", # query
+ "https://example.com#frag", # fragment
+ "https://user:pw@example.com", # credentials
+ "javascript:alert(1)", # non-network scheme
+ "https://example.com ", # whitespace
+ ],
+)
+def test_is_valid_csp_origin_rejects_unsafe_values(origin: str) -> None:
+ assert is_valid_csp_origin(origin) is False
+
+
+def test_is_valid_csp_directive() -> None:
+ assert is_valid_csp_directive("frame-src") is True
+ assert is_valid_csp_directive("img-src") is True
+ # script-src is intentionally not allowlistable
+ assert is_valid_csp_directive("script-src") is False
+ assert is_valid_csp_directive("nonsense") is False
+
+
+def test_merge_seeds_a_missing_directive_with_self() -> None:
+ base = "default-src 'self'; img-src 'self' data:"
+ merged = merge_allowlist_into_csp(base, {"frame-src": ["https://maps.example"]})
+ assert "frame-src 'self' https://maps.example" in merged
+ # existing directives are preserved
+ assert "default-src 'self'" in merged
+ assert "img-src 'self' data:" in merged
+
+
+def test_merge_appends_to_existing_directive_without_duplicates() -> None:
+ base = "default-src 'self'; img-src 'self' data:"
+ merged = merge_allowlist_into_csp(
+ base, {"img-src": ["https://cdn.example", "data:"]}
+ )
+ assert "https://cdn.example" in merged
+ # the already-present 'data:' source is not duplicated
+ assert merged.count("data:") == 1
+
+
+def test_merge_is_a_noop_for_empty_additions() -> None:
+ base = "default-src 'self'"
+ assert merge_allowlist_into_csp(base, {}) == base
+
+
+CSP_HEADER = "Content-Security-Policy"
+
+
+def _response_with_csp() -> Response:
+ response = Response()
+ response.headers[CSP_HEADER] = "default-src 'self'"
+ return response
+
+
+def test_apply_runtime_csp_allowlist_noop_when_flag_disabled() -> None:
+ with patch.object(
+ csp_module.feature_flag_manager, "is_feature_enabled", return_value=False
+ ):
+ response = _response_with_csp()
+ apply_runtime_csp_allowlist(response)
+ assert response.headers[CSP_HEADER] == "default-src 'self'"
+
+
+def test_apply_runtime_csp_allowlist_noop_when_allowlist_empty() -> None:
+ with (
+ patch.object(
+ csp_module.feature_flag_manager,
+ "is_feature_enabled",
+ return_value=True,
+ ),
+ patch.object(csp_module.csp_allowlist_cache, "get", return_value={}),
+ ):
+ response = _response_with_csp()
+ apply_runtime_csp_allowlist(response)
+ assert response.headers[CSP_HEADER] == "default-src 'self'"
+
+
+def test_apply_runtime_csp_allowlist_merges_when_enabled() -> None:
+ with (
+ patch.object(
+ csp_module.feature_flag_manager,
+ "is_feature_enabled",
+ return_value=True,
+ ),
+ patch.object(
+ csp_module.csp_allowlist_cache,
+ "get",
+ return_value={"frame-src": ["https://embed.example"]},
+ ),
+ ):
+ response = _response_with_csp()
+ apply_runtime_csp_allowlist(response)
+ assert "frame-src 'self' https://embed.example" in response.headers[CSP_HEADER]
+
+
+def test_apply_runtime_csp_allowlist_skips_response_without_header() -> None:
+ with (
+ patch.object(
+ csp_module.feature_flag_manager,
+ "is_feature_enabled",
+ return_value=True,
+ ),
+ patch.object(
+ csp_module.csp_allowlist_cache,
+ "get",
+ return_value={"frame-src": ["https://embed.example"]},
+ ),
+ ):
+ response = Response() # no CSP header set
+ apply_runtime_csp_allowlist(response)
+ assert CSP_HEADER not in response.headers