Skip to content

The agent that grows with you

fix(desktop): SandboxedFrame takes an explicit prop allowlist, not iframe attrs

teknium1c2645ef14c62
diff --git a/apps/desktop/src/components/ui/sandboxed-frame.test.tsx b/apps/desktop/src/components/ui/sandboxed-frame.test.tsx
index 3f36f109b0f..22e6b6d53f9 100644
--- a/apps/desktop/src/components/ui/sandboxed-frame.test.tsx
+++ b/apps/desktop/src/components/ui/sandboxed-frame.test.tsx
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
import { cleanup, render } from '@testing-library/react'
-import { afterEach, describe, expect, it } from 'vitest'
+import { afterEach, describe, expect, it, vi } from 'vitest'
import { SANDBOXED_FRAME_DEFAULT_SANDBOX, SandboxedFrame, sanitizeFrameSandbox } from './sandboxed-frame'
@@ -49,10 +49,23 @@ describe('SandboxedFrame', () => {
})
it('keeps its posture when a caller tries to re-open it through props', () => {
+ // The props type is an explicit allowlist, so a plugin has to cast to get
+ // these past the compiler — and the DOM must still never see them:
+ // `allow` delegates Permissions-Policy (the electron permission handlers
+ // grant media without checking the frame's origin), `srcdoc` overrides
+ // `src`, `name` makes the frame a navigation target.
+ const hostile = {
+ allow: 'camera; microphone',
+ allowFullScreen: true,
+ loading: 'eager',
+ name: 'target',
+ referrerPolicy: 'unsafe-url',
+ srcDoc: '<script>1</script>'
+ } as Record<string, unknown>
+
const { container } = render(
<SandboxedFrame
- loading="eager"
- referrerPolicy="unsafe-url"
+ {...hostile}
sandbox="allow-scripts allow-same-origin allow-popups"
src="https://example.com"
title="Feed"
@@ -64,5 +77,23 @@ describe('SandboxedFrame', () => {
expect(frame.getAttribute('sandbox')).toBe('allow-scripts')
expect(frame.getAttribute('referrerpolicy')).toBe('no-referrer')
expect(frame.getAttribute('loading')).toBe('lazy')
+
+ for (const attr of ['allow', 'allowfullscreen', 'name', 'srcdoc']) {
+ expect(frame.hasAttribute(attr), attr).toBe(false)
+ }
+ })
+
+ it('renders nothing for a src outside http(s)/data and says why', () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
+
+ for (const src of ['file:///etc/passwd', 'blob:https://example.com/x', 'javascript:1', '/relative']) {
+ const { container, unmount } = render(<SandboxedFrame src={src} title="Feed" />)
+
+ expect(container.querySelector('iframe'), src).toBeNull()
+ unmount()
+ }
+
+ expect(warn).toHaveBeenCalledTimes(4)
+ warn.mockRestore()
})
})
diff --git a/apps/desktop/src/components/ui/sandboxed-frame.tsx b/apps/desktop/src/components/ui/sandboxed-frame.tsx
index b08ee9af2ca..e391f6224e1 100644
--- a/apps/desktop/src/components/ui/sandboxed-frame.tsx
+++ b/apps/desktop/src/components/ui/sandboxed-frame.tsx
@@ -45,9 +45,28 @@ export function sanitizeFrameSandbox(sandbox: string | undefined): string {
return tokens.length > 0 ? [...new Set(tokens)].join(' ') : SANDBOXED_FRAME_DEFAULT_SANDBOX
}
-export interface SandboxedFrameProps extends Omit<React.ComponentProps<'iframe'>, 'src'> {
- /** Absolute `http(s):` (or `data:`) URL to embed. */
+/** Schemes an embed may point at. `file:` would read the user's disk into a
+ * scripted frame, `blob:`/`javascript:` are host-realm content, and a
+ * relative path resolves against the app's own origin. */
+const ALLOWED_SRC_SCHEMES = new Set(['data:', 'http:', 'https:'])
+
+/** Explicit allowlist, NOT `ComponentProps<'iframe'>`: an iframe's attribute
+ * surface is how a frame gains authority — `allow` delegates
+ * Permissions-Policy (the electron permission handlers grant media capture
+ * without checking which frame asked), `srcdoc` replaces `src` with caller
+ * markup, `name` makes the frame a navigation target, `csp`/`credentialless`
+ * change the guest realm. None of those are props here, and no spread reaches
+ * the element, so a plugin cannot smuggle one through a cast either. */
+export interface SandboxedFrameProps {
+ className?: string
+ onError?: React.ReactEventHandler<HTMLIFrameElement>
+ onLoad?: React.ReactEventHandler<HTMLIFrameElement>
+ ref?: React.Ref<HTMLIFrameElement>
+ /** Extra sandbox tokens; filtered through the allowlist above. */
+ sandbox?: string
+ /** Absolute `http(s):` or `data:` URL to embed. Anything else renders nothing. */
src: string
+ style?: React.CSSProperties
/** Accessible title (required — an untitled frame is unlabelled in the a11y tree). */
title: string
}
@@ -63,20 +82,37 @@ export interface SandboxedFrameProps extends Omit<React.ComponentProps<'iframe'>
* 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`).
+ * `loading` and `referrerPolicy` are the primitive's posture, not props
+ * (`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) {
+export function SandboxedFrame({ className, onError, onLoad, ref, sandbox, src, style, title }: SandboxedFrameProps) {
+ if (!hasAllowedScheme(src)) {
+ console.warn(`[SandboxedFrame] refusing to embed ${JSON.stringify(src)}: only http(s): and data: URLs are allowed`)
+
+ return null
+ }
+
return (
<iframe
- {...props}
className={cn('size-full border-0 bg-transparent', className)}
loading="lazy"
+ onError={onError}
+ onLoad={onLoad}
+ ref={ref}
referrerPolicy="no-referrer"
sandbox={sanitizeFrameSandbox(sandbox)}
src={src}
+ style={style}
title={title}
/>
)
}
+
+function hasAllowedScheme(src: string): boolean {
+ try {
+ return ALLOWED_SRC_SCHEMES.has(new URL(src).protocol)
+ } catch {
+ return false
+ }
+}
diff --git a/apps/desktop/src/sdk/index.ts b/apps/desktop/src/sdk/index.ts
index fae4e24603a..64c5e7572f5 100644
--- a/apps/desktop/src/sdk/index.ts
+++ b/apps/desktop/src/sdk/index.ts
@@ -1778,12 +1778,7 @@ 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 { SandboxedFrame, type SandboxedFrameProps } 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 1897b11cf9e..a2622df8559 100644
--- a/website/docs/developer-guide/desktop-plugin-sdk.md
+++ b/website/docs/developer-guide/desktop-plugin-sdk.md
@@ -704,25 +704,33 @@ lazy loading. Never mount a raw Electron `<webview>`: it lands on the app's
`persist:` preview partition, sharing the app's cookies and storage.
```ts
-interface SandboxedFrameProps extends Omit<ComponentProps<'iframe'>, 'src'> {
- src: string // http(s): or data: URL to embed
- title: string // required — an untitled frame is unlabelled in the a11y tree
- sandbox?: string // extra tokens; filtered through sanitizeFrameSandbox
+interface SandboxedFrameProps {
+ src: string // absolute http(s): or data: URL; any other scheme renders nothing (console.warn)
+ title: string // required — an untitled frame is unlabelled in the a11y tree
+ sandbox?: string // extra tokens; filtered through the allowlist below
+ className?: string; style?: CSSProperties
+ onLoad?, onError?: ReactEventHandler<HTMLIFrameElement>
+ ref?: Ref<HTMLIFrameElement>
}
-SANDBOXED_FRAME_DEFAULT_SANDBOX = 'allow-scripts'
-sanitizeFrameSandbox(sandbox?: string): string
```
+The props are an explicit allowlist, not `ComponentProps<'iframe'>`: `allow`
+(Permissions-Policy delegation — would hand a third-party site the mic/camera
+grant the app holds), `srcdoc`, `name`, `allowFullScreen`, `csp`,
+`credentialless` and every other iframe attribute are not props and nothing is
+spread onto the element, so they cannot reach the DOM even through a cast.
+
*Arbitration (allowlist, not blocklist):* the only tokens a caller may add are
`allow-scripts`, `allow-forms`, `allow-downloads`, `allow-pointer-lock`,
`allow-orientation-lock`, `allow-presentation`. Everything else —
`allow-same-origin`, `allow-top-navigation*`, `allow-popups*`, `allow-modals`,
`allow-storage-access-by-user-activation`, and any token the primitive does not
know — is dropped case-insensitively even if passed; an emptied set falls back
-to the default posture, because a frame with **no** `sandbox` attribute is
-fully privileged. `loading="lazy"` and `referrerPolicy="no-referrer"` cannot be
-overridden through props. The opaque origin IS the containment: guest content
-cannot reach the app, its storage, or the preload bridge.
+to the default posture (`allow-scripts`), because a frame with **no** `sandbox`
+attribute is fully privileged. `loading="lazy"` and
+`referrerPolicy="no-referrer"` are not props. The opaque origin IS the
+containment: guest content cannot reach the app, its storage, or the preload
+bridge.
*Teardown:* it is a plain React element — unmounting your pane/page removes the
frame and its realm; nothing is registered app-side.