Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

fix(ssh): /api-ssh 支持壳层 webview 跨源轮询(CORS 白名单:tauri 壳 scheme + dev 1420)

Menghuan191819eca196066a
diff --git a/packages/dsh-tauri-ssh/src/host/routes/index.test.ts b/packages/dsh-tauri-ssh/src/host/routes/index.test.ts
index d49c4cbb..3afcd7d5 100644
--- a/packages/dsh-tauri-ssh/src/host/routes/index.test.ts
+++ b/packages/dsh-tauri-ssh/src/host/routes/index.test.ts
@@ -47,17 +47,21 @@ function fakeHost(overrides: Partial<SshApiHost> = {}): SshApiHost {
async function call(
host: SshApiHost,
body: string,
- options: { method?: string, address?: string } = {},
-): Promise<{ status: number, body: SshApiResponse }> {
+ options: { method?: string, address?: string, origin?: string } = {},
+): Promise<{ status: number, body: SshApiResponse, headers: Record<string, unknown> }> {
const socket = new Socket()
Object.defineProperty(socket, 'remoteAddress', { value: options.address ?? '127.0.0.1', configurable: true })
const req = new IncomingMessage(socket)
req.method = options.method ?? 'POST'
+ if (options.origin !== undefined)
+ req.headers.origin = options.origin
const res = new ServerResponse(req)
let status = 0
let payload = ''
- res.writeHead = ((code: number) => {
+ let headers: Record<string, unknown> = {}
+ res.writeHead = ((code: number, head?: Record<string, unknown>) => {
status = code
+ headers = head ?? {}
return res
}) as typeof res.writeHead
res.end = ((chunk?: unknown) => {
@@ -68,7 +72,8 @@ async function call(
req.push(Buffer.from(body))
req.push(null)
await createSshApiHandler(host)(req, res)
- return { status, body: JSON.parse(payload) as SshApiResponse }
+ // 204 preflight responses carry no body; parse only actual JSON payloads.
+ return { status, body: (payload === '' ? {} : JSON.parse(payload)) as SshApiResponse, headers }
}
describe('isLoopbackPeer', () => {
@@ -82,6 +87,37 @@ describe('isLoopbackPeer', () => {
})
describe('/api-ssh handler', () => {
+ it('answers the shell webview\'s CORS preflight and echoes its origin', async () => {
+ const { status, headers } = await call(fakeHost(), '', { method: 'OPTIONS', origin: 'http://localhost:1420' })
+ expect(status).toBe(204)
+ expect(headers['access-control-allow-origin']).toBe('http://localhost:1420')
+ expect(headers['access-control-allow-methods']).toBe('POST, OPTIONS')
+ })
+
+ it('allows every Tauri shell scheme origin on preflight', async () => {
+ for (const origin of ['tauri://localhost', 'http://tauri.localhost']) {
+ const { status, headers } = await call(fakeHost(), '', { method: 'OPTIONS', origin })
+ expect(status).toBe(204)
+ expect(headers['access-control-allow-origin']).toBe(origin)
+ }
+ })
+
+ it('refuses preflight from a non-shell origin', async () => {
+ const { status, body, headers } = await call(fakeHost(), '', { method: 'OPTIONS', origin: 'http://evil.example' })
+ expect(status).toBe(403)
+ expect(body).toMatchObject({ ok: false, error: { code: 'forbidden' } })
+ expect(headers['access-control-allow-origin']).toBeUndefined()
+ })
+
+ it('carries the CORS headers on cross-origin POST responses for shell origins only', async () => {
+ const allowed = await call(fakeHost(), JSON.stringify({ method: 'machine.list' }), { origin: 'tauri://localhost' })
+ expect(allowed.status).toBe(200)
+ expect(allowed.headers['access-control-allow-origin']).toBe('tauri://localhost')
+ const rogue = await call(fakeHost(), JSON.stringify({ method: 'machine.list' }), { origin: 'http://evil.example' })
+ expect(rogue.status).toBe(200)
+ expect(rogue.headers['access-control-allow-origin']).toBeUndefined()
+ })
+
it('refuses non-loopback peers', async () => {
const { status, body } = await call(fakeHost(), JSON.stringify({ method: 'machine.list' }), { address: '10.0.0.9' })
expect(status).toBe(403)
diff --git a/packages/dsh-tauri-ssh/src/host/routes/index.ts b/packages/dsh-tauri-ssh/src/host/routes/index.ts
index 4734753c..ee283dd4 100644
--- a/packages/dsh-tauri-ssh/src/host/routes/index.ts
+++ b/packages/dsh-tauri-ssh/src/host/routes/index.ts
@@ -102,6 +102,34 @@ async function readBody(req: IncomingMessage): Promise<unknown> {
return JSON.parse(Buffer.concat(chunks).toString('utf8')) as unknown
}
+/**
+ * The origins the desktop shell's webview may cross-origin read this API from:
+ * the Tauri 2 shell schemes (macOS/Linux `tauri://localhost`, Windows
+ * `http://tauri.localhost`) and the dev-shell Vite origin. The embedded web UI
+ * itself is same-origin and needs none of this; without these headers the
+ * shell's switcher poll (`fetch` from the webview) is CORS-blocked with a bare
+ * "Load failed". Any other origin stays blocked, so a rogue local page cannot
+ * read (or preflight into) the API.
+ */
+const SHELL_ORIGINS = new Set([
+ 'tauri://localhost',
+ 'http://tauri.localhost',
+ 'http://localhost:1420',
+])
+
+/** CORS headers for one request's Origin, or none when the origin is not a shell origin. */
+function shellCorsHeaders(origin: string | undefined): Record<string, string> {
+ if (origin === undefined || !SHELL_ORIGINS.has(origin))
+ return {}
+ return {
+ 'access-control-allow-origin': origin,
+ 'access-control-allow-methods': 'POST, OPTIONS',
+ 'access-control-allow-headers': 'content-type',
+ 'access-control-max-age': '86400',
+ vary: 'Origin',
+ }
+}
+
/**
* Build the `/api-ssh` request handler over one service.
* @param host - the manager-backed service face.
@@ -109,14 +137,25 @@ async function readBody(req: IncomingMessage): Promise<unknown> {
*/
export function createSshApiHandler(host: SshApiHost): (req: IncomingMessage, res: ServerResponse) => Promise<void> {
return async (req, res) => {
+ const cors = shellCorsHeaders(req.headers.origin)
const respond = (status: number, body: SshApiResponse): void => {
- res.writeHead(status, { 'content-type': 'application/json' })
+ res.writeHead(status, { 'content-type': 'application/json', ...cors })
res.end(JSON.stringify(body))
}
if (!isLoopbackPeer(req.socket.remoteAddress)) {
respond(403, { ok: false, error: { code: 'forbidden', message: 'this API is loopback-only' } })
return
}
+ // CORS preflight from the shell webview: answer without touching the body.
+ if (req.method === 'OPTIONS') {
+ if (cors['access-control-allow-origin'] === undefined) {
+ respond(403, { ok: false, error: { code: 'forbidden', message: 'origin not allowed' } })
+ return
+ }
+ res.writeHead(204, cors)
+ res.end()
+ return
+ }
if (req.method !== 'POST') {
respond(405, { ok: false, error: { code: 'method-not-allowed', message: 'POST only' } })
return