dsh-instruction-memory 1.6.5 → 1.6.7

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.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,116 @@
7
7
  > 1.0.13 起由 CI 经 OIDC Trusted Publishing 发布(首个带 provenance 的版本)。
8
8
  > 这些历史 tag **不应回填**——npm 上的版本号已被占用。
9
9
 
10
+ ## [1.6.7] — 2026-12-04
11
+
12
+ 第四轮审计(技术评审团三条线:架构 / 应用安全 / 验收)的修复批次。本轮**没有新功能**,只修审计
13
+ 在 1.6.3→1.6.6 之间确认的缺陷,其中两条是"一端坏、另一端好"的真差异。
14
+
15
+ 先把前提说清楚:**1.6.4 从未发布**——无 tag、无独立提交、npm 上 `@1.6.4` 返回 404,其客户端改动
16
+ (444+/101−)被压进了 1.6.5 的提交。上一节的 `[1.6.4]` 已改标「未发布」。因此"近三个版本"实际是
17
+ 1.6.3 / 1.6.5 / 1.6.6,而"1.6.5 修好了 1.6.4 的 P0"这句话在证据上不可二分。
18
+
19
+ ### 修复(写门)
20
+
21
+ - **A1 · 门禁 verb 抛错不再决定整个请求**(`lib/index.js` `connectionGateVerdict`)。1.6.6 按
22
+ "第一个存在的 verb"择一,且调用方把抛错归一成 401——于是当宿主同时暴露 `requestRejection`
23
+ (内部可能转调 `admit`,而 `admit()` 在未 inject `connection.authentication` 的 fiber 上会抛)
24
+ 时,**读也 401,设置页整页打不开**,提示还误导成"需要登录"。现在三个 verb 按强度逐个试,
25
+ 抛错记日志继续下一个,全部不可用才拒绝,并在文案里说明"这是宿主版本不匹配,不是登录问题"。
26
+ - **A2 · trust-only 门只放行读,写一律 503**(`connectionGateVerdict` / `rejectUntrustedCaller` /
27
+ `writeGateRefusal`)。`isTrustedRequest` 只校验来源,宿主源码自述它 "never establishes identity"。
28
+ 在只暴露该 verb 的宿主(0.2.3-alpha.3 形状)上,本机任意进程带 `Host: 127.0.0.1:PORT` 即可穿过,
29
+ 而本路由的每一次写都会进"注入到此后每轮对话"的文本。现在把"门只证来源"这一档标成
30
+ `gate-trust-only`:读照常(面板要能打开并说清原因),写返 503 并给出原因与两条出路。
31
+ - **A2 附带**:`admit()` 从"最后备选"提到 `isTrustedRequest` 之前——它是唯一能确认身份的 verb,
32
+ 有它就不该退到弱的那一档。
33
+ - **B1 · 只有桌面端会踩的死锁,改为说清楚**(`writeGateRefusal` 的 403 文案)。fence-less 且用户
34
+ 按 503 文案设了 `DSH_INSTRUCTION_MEMORY_ALLOW_UNGUARDED_WRITES=1` 之后,写仍要求 `Origin` 或
35
+ `Sec-Fetch-Site: same-origin`,而桌面壳转发时会**删掉这两个头**——桌面端永久 403、网页端能写。
36
+ **判定不改**(放宽来源校验等于签收一个调用方自带的头,这正是 1.6.3 已否决的设计),只把事实写进
37
+ 文案:桌面端该开关无效,要写只能让宿主提供 `connection` 服务。
38
+
39
+ ### 修复(客户端)
40
+
41
+ - **A3 · `confirmThen` 不再有返回值**(`lib/client.js`)。1.6.5 三个分支都 `return true`,返回值零
42
+ 信息量;而 1.6.4 的 P0("点删除→确定→什么都不发生")正是一个把它当布尔读的调用点。现在函数返回
43
+ `undefined`,调用点统一是"裸调用 + `return`"(九处已全部是这个形态),`lint.mjs` 新增**位置门禁**:
44
+ `confirmThen(` 不得出现在 `if (...)` / `while` / `switch` 头、`return`、三元 `?`、`&&` / `||` / `!`
45
+ 或普通的 `=` 赋值之后;注释里引用的示例不误报。
46
+ - **A4 · 待确认条从单槽改为队列**(`pendingConfirm` → `pendingConfirms`)。原来第二条问题会**覆盖**
47
+ 第一条:用户看到一条,点【确认】执行的却是第二条动作(意图错位,不只是丢失)。现在队列头部渲染、
48
+ 确认/取消各弹出队首、下一条自动接上;打开编辑器仍清空整个队列(此时用户已不在这批问题上)。
49
+ - **A4 连带 · 一次性闸门改成按问题 id**(`confirmFiringRef`)。队列化把这个布尔闸门的两个错解同时暴露
50
+ 出来:一直保持 `true` 会把**下一条**永久锁死(它的按钮永远是 disabled),每次出队都重置又会让一次
51
+ 双击执行**两条不同的**删除。现在闸门存"刚执行的那条问题的 id",只对同一条判等;`ui-test` 新增
52
+ 「连续排队两条 → 两条都能依次确认」的用例,正是这条的回归。
53
+ - **B2 · 桌面分支在 http 入口也能认出来**(`isDesktopShell`)。判据原来只认 `location.protocol ===
54
+ 'dsh-app:'`,而桌面宿主同时用 `http://127.0.0.1:PORT` 提供同一个 profile——从那个入口打开时桌面
55
+ 分支不启用,导出退化成"已请求保存",保底 JSON 不给。现在补一条"回环 host + UA 含 `Electron`"。
56
+
57
+ ### 修复(交付面)
58
+
59
+ - **A5 · CHANGELOG 不再把未发布批次写成已发布**。`[1.6.4]` 改标「未发布(内容已并入 1.6.5)」;
60
+ 1.6.5 的"工作区仍未提交"一条更正为已在 `101f0a5` 提交。`check-metadata.mjs` 新增双向校验:
61
+ 每个 `## [x.y.z]` 节要么有 `v<版本>` tag、要么写明未发布(1.0.10–1.0.12 按文件头的既有声明豁免);
62
+ 反过来,`package.json` 的版本必须有同名节与同名 tag。无 git 时只跳过 tag 那一半,不静默通过。
63
+
64
+ ### 测试
65
+
66
+ - `contract-test.mjs`:翻转 `alpha.3 fence: isTrustedRequest() true admits a write`,改为"读 200 /
67
+ 写 503"两条;新增 `1.6.7: a throwing requestRejection falls through to admit()(读仍 200)`与
68
+ `1.6.7: every verb throwing is still a refusal (401)`。
69
+ - `lint.mjs`:确认门禁从"第二实参是否含 `send(`"扩到"调用所处的语法位置",并豁免注释。
70
+ - `check-metadata.mjs`:见上。
71
+ - 全量:`node test-all.mjs` → 九段全绿;本轮另跑了一次单段 `contract` / `ui` / `mobile-shape` 复核。
72
+
73
+ ### 已知边界(本轮仍未解决,不要当成已修)
74
+
75
+ - **桌面渲染进程(`dsh-app://app`)仍无端到端证据**:`grep isDesktopShell|desktopHint *.mjs` 在
76
+ 本轮之前零命中,B2 的修复也只到"判据正确",没有真窗口点击、导出落地、`EventSource` 走自定义
77
+ 协议的实测。这一条是验收门,不是代码缺陷。
78
+ - **alpha.3 实机仍未复测**:A2 的 503 档在单元层面已锁,但没有在真实的 0.2.3-alpha.3 宿主上跑过;
79
+ 复测产物需同时记录宿主版本与探测到的 verb,否则"通过"不可解释。
80
+ - **W1(网页端 cookie 按 Host 名签发,`localhost` / `[::1]` 混用即 401)** 仍归宿主,插件侧只在
81
+ 文档里写明"用 `127.0.0.1`"。
82
+ - **`1.6.4` 的客户端改动** 仍在 1.6.5 的提交里,无法按版本二分;只能按本节叙述回溯。
83
+
84
+
85
+
86
+ 写门禁恢复对 DSH 0.2.3-alpha.3 的兼容。
87
+
88
+ alpha.3 的 `HostConnectionService` **移除了 `requestRejection()`**,改为公开
89
+ `admit(req) -> { peer } | { rejection: 401|403|503 }`,并保留了只做来源校验的
90
+ `isTrustedRequest(req)`。插件仍只探测旧 verb,于是在 alpha.3 上 `connectionGate` 一直是
91
+ null:宿主明明已经完成会话鉴权,设置页却只能读,所有写操作(保存、删除、导入、选项)
92
+ 一律 503「没有宿主的 connection 会话鉴权门」。
93
+
94
+ ### 修复
95
+
96
+ - **门禁探测兼容三种契约**(`lib/index.js`):`requestRejection`(≤0.2.2)、
97
+ `isTrustedRequest`、`admit`;判断"服务可用"的条件同步放宽。
98
+ - **alpha.3 优先 `isTrustedRequest`**。`admit()` 通过 Cordis 从**调用方 fiber** 解析
99
+ `this.authentication`,插件未注入 `connection.authentication` 时调用它会抛
100
+ "cannot get property … without inject",并让所有请求变成 401。该路由在宿主 webserver
101
+ 层默认 `auth: 'required'`,cookie 已在 `webserver/authorize` 校验,因此这里可用的
102
+ 最强 verb 是来源/宿主信任位;`admit` 保留为后备。
103
+ - **未文档化的 admission 形状一律拒绝**(401),不把意外对象当放行。
104
+
105
+ ### 测试
106
+
107
+ - `contract-test.mjs` 新增 11 条断言:`admit()` 返回 `{ peer }` 放行;401/403/503
108
+ 透传;`undefined` / `{}` / `{ peer: null }` / `{ status: 200 }` fail-closed;
109
+ `isTrustedRequest` true 放行、false 403;无 `connection` 服务时写入仍 503。
110
+ - 隔离宿主复测:alpha.3 与 0.2.2-rc.5 各 21/22;唯一失败是既有的
111
+ `ipv6-loopback-equivalent` 口径(cookie 按 `127.0.0.1` 签发,测试改用 `[::1]`),
112
+ 与插件无关。
113
+
114
+ ### 已知边界
115
+
116
+ - `isTrustedRequest` 只覆盖 Host/Origin 半边;cookie 依赖宿主把该路由按
117
+ `auth: 'required'` 处理。若宿主改掉这个默认值,需要在 alpha.3 上重新确认真实的
118
+ 未登录请求仍会被挡在 handler 之前。
119
+
10
120
  ## [1.6.5] — 2026-11-25
11
121
 
12
122
  第三轮手机适配(1.6.4)的**回归修复**。1.6.4 把八处确认改成了"confirmThen + 重放处理函数"的形态,
@@ -90,10 +200,10 @@
90
200
  (装载、注入进组装后的提示、路由应答、设置页下发)再扩声明;本轮只把 guard 的假红改成 SKIP。
91
201
  - 无 Android 真机:手机侧仍为代码级结论(帧预算现在按壳的真实口径计算,但壳侧丢弃行为未在
92
202
  设备上确认)。
93
- - 工作区仍未提交(`mobile-shape-test.mjs` 与 `test-all.mjs` 是新增文件);不提交则别的机器/CI
94
- 的 `npm test` 会 `MODULE_NOT_FOUND`,`prepublishOnly` 会拒绝发布。
203
+ - ~~工作区仍未提交(`mobile-shape-test.mjs` 与 `test-all.mjs` 是新增文件)~~:**1.6.7 更正**——
204
+ 两者已在 `101f0a5`(1.6.5)提交,`npm test` 九段全跑;本条在发布时即已过期。
95
205
 
96
- ## [1.6.4] — 2026-11-21
206
+ ## [1.6.4] — 未发布(内容已并入 1.6.5)
97
207
 
98
208
  DSH Go(Android + WebView island)适配批次(技术方案评审团三条线的结论)。**没有引入新的手机功能,
99
209
  而是把三处"看起来能用、实际静默失效"的路径修掉**,并把 Android 分支第一次纳入自动化门禁。
package/lib/client.js CHANGED
@@ -2320,9 +2320,32 @@ window.__ModuleLoader__.load({
2320
2320
  * untrustworthy the question is parked here and rendered as a row with
2321
2321
  * 【确认】/【取消】; the action keeps working, one tap later.
2322
2322
  */
2323
- const [pendingConfirm, setPendingConfirm] = React.useState(null)
2324
- /** R2 (1.6.5): one activation of the confirm button runs the action once. */
2325
- const confirmFiringRef = React.useRef(false)
2323
+ /**
2324
+ * 1.6.7 (A4): a QUEUE, not a single slot.
2325
+ *
2326
+ * `pendingConfirm` used to be one slot: a second question arriving before
2327
+ * the first was answered OVERWROTE it, so the user saw one bar, clicked
2328
+ * 【确认】, and the action that ran was the second one. Losing a question
2329
+ * is annoying; running a different destructive action than the one you
2330
+ * read is the failure mode that matters.
2331
+ *
2332
+ * The bar still shows one question at a time (head of the queue); the
2333
+ * rest wait their turn.
2334
+ */
2335
+ const [pendingConfirms, setPendingConfirms] = React.useState([])
2336
+ const pendingConfirm = pendingConfirms.length > 0 ? pendingConfirms[0] : null
2337
+ /**
2338
+ * R2 (1.6.5) / 1.6.7 (A4): one activation runs THAT question's action once.
2339
+ *
2340
+ * It stores the id of the parked question that was just fired, not a
2341
+ * boolean. With a queue a boolean has two wrong answers: keeping it
2342
+ * `true` locks the NEXT question out (its button stays disabled forever,
2343
+ * because nothing re-arms it once the queue advances), and resetting it on
2344
+ * every dequeue lets a double tap fire two DIFFERENT deletes. An id
2345
+ * compares equal only against the same question.
2346
+ */
2347
+ const confirmFiringRef = React.useRef(null)
2348
+ const confirmSeqRef = React.useRef(0)
2326
2349
  /**
2327
2350
  * The single confirmation entry point (1.6.5 contract).
2328
2351
  *
@@ -2335,22 +2358,32 @@ window.__ModuleLoader__.load({
2335
2358
  * silent no-op on desktop and, later, what made a leftover approval leak
2336
2359
  * into the next action with the same wording.
2337
2360
  *
2338
- * So every call site reads `if (confirmThen(message, action)) return` and
2339
- * the action body exists in exactly one place.
2361
+ * So every call site is a bare `confirmThen(...)` followed by `return`:
2362
+ * the guard always stops the caller, and the action body exists in
2363
+ * exactly one place.
2340
2364
  */
2341
2365
  const confirmThen = (message, action) => {
2342
2366
  const ask = askConfirm(message)
2343
2367
  if (ask === CONFIRM_YES) {
2344
2368
  void action()
2345
- return true
2369
+ return
2346
2370
  }
2347
2371
  if (ask === CONFIRM_UNAVAILABLE) {
2348
- // A fresh question re-arms the one-shot firing guard (R2).
2349
- confirmFiringRef.current = false
2350
- setPendingConfirm({ message, action })
2351
- return true
2372
+ // A fresh question gets a fresh id, so the one-shot firing guard
2373
+ // (R2) still distinguishes "the same question twice" from "the next
2374
+ // question" (1.6.7 A4).
2375
+ confirmSeqRef.current += 1
2376
+ setPendingConfirms((queue) => queue.concat([{ message, action, id: confirmSeqRef.current }]))
2352
2377
  }
2353
- return true
2378
+ // 1.6.7 (A3): NO return value.
2379
+ //
2380
+ // 1.6.5 answered `true` from every branch, which made the value pure
2381
+ // noise — and the 1.6.4 P0 ("click delete → confirm → nothing happens")
2382
+ // was exactly a call site that read it as a boolean. Call sites now
2383
+ // read `confirmThen(...)` followed by `return`: the guard always stops
2384
+ // the caller, and the action exists in one place. `lint.mjs` refuses
2385
+ // the call inside `if (...)` / `? :` / an assignment / `return`, so the
2386
+ // old shape cannot come back unnoticed.
2354
2387
  }
2355
2388
  /** True once the event stream has failed long enough to stop pretending. */
2356
2389
  const [liveDown, setLiveDown] = React.useState(false)
@@ -3091,8 +3124,32 @@ window.__ModuleLoader__.load({
3091
3124
  */
3092
3125
  function isDesktopShell() {
3093
3126
  try {
3094
- return typeof location !== 'undefined' && typeof location.protocol === 'string'
3095
- && location.protocol === 'dsh-app:'
3127
+ if (typeof location === 'undefined' || typeof location.protocol !== 'string') return false
3128
+ // The renderer's own protocol: an unmistakeable observation.
3129
+ if (location.protocol === 'dsh-app:') return true
3130
+ /**
3131
+ * 1.6.7 (B2): the desktop host ALSO serves the same profile over
3132
+ * `http://127.0.0.1:PORT`, and that is how the panel is usually
3133
+ * opened (the Web GUI URL this session runs against is exactly that
3134
+ * shape). Judging only `dsh-app:` therefore missed the desktop
3135
+ * surface on the http entry: the export fell through to the generic
3136
+ * `export.saved` ("requested a save") and the guaranteed in-panel
3137
+ * copy was never offered, even though this IS the shell whose
3138
+ * `will-download` handler cannot confirm a blob.
3139
+ *
3140
+ * The extra test is loopback host + an Electron user agent, which
3141
+ * only the shell presents. A plain browser on the same loopback URL
3142
+ * keeps the ordinary browser branch, so the message stays accurate
3143
+ * there.
3144
+ */
3145
+ const host = typeof location.hostname === 'string' ? location.hostname : ''
3146
+ const loopback = host === '127.0.0.1' || host === 'localhost'
3147
+ || host === '[::1]' || host === '::1'
3148
+ if (!loopback) return false
3149
+ const agent = typeof navigator !== 'undefined' && typeof navigator.userAgent === 'string'
3150
+ ? navigator.userAgent
3151
+ : ''
3152
+ return agent.indexOf('Electron') !== -1
3096
3153
  } catch {
3097
3154
  return false
3098
3155
  }
@@ -4295,22 +4352,27 @@ window.__ModuleLoader__.load({
4295
4352
  // delete survived on the host's idempotency, but import/restore
4296
4353
  // were never verified. `busy` also blocks it here, so an action
4297
4354
  // that is already in flight cannot be re-fired from the bar.
4298
- disabled: busy === true || confirmFiringRef.current === true,
4355
+ disabled: busy === true
4356
+ || (pendingConfirm !== null && confirmFiringRef.current === pendingConfirm.id),
4299
4357
  onClick: () => {
4300
4358
  const pending = pendingConfirm
4301
- if (pending === null || confirmFiringRef.current === true) return
4302
- // Stays true until the NEXT question is parked: a second
4303
- // activation in the same tick (double tap, touch jitter) must
4304
- // not run the action twice. The delete survived on the host's
4305
- // idempotency; import/restore never had that guarantee.
4306
- confirmFiringRef.current = true
4307
- setPendingConfirm(null)
4359
+ if (pending === null || confirmFiringRef.current === pending.id) return
4360
+ // Stays set to THIS question's id: a second activation of the
4361
+ // same bar (double tap, touch jitter) must not run the action
4362
+ // twice, while the NEXT question — a different id — is still
4363
+ // confirmable. The delete survived on the host's idempotency;
4364
+ // import/restore never had that guarantee.
4365
+ confirmFiringRef.current = pending.id
4366
+ // 1.6.7 (A4): pop the HEAD. Any other parked question keeps
4367
+ // its place and is shown next, instead of having been
4368
+ // silently overwritten by this one.
4369
+ setPendingConfirms((queue) => queue.slice(1))
4308
4370
  void pending.action()
4309
4371
  },
4310
4372
  }, t('confirm.yes')),
4311
4373
  h('button', {
4312
4374
  className: 'im-btn',
4313
- onClick: () => setPendingConfirm(null),
4375
+ onClick: () => setPendingConfirms((queue) => queue.slice(1)),
4314
4376
  }, t('confirm.no'))))
4315
4377
  : null,
4316
4378
  error === '' && notice !== '' ? h('div', { className: 'im-ok', role: 'status', 'aria-live': 'polite' }, notice) : null,
@@ -4483,7 +4545,10 @@ window.__ModuleLoader__.load({
4483
4545
  // A6 (1.6.5): the editor is an early return, so a confirmation
4484
4546
  // parked in the list view would be invisible while the draft
4485
4547
  // is open — the action would hang until the editor closed.
4486
- setPendingConfirm(null)
4548
+ // 1.6.7 (A4): that applies to the whole QUEUE now, and it is
4549
+ // still an intentional drop — the operator opened an editor,
4550
+ // so they are no longer answering those questions.
4551
+ setPendingConfirms([])
4487
4552
  setDraft(cloneEntry(entry))
4488
4553
  setProfileDraft(null)
4489
4554
  setError('')
package/lib/index.js CHANGED
@@ -4239,8 +4239,9 @@ export function apply(ctx, deps = {}) {
4239
4239
  * RIGHT NOW. `hostGuarded === false` on its own reads as "no
4240
4240
  * authentication at all", which is only true when this is false too:
4241
4241
  * with the fence attached the route still requires a logged-in browser
4242
- * session (`connection.requestRejection`), and the panel must be able
4243
- * to say which of the two situations it is looking at.
4242
+ * session (`connection.requestRejection` / `connection.admit`), and the
4243
+ * panel must be able to say which of the two situations it is looking
4244
+ * at.
4244
4245
  */
4245
4246
  authFence: connectionGate !== null,
4246
4247
  /**
@@ -6103,19 +6104,117 @@ export function apply(ctx, deps = {}) {
6103
6104
  /**
6104
6105
  * F19: the composition's `connection` service, when the profile has one.
6105
6106
  *
6106
- * It is the harness's single authentication fence: `requestRejection(req)`
6107
- * applies the Host/Origin trust check AND requires the browser login cookie
6107
+ * It is the harness's single authentication fence: the host uses it to
6108
+ * apply the Host/Origin trust check AND require the browser login cookie
6108
6109
  * (the same gate that makes `/api` answer 401 to an anonymous caller). This
6109
6110
  * route used to rely on the Origin guesswork below alone, which left it as
6110
6111
  * the only unauthenticated read/write endpoint on the instance — a stored
6111
6112
  * prompt-injection primitive for any local process.
6112
6113
  *
6114
+ * The service's verb changed across host versions, so probe all three:
6115
+ * - `requestRejection(req)` — through 0.2.2: a rejection status, or
6116
+ * null/undefined when admitted;
6117
+ * - `isTrustedRequest(req)` — the source/Host half without credentials;
6118
+ * 0.2.3-alpha.3 exposes it and its `admit()` cannot be called from a
6119
+ * fiber that did not inject `connection.authentication`, so this is the
6120
+ * verb actually used there (the host's `webserver/authorize` listener has
6121
+ * already verified the cookie for this `auth: 'required'` route);
6122
+ * - `admit(req)` — public contract: `{ peer }` or
6123
+ * `{ rejection: 401|403|503 }`, used when the trust verb is absent.
6124
+ * 0.2.3-alpha.3 REMOVED `requestRejection`, so probing only that verb left
6125
+ * the gate null there and answered every write 503 even though the host had
6126
+ * already authenticated the request.
6127
+ *
6113
6128
  * Read through a NON-BLOCKING child fiber for the same reason `webServer` is
6114
6129
  * (headless/tui profiles must still inject the prompt), and kept nullable:
6115
6130
  * when the service is absent the loopback-only rule below still applies.
6116
6131
  */
6117
6132
  let connectionGate = null
6118
6133
 
6134
+ /** Does the published `connection` service expose a verb usable as a gate? */
6135
+ const hasConnectionGate = (service) => service !== null && service !== undefined
6136
+ && (typeof service.requestRejection === 'function'
6137
+ || typeof service.admit === 'function'
6138
+ || typeof service.isTrustedRequest === 'function')
6139
+
6140
+ /**
6141
+ * Ask the host fence about one request and normalize its answer to the
6142
+ * rejection code the caller already understands (`undefined` = admitted).
6143
+ *
6144
+ * An admission shape the host did not document is a refusal, never an
6145
+ * admit: a gate that silently opens on an unexpected object is not a gate.
6146
+ */
6147
+ const connectionGateVerdict = (service, req) => {
6148
+ /**
6149
+ * 1.6.7 (A1/A2): probe EVERY verb, in strength order, and keep going when
6150
+ * one throws.
6151
+ *
6152
+ * 1.6.6 picked the first verb that EXISTED and let it decide the whole
6153
+ * request. Two failure modes came out of that:
6154
+ *
6155
+ * - A verb that throws was normalised to 401 by the caller, so a host
6156
+ * that still exposed `requestRejection` (which may internally call
6157
+ * `admit()`, and `admit()` throws on a fiber that did not inject
6158
+ * `connection.authentication`) answered **401 to reads as well** — the
6159
+ * settings page never mounted, and the message pointed at "log in"
6160
+ * instead of at the real cause. Now a throwing verb is logged and the
6161
+ * next one is tried; only when nothing usable answers do we refuse.
6162
+ *
6163
+ * - `admit()` used to be the LAST resort. It is the only verb that
6164
+ * establishes identity (it checks the browser cookie), so a host that
6165
+ * exposes both it and `isTrustedRequest` fell through to the weaker
6166
+ * one. `admit` now comes second, ahead of the trust-only verb.
6167
+ *
6168
+ * `trustOnly` is carried out of here on purpose: `isTrustedRequest` checks
6169
+ * the source half only (`dsh-client-connection` documents that it "never
6170
+ * establishes identity"), so a fence that answered with it is not a
6171
+ * credential check. Reads stay open — the panel has to load — and
6172
+ * mutations fail closed in `writeGateRefusal`.
6173
+ *
6174
+ * @returns {{ rejection: (number|undefined), trustOnly: boolean, verb: string }}
6175
+ */
6176
+ const thrown = []
6177
+ // 1) Full gate (Host/Origin + login cookie) — the contract through 0.2.2.
6178
+ if (typeof service.requestRejection === 'function') {
6179
+ try {
6180
+ return { rejection: service.requestRejection(req), trustOnly: false, verb: 'requestRejection' }
6181
+ } catch (err) {
6182
+ thrown.push('requestRejection: ' + describeError(err))
6183
+ }
6184
+ }
6185
+ // 2) The public admission contract: `{ peer }` or `{ rejection }`.
6186
+ if (typeof service.admit === 'function') {
6187
+ try {
6188
+ const admission = service.admit(req)
6189
+ if (admission === null || typeof admission !== 'object') {
6190
+ return { rejection: 401, trustOnly: false, verb: 'admit' }
6191
+ }
6192
+ if ('rejection' in admission) {
6193
+ return { rejection: admission.rejection, trustOnly: false, verb: 'admit' }
6194
+ }
6195
+ if (admission.peer !== undefined && admission.peer !== null) {
6196
+ return { rejection: undefined, trustOnly: false, verb: 'admit' }
6197
+ }
6198
+ return { rejection: 401, trustOnly: false, verb: 'admit' }
6199
+ } catch (err) {
6200
+ thrown.push('admit: ' + describeError(err))
6201
+ }
6202
+ }
6203
+ // 3) The source-only half. Never an identity check — see `trustOnly`.
6204
+ if (typeof service.isTrustedRequest === 'function') {
6205
+ try {
6206
+ const trusted = service.isTrustedRequest(req) === true
6207
+ return { rejection: trusted ? undefined : 403, trustOnly: true, verb: 'isTrustedRequest' }
6208
+ } catch (err) {
6209
+ thrown.push('isTrustedRequest: ' + describeError(err))
6210
+ }
6211
+ }
6212
+ // Nothing usable answered. Refuse, and say what actually happened instead
6213
+ // of reporting a bare 401 that reads like "log in".
6214
+ logInfo('connection fence exposed no usable verdict' + (thrown.length === 0 ? '' : ' (' + thrown.join('; ') + ')') + ', refusing')
6215
+ return { rejection: 401, trustOnly: false, verb: 'none' }
6216
+ }
6217
+
6119
6218
  /**
6120
6219
  * Does this stream subscriber want lightweight frames?
6121
6220
  *
@@ -6254,14 +6353,15 @@ export function apply(ctx, deps = {}) {
6254
6353
  */
6255
6354
  const rejectUntrustedCaller = (req, res) => {
6256
6355
  if (connectionGate !== null) {
6257
- let rejection
6356
+ let verdict
6258
6357
  try {
6259
- rejection = connectionGate.requestRejection(req)
6358
+ verdict = connectionGateVerdict(connectionGate, req)
6260
6359
  } catch (err) {
6261
6360
  // A throwing fence must not become an open door: treat it as 401.
6262
6361
  logInfo('connection fence threw, refusing the request: ' + describeError(err))
6263
- rejection = 401
6362
+ verdict = { rejection: 401, trustOnly: false, verb: 'threw' }
6264
6363
  }
6364
+ const rejection = verdict.rejection
6265
6365
  // Only the documented "admit" values pass. Anything else — a future
6266
6366
  // `{status:401}`, an Error, a string — is treated as a refusal, because
6267
6367
  // an authentication gate that silently opens on an unexpected shape is
@@ -6269,18 +6369,24 @@ export function apply(ctx, deps = {}) {
6269
6369
  const admitted = rejection === undefined || rejection === null || rejection === false
6270
6370
  if (!admitted) {
6271
6371
  const status = typeof rejection === 'number' && rejection >= 400 && rejection <= 599 ? rejection : 401
6272
- logInfo('route refused a request (' + status + ', fence returned '
6372
+ logInfo('route refused a request (' + status + ', fence verb ' + verdict.verb + ' returned '
6273
6373
  + (typeof rejection === 'number' ? rejection : typeof rejection) + ')')
6274
6374
  res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
6275
6375
  res.end(JSON.stringify({
6276
6376
  ok: false,
6277
6377
  message: rejection === 403
6278
6378
  ? '请求未被宿主信任(Host/Origin 校验失败)'
6279
- : '需要登录 DSH 后的浏览器会话才能访问该路由',
6379
+ : (verdict.verb === 'none'
6380
+ ? '宿主的 connection 服务没有给出可用的鉴权结论(三个 verb 全部缺失或抛错),已按失败关闭处理;这是宿主版本不匹配,不是登录问题。'
6381
+ : '需要登录 DSH 后的浏览器会话才能访问该路由'),
6280
6382
  }))
6281
6383
  return 'rejected'
6282
6384
  }
6283
- return 'gate'
6385
+ // 1.6.7 (A2): report WHICH half answered. A trust-only fence settled the
6386
+ // Host/Origin question, so `originVerdictRefuses` must still skip itself,
6387
+ // but it proved nothing about identity — `writeGateRefusal` refuses every
6388
+ // mutation on this tier.
6389
+ return verdict.trustOnly ? 'gate-trust-only' : 'gate'
6284
6390
  }
6285
6391
  /**
6286
6392
  * Deeper defence for deployments where loopback is not a boundary: on
@@ -6330,7 +6436,12 @@ export function apply(ctx, deps = {}) {
6330
6436
  * @returns true when the request was answered with 403 and must not continue.
6331
6437
  */
6332
6438
  const originVerdictRefuses = (req, res, fence) => {
6333
- if (fence === 'gate') return false
6439
+ // 1.6.7: BOTH gate tiers already answered the Host/Origin question through
6440
+ // the host's own listener. 'gate-trust-only' differs in what it proves
6441
+ // about identity (nothing), not in which question it settled — so the
6442
+ // local verdict must still skip itself, or a reverse-proxy client would
6443
+ // get the false 403 this function exists to prevent.
6444
+ if (fence === 'gate' || fence === 'gate-trust-only') return false
6334
6445
  const verdict = requestOriginVerdict(req.headers, guardHost, expectedOriginHostname, trustedHosts)
6335
6446
  if (verdict.ok) return false
6336
6447
  logInfo('route rejected a request: ' + verdict.reason)
@@ -6398,12 +6509,38 @@ export function apply(ctx, deps = {}) {
6398
6509
  */
6399
6510
  const READ_ONLY_METHODS = new Set(READ_ONLY_METHOD_NAMES)
6400
6511
 
6401
- const writeGateRefusal = (method, headers) => {
6512
+ const writeGateRefusal = (method, headers, fence) => {
6402
6513
  const name = typeof method === 'string' ? method : ''
6403
6514
  // One table decides kind (see METHOD_KINDS). An unknown name is not a write
6404
6515
  // — dispatch answers it — but it is also not a read, so it falls through to
6405
6516
  // the same rules a write meets only when a fence is present.
6406
6517
  if (METHOD_KINDS[name] === 'read') return null
6518
+ /**
6519
+ * 1.6.7 (A2): a TRUST-ONLY fence is not an authentication fence.
6520
+ *
6521
+ * `isTrustedRequest` checks Host/Origin and nothing else; the host's own
6522
+ * source says it "never establishes identity". On a host that exposes only
6523
+ * that verb (0.2.3-alpha.3 shape), a same-user process that sends
6524
+ * `Host: 127.0.0.1:PORT` walks through it. Every mutation here rewrites the
6525
+ * text injected into all later turns, so a source check alone is not a
6526
+ * gate for writes: refuse them (503) while keeping reads open so the panel
6527
+ * can still load and say why.
6528
+ *
6529
+ * This is deliberately NOT a hard failure of the whole route: a read-only
6530
+ * panel that explains the problem beats a settings page that does not
6531
+ * load, and it keeps `state`/`reload` working for an operator who only
6532
+ * wants to inspect what is already stored.
6533
+ */
6534
+ if (fence === 'gate-trust-only') {
6535
+ return {
6536
+ status: 503,
6537
+ message: '拒绝修改请求:当前宿主的 connection 服务只提供了来源校验(isTrustedRequest),'
6538
+ + '没有提供能确认登录会话的凭据校验(admit / requestRejection)。来源校验只说明"请求来自本机",'
6539
+ + '不说明"请求来自设置页"——本机任何进程都能自带该头,因此写操作已停用(读操作不受影响)。'
6540
+ + '请升级宿主到同时提供 admit() 与 requestRejection() 的版本,或改用 ' + REQUIRE_AUTH_FENCE_ENV
6541
+ + '=1 关闭整条路由。',
6542
+ }
6543
+ }
6407
6544
  if (connectionGate !== null) return null
6408
6545
  const head = headers !== null && typeof headers === 'object' ? headers : {}
6409
6546
  if (guardHost !== true && trustedHosts.length > 0) {
@@ -6444,7 +6581,15 @@ export function apply(ctx, deps = {}) {
6444
6581
  status: 403,
6445
6582
  message: '拒绝无来源标记的修改请求:已通过 ' + ALLOW_UNGUARDED_WRITES_ENV + '=1 允许无鉴权写,'
6446
6583
  + '因此修改仍需带浏览器来源标记(Origin 或 Sec-Fetch-Site: same-origin)。这不是身份校验——'
6447
- + '两个头本机进程也能自带;要真正关闭该路由,请设置 ' + REQUIRE_AUTH_FENCE_ENV + '=1,或让宿主提供 connection 服务。',
6584
+ + '两个头本机进程也能自带;要真正关闭该路由,请设置 ' + REQUIRE_AUTH_FENCE_ENV + '=1,或让宿主提供 connection 服务。'
6585
+ // 1.6.7 (B1): say out loud that this switch cannot work on one of the
6586
+ // two first-party surfaces. The DSH desktop shell DELETES `Origin` and
6587
+ // `Sec-Fetch-Site` when it forwards (see TRUSTED_HOSTS above), so a
6588
+ // desktop user who follows the 503 above and sets the switch gets a
6589
+ // permanent 403 here while the browser surface works — the message
6590
+ // used to promise "set the switch and you can write" on both.
6591
+ + '【DSH 桌面端注意:桌面壳转发时会删除 Origin 与 Sec-Fetch-Site,本开关在桌面端无效——'
6592
+ + '桌面端要恢复写入,只能让宿主提供 connection 服务。网页端从 127.0.0.1 直连时本开关有效。】',
6448
6593
  }
6449
6594
  }
6450
6595
  return null
@@ -6594,7 +6739,7 @@ export function apply(ctx, deps = {}) {
6594
6739
  // A9/A12: mutations on a fence-less route need a browser origin
6595
6740
  // marker. Checked after parsing (the method is only known now) and
6596
6741
  // before dispatch, so a refused write never touches the store.
6597
- const gateRefusal = writeGateRefusal(payload.method, req.headers)
6742
+ const gateRefusal = writeGateRefusal(payload.method, req.headers, fence)
6598
6743
  if (gateRefusal !== null) {
6599
6744
  logInfo('route refused a mutation: ' + gateRefusal.message)
6600
6745
  res.writeHead(gateRefusal.status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
@@ -6673,8 +6818,8 @@ export function apply(ctx, deps = {}) {
6673
6818
  // wait for one.
6674
6819
  ctx.inject(['connection'], (scope) => {
6675
6820
  const service = scope.connection
6676
- if (service === undefined || service === null || typeof service.requestRejection !== 'function') {
6677
- logDebug('no connection service; the route falls back to the loopback-only rule')
6821
+ if (!hasConnectionGate(service)) {
6822
+ logDebug('no usable connection service; the route falls back to the loopback-only rule')
6678
6823
  return
6679
6824
  }
6680
6825
  connectionGate = service
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-instruction-memory",
3
- "version": "1.6.5",
3
+ "version": "1.6.7",
4
4
  "description": "User-only instruction memory for DSH: long-term instructions maintained in the settings UI and auto-injected into every conversation — the plugin registers no model-facing tool, so the model has no in-plugin write path (a same-permission process can still edit the file directly). 指令记忆:只由用户在设置页维护的长期指令,自动注入此后每轮对话;插件不注册任何面向模型的工具,模型没有插件内的写入口。",
5
5
  "license": "MIT",
6
6
  "type": "module",