dsh-mobile-plugin 0.5.76 → 0.5.77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -79,4 +79,34 @@ function credentialCandidates(headers) {
79
79
  return out;
80
80
  }
81
81
 
82
- module.exports = { snapshotAuthorized, credentialCandidates };
82
+ /**
83
+ * 与 `snapshotAuthorized` 同口径,但把**命中的那条记录**一并取出来。
84
+ *
85
+ * 存在只为一件事:判「这枚凭据是不是按协议 v2 配的」。绑了 `deviceKey` 的凭据说明设备
86
+ * 当初走的是逐请求信封,它的高频读一旦被中转删掉 `x-dsh-proto` 头就会被当成老客户端,
87
+ * 而快照捷径**不封信封**——那样列表 / 工作区就会以明文经中转送出去(主进程那条
88
+ * `e2ee-required` 守不到捷径)。调用方拿到 `deviceKey` 就回源,交给主进程如实驳回。
89
+ *
90
+ * 只回本机记录;走旧无状态令牌兜底的(升级前老 App / 中转签发)没有记录,返回 null——
91
+ * 那种凭据本来就不是 v2 绑定,不触发降级拦截。
92
+ *
93
+ * @param {object} headers 请求头(原始大小写都行)
94
+ * @param {{ lookup?: (presented: string) => object|null }} deps
95
+ * @returns {object|null} 命中且有效(未吊销 / 未过期)的本机凭据记录,否则 null
96
+ */
97
+ function snapshotAuthRecord(headers, deps = {}) {
98
+ const { lookup } = deps;
99
+ const candidates = credentialCandidates(headers);
100
+ if (!candidates.length) return null;
101
+ const now = Date.now();
102
+ for (const presented of candidates) {
103
+ const known = typeof lookup === 'function' ? lookup(presented) : null;
104
+ if (!known) continue;
105
+ if (known.revokedAt) return null;
106
+ if (known.expiresAt && known.expiresAt <= now) return null;
107
+ return known;
108
+ }
109
+ return null;
110
+ }
111
+
112
+ module.exports = { snapshotAuthorized, snapshotAuthRecord, credentialCandidates };
@@ -4,6 +4,7 @@ import path from 'node:path'
4
4
  import { dshHome } from '../shared/paths.js'
5
5
  import { compareVersion, generationModulesError } from './manager.js'
6
6
  import { movePath, readManifest, sha256Hex, unpackTarGz, verifyManifest } from './ota-package.js'
7
+ import { verifyOtaSignature, otaPublicKeyPem } from './ota-signing.js'
7
8
  /**
8
9
  * OTA 包落在哪。
9
10
  *
@@ -118,7 +119,7 @@ export function snapshotGeneration(sourceDir, version) {
118
119
  return { created: false, error: String((error && error.message) || error) }
119
120
  }
120
121
  }
121
- export function createDeviceOta({ fetchImpl = fetch, installed = () => null, platform = PLATFORM, channel = CHANNEL, runtime = null } = {}) {
122
+ export function createDeviceOta({ fetchImpl = fetch, installed = () => null, platform = PLATFORM, channel = CHANNEL, runtime = null, publicKeyPem = otaPublicKeyPem() } = {}) {
122
123
  async function reconcileOfficial() {
123
124
  const active = runtime?.status()
124
125
  const officialVersion = installed()
@@ -162,7 +163,7 @@ export function createDeviceOta({ fetchImpl = fetch, installed = () => null, pla
162
163
  const absorbed = Boolean(validVersion(officialVersion) && newest && compareVersion(officialVersion, newest) >= 0 && runtime?.status()?.source !== 'ota')
163
164
  // 中转没给出合法 sha256 时这里归一成 null,apply 会据此**拒绝安装**(见那里的判据),
164
165
  // 绝不能让「没有哈希」被当成「不必校验」。
165
- return { available: Boolean(newest && !absorbed && compareVersion(newest, installedVersion || '0.0.0') > 0), version: newest, platform: release.platform || platform, channel: release.channel || channel, installedVersion, otaVersion: ota?.version ?? null, officialVersion, absorbed, packageUrl: release.packageUrl, sha256: typeof release.sha256 === 'string' && /^[0-9a-f]{64}$/i.test(release.sha256) ? release.sha256.toLowerCase() : null }
166
+ return { available: Boolean(newest && !absorbed && compareVersion(newest, installedVersion || '0.0.0') > 0), version: newest, platform: release.platform || platform, channel: release.channel || channel, installedVersion, otaVersion: ota?.version ?? null, officialVersion, absorbed, packageUrl: release.packageUrl, sha256: typeof release.sha256 === 'string' && /^[0-9a-f]{64}$/i.test(release.sha256) ? release.sha256.toLowerCase() : null, signature: typeof release.signature === 'string' ? release.signature : null }
166
167
  }
167
168
  async function apply() {
168
169
  const info = await check()
@@ -171,6 +172,16 @@ export function createDeviceOta({ fetchImpl = fetch, installed = () => null, pla
171
172
  // 没有合法 sha256 就等于完整性校验整段被跳过:从旧库升级上来的记录天然是空 sha,
172
173
  // 这种包一律不装 —— 宁可装不上,也不装一份验不了的包。
173
174
  if (!info.sha256) throw new Error('OTA 包缺少有效的 sha256,拒绝安装')
175
+ // 先验签:中转只转发、没有离线私钥就伪造不出签名。字段真实之后,再靠 sha256 校验下载
176
+ // 内容没被换过。两步任一不过都不装——「没签名」绝不等于「不必校验」。
177
+ verifyOtaSignature({
178
+ version: info.version,
179
+ platform: info.platform,
180
+ channel: info.channel,
181
+ sha256: info.sha256,
182
+ packageUrl: info.packageUrl,
183
+ signature: info.signature,
184
+ }, publicKeyPem)
174
185
  // 暂存目录**建在目标根里面**,不是系统临时目录:最后一步要 rename 到 `<root>/<version>`,
175
186
  // 跨盘的 rename 在 Windows 上直接 EXDEV(系统临时目录常在 C:,而 profile 可能在别的盘)。
176
187
  fs.mkdirSync(ROOT(), { recursive: true })
@@ -0,0 +1,7 @@
1
+ /**
2
+ * 桌面完整包 OTA 的内置签名公钥(Ed25519,SPKI PEM)。
3
+ *
4
+ * 私钥离线保管、不在这里;换发布方就换这一份,或用环境变量 DSH_OTA_PUBLIC_KEY_PEM 覆盖。
5
+ * 公开公钥不涉及泄密,随包分发是正常的。
6
+ */
7
+ export const OTA_PUBLIC_KEY_PEM = "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAKyPrnI61/wQkqWMfLAZCbPMdiYs7zQ5nhCg/RMltE/w=\n-----END PUBLIC KEY-----"
@@ -0,0 +1,76 @@
1
+ /**
2
+ * 桌面完整包 OTA 的签名校验。
3
+ *
4
+ * 现状(被审阅点名的那个洞):中转下发的 sha256 与包内清单都来自**同一台发布服务器**,
5
+ * 攻击者拿住那台机器就能把包和「用来证明包没被改」的哈希一起换掉——等于没有校验。这里
6
+ * 用 Ed25519 离线签名补上「来源真实」那一半:发布方在**离线**环境用私钥对「版本 / 平台 /
7
+ * 通道 / sha256 / 下载地址」这组字段签名,客户端内置**公钥**验签。中转只能转发,没有私钥
8
+ * 就伪造不出签名,也就推不了带毒的包。
9
+ *
10
+ * 边界:签名只证明「这组字段由私钥持有者发布」,不加密内容;内容完整性仍由 sha256 钉住。
11
+ * 两者一道:先验签(字段真实)→ 再按 sha256 校验下载内容(内容未改)。
12
+ *
13
+ * 私钥绝不进仓库、绝不与发布服务器同机(否则服务器被黑连签名一起伪造)。
14
+ */
15
+
16
+ import crypto from 'node:crypto'
17
+ import { OTA_PUBLIC_KEY_PEM } from './ota-public-key.js'
18
+
19
+ /** 签名字段集合的版本前缀,改字段就改它,免得新旧签法互相误认。 */
20
+ const SIGNING_DOMAIN = 'dsh-ota-v1'
21
+
22
+ /**
23
+ * 把要签的字段规范成一个稳定字符串。发布脚本与客户端**必须**用同一份,签出来才对得上。
24
+ *
25
+ * @param {{version:string, platform:string, channel:string, sha256:string, packageUrl:string}} release
26
+ * @returns {string}
27
+ */
28
+ export function otaSignaturePayload(release) {
29
+ return [
30
+ SIGNING_DOMAIN,
31
+ String(release.version ?? ''),
32
+ String(release.platform ?? ''),
33
+ String(release.channel ?? ''),
34
+ String(release.sha256 ?? ''),
35
+ String(release.packageUrl ?? ''),
36
+ ].join('\n')
37
+ }
38
+
39
+ /**
40
+ * 内置的 OTA 签名公钥。可用 `DSH_OTA_PUBLIC_KEY_PEM` 覆盖(自建发布方换自己的公钥)。
41
+ * 读不到就返回空串——调用方据此拒绝安装(没有公钥等于验不了签)。
42
+ *
43
+ * @returns {string}
44
+ */
45
+ export function otaPublicKeyPem() {
46
+ const override = process.env.DSH_OTA_PUBLIC_KEY_PEM
47
+ if (typeof override === 'string' && override.trim()) return override
48
+ return OTA_PUBLIC_KEY_PEM
49
+ }
50
+
51
+ /**
52
+ * 校验一条 OTA 发布记录的签名。任何一项不满足都抛错——绝不「没签名就当不校验」。
53
+ *
54
+ * @param {object} release 含 version / platform / channel / sha256 / packageUrl / signature
55
+ * @param {string} [publicKeyPem] 公钥 PEM;缺省用内置 / 环境变量
56
+ * @returns {true}
57
+ */
58
+ export function verifyOtaSignature(release, publicKeyPem = otaPublicKeyPem()) {
59
+ const signature = release && typeof release.signature === 'string' ? release.signature.trim() : ''
60
+ if (!signature) throw new Error('OTA 包缺少签名,拒绝安装')
61
+ if (!publicKeyPem) throw new Error('未配置 OTA 签名公钥,拒绝安装')
62
+ let key
63
+ try {
64
+ key = crypto.createPublicKey(publicKeyPem)
65
+ } catch {
66
+ throw new Error('OTA 签名公钥无法解析,拒绝安装')
67
+ }
68
+ let ok = false
69
+ try {
70
+ ok = crypto.verify(null, Buffer.from(otaSignaturePayload(release), 'utf8'), key, Buffer.from(signature, 'base64'))
71
+ } catch {
72
+ ok = false
73
+ }
74
+ if (!ok) throw new Error('OTA 包签名校验失败,拒绝安装')
75
+ return true
76
+ }
@@ -73,7 +73,7 @@ function installedPackageDirSafe() {
73
73
  */
74
74
  export function shellFingerprint(pkgDir) {
75
75
  if (!pkgDir) return null
76
- const files = ['lib/index.js', 'lib/shell/runtime.js', 'lib/shell/manager.js', 'lib/shell/updater.js', 'lib/shell/device-ota.js', 'lib/shell/ota-package.js', 'lib/shell/half-route.js', 'lib/shell/half-view.js', 'lib/shell/self-check-after-swap.js']
76
+ const files = ['lib/index.js', 'lib/shell/runtime.js', 'lib/shell/manager.js', 'lib/shell/updater.js', 'lib/shell/device-ota.js', 'lib/shell/ota-package.js', 'lib/shell/ota-signing.js', 'lib/shell/ota-public-key.js', 'lib/shell/half-route.js', 'lib/shell/half-view.js', 'lib/shell/self-check-after-swap.js']
77
77
  const hash = createHash('sha256')
78
78
  let seen = 0
79
79
  for (const rel of files) {
@@ -32,7 +32,7 @@ const core = require('./relay/core.js')
32
32
  const { orderByLatency, probeRelayHealth } = require('./relay/relay-probe.js')
33
33
  const { createReconnectScheduler } = require('./reconnect-scheduler.cjs')
34
34
  const { isMirrorSnapshotFresh } = require('./mirror-snapshot.cjs')
35
- const { snapshotAuthorized } = require('./relay/snapshot-auth.js')
35
+ const { snapshotAuthorized, snapshotAuthRecord } = require('./relay/snapshot-auth.js')
36
36
  // 凭据库在这个进程里**只读**(§3.7):写盘只有宿主主进程一家。签发与吊销都经 IPC 交给它,
37
37
  // 本地这份按文件指纹重载 —— 两个进程各持一份内存副本、各自整份覆写,后写的赢,于是
38
38
  // 「经中转配上的手机回局域网 401」「吊销被回滚」这两条必然出现。
@@ -300,6 +300,14 @@ function snapshotRead(info) {
300
300
  lookup: (presented) => credentials.lookup(presented, config.instanceId),
301
301
  verifyLanToken: (presented) => core.verifyLanToken(config.lanSecret, presented, config.instanceId),
302
302
  })) return null
303
+ // **v2 绑定设备不能因中转删掉协议版本头就回明文快照**:这条捷径不封信封,一旦把一条
304
+ // 绑了 `deviceKey` 的凭据当成老客户端,它要的列表 / 工作区就会以明文经中转送出去——
305
+ // 主进程那条 `e2ee-required` 拦不到捷径这一层。认得出是设备密钥凭据就回源,交给主进程
306
+ // 按「加密会话没带信封」如实驳回(见 lib/http/routing.js)。
307
+ const boundRecord = snapshotAuthRecord(info && info.headers, {
308
+ lookup: (presented) => credentials.lookup(presented, config.instanceId),
309
+ })
310
+ if (boundRecord && boundRecord.deviceKey) return null
303
311
  let url
304
312
  try { url = new URL(String((info && info.path) || ''), 'http://127.0.0.1') } catch { return null }
305
313
  const pathname = url.pathname
@@ -70,12 +70,12 @@ function createTailcatSession(options) {
70
70
  try {
71
71
  const state = options.readState() || {};
72
72
  return {
73
- enabled: state[STATE.enabled] !== false,
73
+ enabled: state[STATE.enabled] === true,
74
74
  identity: typeof state[STATE.identity] === 'string' ? state[STATE.identity] : null,
75
75
  allowed: Array.isArray(state[STATE.allowed]) ? state[STATE.allowed] : [],
76
76
  };
77
77
  } catch {
78
- return { enabled: true, identity: null, allowed: [] };
78
+ return { enabled: false, identity: null, allowed: [] };
79
79
  }
80
80
  }
81
81
 
@@ -66,7 +66,7 @@ function mountTailcat(options) {
66
66
  status: () => {
67
67
  const base = session
68
68
  ? session.status()
69
- : { available: Boolean(ingress), enabled: true, ready: false, address: '', paired: 0, error: 'starting' };
69
+ : { available: Boolean(ingress), enabled: false, ready: false, address: '', paired: 0, error: 'starting' };
70
70
  return { ...base, targetPort: resolvedTargetPort() };
71
71
  },
72
72
  openPairingWindow: ttl => (session ? session.openPairingWindow(ttl) : Promise.resolve(null)),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mobile-plugin",
3
- "version": "0.5.76",
3
+ "version": "0.5.77",
4
4
  "description": "Mobile API (/api/mobile/v1) and relay client for the DSH mobile app — desktop profile build (dsh 0.2.x, peers resolved by the host)",
5
5
  "license": "MIT",
6
6
  "icon": "./icon.png",