Files
apes-sidebar/tests/trusted-hosts.spec.ts
T
Rookie_Zoe ea250c9e87 fix: /sidebar 信任栅栏改用 webRuntime.trustedHosts,修复远程访问全部 403
根因:trustedHostsOf 按 entry.options.name === 'connection' 查找 connection
行,但该行 name 是模块标识符(@deepseek-ai/dsh-client-connection,id 才是
connection),查找永远落空 → trustedHosts 为空 → 栅栏只放行回环 Host → 通过
域名或局域网 IP 访问时 /sidebar/bundle chunk 加载失败、资源管理器/代码管理器
全部 403 forbidden(/api 不受影响,它读的是 apply 时已求值的配置)。

修复(按 review 建议):host half 直接注入 webRuntime 服务,栅栏每请求实时
读取 ctx.webRuntime.trustedHosts —— 与 /api 网关同一信任列表(启动时采样的
LAN IP 字面量 + --trusted-host 权威),不再依赖 Loader entry / fiber /
!!js 节点求值;自定义信任域名由 dsh web --trusted-host 提供。

新增 tests/trusted-hosts.spec.ts 回归:域名/局域网放行、空列表仅回环、
跨站标记拒绝、每请求实时读取;plugin-shape 断言同步更新。
2026-08-14 23:03:26 +08:00

182 lines
5.9 KiB
TypeScript

/**
* Remote-access trust regression tests: the /sidebar fence must accept a
* reverse-proxy domain or LAN IP listed in the DSH web runtime's
* `webRuntime.trustedHosts` — the same trust list the /api gateway accepts
* (LAN IP literals sampled at bind plus `--trusted-host` authorities).
*
* Regression: the fence used to read the connection row's trustedHosts
* through the Loader (`entry.options.name === 'connection'`, which never
* matched the row's module-specifier name), so trustedHosts stayed empty and
* every remote /sidebar request was 403 "forbidden".
*/
import { describe, expect, it } from 'vitest'
import type { IncomingMessage, ServerResponse } from 'node:http'
import { apply } from '../src/index.ts'
import type { SidebarWebRoute, SidebarWebUpgradeRoute } from '../src/context-types.ts'
interface FakeRes {
status: number
headers: Record<string, string>
body: string
writeHead(status: number, headers?: Record<string, string>): void
end(body?: string | Buffer): void
}
function fakeRes(): FakeRes {
return {
status: 0,
headers: {},
body: '',
writeHead(status, headers = {}) {
this.status = status
this.headers = headers
},
end(body) {
if (body !== undefined) this.body = body.toString()
},
} as FakeRes
}
function req(
method: string,
url: string,
headers: Record<string, string>,
body = '{}',
): IncomingMessage {
const chunks = [Buffer.from(body)]
return {
method,
url,
headers,
[Symbol.asyncIterator]: async function* () {
for (const chunk of chunks) yield chunk
},
} as unknown as IncomingMessage
}
/** Mount apply() against a fake context with a replaceable webRuntime trust list. */
function mount(initialTrustedHosts: readonly string[] = []): {
api: (r: IncomingMessage, s: ServerResponse) => Promise<void>
setTrustedHosts: (hosts: readonly string[]) => void
cleanup: () => void
} {
const runtime = { trustedHosts: [...initialTrustedHosts] }
const routes: SidebarWebRoute[] = []
const upgrades: SidebarWebUpgradeRoute[] = []
const effects: Array<() => void | (() => void)> = []
const ctx = {
webRuntime: runtime,
webServer: {
register: (route: SidebarWebRoute) => { routes.push(route); return () => {} },
registerUpgrade: (route: SidebarWebUpgradeRoute) => { upgrades.push(route); return () => {} },
},
sessions: { get: () => undefined },
tools: { register: () => () => {} },
effect: (fn: () => void | (() => void)) => {
const cleanup = fn()
if (typeof cleanup === 'function') effects.push(cleanup)
},
inject: () => () => {},
get: () => undefined,
}
apply(ctx as never)
const api = routes.find(route => route.path === '/sidebar/api')?.handler
if (api === undefined) throw new Error('test setup: /sidebar/api route not registered')
return {
api: api as (r: IncomingMessage, s: ServerResponse) => Promise<void>,
setTrustedHosts: (hosts) => { runtime.trustedHosts = [...hosts] },
cleanup: () => { for (const cleanup of effects) cleanup() },
}
}
/** A remote-domain request with the browser markers a same-origin fetch sends. */
function remoteDomainRequest(): IncomingMessage {
return req('POST', '/sidebar/api/session.cwd', {
host: 'example.com',
'sec-fetch-site': 'same-origin',
origin: 'https://example.com',
})
}
/** A LAN-IP request with the browser markers a same-origin fetch sends. */
function lanRequest(): IncomingMessage {
return req('POST', '/sidebar/api/session.cwd', {
host: '192.0.2.1:3080',
'sec-fetch-site': 'same-origin',
origin: 'http://192.0.2.1:3080',
})
}
describe('remote-access trust (webRuntime.trustedHosts)', () => {
it('accepts a reverse-proxy domain configured in webRuntime.trustedHosts', async () => {
const { api, cleanup } = mount(['example.com'])
try {
const res = fakeRes()
await api(remoteDomainRequest(), res as unknown as ServerResponse)
expect(res.status).not.toBe(403)
} finally {
cleanup()
}
})
it('accepts LAN IP literals from webRuntime.trustedHosts', async () => {
const { api, cleanup } = mount(['192.0.2.1'])
try {
const res = fakeRes()
await api(lanRequest(), res as unknown as ServerResponse)
expect(res.status).not.toBe(403)
} finally {
cleanup()
}
})
it('stays loopback-only when webRuntime.trustedHosts is empty', async () => {
const { api, cleanup } = mount([])
try {
const remote = fakeRes()
await api(remoteDomainRequest(), remote as unknown as ServerResponse)
expect(remote.status).toBe(403)
const loopback = fakeRes()
await api(req('POST', '/sidebar/api/session.cwd', {
host: '127.0.0.1:3080',
'sec-fetch-site': 'same-origin',
}), loopback as unknown as ServerResponse)
expect(loopback.status).not.toBe(403)
} finally {
cleanup()
}
})
it('rejects cross-site browser markers even for a trusted host', async () => {
const { api, cleanup } = mount(['example.com'])
try {
const res = fakeRes()
await api(req('POST', '/sidebar/api/session.cwd', {
host: 'example.com',
'sec-fetch-site': 'cross-site',
origin: 'https://evil.example.net',
}), res as unknown as ServerResponse)
expect(res.status).toBe(403)
} finally {
cleanup()
}
})
it('reads webRuntime.trustedHosts per request, not once at apply', async () => {
const { api, setTrustedHosts, cleanup } = mount([])
try {
const before = fakeRes()
await api(remoteDomainRequest(), before as unknown as ServerResponse)
expect(before.status).toBe(403)
// The trust list gains an authority after mount (e.g. config reload):
// the already-mounted fence must pick it up without a plugin restart.
setTrustedHosts(['example.com'])
const after = fakeRes()
await api(remoteDomainRequest(), after as unknown as ServerResponse)
expect(after.status).not.toBe(403)
} finally {
cleanup()
}
})
})