DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(security): iframe postMessage 校验来源并收紧 CSP 与权限
chenlongd20d903cddc9
diff --git a/src-tauri/capabilities/default.json b/src-tauri/capabilities/default.jsonindex 980e62cc..2aa3dd75 100644--- a/src-tauri/capabilities/default.json+++ b/src-tauri/capabilities/default.json@@ -1,11 +1,8 @@{"$schema": "../gen/schemas/desktop-schema.json","identifier": "default",- "description": "Capability for the main window and the embedded Harness GUI; grants IPC (e.g. opener|open_url) to the remote-origin dsh page.",+ "description": "Capability for the trusted desktop shell. The remote Harness iframe communicates only through the validated postMessage bridge.","windows": ["main"],- "remote": {- "urls": ["http://127.0.0.1:*"]- },"permissions": ["core:default","core:event:allow-listen",diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.jsonindex c3bb0ab5..6b952564 100644--- a/src-tauri/tauri.conf.json+++ b/src-tauri/tauri.conf.json@@ -11,7 +11,8 @@},"app": {"security": {- "csp": null+ "csp": "default-src 'self'; connect-src 'self' ipc: http://ipc.localhost; img-src 'self' asset: http://asset.localhost data:; style-src 'self' 'unsafe-inline'; font-src 'self' data:; script-src 'self'; frame-src http://127.0.0.1:*",+ "devCsp": "default-src 'self'; connect-src 'self' ipc: http://ipc.localhost http://localhost:1420 ws://localhost:1421; img-src 'self' asset: http://asset.localhost data:; style-src 'self' 'unsafe-inline'; font-src 'self' data:; script-src 'self'; frame-src http://127.0.0.1:*"}},"bundle": {diff --git a/src/components/harness-webview.tsx b/src/components/harness-webview.tsxindex 3fea6acd..04fb1b22 100644--- a/src/components/harness-webview.tsx+++ b/src/components/harness-webview.tsx@@ -78,7 +78,7 @@ export default function HarnessWebview() {ref={iframeRef}className="block h-full w-full border-none bg-load-bg"src={iframeSrc}- allow="clipboard-read; clipboard-write; camera; microphone; geolocation; display-capture; autoplay; encrypted-media; fullscreen; notifications *"+ allow="clipboard-read; clipboard-write; fullscreen"sandbox="allow-same-origin allow-scripts allow-popups allow-forms allow-modals allow-downloads allow-storage-access-by-user-activation"onLoad={harness.markIframeLoaded}onError={harness.markIframeError}diff --git a/src/hooks/use-iframe-shim.ts b/src/hooks/use-iframe-shim.tsindex 9088510c..775ca5fb 100644--- a/src/hooks/use-iframe-shim.ts+++ b/src/hooks/use-iframe-shim.ts@@ -4,6 +4,7 @@ import { listen } from '@tauri-apps/api/event'import { getCurrentWindow } from '@tauri-apps/api/window'import { useEffect } from 'react'import { useEvent, useInterval, useMountedState } from 'react-use'+import { getIframeOrigin } from '@/utils/iframe-origin'interface NativeNotificationMessage {source?: 'dsh-notification-bridge'@@ -28,6 +29,10 @@ export function useIframeShim(iframeRef: RefObject<HTMLIFrameElement | null>) {if (event.source !== iframeRef.current?.contentWindow) {return}+ const iframeOrigin = getIframeOrigin(iframeRef)+ if (!iframeOrigin || event.origin !== iframeOrigin) {+ return+ }if (data.type !== 'dsh://native-notification') {return}@@ -51,6 +56,9 @@ export function useIframeShim(iframeRef: RefObject<HTMLIFrameElement | null>) {'dsh-notification-clicked',(event) => {const payload = event.payload+ const iframeOrigin = getIframeOrigin(iframeRef)+ if (!iframeOrigin)+ returniframeRef.current?.contentWindow?.postMessage({type: 'dsh://focus-session',@@ -58,7 +66,7 @@ export function useIframeShim(iframeRef: RefObject<HTMLIFrameElement | null>) {title: payload.title || undefined,tag: payload.tag || undefined,},- '*',+ iframeOrigin,)},).then((unlistener) => {@@ -86,9 +94,12 @@ export function useIframeShim(iframeRef: RefObject<HTMLIFrameElement | null>) {appWindow.isVisible(),])const hidden = minimized || !visible+ const iframeOrigin = getIframeOrigin(iframeRef)+ if (!iframeOrigin)+ returniframeRef.current?.contentWindow?.postMessage({ type: 'dsh://visibility-state', hidden },- '*',+ iframeOrigin,)}catch (error) {diff --git a/src/hooks/use-iframe-tauri.ts b/src/hooks/use-iframe-tauri.tsindex b0fe3ed2..ebc67849 100644--- a/src/hooks/use-iframe-tauri.ts+++ b/src/hooks/use-iframe-tauri.ts@@ -1,6 +1,7 @@import type { RefObject } from 'react'import { useState } from 'react'import { useEvent } from 'react-use'+import { getIframeOrigin } from '@/utils/iframe-origin'/*** 壳层导航桥(宿主侧):ShellNavBar 左侧三个控件的消息通道。@@ -60,6 +61,10 @@ export function useIframeTauri(if (event.source !== iframeRef?.current?.contentWindow) {return}+ const iframeOrigin = iframeRef ? getIframeOrigin(iframeRef) : null+ if (!iframeOrigin || event.origin !== iframeOrigin) {+ return+ }switch (data.type) {case 'dsh://sidebar:collapsed':setSidebarCollapsed(Boolean(data.collapsed))@@ -76,9 +81,14 @@ export function useIframeTauri(useEvent('message', handleMessage)function sendNav(action: ShellNavAction) {+ if (!iframeRef)+ return+ const iframeOrigin = getIframeOrigin(iframeRef)+ if (!iframeOrigin)+ returniframeRef?.current?.contentWindow?.postMessage({ source: 'dsh-desktop', type: COMMAND_TYPES[action] },- '*',+ iframeOrigin,)}diff --git a/src/store/modules/harness/store.ts b/src/store/modules/harness/store.tsindex 522a9796..d434d3cf 100644--- a/src/store/modules/harness/store.ts+++ b/src/store/modules/harness/store.ts@@ -160,6 +160,10 @@ export const harness = defineStore({this.iframeError = falseawait invoke('launch_harness')this.serviceRunning = true+ // 后端遇到端口占用时会自动递增并持久化端口,启动后重新读取真实地址。+ const runtimeInfo = await invoke<{ service_url: string }>('get_runtime_info')+ this.serviceUrl = runtimeInfo.service_url+ this.iframeSrc = generateTimestampedUrl(runtimeInfo.service_url)let healthy = falsefor (let attempt = 0; attempt < MAX_RETRIES && !healthy; attempt++) {diff --git a/src/utils/iframe-origin.ts b/src/utils/iframe-origin.tsnew file mode 100644index 00000000..efff0e78--- /dev/null+++ b/src/utils/iframe-origin.ts@@ -0,0 +1,15 @@+import type { RefObject } from 'react'++/** 返回当前 Harness iframe 的精确来源;无有效 HTTP(S) 来源时拒绝通信。 */+export function getIframeOrigin(iframeRef: RefObject<HTMLIFrameElement | null>): string | null {+ const source = iframeRef.current?.src+ if (!source)+ return null+ try {+ const url = new URL(source)+ return url.protocol === 'http:' || url.protocol === 'https:' ? url.origin : null+ }+ catch {+ return null+ }+}