Skip to content

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

fix(dsh-tauri): 载体鉴权同时接管原型,修 0.1.7 覆写装不上静态服务的情况

hairyf20d6bc70d489
diff --git a/packages/dsh-tauri/src/host/service/gate.test.ts b/packages/dsh-tauri/src/host/service/gate.test.ts
index d9186721..2abe2daf 100644
--- a/packages/dsh-tauri/src/host/service/gate.test.ts
+++ b/packages/dsh-tauri/src/host/service/gate.test.ts
@@ -76,6 +76,27 @@ describe('gate.attach with the carrier marker', () => {
detach()
})
+ /** 类实例(方法在原型上):只覆写实例属性时 `ctx.connection` 换一个取用对象就失效。 */
+ it('patches a class prototype so every access path sees the bypass', () => {
+ class Service {
+ calls = 0
+ authorizeIndex(): boolean {
+ this.calls += 1
+ return false
+ }
+ }
+ const connection = new Service()
+ const original = Service.prototype.authorizeIndex
+ setCurrentHostInstance({ connection } as never)
+
+ const detach = gate.attach()
+
+ expect(connection.authorizeIndex()).toBe(true)
+ expect(connection.calls).toBe(0)
+ detach()
+ expect(Service.prototype.authorizeIndex).toBe(original)
+ })
+
/** 0.1.7 起 `requestRejection` 移到 peer 上,`connection` 只剩 `authorizeIndex`。 */
it('still bypasses the index gate when the core dropped requestRejection', () => {
const warn = vi.fn()
diff --git a/packages/dsh-tauri/src/host/service/gate.ts b/packages/dsh-tauri/src/host/service/gate.ts
index 430ebb4a..6b610d3c 100644
--- a/packages/dsh-tauri/src/host/service/gate.ts
+++ b/packages/dsh-tauri/src/host/service/gate.ts
@@ -38,23 +38,35 @@ export const gate = defineService({
const restores: Array<() => void> = []
- const rejection = connection.requestRejection
- if (typeof rejection === 'function') {
- connection.requestRejection = (request: IncomingMessage) => {
- const rejected = rejection.call(connection, request)
- return rejected === 401 ? undefined : rejected
- }
- restores.push(() => {
- connection.requestRejection = rejection
- })
+ // `ctx.connection` 可能只是 cordis 的取用代理:只覆写实例属性时,赋值落在代理
+ // 自身,`dsh-host-frontend-static` 里 `ctx.connection.authorizeIndex` 取到的仍是
+ // 原型上的真方法(0.1.7 实测:无告警、覆写已装,401 照旧)。原型与实例一起接管,
+ // detach 时逐条还原。
+ const targets: ConnectionHost['connection'][] = [connection]
+ const prototype = Object.getPrototypeOf(connection) as ConnectionHost['connection'] | null
+ if (prototype !== null && prototype !== connection && prototype !== Object.prototype) {
+ targets.push(prototype)
}
- const authorize = connection.authorizeIndex
- if (typeof authorize === 'function') {
- connection.authorizeIndex = () => true
- restores.push(() => {
- connection.authorizeIndex = authorize
- })
+ for (const target of targets) {
+ const rejection = target.requestRejection
+ if (typeof rejection === 'function') {
+ target.requestRejection = (request: IncomingMessage) => {
+ const rejected = rejection.call(target, request)
+ return rejected === 401 ? undefined : rejected
+ }
+ restores.push(() => {
+ target.requestRejection = rejection
+ })
+ }
+
+ const authorize = target.authorizeIndex
+ if (typeof authorize === 'function') {
+ target.authorizeIndex = () => true
+ restores.push(() => {
+ target.authorizeIndex = authorize
+ })
+ }
}
if (restores.length === 0) {