dsh-dock 0.23.1 → 0.24.0

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.
@@ -442,37 +442,33 @@ function endReasonSpeech(reason) {
442
442
  return "完成了";
443
443
  }
444
444
  /** 结束播报文案(success 由调用方按 isSuccessReason 给)。kind: complete|error
445
- * 子任务结束(主任务可能还在跑)必须报「子任务」而不是「任务」——否则听着像整个
446
- * 任务完成了(2026-11-05 用户反馈)。简洁只报「子任务完成/停止了」;适中点名
447
- * 主任务与子任务;详细再带统计与摘要。归属字段来自完成记录/活跃快照(task-track)。 */
445
+ * 统一口径(用户 2026-11-07 要求):**先说「主任务/子任务」,再说事件(完成了/停止了),
446
+ * 任务名接在后面**,例如「主任务完成了「标题」」「子任务停止了「标题」」。
447
+ * 子任务结束(主任务可能还在跑)必须报「子任务」而不是「主任务」——否则听着像整个任务结束。*/
448
448
  export function endBroadcastText(success, record, task, cfg) {
449
449
  const mode = voiceTone(cfg);
450
450
  const reason = (record && record.endReason) || "";
451
451
  const title = (record && record.title) || (task && task.title) || "";
452
452
  const sub = !!((record && record.isSubtask) || (task && task.isSubtask));
453
+ const role = sub ? "子任务" : "主任务";
453
454
  const parentTitle = (record && record.parentTitle) || (task && task.parentTitle) || "";
454
- if (mode === "brief") {
455
- // 简洁口径不分原因细念:正常完成「任务完成」,其余一律「任务停止了」;子任务换主语
456
- if (sub) return success ? "子任务完成" : "子任务停止了";
457
- return success ? "任务完成" : "任务停止了";
458
- }
455
+ // 先说角色 + 事件,再跟任务名
456
+ const head = role + (success ? "完成了" : "停止了") + (title ? "「" + truncate(title, 30) + "」" : "");
457
+ if (mode === "brief") return head;
459
458
  if (mode === "medium") {
460
- // 适中口径:点名是哪个任务 + 结果 + (停止时)原因 + 耗时;不念回合/Token 统计
461
- // 子任务:主任务名截短些(20 字),整句别太长
462
- const who = sub
463
- ? (parentTitle ? "主任务「" + truncate(parentTitle, 20) + "」的" : "") + (title ? "子任务「" + truncate(title, 30) + "」" : "子任务")
464
- : (title ? "任务「" + truncate(title, 30) + "」" : "任务");
465
- const parts = [success ? who + "完成了" : who + "停止了"];
459
+ // 适中口径:角色+结果+任务名 +(停止原因)+(子任务点名主任务)+ 耗时;不念回合/Token
460
+ const parts = [head];
466
461
  if (!success) parts.push(",原因:" + endReasonSpeech(reason));
462
+ if (sub && parentTitle) parts.push("(主任务「" + truncate(parentTitle, 20) + "」)");
467
463
  const dur = (record && record.duration) || 0;
468
464
  if (dur > 0) parts.push(",耗时" + fmtDur(dur));
469
465
  parts.push("。");
470
466
  return parts.join("");
471
467
  }
472
- const parts = [];
468
+ // 详细:角色+结果+任务名 → 主任务(子任务时)→ 原因/耗时 → 统计 → 摘要
469
+ const parts = [head + "。"];
473
470
  if (sub && parentTitle) parts.push("主任务:" + truncate(parentTitle, 30) + "。");
474
- if (title) parts.push((sub ? "子任务:" : "任务:") + truncate(title, 30) + "。");
475
- parts.push(success ? "已完成。" : "结束,原因:" + endReasonSpeech(reason) + "。");
471
+ if (!success) parts.push("原因:" + endReasonSpeech(reason) + "。");
476
472
  const dur = (record && record.duration) || 0;
477
473
  if (dur > 0) parts.push("耗时" + fmtDur(dur) + "。");
478
474
  const turns = (record && record.turns) || (task && task.turns) || 0;
@@ -486,63 +482,59 @@ export function endBroadcastText(success, record, task, cfg) {
486
482
  }
487
483
  return parts.join("");
488
484
  }
489
- /** 需授权播报文案。 */
485
+ /** 需授权播报文案。统一口径:先说「主任务/子任务」,再说「需要授权」,任务名接在后面。 */
490
486
  export function confirmBroadcastText(task, approval, cfg) {
491
487
  const mode = voiceTone(cfg);
492
- if (mode === "brief") return "任务需要授权";
488
+ const role = task && task.isSubtask ? "子任务" : "主任务";
489
+ const title = (task && task.title) || "";
490
+ const head = role + "需要授权" + (title ? "「" + truncate(title, 30) + "」" : "");
493
491
  const tool = (approval && approval.toolName) || "工具";
494
492
  const scene = toolLabel(tool);
495
- if (mode === "medium") {
496
- // 适中口径:点名任务 + 要批准的场景;不念工具名与「回到会话」的叮嘱
497
- const who = task && task.title ? "任务「" + truncate(task.title, 30) + "」" : "任务";
498
- return who + "需要授权,正在等你批准" + (scene || "工具 " + tool) + "。";
499
- }
500
- const parts = [];
501
- if (task && task.title) parts.push("任务:" + truncate(task.title, 30) + "。");
493
+ if (mode === "brief") return head;
494
+ if (mode === "medium") return head + ",正在等你批准" + (scene || "工具 " + tool) + "。";
495
+ const parts = [head + "。"];
502
496
  parts.push("正在等待你批准" + (scene ? "执行" + scene : "") + ",工具是" + tool + ",请回到会话处理。");
503
497
  return parts.join("");
504
498
  }
505
499
  /**
506
500
  * 需回答播报文案(ask_user_question 挂起,等人选答案)。
507
- * 与「需授权」是两件事:批准是"要不要执行",提问是"要选哪个答案"——播报不能混说。
508
- * 简洁口径只说「任务需要回答」;适中点名任务与问题;详细再补"共几问"与回到会话的叮嘱。
501
+ * 与「需授权」是两件事。统一口径:先说「主任务/子任务」,再说「需要你回答」,任务名/问题接后面。
509
502
  */
510
503
  export function questionBroadcastText(task, question, cfg) {
511
504
  const mode = voiceTone(cfg);
512
- if (mode === "brief") return "任务需要你回答";
513
- const who = task && task.title ? "任务「" + truncate(task.title, 30) + "」" : "任务";
514
- const head = (question && question.header) || "";
505
+ const role = task && task.isSubtask ? "子任务" : "主任务";
506
+ const title = (task && task.title) || "";
507
+ const head = role + "需要你回答" + (title ? "「" + truncate(title, 30) + "」" : "");
508
+ const qhead = (question && question.header) || "";
515
509
  const body = (question && question.question) || "";
516
510
  const what = body ? truncate(body.replace(/[#*_`>\[\]()!]/g, ""), 40) : "";
511
+ if (mode === "brief") return head;
517
512
  if (mode === "medium") {
518
- // 适中口径:点名任务 + 问的是什么(问题正文优先,没有正文退回标题);句号统一补
519
- const tail = what ? ":" + what : (head ? ":" + head : "");
520
- return who + "在等你回答" + tail + "。";
513
+ const tail = what ? ":" + what : (qhead ? ":" + qhead : "");
514
+ return head + tail + "。";
521
515
  }
522
- const parts = [];
523
- if (task && task.title) parts.push("任务:" + truncate(task.title, 30) + "。");
524
- parts.push(what ? "正在等你回答问题:" + what + "。" : "正在等你回答问题。");
516
+ const parts = [head + "。"];
517
+ parts.push(what ? "问题:" + what + "。" : (qhead ? "问题:" + qhead + "。" : "正在等你回答问题。"));
525
518
  const count = (question && question.count) || 0;
526
519
  if (count > 1) parts.push("共 " + count + " 个问题。");
527
520
  parts.push("请回到会话选择答案。");
528
521
  return parts.join("");
529
522
  }
530
523
  /**
531
- * 卡住播报文案:必须回答「哪里卡住了」——主任务还是子任务、哪个会话、停了多久、
532
- * 最后在干什么。子任务静默会把「主任务明明在跑却突然播报」放大成困惑(2026-10-30
533
- * 用户实测:主任务进行中突然播「任务卡住了」,屏幕上却看不到卡住的东西),
534
- * 所以简洁口径也带「主/子 + 标题」,详细口径再补停滞时长与最后动作。
524
+ * 卡住播报文案:统一口径(用户 2026-11-07 要求)**先说「主任务/子任务」,再说「卡住了」,
525
+ * 任务名接在后面**,详细再补停滞时长与最后动作。例:「主任务卡住了「标题」」。
535
526
  * 归属字段来自宿主追踪快照(task-track 的 isSubtask/lastToolName)。
536
527
  */
537
528
  export function stuckBroadcastText(task, cfg) {
538
529
  const mode = voiceTone(cfg);
539
- const where = (task && task.isSubtask ? "子任务" : "主任务")
540
- + (task && task.title ? "「" + truncate(task.title, 30) + "」" : "");
541
- if (mode === "brief") return where + "卡住了";
530
+ const role = task && task.isSubtask ? "子任务" : "主任务";
531
+ const title = (task && task.title) || "";
532
+ const head = role + "卡住了" + (title ? "「" + truncate(title, 30) + "」" : "");
533
+ if (mode === "brief") return head;
542
534
  if (mode === "medium") {
543
- return where + "卡住了,已经" + fmtDur(Math.max(0, (cfg && cfg.stuckMs) || STUCK_MS)) + "没有任何动静。";
535
+ return head + ",已经" + fmtDur(Math.max(0, (cfg && cfg.stuckMs) || STUCK_MS)) + "没有任何动静。";
544
536
  }
545
- const parts = [where + "卡住了。"];
537
+ const parts = [head + "。"];
546
538
  parts.push("编程阶段已经" + fmtDur(Math.max(0, (cfg && cfg.stuckMs) || STUCK_MS)) + "没有任何动静,");
547
539
  const tool = (task && task.lastToolName) || "";
548
540
  const scene = tool ? toolLabel(tool) : "";
@@ -206,8 +206,8 @@ export const feature = {
206
206
  setup(ctx) {
207
207
  const webServer = ctx.get('webServer')
208
208
  if (webServer === undefined) throw new Error('webServer 服务不可用,无法提供模型提供商路由')
209
- const handler = async (req, res) => {
210
- if (rejectUntrustedRequest(ctx, req, res)) return
209
+ const handler = async (req, res, peer) => {
210
+ if (rejectUntrustedRequest(ctx, req, res, peer)) return
211
211
  try {
212
212
  if (req.method === 'GET') {
213
213
  const payload = readModelDirectory(ctx)
@@ -230,6 +230,6 @@ export const feature = {
230
230
  sendJson(res, status, { ok: false, error: e instanceof Error ? e.message : String(e) })
231
231
  }
232
232
  }
233
- return webServer.register({ kind: 'exact', path: '/dsh-dock/providerconfig', handler })
233
+ return webServer.register({ kind: 'exact', auth: 'required', path: '/dsh-dock/providerconfig', handler })
234
234
  },
235
235
  }
@@ -35,9 +35,10 @@ export const feature = {
35
35
  disposers.push(ctx.inject(['webServer'], (wsCtx) => {
36
36
  wsCtx.effect(() => wsCtx.webServer.register({
37
37
  kind: 'prefix',
38
+ auth: 'required',
38
39
  path: '/dsh-dock/runstate',
39
- async handler(req, res) {
40
- if (rejectUntrustedRequest(ctx, req, res)) return
40
+ async handler(req, res, peer) {
41
+ if (rejectUntrustedRequest(ctx, req, res, peer)) return
41
42
  try {
42
43
  const url = new URL(req.url || '/', 'http://dsh.internal')
43
44
  const method = url.pathname.replace(/^\/dsh-dock\/runstate\/?/, '').split('/')[0] || ''
@@ -1035,9 +1035,10 @@ export const feature = {
1035
1035
  disposers.push(ctx.inject(['webServer'], (wsCtx) => {
1036
1036
  wsCtx.effect(() => wsCtx.webServer.register({
1037
1037
  kind: 'prefix',
1038
+ auth: 'required',
1038
1039
  path: '/dsh-dock/tokenlog',
1039
- async handler(req, res) {
1040
- if (rejectUntrustedRequest(ctx, req, res)) return
1040
+ async handler(req, res, peer) {
1041
+ if (rejectUntrustedRequest(ctx, req, res, peer)) return
1041
1042
  try {
1042
1043
  const url = new URL(req.url || '/', 'http://dsh.internal')
1043
1044
  const method = url.pathname.replace(/^\/dsh-dock\/tokenlog\/?/, '').split('/')[0] || ''
@@ -119,9 +119,10 @@ export const feature = {
119
119
  disposers.push(ctx.inject(['webServer'], (wsCtx) => {
120
120
  wsCtx.effect(() => wsCtx.webServer.register({
121
121
  kind: 'prefix',
122
+ auth: 'required',
122
123
  path: '/dsh-dock/tts',
123
- async handler(req, res) {
124
- if (rejectUntrustedRequest(ctx, req, res)) return
124
+ async handler(req, res, peer) {
125
+ if (rejectUntrustedRequest(ctx, req, res, peer)) return
125
126
  try {
126
127
  const url = new URL(req.url || '/', 'http://dsh.internal')
127
128
  const method = url.pathname.replace(/^\/dsh-dock\/tts\/?/, '').split('/')[0] || ''
package/index.js CHANGED
@@ -32,6 +32,7 @@ import { feature as fRunState } from './features/runstate/host.js'
32
32
  import { feature as fTts } from './features/tts/host.js'
33
33
  import { feature as fMobileRelay } from './features/mobile-relay/host.js'
34
34
  import { feature as fMcpConfig } from './features/mcpconfig/host.js'
35
+ import { feature as fDshTools } from './features/dshtools/host.js'
35
36
 
36
37
  export const name = 'dsh-dock'
37
38
 
@@ -76,6 +77,7 @@ export function apply(ctx, config) {
76
77
  fTts,
77
78
  fMobileRelay,
78
79
  fMcpConfig,
80
+ fDshTools,
79
81
  ]
80
82
 
81
83
  // 绑定 Config 镜像(替代已移除的 settings.get);volatile 字段热更时重绑。
@@ -197,9 +199,10 @@ export function apply(ctx, config) {
197
199
  if (webServer) {
198
200
  webServer.register({
199
201
  kind: 'exact',
202
+ auth: 'required',
200
203
  path: '/dsh-dock/features',
201
- handler: async (req, res) => {
202
- if (rejectUntrustedRequest(ctx, req, res)) return
204
+ handler: async (req, res, peer) => {
205
+ if (rejectUntrustedRequest(ctx, req, res, peer)) return
203
206
  try {
204
207
  if (req.method === 'POST') {
205
208
  const body = await readBody(req)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-dock",
3
- "version": "0.23.1",
4
- "description": "DeepSeek Harness 功能坞插件(dsh-dock):模块化功能中枢——每个功能一个 features/ 模块(可单独提取成包发布,又可经 dockBridge 装回面板)。含用量记录(LLM 调用记账/费用统计,支持各模型自定义单价、多段分时价与周末/节假日谷价)、模型能力、图片理解代理、模型余额、任务动画(19 种运行动画 + 桌面伙伴 3D 场景)、任务通知(完成/异常/需确认通知、提示音、语音播报可选音色、系统通知、钉钉/飞书推送)、运行状态(进行中任务与最近完成一览)、语音朗读(浏览器 TTS 读回复:回合下方喇叭按钮 + 自动朗读);侧栏入口 + 可拖拽弹窗(最大化/最小化/缩放)。",
3
+ "version": "0.24.0",
4
+ "description": "DeepSeek Harness 功能坞插件(dsh-dock):模块化功能中枢——每个功能一个 features/ 模块(可单独提取成包发布,又可经 dockBridge 装回面板)。含用量记录(LLM 调用记账/费用统计,支持各模型自定义单价、多段分时价与周末/节假日谷价)、模型能力、图片理解代理、模型余额、任务动画(19 种运行动画 + 桌面伙伴 3D 场景)、任务通知(完成/异常/需确认通知、提示音、语音播报可选音色、系统通知、钉钉/飞书推送)、运行状态(进行中任务与最近完成一览)、语音朗读(浏览器 TTS 读回复:回合下方喇叭按钮 + 自动朗读)、DSH功能(给 DSH 自带界面补能力:文件右键大文件流式下载);侧栏入口 + 可拖拽弹窗(最大化/最小化/缩放)。",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "files": [
@@ -9,8 +9,7 @@
9
9
  "client.js",
10
10
  "src/",
11
11
  "features/",
12
- "cordis.patch.yml",
13
- "screenshots"
12
+ "cordis.patch.yml"
14
13
  ],
15
14
  "exports": {
16
15
  ".": "./index.js",
@@ -42,8 +41,8 @@
42
41
  "license": "MIT",
43
42
  "scripts": {
44
43
  "build:client": "node scripts/build-client.mjs",
45
- "test:client": "node scripts/test-client-views.mjs",
46
- "test:host": "node scripts/test-task-host.mjs && node scripts/test-mcpconfig-host.mjs && node scripts/test-tokenlog-host.mjs && node scripts/test-balance-host.mjs && node scripts/test-route-trust-host.mjs && node scripts/test-tts-host.mjs && node scripts/test-legacy-import.mjs && node scripts/test-mobile-relay-host.mjs && node scripts/test-mobile-relay.mjs && node scripts/test-welcome-ack.mjs",
44
+ "test:client": "node scripts/test-client-views.mjs && node scripts/test-dshtools-client.mjs",
45
+ "test:host": "node scripts/test-task-host.mjs && node scripts/test-mcpconfig-host.mjs && node scripts/test-tokenlog-host.mjs && node scripts/test-balance-host.mjs && node scripts/test-route-trust-host.mjs && node scripts/test-tts-host.mjs && node scripts/test-legacy-import.mjs && node scripts/test-mobile-relay-host.mjs && node scripts/test-mobile-relay.mjs && node scripts/test-welcome-ack.mjs && node scripts/test-dshtools-host.mjs",
47
46
  "dev:client": "node scripts/watch-client.mjs",
48
47
  "dev:link": "node scripts/dev-link-deps.mjs"
49
48
  },
package/src/client.jsx CHANGED
@@ -29,12 +29,13 @@ import { feature as fTts } from "../features/tts/view.jsx";
29
29
  import { feature as fGames } from "../features/games/view.jsx";
30
30
  import { feature as fMobileRelay } from "../features/mobile-relay/view.jsx";
31
31
  import { feature as fMcpConfig } from "../features/mcpconfig/view.js";
32
+ import { feature as fDshTools, installClient as installDshTools } from "../features/dshtools/view.jsx";
32
33
 
33
34
  const name = "dsh-dock";
34
- const DOCK_VERSION = "0.23.1";
35
+ const DOCK_VERSION = "0.24.0";
35
36
 
36
37
  // ---- 内置功能注册表:新功能 = features/<id>/ 加模块 + 这里 import 一行 ----
37
- const BUILTIN_FEATURES = [fTokenlog, fModelconfig, fProviderConfig, fMcpConfig, fBalance, fAnimation, fNotify, fRunState, fTts, fGames, fMobileRelay];
38
+ const BUILTIN_FEATURES = [fTokenlog, fModelconfig, fProviderConfig, fMcpConfig, fBalance, fAnimation, fNotify, fRunState, fTts, fGames, fMobileRelay, fDshTools];
38
39
  // 规划占位(路线图):接入后移除并建 features/<id>/ 模块
39
40
  const PLANNED_FEATURES = [];
40
41
  const PLANNED_NOTES = {};
@@ -784,6 +785,8 @@ export function apply(ctx) {
784
785
  slots.inject("conversation.chat.turnTail", () => slots.register(
785
786
  { name: "conversation.chat.turnTail", id: "dsh-dock-turn-tail", order: 40, label: "功能坞" },
786
787
  (owner) => react.createElement(FeatureTurnTail, owner)));
788
+ // 【DSH功能】的全局接线(右键菜单等):模块自带延迟注入,功能开关在菜单解析时判断
789
+ installDshTools(ctx);
787
790
  }
788
791
 
789
792
  export const inject = ["timer"];
package/src/host-core.js CHANGED
@@ -184,6 +184,16 @@ export const DockConfig = z.object({
184
184
  /** 自动朗读时只读回复的前 N 句(0 = 不限)。眼睛不舒服想听结论的人通常只要前几句。 */
185
185
  maxSentences: z.number().default(0),
186
186
  }).default({}).volatile(),
187
+ // 【DSH功能】模块:给 DSH 自带界面补充能力,按「分组 × 勾选项」组织。
188
+ // 分组/键名以 features/dshtools/host.js 的 DSH_TOOL_GROUPS 为准;这里只声明可持久化的段,
189
+ // 新增能力时在清单里加一项即可(旧设置里没有的键按清单 default 兜底)。
190
+ dshtools: z.object({
191
+ /** 文件右键菜单的补充项。 */
192
+ fileContextMenu: z.object({
193
+ /** 大文件下载(流式):文件超过 32 MiB 时右键多出「下载(大文件)」。 */
194
+ streamingDownload: z.boolean().default(true),
195
+ }).default({}),
196
+ }).default({}).volatile(),
187
197
  })
188
198
 
189
199
  // ── 本地配置镜像(替代已移除的 settings.get) ─────────────────────────────
@@ -523,31 +533,60 @@ export function sendJson(res, status, value) {
523
533
  let warnedNoTrustSurface = false
524
534
 
525
535
  /**
526
- * 浏览器信任栅栏:**每个自行注册的 `webServer` 路由都必须先调它**。
536
+ * 浏览器信任栅栏:**每个自行注册的 `webServer` 路由都必须先调它**(并把 handler 的
537
+ * 第三个参数 `peer` 传进来)。
538
+ *
539
+ * 为什么必须:`webServer` 的命名路由由载体直接派发给 handler。DSH 0.2.2-rc.2 之前载体
540
+ * 「不拥有认证或 Origin 策略」,`/api` 的 Host/Origin 校验与会话认证由 Connection 在它
541
+ * 自己的 FetchHandler 上做,**管不到插件注册的命名路由**——不检查的插件路由就是一扇对
542
+ * 任何能连到端口的人都敞开的门,dsh-access 这类门禁插件也拦不到。
543
+ *
544
+ * 0.2.2-rc.2 起准入换了地方(见 dsh 的
545
+ * `docs/upgrade-guide/v0.2.2-rc.2/connection-page-peer/guide.zh.md`):Connection 上的
546
+ * `requestRejection`、`authenticatedUrl` 等被删除;`webServer.register` 的路由项新增
547
+ * `auth: 'anonymous' | 'required' | 'deny'`(**默认 required**),由载体在派发前完成
548
+ * Host/Origin 校验与认证,并把准入得到的 Peer 作为 handler 的第三个参数交下来。
527
549
  *
528
- * 为什么必须:`webServer` 的命名路由由载体直接派发给 handler(见 dsh 的
529
- * `packages/host/webserver` —— 匹配到路由就 `route.handler(req, res)`),载体本身
530
- * 「不拥有认证或 Origin 策略」;`/api` 的 Host/Origin 校验与会话认证由 Connection 在
531
- * 它自己的 FetchHandler 上做,**管不到插件注册的命名路由**。所以不检查的插件路由就是
532
- * 一扇对任何能连到端口的人都敞开的门,dsh-access 这类门禁插件也拦不到(它只有
533
- * exact 表 / fallback / connection-request / upgrades 四个缝)。
550
+ * 本函数因此同时兼容两代宿主,判断顺序很关键:
551
+ * 1. handler 收到了 `peer` —— 载体已经放行,直接返回 false。**绝不再调一次
552
+ * `connection.admit`**:那会重新认证出一个新 Peer,白耗一份凭据与生命周期。
553
+ * 2. 没有 `peer` 但宿主提供 `requestRejection` —— 老宿主(命名路由不鉴权),照旧自己问
554
+ * 它,值即 401(Host 可信但无会话)或 403(Host/Origin 栅栏拒绝),与官方
555
+ * `dsh-host-open-in-app`、`dsh-experimental-inspector` 当年的写法一致。
556
+ * 3. 两者都没有 —— fail-closed 按 401 拒绝。dsh-dock 的路由本来就是 web 专用的,
557
+ * 宁可面板报错,也不能给身份不明的请求开门。
534
558
  *
535
- * 做法与官方插件一致:`dsh-host-open-in-app`、`dsh-experimental-inspector` 都在自己的
536
- * 路由入口调 `connection.requestRejection(req)`,值即 401(Host 可信但无会话)或
537
- * 403(Host/Origin 栅栏拒绝)。Connection 不可用时按 fail-closed 处理——dsh-dock 的
538
- * 路由本身就是 web 专用的。
559
+ * 走第 1 条的前提是每个注册点都**显式**写 `auth: 'required'`:不写也落在默认值上,但
560
+ * 显式声明才挡得住将来默认值变化。`scripts/test-route-trust-host.mjs` 静态检查这一点。
539
561
  *
562
+ * @param ctx 插件上下文
563
+ * @param req 原始 HTTP 请求
564
+ * @param res 待写响应
565
+ * @param peer 新宿主准入后交给 handler 的 Peer;老宿主与测试桩为 undefined
540
566
  * @returns {boolean} true = 已拒绝并写完响应,调用方必须立即 return。
541
567
  */
542
- export function rejectUntrustedRequest(ctx, req, res) {
568
+ export function rejectUntrustedRequest(ctx, req, res, peer) {
569
+ // ① 新宿主:载体已完成准入,拿到 peer 就是放行过的请求。
570
+ if (peer !== undefined && peer !== null) return false
571
+
543
572
  const connection = ctx && typeof ctx.get === 'function' ? ctx.get('connection') : null
544
573
  const requestRejection = connection && typeof connection.requestRejection === 'function'
545
574
  ? connection.requestRejection.bind(connection)
546
575
  : null
547
576
  if (requestRejection === null) {
577
+ // ③-a 新宿主 + 本路由没声明 auth: 'required'(例如 anonymous):载体不会给它 peer。
578
+ if (connection && typeof connection.admit === 'function') {
579
+ if (!warnedNoTrustSurface) {
580
+ warnedNoTrustSurface = true
581
+ console.warn("[dsh-dock] 宿主已提供准入但本路由未拿到 peer:检查 webServer.register 是否显式声明 auth: 'required'")
582
+ }
583
+ sendJson(res, 401, { ok: false, error: { code: 'untrusted', message: "路由未声明 auth: 'required',未通过宿主准入,已拒绝请求" } })
584
+ return true
585
+ }
586
+ // ③-b 既没有载体的 peer,也没有老宿主的栅栏面:fail-closed。
548
587
  if (!warnedNoTrustSurface) {
549
588
  warnedNoTrustSurface = true
550
- console.warn('[dsh-dock] connection 服务不可用:/dsh-dock 路由将按未受信请求一律拒绝(401)')
589
+ console.warn('[dsh-dock] 宿主未提供路由准入(既无 peer 也无 connection.requestRejection):/dsh-dock 路由一律按未受信请求拒绝(401)')
551
590
  }
552
591
  sendJson(res, 401, { ok: false, error: { code: 'untrusted', message: '浏览器信任栅栏不可用,已拒绝请求' } })
553
592
  return true
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file