Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(whiteboard): make "Copy as image" work under the app's CSP

review6af32757f834
diff --git a/src/renderer/nodes/WhiteboardNode.test.tsx b/src/renderer/nodes/WhiteboardNode.test.tsx
index b8c1fe7b4..fa6423e20 100644
--- a/src/renderer/nodes/WhiteboardNode.test.tsx
+++ b/src/renderer/nodes/WhiteboardNode.test.tsx
@@ -220,3 +220,63 @@ describe('a hosted-team Viewer', () => {
expect(props.readOnly).toBeFalsy()
})
})
+
+describe('Copy as image', () => {
+ // The app's CSP (`default-src 'self'`, no `connect-src data:`) refuses `fetch` of a data: URL —
+ // measured in Chrome with the exact index.html policy: "TypeError: Failed to fetch". The stub
+ // answers the same way, so a copy that goes through fetch fails here as it does in the app.
+ const PNG_BASE64 = 'iVBORw0KGgo='
+ let written: Array<Record<string, Blob>>
+ const realFetch = globalThis.fetch
+
+ beforeEach(() => {
+ written = []
+ globalThis.fetch = vi.fn(async () => {
+ throw new TypeError('Failed to fetch')
+ }) as typeof fetch
+ ;(globalThis as { ClipboardItem?: unknown }).ClipboardItem = class {
+ constructor(public items: Record<string, Blob>) {}
+ }
+ Object.defineProperty(navigator, 'clipboard', {
+ configurable: true,
+ value: {
+ write: vi.fn(async (items: Array<{ items: Record<string, Blob> }>) => {
+ written.push(...items.map((i) => i.items))
+ })
+ }
+ })
+ })
+ afterEach(() => {
+ globalThis.fetch = realFetch
+ })
+
+ it('puts the board picture on the clipboard as a PNG without fetching a data: URL', async () => {
+ files.readWhiteboard.mockResolvedValue({ thumbnailBase64: PNG_BASE64, updatedAt: 1, rev: 'r1', fromSource: false })
+ mount()
+ await settle()
+ const copy = button(/copy whiteboard as image/i)
+ expect(copy?.disabled).toBe(false)
+ await act(async () => copy!.click())
+ await settle()
+ expect(written).toHaveLength(1)
+ const blob = written[0]!['image/png']!
+ expect(blob.type).toBe('image/png')
+ const bytes = new Uint8Array(await blob.arrayBuffer())
+ expect([...bytes]).toEqual([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
+ expect(globalThis.fetch).not.toHaveBeenCalled()
+ })
+
+ it('a clipboard that refuses the image is reported, not swallowed', async () => {
+ ;(navigator.clipboard.write as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error('NotAllowedError'))
+ const toasts: unknown[] = []
+ const onToast = (e: Event) => toasts.push((e as CustomEvent).detail)
+ window.addEventListener('nodeterm:toast', onToast)
+ files.readWhiteboard.mockResolvedValue({ thumbnailBase64: 'iVBORw0KGgo=', updatedAt: 1, rev: 'r1', fromSource: false })
+ mount()
+ await settle()
+ await act(async () => button(/copy whiteboard as image/i)!.click())
+ await settle()
+ window.removeEventListener('nodeterm:toast', onToast)
+ expect(toasts).toEqual([expect.objectContaining({ kind: 'error' })])
+ })
+})
diff --git a/src/renderer/nodes/WhiteboardNode.tsx b/src/renderer/nodes/WhiteboardNode.tsx
index eef1c1fa4..ee902faab 100644
--- a/src/renderer/nodes/WhiteboardNode.tsx
+++ b/src/renderer/nodes/WhiteboardNode.tsx
@@ -44,6 +44,16 @@ interface Geometry {
const pngDataUrl = (base64: string): string => `data:image/png;base64,${base64}`
+/** The resting picture's bytes as a PNG Blob, decoded here. NOT `fetch(dataUrl)`: the app's CSP
+ * (`default-src 'self'`, no `connect-src data:`) refuses a fetch of a data: URL, which is how
+ * "Copy as image" used to fail without a word. */
+export function pngBlobFromBase64(base64: string): Blob {
+ const binary = atob(base64)
+ const bytes = new Uint8Array(binary.length)
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i)
+ return new Blob([bytes], { type: 'image/png' })
+}
+
/**
* A whiteboard: an infinite Excalidraw canvas inside a canvas node.
*
@@ -81,6 +91,7 @@ export function WhiteboardNode({ id, data, selected, positionAbsoluteX, position
const overlayRef = useRef<HTMLDivElement>(null)
const [mode, setMode] = useState<null | 'edit' | 'present'>(null)
const [geom, setGeom] = useState<Geometry | null>(null)
+ // The resting picture, base64 (the data: URL for the <img> is derived at render).
const [thumb, setThumb] = useState<string>()
// Bumped when editing ends, so the resting picture is read again: after "Discard my changes" the
// board on disk may be someone else's newer version, which no node-data change announces here.
@@ -113,7 +124,7 @@ export function WhiteboardNode({ id, data, selected, positionAbsoluteX, position
.then(() => api.files.readWhiteboard(projectIdRef.current, id, data.whiteboardSource, false))
.then((file) => {
if (!live) return
- setThumb(file?.thumbnailBase64 ? pngDataUrl(file.thumbnailBase64) : undefined)
+ setThumb(file?.thumbnailBase64 || undefined)
setLoadState('ready')
})
.catch(() => {
@@ -174,7 +185,7 @@ export function WhiteboardNode({ id, data, selected, positionAbsoluteX, position
revRef.current = typeof res === 'string' ? undefined : res.rev
setSaveState('saved')
sceneRef.current = scene
- if (thumbnailBase64 !== undefined) setThumb(thumbnailBase64 ? pngDataUrl(thumbnailBase64) : undefined)
+ if (thumbnailBase64 !== undefined) setThumb(thumbnailBase64 || undefined)
// A copy has its own file now; it stops reading the original's.
if (dataRef.current.whiteboardSource) {
updateNodeData(id, { whiteboardSource: undefined })
@@ -248,12 +259,18 @@ export function WhiteboardNode({ id, data, selected, positionAbsoluteX, position
const copyImage = useCallback(async () => {
if (!thumb) return
try {
- const blob = await (await fetch(thumb)).blob()
+ const blob = pngBlobFromBase64(thumb)
await navigator.clipboard.write([new ClipboardItem({ 'image/png': blob })])
setCopied(true)
setTimeout(() => setCopied(false), 1500)
} catch {
- // Clipboard refused (no focus / permission): nothing was copied, the icon simply stays put.
+ // Clipboard refused (no focus, no permission, a browser without image clipboard): say so — a
+ // copy button that does nothing reads as broken.
+ window.dispatchEvent(
+ new CustomEvent('nodeterm:toast', {
+ detail: { kind: 'error', message: 'Could not copy the whiteboard: the clipboard refused the image.' }
+ })
+ )
}
}, [thumb])
@@ -362,7 +379,7 @@ export function WhiteboardNode({ id, data, selected, positionAbsoluteX, position
{loadState === 'unreadable' ? (
<span className="whiteboard-node__hint">This board could not be read here</span>
) : thumb ? (
- <img className="whiteboard-node__thumb" src={thumb} alt="" draggable={false} />
+ <img className="whiteboard-node__thumb" src={pngDataUrl(thumb)} alt="" draggable={false} />
) : loadState === 'ready' ? (
<span className="whiteboard-node__hint">Double-click to draw</span>
) : null}