dsh-drydock 1.0.1 → 1.0.3

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.
@@ -238,6 +238,8 @@ h1{font-size:20px;margin:0 0 4px;color:#fff}.sub{color:#8b93a3;font-size:13px;ma
238
238
  .row{display:flex;gap:10px;flex-wrap:wrap;margin-bottom:14px}
239
239
  button{padding:9px 16px;border-radius:8px;border:1px solid #3a4050;background:#242a36;color:#e6e6e6;cursor:pointer;font-size:14px}
240
240
  button:hover{background:#2d3544}button.danger{background:#7f1d1d;border-color:#a03030}
241
+ button:disabled{opacity:.55;cursor:progress}button.done{animation:doneflash .9s ease-out}
242
+ @keyframes doneflash{0%{box-shadow:0 0 0 0 rgba(96,165,250,0)}30%{box-shadow:0 0 0 3px #2563eb}100%{box-shadow:0 0 0 0 rgba(96,165,250,0)}}
241
243
  button.primary{background:#1d4ed8;border-color:#2563eb}
242
244
  pre{background:#0b0d11;border:1px solid #2a2e38;border-radius:8px;padding:12px;font-size:12px;white-space:pre-wrap;max-height:320px;overflow:auto}
243
245
  .result{font-size:13px;white-space:pre-wrap}.ok{color:#4ade80}.bad{color:#f87171}.warn{color:#fbbf24}
@@ -246,11 +248,11 @@ a{color:#60a5fa}
246
248
  <h1>${drydockLogoMarkSvg(22)} 🛟 DSH 独立救援中心</h1>
247
249
  <div class="sub">独立于主引擎的救援入口 · 引擎挂了这里依然可用 · 停止引擎前会校验 pid 与端口归属 · 写操作需同源 + 一次性令牌</div>
248
250
  <div class="row">
249
- <button class="primary" onclick="startAndOpen()">启动引擎并打开主界面</button>
250
- <button onclick="runVerify()">运行检查</button>
251
- <button class="danger" onclick="runFix()">修复引擎配置</button>
252
- <button onclick="runStatus()">状态</button>
253
- <button class="danger" onclick="runStop()">停止引擎</button>
251
+ <button class="primary" onclick="startAndOpen(this)">启动引擎并打开主界面</button>
252
+ <button onclick="runVerify(this)">运行检查</button>
253
+ <button class="danger" onclick="runFix(this)">修复引擎配置</button>
254
+ <button onclick="runStatus(this)">状态</button>
255
+ <button class="danger" onclick="runStop(this)">停止引擎</button>
254
256
  </div>
255
257
  <div class="result" id="out">就绪。先点"运行检查"看 profile 是否健康。</div>
256
258
  <script>
@@ -258,56 +260,89 @@ const API = ${JSON.stringify(apiPrefix)};
258
260
  const TOKEN = (document.querySelector('meta[name="dsh-pm-token"]') || {}).content || "";
259
261
  const out = document.getElementById("out");
260
262
  function show(html, cls){ out.innerHTML = html; out.className = "result " + (cls||""); }
263
+ // U1:每次点击先让按钮进入 loading 态(禁用 + 文案),结束还原并闪一下 —— 用户能看出"点到了/还在跑"
264
+ function busy(btn, label){
265
+ if (!btn) return function(){};
266
+ const old = btn.textContent;
267
+ btn.disabled = true;
268
+ btn.textContent = label || "处理中…";
269
+ return function(){ btn.disabled = false; btn.textContent = old; };
270
+ }
271
+ async function withBusy(btn, label, fn){
272
+ const done = busy(btn, label);
273
+ let result;
274
+ try { result = await fn(); } finally { done(); }
275
+ if (btn) {
276
+ btn.classList.remove("done");
277
+ void btn.offsetWidth;
278
+ btn.classList.add("done");
279
+ setTimeout(function(){ btn.classList.remove("done"); }, 1000);
280
+ }
281
+ return result;
282
+ }
261
283
  async function api(path, method, body){
262
284
  const r = await fetch(API + path, { method: method || "GET", headers: { "Content-Type": "application/json", "X-DSH-PM-Token": TOKEN }, body: body ? JSON.stringify(body) : undefined });
263
285
  return r.json();
264
286
  }
265
- async function runVerify(){
266
- show("检查中…");
267
- try {
268
- const r = await api("/api/verify");
269
- if (r.ok === true && Array.isArray(r.issues) && r.issues.length === 0) show("✓ profile 配置正常,引擎可以正常启动。", "ok");
270
- else if (r.ok === true) show("⚠ 发现 " + r.issues.length + " 个问题:\\n" + r.issues.map(i=>"• "+i.name+": "+i.reason).join("\\n"), "bad");
271
- else show("检查失败:" + (r.error||"未知错误"), "bad");
272
- } catch(e){ show("请求失败:" + e.message, "bad"); }
287
+ async function runVerify(btn){
288
+ return withBusy(btn, "检查中…", async function(){
289
+ show("检查中…(读取 profile 与 bundle 可解析性)");
290
+ try {
291
+ const r = await api("/api/verify");
292
+ if (r.ok === true && Array.isArray(r.issues) && r.issues.length === 0) show("✓ profile 配置正常,引擎可以正常启动。", "ok");
293
+ else if (r.ok === true) show("⚠ 发现 " + r.issues.length + " 个问题:\\n" + r.issues.map(i=>"• "+i.name+": "+i.reason).join("\\n"), "bad");
294
+ else show("检查失败:" + (r.error||"服务端未返回原因"), "bad");
295
+ } catch(e){ show("请求失败:" + e.message, "bad"); }
296
+ });
273
297
  }
274
- async function runFix(){
275
- if(!confirm("确认执行修复?将隔离损坏的 bundle 并还原损坏的补丁文件(均带备份)。")) return;
276
- show("修复中…");
277
- try {
278
- const r = await api("/api/fix", "POST");
279
- if (r.ok === true) show("✓ 修复完成\\n" + (r.message||""), "ok");
280
- else show("修复失败:" + JSON.stringify(r), "bad");
281
- } catch(e){ show("请求失败:" + e.message, "bad"); }
298
+ async function runFix(btn){
299
+ return withBusy(btn, "修复中…", async function(){
300
+ if(!confirm("确认执行修复?将隔离损坏的 bundle 并还原损坏的补丁文件(均带备份)。")) { show("已取消修复(未改动任何文件)。", "warn"); return; }
301
+ show("修复中…(隔离损坏 bundle → 还原补丁 → 逐项验证,均带备份)");
302
+ try {
303
+ const r = await api("/api/fix", "POST");
304
+ if (r.ok === true) show("✓ 修复完成\\n" + (r.message||""), "ok");
305
+ else show("修复失败:" + (r.error || JSON.stringify(r)), "bad");
306
+ } catch(e){ show("请求失败:" + e.message, "bad"); }
307
+ });
282
308
  }
283
- async function runStatus(){
284
- try {
285
- const r = await api("/api/status");
286
- let text = "引擎:" + (r.engineUp ? "运行中(HTTP 握手正常,端口 " + r.enginePort + ")" : "未运行");
287
- if (r.engineOccupiedByOther) text += "\\n⚠ 端口 " + r.enginePort + " 被 pid " + r.engineOccupiedByOther + " 占用,但它不是 dsh 引擎";
288
- if (r.engineProcess) text += "\\n引擎 PID 文件:" + JSON.stringify(r.engineProcess);
289
- text += "\\n救援守护 PID:" + r.pid + "(端口 " + r.port + ")";
290
- show(text, r.engineUp ? "ok" : "warn");
291
- } catch(e){ show("请求失败:" + e.message, "bad"); }
309
+ async function runStatus(btn){
310
+ return withBusy(btn, "读取中…", async function(){
311
+ show("正在读取引擎 / 守护 / 端口状态…");
312
+ try {
313
+ const r = await api("/api/status");
314
+ let text = "引擎:" + (r.engineUp ? "运行中(HTTP 握手正常,端口 " + r.enginePort + ")" : "未运行");
315
+ if (r.engineOccupiedByOther) text += "\\n⚠ 端口 " + r.enginePort + " 被 pid " + r.engineOccupiedByOther + " 占用,但它不是 dsh 引擎";
316
+ if (r.engineProcess) text += "\\n引擎 PID 文件:" + JSON.stringify(r.engineProcess);
317
+ text += "\\n救援守护 PID:" + r.pid + "(端口 " + r.port + ")";
318
+ show(text, r.engineUp ? "ok" : "warn");
319
+ } catch(e){ show("请求失败:" + e.message, "bad"); }
320
+ });
292
321
  }
293
- async function startAndOpen(){
294
- show("正在启动引擎(自检→启动→等待就绪)…");
295
- try {
296
- const r = await api("/api/start", "POST");
297
- show((r.alreadyRunning ? "引擎已在运行。" : (r.message||"已请求启动。")), r.ok ? "ok" : "bad");
298
- if (r.ok) setTimeout(() => location.href = "http://127.0.0.1:${enginePort}/", 800);
299
- } catch(e){ show("请求失败:" + e.message, "bad"); }
322
+ async function startAndOpen(btn){
323
+ return withBusy(btn, "启动中…", async function(){
324
+ show("正在启动引擎(自检→启动→等待就绪)…启动期间本按钮保持禁用,避免重复拉起。");
325
+ try {
326
+ const r = await api("/api/start", "POST");
327
+ show((r.alreadyRunning ? "引擎已在运行。" : (r.message||"已请求启动。")), r.ok ? "ok" : "bad");
328
+ if (r.ok) setTimeout(() => location.href = "http://127.0.0.1:${enginePort}/", 800);
329
+ } catch(e){ show("请求失败:" + e.message, "bad"); }
330
+ });
300
331
  }
301
- async function runStop(){
302
- if(!confirm("确认停止引擎?会先校验 PID 与端口归属,校验不过会被拒绝。")) return;
303
- try {
304
- let r = await api("/api/stop", "POST", {});
305
- if (r.refused) {
306
- if (!confirm("已拒绝结束:\\n" + r.message + "\\n\\n确认强制结束该进程树?(仅在确认 pid 就是本机的 dsh 引擎时使用)")) { show(r.message, "warn"); return; }
307
- r = await api("/api/stop", "POST", { force: true });
308
- }
309
- show(r.message || JSON.stringify(r), r.ok ? "ok" : "bad");
310
- } catch(e){ show("请求失败:" + e.message, "bad"); }
332
+ async function runStop(btn){
333
+ return withBusy(btn, "停止中…", async function(){
334
+ if(!confirm("确认停止引擎?会先校验 PID 与端口归属,校验不过会被拒绝。")) { show("已取消停止(引擎未被动过)。", "warn"); return; }
335
+ show("正在停止引擎(先校验 PID 与端口归属)…");
336
+ try {
337
+ let r = await api("/api/stop", "POST", {});
338
+ if (r.refused) {
339
+ if (!confirm("已拒绝结束:\\n" + r.message + "\\n\\n确认强制结束该进程树?(仅在确认 pid 就是本机的 dsh 引擎时使用)")) { show(r.message, "warn"); return; }
340
+ show("正在强制结束进程树(已确认 pid 归属)…");
341
+ r = await api("/api/stop", "POST", { force: true });
342
+ }
343
+ show(r.message || JSON.stringify(r), r.ok ? "ok" : "bad");
344
+ } catch(e){ show("请求失败:" + e.message, "bad"); }
345
+ });
311
346
  }
312
347
  </script>
313
348
  <div class="sub" style="margin:14px 0 0">唯一网页入口(浏览器主页 / 桌面快捷方式):<a href="${entryUrl}">${entryUrl}</a></div>
package/lib/aggregate.js CHANGED
@@ -1,36 +1,120 @@
1
1
  /**
2
2
  * aggregate.js — 多更新源版本聚合(纯函数,可单测)。
3
3
  *
4
+ * 安全修复(CWE-494 供应链投毒)——**权威层是显式的服务端信任判定,不再由源类型决定**。
5
+ *
6
+ * 1.0.2 的规则是「type === "registry" 的源权威,其它源只作兜底」。这还留着一条攻击链:
7
+ * `normalizeSourceType` 把"任何非 github"都归一成 registry,调用方加一个
8
+ * `http://attacker/registry`(或客户端自报 `official:true`)就进了权威层,它的 dist-tags
9
+ * 直接决定安装版本。1.0.3 起把「权威」独立成 `authoritative` 字段:
10
+ * · lib/index.js 只给内置白名单源(npm 官方 / npmmirror)或**显式 authoritative:true**
11
+ * (http 源还需同时 allowInsecure)的源置 authoritative:true,其余一律 false;
12
+ * · 本文件只按 authoritative 分层,不再看 type —— type 仍随结果带出,供安装路径判定
13
+ * (registry → pnpm / github → 下载器)。
14
+ *
4
15
  * 规则:
5
- * - 取所有启用源返回的**最高版本**;
6
- * - 多个源并列最高时**随机挑选一个**——官方源权重一致,
7
- * 不偏向列表顺序(否则永远是第一个源 = 默认的 npm)。
16
+ * 1) 有权威源给出确定版本 → 只在权威源之间取最高(并列随机),sourceTrust:"authoritative",
17
+ * degraded:false,repo 置 null(权威源本就没有仓库概念,顺手掐断"伪造仓库"的传播)。
18
+ * 2) 没有权威源版本 → 所有源取最高作兜底,sourceTrust:"fallback"、degraded:true
19
+ * (UI 据此提示「版本来自非权威源、未经 npm 校验」)。
20
+ * 3) 同一信任层内并列时仍随机挑选(源权重一致,不偏列表顺序)。
21
+ * 4) 向后兼容:
22
+ * · entries 里**既没有** authoritative 也**没有** type 字段(很老的调用/夹具)→
23
+ * 退化为原来的全局取最高行为,返回值不带任何信任字段;
24
+ * · 只有 type 没有 authoritative(1.0.2 的测试夹具/旧调用)→ type==="registry"
25
+ * 视作权威、"github" 视作非权威,保持旧断言语义,避免大面积改旧测试。
8
26
  */
9
27
  import { compareVersions } from "./compare-versions.js";
10
28
 
11
29
  /**
12
- * @param entries - [{version: string|null, sourceName, repo, official}](已过滤禁用源)。
13
- * @returns {version, sourceName, repo} 聚合结果;全部无版本时 version=null。
30
+ * 信任层:authoritative(权威源)/ fallback(非权威源的兜底)。
31
+ * REGISTRY 是 1.0.2 的旧名,值已改为 "authoritative":只为不让旧导入/旧断言直接崩掉,
32
+ * 新代码请用 AUTHORITATIVE。
33
+ */
34
+ export const SOURCE_TRUST = Object.freeze({
35
+ AUTHORITATIVE: "authoritative",
36
+ FALLBACK: "fallback",
37
+ REGISTRY: "authoritative"
38
+ });
39
+
40
+ /** 取条目声明的源类型;缺失/空串返回 null(= 老夹具,不参与分级)。 */
41
+ function entryType(entry) {
42
+ return typeof entry?.type === "string" && entry.type !== "" ? entry.type : null;
43
+ }
44
+
45
+ /**
46
+ * 条目是否属于权威层:优先看服务端字段 authoritative(boolean 才采信);
47
+ * 旧夹具只有 type 时按 1.0.2 语义退化(registry=权威、github=非权威)。
14
48
  */
15
- export function aggregateLatest(entries) {
16
- let best = { version: null, sourceName: null, repo: null };
49
+ function isAuthoritativeEntry(entry) {
50
+ if (typeof entry?.authoritative === "boolean") return entry.authoritative === true;
51
+ return entryType(entry) === "registry";
52
+ }
53
+
54
+ /**
55
+ * 在一组候选里取最高版本;并列随机(同一信任层内语义不变)。
56
+ * sourceUrl/sourceType 一并带出:调用方要按**刷新时命中的那个源**定位安装路径,
57
+ * 按源名反查 + "查不到就随便拿一个启用源"的兜底会造成 github/registry 装法错配。
58
+ * @returns {version, sourceName, sourceUrl, sourceType, repo} | null(没有可用版本)。
59
+ */
60
+ function pickHighest(entries) {
61
+ let best = null;
17
62
  const ties = [];
18
63
  for (const entry of entries) {
19
64
  const version = entry?.version ?? null;
20
65
  if (version === null || typeof version !== "string" || version === "") continue;
21
- const cmp = best.version === null ? 1 : compareVersions(version, best.version);
66
+ const cmp = best === null ? 1 : compareVersions(version, best.version);
22
67
  if (cmp > 0) {
23
- best = { version, sourceName: entry.sourceName ?? null, repo: entry.repo ?? null };
68
+ best = snapshotOf(entry);
24
69
  ties.length = 0;
25
70
  ties.push(entry);
26
71
  } else if (cmp === 0) {
27
72
  ties.push(entry);
28
73
  }
29
74
  }
75
+ if (best === null) return null;
30
76
  if (ties.length > 1) {
31
- // 并列:随机挑选,官方源权重一致
77
+ // 并列:随机挑选,同一信任层内源权重一致
32
78
  const pick = ties[Math.floor(Math.random() * ties.length)];
33
- best = { version: best.version, sourceName: pick.sourceName ?? null, repo: pick.repo ?? null };
79
+ best = { ...best, sourceName: pick.sourceName ?? null, sourceUrl: pick.sourceUrl ?? null, sourceType: pick.sourceType ?? null, repo: pick.repo ?? null };
34
80
  }
35
81
  return best;
36
82
  }
83
+
84
+ /** 从条目提炼"来源身份"字段(version 另算,并列时保留最高的那个)。 */
85
+ function snapshotOf(entry) {
86
+ return {
87
+ version: entry.version,
88
+ sourceName: entry.sourceName ?? null,
89
+ sourceUrl: entry.sourceUrl ?? null,
90
+ // sourceType 与 type 等价:调用方装哪个源要按类型判定(registry→pnpm / github→下载器)
91
+ sourceType: entry.sourceType ?? entry.type ?? null,
92
+ repo: entry.repo ?? null
93
+ };
94
+ }
95
+
96
+ /**
97
+ * @param entries - [{version: string|null, sourceName, sourceUrl?, sourceType?, repo, authoritative?, official?, type?}]
98
+ * (已过滤禁用源)。authoritative 是服务端判定的信任层字段;type 只决定安装路径。
99
+ * @param [options] - {trustTiers?: boolean}:trustTiers:false 强制旧行为(全局取最高),仅兼容用。
100
+ * @returns {version, sourceName, sourceUrl, sourceType, repo, sourceTrust?, degraded?};
101
+ * 全部无版本时 version=null(sourceTrust/degraded 仅在分级生效时出现)。
102
+ */
103
+ export function aggregateLatest(entries, options = {}) {
104
+ const list = Array.isArray(entries) ? entries : [];
105
+ const tiered = list.some((entry) => typeof entry?.authoritative === "boolean" || entryType(entry) !== null);
106
+ // 向后兼容:既无 authoritative 也无 type(旧调用/旧夹具)或显式关闭分级 → 原行为,全局取最高
107
+ if (!tiered || options?.trustTiers === false) {
108
+ return pickHighest(list) ?? { version: null, sourceName: null, sourceUrl: null, sourceType: null, repo: null };
109
+ }
110
+ const authoritativeEntries = list.filter(isAuthoritativeEntry);
111
+ if (authoritativeEntries.length > 0) {
112
+ // 规则 1:权威层 —— 非权威源(哪怕版本号更高)一律不参与
113
+ const best = pickHighest(authoritativeEntries);
114
+ if (best !== null) return { ...best, repo: null, sourceTrust: SOURCE_TRUST.AUTHORITATIVE, degraded: false };
115
+ }
116
+ // 规则 2:权威源没给出确定版本 → 所有源里取最高作兜底,并标记「未经 npm 校验」
117
+ const fallback = pickHighest(list);
118
+ if (fallback === null) return { version: null, sourceName: null, sourceUrl: null, sourceType: null, repo: null, sourceTrust: null, degraded: false };
119
+ return { ...fallback, sourceTrust: SOURCE_TRUST.FALLBACK, degraded: true };
120
+ }