dsh-drydock 1.0.1 → 1.0.2

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,97 @@
1
1
  /**
2
2
  * aggregate.js — 多更新源版本聚合(纯函数,可单测)。
3
3
  *
4
- * 规则:
5
- * - 取所有启用源返回的**最高版本**;
6
- * - 多个源并列最高时**随机挑选一个**——官方源权重一致,
7
- * 不偏向列表顺序(否则永远是第一个源 = 默认的 npm)。
4
+ * 安全修复(CWE-494 供应链投毒)——**registry 源权威,其它源只作兜底**。
5
+ *
6
+ * 修复前的规则是「所有源取最高版本」:GitHub 上一个免费账号建个同名仓库、
7
+ * 发个 v999.0.0 release,就能在默认更新流程里赢过 npm 上的真实发布,
8
+ * 宿主再从这个伪造仓库取 release 资产(tgz / torrent / magnet / codeload)安装,
9
+ * pnpm 安装时的生命周期脚本随即执行。**取最高版本正是这条攻击链能生效的直接原因**。
10
+ *
11
+ * 现在的规则:
12
+ * 1) 只要有 registry 源给出确定版本 → 只在 registry 源之间取最高(并列随机),
13
+ * 返回 sourceTrust: "registry";github 等非 registry 源**一律不参与**,
14
+ * repo 也强制为 null(registry 源本就没有仓库概念,顺手掐断"伪造仓库"的传播)。
15
+ * 2) registry 源都没有确定版本 → 才在所有源里取最高作为兜底,
16
+ * 返回 sourceTrust: "fallback" + degraded: true(UI 据此提示「版本来自非官方源、未经 npm 校验」)。
17
+ * 3) 同一信任层内并列时仍随机挑选(官方源权重一致,不偏列表顺序)。
18
+ * 4) 向后兼容:entries 里**完全没有** type 字段时(旧调用/旧测试夹具),
19
+ * 退化为原来的全局取最高行为,返回值也不带任何信任字段。
8
20
  */
9
21
  import { compareVersions } from "./compare-versions.js";
10
22
 
23
+ /** 信任层:registry(npm 兼容源,权威)/ fallback(github 等非 registry 源的兜底)。 */
24
+ export const SOURCE_TRUST = Object.freeze({ REGISTRY: "registry", FALLBACK: "fallback" });
25
+
26
+ /** 取条目声明的源类型;缺失/空串返回 null(= 老夹具,不参与分级)。 */
27
+ function entryType(entry) {
28
+ return typeof entry?.type === "string" && entry.type !== "" ? entry.type : null;
29
+ }
30
+
11
31
  /**
12
- * @param entries - [{version: string|null, sourceName, repo, official}](已过滤禁用源)。
13
- * @returns {version, sourceName, repo} 聚合结果;全部无版本时 version=null。
32
+ * 在一组候选里取最高版本;并列随机(同一信任层内语义不变)。
33
+ * sourceUrl/sourceType 一并带出:调用方要按**刷新时命中的那个源**定位安装路径,
34
+ * 按源名反查 + "查不到就随便拿一个启用源"的兜底会造成 github/registry 装法错配。
35
+ * @returns {version, sourceName, sourceUrl, sourceType, repo} | null(没有可用版本)。
14
36
  */
15
- export function aggregateLatest(entries) {
16
- let best = { version: null, sourceName: null, repo: null };
37
+ function pickHighest(entries) {
38
+ let best = null;
17
39
  const ties = [];
18
40
  for (const entry of entries) {
19
41
  const version = entry?.version ?? null;
20
42
  if (version === null || typeof version !== "string" || version === "") continue;
21
- const cmp = best.version === null ? 1 : compareVersions(version, best.version);
43
+ const cmp = best === null ? 1 : compareVersions(version, best.version);
22
44
  if (cmp > 0) {
23
- best = { version, sourceName: entry.sourceName ?? null, repo: entry.repo ?? null };
45
+ best = snapshotOf(entry);
24
46
  ties.length = 0;
25
47
  ties.push(entry);
26
48
  } else if (cmp === 0) {
27
49
  ties.push(entry);
28
50
  }
29
51
  }
52
+ if (best === null) return null;
30
53
  if (ties.length > 1) {
31
54
  // 并列:随机挑选,官方源权重一致
32
55
  const pick = ties[Math.floor(Math.random() * ties.length)];
33
- best = { version: best.version, sourceName: pick.sourceName ?? null, repo: pick.repo ?? null };
56
+ best = { ...best, sourceName: pick.sourceName ?? null, sourceUrl: pick.sourceUrl ?? null, sourceType: pick.sourceType ?? null, repo: pick.repo ?? null };
34
57
  }
35
58
  return best;
36
59
  }
60
+
61
+ /** 从条目提炼"来源身份"字段(version 另算,并列时保留最高的那个)。 */
62
+ function snapshotOf(entry) {
63
+ return {
64
+ version: entry.version,
65
+ sourceName: entry.sourceName ?? null,
66
+ sourceUrl: entry.sourceUrl ?? null,
67
+ // sourceType 与 type 等价:调用方装哪个源要按类型判定(registry→pnpm / github→下载器)
68
+ sourceType: entry.sourceType ?? entry.type ?? null,
69
+ repo: entry.repo ?? null
70
+ };
71
+ }
72
+
73
+ /**
74
+ * @param entries - [{version: string|null, sourceName, sourceUrl?, sourceType?, repo, official, type?}](已过滤禁用源)。
75
+ * type = "registry" | "github"(旧夹具可能没有 type 字段)。
76
+ * @param [options] - {trustTiers?: boolean}:trustTiers:false 强制旧行为(全局取最高),仅兼容用。
77
+ * @returns {version, sourceName, sourceUrl, sourceType, repo, sourceTrust?, degraded?};
78
+ * 全部无版本时 version=null(sourceTrust/degraded 仅在分级生效时出现)。
79
+ */
80
+ export function aggregateLatest(entries, options = {}) {
81
+ const list = Array.isArray(entries) ? entries : [];
82
+ const typed = list.some((entry) => entryType(entry) !== null);
83
+ // 向后兼容:没有 type 字段(旧调用/旧夹具)或显式关闭分级 → 原行为,全局取最高
84
+ if (!typed || options?.trustTiers === false) {
85
+ return pickHighest(list) ?? { version: null, sourceName: null, sourceUrl: null, sourceType: null, repo: null };
86
+ }
87
+ const registryEntries = list.filter((entry) => entryType(entry) === SOURCE_TRUST.REGISTRY);
88
+ if (registryEntries.length > 0) {
89
+ // 规则 1:registry 权威 —— 非 registry 源(哪怕版本号更高)一律不参与
90
+ const best = pickHighest(registryEntries);
91
+ if (best !== null) return { ...best, sourceType: SOURCE_TRUST.REGISTRY, repo: null, sourceTrust: SOURCE_TRUST.REGISTRY, degraded: false };
92
+ }
93
+ // 规则 2:registry 没给出确定版本 → 所有源里取最高作兜底,并标记「未经 npm 校验」
94
+ const fallback = pickHighest(list);
95
+ if (fallback === null) return { version: null, sourceName: null, sourceUrl: null, sourceType: null, repo: null, sourceTrust: null, degraded: false };
96
+ return { ...fallback, sourceTrust: SOURCE_TRUST.FALLBACK, degraded: true };
97
+ }