Skip to content

The agent that grows with you

feat(desktop): sandboxed embed primitive for plugins

tobenwarriord81db86d618c
diff --git a/apps/desktop/src/components/ui/sandboxed-frame.test.tsx b/apps/desktop/src/components/ui/sandboxed-frame.test.tsx
new file mode 100644
index 00000000000..ebbc18cae02
--- /dev/null
+++ b/apps/desktop/src/components/ui/sandboxed-frame.test.tsx
@@ -0,0 +1,82 @@
+// @vitest-environment jsdom
+import { cleanup, render } from '@testing-library/react'
+import { afterEach, describe, expect, it } from 'vitest'
+
+import { SANDBOXED_FRAME_DEFAULT_SANDBOX, SandboxedFrame, sanitizeFrameSandbox } from './sandboxed-frame'
+
+afterEach(cleanup)
+
+describe('sanitizeFrameSandbox', () => {
+ it('defaults to the opaque-origin posture for an empty or missing sandbox', () => {
+ expect(sanitizeFrameSandbox(undefined)).toBe(SANDBOXED_FRAME_DEFAULT_SANDBOX)
+ expect(sanitizeFrameSandbox('')).toBe(SANDBOXED_FRAME_DEFAULT_SANDBOX)
+ expect(sanitizeFrameSandbox(' ')).toBe(SANDBOXED_FRAME_DEFAULT_SANDBOX)
+ })
+
+ it('keeps safe tokens and dedupes them', () => {
+ expect(sanitizeFrameSandbox('allow-scripts allow-forms')).toBe('allow-scripts allow-forms')
+ expect(sanitizeFrameSandbox('allow-scripts allow-scripts allow-forms')).toBe('allow-scripts allow-forms')
+ })
+
+ it('is case-insensitive, because the attribute is (a mixed-case escape used to pass)', () => {
+ // HTML: "an unordered set of unique space-separated tokens that are ASCII
+ // case-insensitive", and Chromium lower-cases each token before matching.
+ expect(sanitizeFrameSandbox('ALLOW-SAME-ORIGIN allow-scripts')).toBe('allow-scripts')
+ expect(sanitizeFrameSandbox('Allow-Top-Navigation allow-forms')).toBe('allow-forms')
+ expect(sanitizeFrameSandbox('ALLOW-SCRIPTS Allow-Forms')).toBe('allow-scripts allow-forms')
+ })
+
+ it('drops a token it does not know instead of forwarding it', () => {
+ // An allowlist, not a blocklist: a token nobody has heard of (or a future
+ // one) must not reach the attribute on the strength of being unlisted.
+ expect(sanitizeFrameSandbox('allow-scripts allow-invented-thing')).toBe('allow-scripts')
+ expect(sanitizeFrameSandbox('allow-invented-thing')).toBe(SANDBOXED_FRAME_DEFAULT_SANDBOX)
+ })
+
+ it('allows the safe tokens a real embed asks for', () => {
+ expect(sanitizeFrameSandbox('allow-downloads allow-forms allow-presentation')).toBe(
+ 'allow-downloads allow-forms allow-presentation'
+ )
+ })
+
+ it('strips every realm-escaping token, falling back to the default when nothing is left', () => {
+ expect(sanitizeFrameSandbox('allow-scripts allow-same-origin')).toBe('allow-scripts')
+ expect(sanitizeFrameSandbox('allow-top-navigation allow-popups')).toBe(SANDBOXED_FRAME_DEFAULT_SANDBOX)
+ expect(sanitizeFrameSandbox('allow-same-origin')).toBe(SANDBOXED_FRAME_DEFAULT_SANDBOX)
+ expect(sanitizeFrameSandbox('allow-modals allow-storage-access-by-user-activation')).toBe(
+ SANDBOXED_FRAME_DEFAULT_SANDBOX
+ )
+ })
+})
+
+describe('SandboxedFrame', () => {
+ it('renders a sandboxed, no-referrer, lazily-loaded iframe', () => {
+ const { container } = render(<SandboxedFrame src="https://example.com/feed" title="Feed" />)
+ const frame = container.querySelector('iframe')!
+
+ expect(frame.getAttribute('sandbox')).toBe(SANDBOXED_FRAME_DEFAULT_SANDBOX)
+ expect(frame.getAttribute('referrerpolicy')).toBe('no-referrer')
+ expect(frame.getAttribute('loading')).toBe('lazy')
+ expect(frame.getAttribute('src')).toBe('https://example.com/feed')
+ expect(frame.getAttribute('title')).toBe('Feed')
+ })
+
+ it('keeps its no-referrer / lazy posture when a caller tries to override it', () => {
+ const { container } = render(
+ <SandboxedFrame loading="eager" referrerPolicy="unsafe-url" src="https://example.com" title="Feed" />
+ )
+
+ const frame = container.querySelector('iframe')!
+
+ expect(frame.getAttribute('referrerpolicy')).toBe('no-referrer')
+ expect(frame.getAttribute('loading')).toBe('lazy')
+ })
+
+ it('refuses an allow-same-origin / popup escape even when asked for one', () => {
+ const { container } = render(
+ <SandboxedFrame sandbox="allow-scripts allow-same-origin allow-popups" src="https://example.com" title="Feed" />
+ )
+
+ expect(container.querySelector('iframe')!.getAttribute('sandbox')).toBe('allow-scripts')
+ })
+})
diff --git a/apps/desktop/src/components/ui/sandboxed-frame.tsx b/apps/desktop/src/components/ui/sandboxed-frame.tsx
new file mode 100644
index 00000000000..e5b66baa2b5
--- /dev/null
+++ b/apps/desktop/src/components/ui/sandboxed-frame.tsx
@@ -0,0 +1,82 @@
+import type * as React from 'react'
+
+import { cn } from '@/lib/utils'
+
+/** The sandbox posture guest content gets by default: scripts run, the frame
+ * has an OPAQUE origin (no reach into the app, its storage, or the bridge). */
+export const SANDBOXED_FRAME_DEFAULT_SANDBOX = 'allow-scripts'
+
+/** The only sandbox tokens a caller may add. An ALLOWLIST, not a blocklist:
+ * HTML defines the attribute as a case-insensitive token set and browsers
+ * lower-case each token before matching it, so a blocklist is only as good as
+ * its spelling — `ALLOW-SAME-ORIGIN` sailed past a case-sensitive set and the
+ * browser honoured it. With an allowlist an unrecognised token is dropped
+ * instead of forwarded, so the posture holds by construction.
+ *
+ * What is deliberately NOT here: `allow-same-origin` (re-opens cookies and
+ * storage for the embedded origin — the opaque origin IS the containment) and
+ * the navigation / popup / modals family (a guest driving the host window).
+ * A caller wanting one of those needs a real review, not a prop.
+ *
+ * `allow-downloads` IS here: it writes to the user's disk from guest content,
+ * which is why it is not in the default posture — but it is a user-visible
+ * download rather than an escape from the sandbox, and a plugin embedding a
+ * document viewer legitimately needs it. */
+const ALLOWED_SANDBOX_TOKENS = new Set([
+ 'allow-downloads',
+ 'allow-forms',
+ 'allow-orientation-lock',
+ 'allow-pointer-lock',
+ 'allow-presentation',
+ 'allow-scripts'
+])
+
+/** Keep only the allowed tokens from a caller's sandbox string (pure, for
+ * tests). Case-insensitive, because the attribute is: every token is
+ * lower-cased before the membership test, so no spelling of a forbidden token
+ * survives. Empty result falls back to the default posture rather than an
+ * unsandboxed frame — a frame with NO sandbox attribute is fully privileged. */
+export function sanitizeFrameSandbox(sandbox: string | undefined): string {
+ const tokens = (sandbox ?? '')
+ .split(/\s+/)
+ .map(token => token.trim().toLowerCase())
+ .filter(token => token && ALLOWED_SANDBOX_TOKENS.has(token))
+
+ return tokens.length > 0 ? [...new Set(tokens)].join(' ') : SANDBOXED_FRAME_DEFAULT_SANDBOX
+}
+
+export interface SandboxedFrameProps extends Omit<React.ComponentProps<'iframe'>, 'src'> {
+ /** Absolute `http(s):` URL to embed. */
+ src: string
+ /** Accessible title (required — an untitled frame is unlabelled in the a11y tree). */
+ title: string
+}
+
+/**
+ * The sanctioned way for a plugin to embed external web content. Renders a
+ * sandboxed iframe with the app's guest-content posture — never a raw
+ * Electron `<webview>` (which would land on the app's own `persist:` preview
+ * partition, sharing its cookies and storage).
+ *
+ * `sandbox` may add tokens from the allowlist (`allow-forms`,
+ * `allow-downloads`, …); everything else — `allow-same-origin`, the
+ * navigation/popup/modals family, and any token this primitive does not know —
+ * is dropped, case-insensitively. See {@link sanitizeFrameSandbox}.
+ *
+ * `loading` and `referrerPolicy` are the primitive's posture, not defaults: the
+ * spread comes first so a caller cannot re-open them (`unsafe-url` would leak
+ * the app's origin to the embedded site, which is the point of `no-referrer`).
+ */
+export function SandboxedFrame({ className, sandbox, src, title, ...props }: SandboxedFrameProps) {
+ return (
+ <iframe
+ {...props}
+ className={cn('size-full border-0 bg-transparent', className)}
+ loading="lazy"
+ referrerPolicy="no-referrer"
+ sandbox={sanitizeFrameSandbox(sandbox)}
+ src={src}
+ title={title}
+ />
+ )
+}
diff --git a/apps/desktop/src/sdk/index.ts b/apps/desktop/src/sdk/index.ts
index 445347c62f2..fae4e24603a 100644
--- a/apps/desktop/src/sdk/index.ts
+++ b/apps/desktop/src/sdk/index.ts
@@ -1775,6 +1775,15 @@ export { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover
* layout classes — it just bakes in `type="button"` and a stable `data-slot`.
* Use it for rows and regions; `Button` is for ordinary compact actions. */
export { RowButton } from '@/components/ui/row-button'
+/** The sanctioned embed primitive for external web content: a sandboxed
+ * iframe (opaque origin, `allow-scripts` by default) — never a raw
+ * `<webview>`, which would land on the app's own preview partition. */
+export {
+ SANDBOXED_FRAME_DEFAULT_SANDBOX,
+ SandboxedFrame,
+ type SandboxedFrameProps,
+ sanitizeFrameSandbox
+} from '@/components/ui/sandboxed-frame'
export { ScrollArea } from '@/components/ui/scroll-area'
export { SearchField } from '@/components/ui/search-field'
export { SegmentedControl } from '@/components/ui/segmented-control'
diff --git a/website/docs/developer-guide/desktop-plugin-sdk.md b/website/docs/developer-guide/desktop-plugin-sdk.md
index 5e13137691b..ed1b1cb6e2e 100644
--- a/website/docs/developer-guide/desktop-plugin-sdk.md
+++ b/website/docs/developer-guide/desktop-plugin-sdk.md
@@ -695,6 +695,16 @@ Migrations for the plugins that motivated this slot:
that reaches into Settings; the settings *values* still go through
`host.settings` (allowlisted keys) and `THEMES_AREA`.
+### Embedding external content
+
+Use the SDK's `<SandboxedFrame src title />` for any external web content
+(reader views, dashboards, docs). It renders a sandboxed iframe with the app's
+guest-content posture: opaque origin, `allow-scripts` by default, `no-referrer`,
+lazy loading. Realm-escaping tokens (`allow-same-origin`, top-navigation,
+popups, modals) are stripped even if passed — the opaque origin IS the
+containment. Never mount a raw Electron `<webview>`: it lands on the app's
+`persist:` preview partition, sharing the app's cookies and storage.
+
### Transcript directives — inline components the model addresses
`TRANSCRIPT_DIRECTIVE_AREA` makes the transcript itself a contribution area.