dsh-drydock 1.0.2 → 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.
- package/README.en.md +44 -35
- package/README.md +39 -26
- package/bin/open-boot.mjs +181 -31
- package/lib/aggregate.js +51 -28
- package/lib/client.js +23 -23
- package/lib/downloader.js +87 -20
- package/lib/index.js +434 -58
- package/lib/remote.js +1 -1
- package/lib/rescue.js +20 -3
- package/package.json +4 -3
package/bin/open-boot.mjs
CHANGED
|
@@ -6,6 +6,18 @@
|
|
|
6
6
|
* 自动 verify → 有问题自动 fix(隔离坏 bundle)→ 拉起 `dsh web` → 跳转 3080。
|
|
7
7
|
*
|
|
8
8
|
* 设计取舍:
|
|
9
|
+
* - 1.0.3(安全性能审查 S3/S4/P1/P2):
|
|
10
|
+
* · **Host 头校验(S3)**:请求入口只接受 `127.0.0.1:<port>` / `localhost:<port>` / `[::1]:<port>`
|
|
11
|
+
* (带端口、大小写不敏感),其余 421 JSON —— 挡住"攻击者域名解析到 127.0.0.1 后同源读接口"
|
|
12
|
+
* 的 DNS 重绑定;校验在**路由之前**。
|
|
13
|
+
* · **统一安全响应头(S4)**:CSP(default-src 'none' + script/style 'unsafe-inline' + connect-src 'self'
|
|
14
|
+
* + frame-ancestors 'none' + base-uri 'none')、X-Frame-Options DENY、nosniff、Referrer-Policy
|
|
15
|
+
* no-referrer,覆盖 sendHtml/sendJson/救援页/404/421;页面内联脚本样式照常可跑。
|
|
16
|
+
* · **bundles GET 也过 Origin+令牌(S3 附带)**:已装插件清单不再裸奔(页面 api() 本就带令牌)。
|
|
17
|
+
* · **updates 缓存 + 单飞(P1)**:30 分钟缓存(key=profile,另存 pnpm-lock.yaml 的 mtime+size 失效)、
|
|
18
|
+
* 并发共用 in-flight Promise(不再重复起 pnpm)、`pnpm --version` 成功结果进程内缓存 → 热调用 < 10ms。
|
|
19
|
+
* · **runPnpm 有界缓冲(P2)**:stdout/stderr 只保末尾 16KB + `<...已截断 N 字节>` 标记,长驻守护不再堆峰值。
|
|
20
|
+
* · **启动页 log() 改逐行 DOM(S4)**:textContent 追加,不再 innerHTML 拼接。
|
|
9
21
|
* - 3081 独立入口,无端口争抢(v0.7.0 的 3080 接管模式已移除)。
|
|
10
22
|
* - **平时隐藏,拉起引擎时弹窗**:常驻守护自身无任何窗口;
|
|
11
23
|
* 只有当引擎(3080)真的不在时,才弹出一个**可见的启动窗口**,
|
|
@@ -1541,7 +1553,15 @@ function stopSpin(){
|
|
|
1541
1553
|
spin.style.display = "none";
|
|
1542
1554
|
if (waited > 0) elapsedEl.textContent = "本次等待 " + waited + " 秒";
|
|
1543
1555
|
}
|
|
1544
|
-
|
|
1556
|
+
// S4:逐行 DOM 追加(textContent),不再 innerHTML += —— 日志里出现什么字符都不会被解析成 HTML,
|
|
1557
|
+
// 也省掉手工转义(旧实现只转义 "<","&" 与属性上下文仍是缺口)。样式沿用 span.cls。
|
|
1558
|
+
function log(line, cls){
|
|
1559
|
+
var row = document.createElement("div");
|
|
1560
|
+
if (cls) { var span = document.createElement("span"); span.className = cls; span.textContent = String(line); row.appendChild(span); }
|
|
1561
|
+
else { row.textContent = String(line); }
|
|
1562
|
+
out.appendChild(row);
|
|
1563
|
+
out.scrollTop = out.scrollHeight;
|
|
1564
|
+
}
|
|
1545
1565
|
function goEntry(r){
|
|
1546
1566
|
stopSpin();
|
|
1547
1567
|
if (r && r.entryUrl) { location.href = r.entryUrl; return; }
|
|
@@ -1687,6 +1707,31 @@ function userBundleNames(profile) {
|
|
|
1687
1707
|
return Object.keys(manifest.dependencies ?? {}).filter((name) => !CORE_BUNDLES.includes(name)).sort();
|
|
1688
1708
|
}
|
|
1689
1709
|
|
|
1710
|
+
/**
|
|
1711
|
+
* P2:pnpm 输出缓冲上限 = 保留末尾 16 KB。调用方普遍只用 `slice(-400)`(错误摘录),
|
|
1712
|
+
* 大 profile 的 `pnpm outdated --json` / `pnpm install` 输出可达 MB 级,长驻守护里是无上限的堆峰值。
|
|
1713
|
+
*/
|
|
1714
|
+
const PNPM_OUTPUT_CAP = 16 * 1024;
|
|
1715
|
+
|
|
1716
|
+
/**
|
|
1717
|
+
* 有界输出缓冲(P2):超出上限时丢**头部**、只留末尾 cap 个字符,并记下丢弃字节数;
|
|
1718
|
+
* 取值时前置 `<...已截断 N 字节>` 标记,让读日志的人知道"这里不是开头"。
|
|
1719
|
+
* 返回值仍是完整字符串(末尾语义不变),所以 `slice(-400)` 这类调用方无需改动。
|
|
1720
|
+
*/
|
|
1721
|
+
function createBoundedBuffer(cap = PNPM_OUTPUT_CAP) {
|
|
1722
|
+
let tail = "";
|
|
1723
|
+
let dropped = 0;
|
|
1724
|
+
return {
|
|
1725
|
+
append(chunk) {
|
|
1726
|
+
tail += String(chunk);
|
|
1727
|
+
if (tail.length > cap) { dropped += tail.length - cap; tail = tail.slice(-cap); }
|
|
1728
|
+
},
|
|
1729
|
+
toString() { return dropped === 0 ? tail : `<...已截断 ${dropped} 字节>${tail}`; },
|
|
1730
|
+
get dropped() { return dropped; },
|
|
1731
|
+
get size() { return tail.length; }
|
|
1732
|
+
};
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1690
1735
|
/**
|
|
1691
1736
|
* 以「pnpm 命令 + 白名单参数」的方式跑子进程(rc.2 P1-4:exec → spawn 拿 pid)。
|
|
1692
1737
|
* resolve 永不 reject,失败也返回结构化结果 { code, stdout, stderr, timedOut, state }:
|
|
@@ -1695,6 +1740,7 @@ function userBundleNames(profile) {
|
|
|
1695
1740
|
* 调用方(卸载事务)据此不做回滚/继续,直接报告 unknown 请人工确认。
|
|
1696
1741
|
* 超时杀的是**本函数自己启动**的子树(taskkill /PID <pid> /T /F),与 P0-1「不 /T 连坐别人」不冲突。
|
|
1697
1742
|
* 测试逃生门:DSH_PM_PNPM_TIMEOUT_MS 环境变量可整体下调默认超时(不设 = 180000)。
|
|
1743
|
+
* P2:stdout/stderr 走有界环形缓冲(保末尾 16KB + 截断标记),不再无上限累积。
|
|
1698
1744
|
*/
|
|
1699
1745
|
function runPnpm(pnpmArgs, { cwd, timeoutMs } = {}) {
|
|
1700
1746
|
const defaultTimeout = Number(process.env.DSH_PM_PNPM_TIMEOUT_MS);
|
|
@@ -1710,15 +1756,16 @@ function runPnpm(pnpmArgs, { cwd, timeoutMs } = {}) {
|
|
|
1710
1756
|
resolveRun({ code: 1, stdout: "", stderr: `pnpm spawn 失败:${error.message}`, timedOut: false, state: "done" });
|
|
1711
1757
|
return;
|
|
1712
1758
|
}
|
|
1713
|
-
|
|
1714
|
-
|
|
1759
|
+
// P2:有界缓冲(保末尾 16KB);返回值语义与旧版一致(末尾内容不变)
|
|
1760
|
+
const stdoutBuf = createBoundedBuffer();
|
|
1761
|
+
const stderrBuf = createBoundedBuffer();
|
|
1715
1762
|
let settled = false;
|
|
1716
1763
|
let timedOut = false;
|
|
1717
1764
|
const finish = (code, state) => {
|
|
1718
1765
|
if (settled) return;
|
|
1719
1766
|
settled = true;
|
|
1720
1767
|
clearTimeout(timer);
|
|
1721
|
-
resolveRun({ code, stdout, stderr, timedOut, state });
|
|
1768
|
+
resolveRun({ code, stdout: stdoutBuf.toString(), stderr: stderrBuf.toString(), timedOut, state });
|
|
1722
1769
|
};
|
|
1723
1770
|
const killTree = () => {
|
|
1724
1771
|
timedOut = true;
|
|
@@ -1740,10 +1787,10 @@ function runPnpm(pnpmArgs, { cwd, timeoutMs } = {}) {
|
|
|
1740
1787
|
const timer = setTimeout(() => {
|
|
1741
1788
|
if (!settled && child.pid) killTree();
|
|
1742
1789
|
}, timeout);
|
|
1743
|
-
child.stdout?.on("data", (d) => {
|
|
1744
|
-
child.stderr?.on("data", (d) => {
|
|
1790
|
+
child.stdout?.on("data", (d) => { stdoutBuf.append(d); });
|
|
1791
|
+
child.stderr?.on("data", (d) => { stderrBuf.append(d); });
|
|
1745
1792
|
child.on("error", (error) => {
|
|
1746
|
-
|
|
1793
|
+
stderrBuf.append(`\n${error.message}`);
|
|
1747
1794
|
finish(1, "done");
|
|
1748
1795
|
});
|
|
1749
1796
|
child.on("close", (code) => finish(typeof code === "number" ? code : 1, "done"));
|
|
@@ -1933,29 +1980,78 @@ async function uninstallPackageTransactional(args, pkgName) {
|
|
|
1933
1980
|
};
|
|
1934
1981
|
}
|
|
1935
1982
|
|
|
1936
|
-
/**
|
|
1937
|
-
|
|
1983
|
+
/**
|
|
1984
|
+
* P1:updates 结果缓存(key = profile,TTL 30 分钟)。
|
|
1985
|
+
* 另存 `pnpm-lock.yaml` 的 mtime+size 作失效键:lockfile 一变(用户装了/卸了包、改了依赖树)
|
|
1986
|
+
* 立刻重算,不让 30 分钟缓存把"刚装的插件"藏起来。只缓存**成功**结果——失败(pnpm 缺失/输出异常)
|
|
1987
|
+
* 留给用户重试,不把一个可修的故障钉在缓存里半小时。
|
|
1988
|
+
*/
|
|
1989
|
+
const UPDATES_CACHE_MS = 30 * 60 * 1000;
|
|
1990
|
+
const updatesCache = new Map();
|
|
1991
|
+
/** P1:单飞——同一 profile 的并发请求共用同一个 in-flight Promise(复用同一结果,不是 409)。 */
|
|
1992
|
+
const updatesInflight = new Map();
|
|
1993
|
+
/** P1:`pnpm --version` 成功结果模块级缓存(失败不缓存:装好 pnpm 后下一次就能恢复)。 */
|
|
1994
|
+
let pnpmVersionCache = null;
|
|
1995
|
+
|
|
1996
|
+
/** lockfile 指纹:mtimeMs + size(文件不存在记 "absent",仍然稳定可比)。 */
|
|
1997
|
+
function lockfileStamp(profile) {
|
|
1998
|
+
try {
|
|
1999
|
+
const st = statSync(join(profile, "pnpm-lock.yaml"));
|
|
2000
|
+
return `${st.mtimeMs}:${st.size}`;
|
|
2001
|
+
} catch { return "absent"; }
|
|
2002
|
+
}
|
|
2003
|
+
|
|
2004
|
+
/** 测试面:清空 updates 缓存 / 单飞 / 版本缓存(生产路径不调用)。 */
|
|
2005
|
+
function resetUpdatesCacheForTests() {
|
|
2006
|
+
updatesCache.clear();
|
|
2007
|
+
updatesInflight.clear();
|
|
2008
|
+
pnpmVersionCache = null;
|
|
2009
|
+
}
|
|
2010
|
+
|
|
2011
|
+
/**
|
|
2012
|
+
* 检查用户插件更新:一次 pnpm outdated --json,再投影到用户 bundle(核心组件不参与)。
|
|
2013
|
+
* P1:30 分钟缓存(key=profile + lockfile 指纹)+ 并发单飞 + pnpm --version 进程内缓存;
|
|
2014
|
+
* `run`/`now` 是测试注入点(默认就是 runPnpm / Date.now)。
|
|
2015
|
+
* 热调用目标 < 10ms:命中缓存时不 spawn 任何子进程。
|
|
2016
|
+
*/
|
|
2017
|
+
async function checkUpdates(args, { run = runPnpm, now = Date.now } = {}) {
|
|
1938
2018
|
const profile = args.profile;
|
|
1939
2019
|
const users = userBundleNames(profile);
|
|
1940
2020
|
if (users.length === 0) return { ok: true, updates: [], message: "profile 没有用户插件。" };
|
|
1941
|
-
const
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
2021
|
+
const lock = lockfileStamp(profile);
|
|
2022
|
+
const cached = updatesCache.get(profile);
|
|
2023
|
+
if (cached && now() - cached.at < UPDATES_CACHE_MS && cached.lock === lock) return cached.result;
|
|
2024
|
+
// 并发进来时复用同一个 Promise:不会重复拉起 pnpm,也不会把第二个请求判成 409
|
|
2025
|
+
const pending = updatesInflight.get(profile);
|
|
2026
|
+
if (pending) return pending;
|
|
2027
|
+
const task = (async () => {
|
|
2028
|
+
if (pnpmVersionCache === null) {
|
|
2029
|
+
const ping = await run(["--version"], { cwd: profile, timeoutMs: 15000 });
|
|
2030
|
+
if (ping.code !== 0) return { ok: false, error: `pnpm 不可用(exit ${ping.code}):${(ping.stderr || ping.stdout).slice(-200)}` };
|
|
2031
|
+
pnpmVersionCache = (ping.stdout || "").trim();
|
|
2032
|
+
}
|
|
2033
|
+
const out = await run(["outdated", "--json"], { cwd: profile, timeoutMs: 120000 });
|
|
2034
|
+
let table = {};
|
|
2035
|
+
try { table = JSON.parse(out.stdout || "{}") || {}; }
|
|
2036
|
+
catch {
|
|
2037
|
+
return { ok: false, error: `pnpm outdated 输出无法解析(exit ${out.code}):${(out.stderr || out.stdout).slice(-300)}` };
|
|
2038
|
+
}
|
|
2039
|
+
const updates = users.map((name) => {
|
|
2040
|
+
const row = table[name] || table[`./${name}`] || null;
|
|
2041
|
+
return {
|
|
2042
|
+
name,
|
|
2043
|
+
current: row?.current ?? null,
|
|
2044
|
+
latest: row?.latest ?? null,
|
|
2045
|
+
needsUpdate: Boolean(row && row.latest && row.current !== row.latest)
|
|
2046
|
+
};
|
|
2047
|
+
});
|
|
2048
|
+
const result = { ok: true, updates };
|
|
2049
|
+
updatesCache.set(profile, { at: now(), lock, result });
|
|
2050
|
+
return result;
|
|
2051
|
+
})();
|
|
2052
|
+
updatesInflight.set(profile, task);
|
|
2053
|
+
try { return await task; }
|
|
2054
|
+
finally { updatesInflight.delete(profile); }
|
|
1959
2055
|
}
|
|
1960
2056
|
|
|
1961
2057
|
/** 更新单个用户插件(与卸载同一套 EBUSY 引导 + P1-3 fail-closed:握手失败但端口有监听者 → 拒绝)。 */
|
|
@@ -2841,6 +2937,47 @@ function readJsonBody(req) {
|
|
|
2841
2937
|
});
|
|
2842
2938
|
}
|
|
2843
2939
|
|
|
2940
|
+
/**
|
|
2941
|
+
* S3:Host 头校验(DNS 重绑定防护)。只接受带端口的回环主机名 ——
|
|
2942
|
+
* 攻击者把 `evil.example` 解析到 127.0.0.1 后,浏览器发出的请求 Host 仍是 `evil.example`,
|
|
2943
|
+
* 只看 Origin/令牌挡不住"读接口被同源化"(/api/status 会暴露 profile 绝对路径 / pid / 版本)。
|
|
2944
|
+
* 严格按审查报告:必须带端口、大小写不敏感、只认这三种回环写法。
|
|
2945
|
+
*/
|
|
2946
|
+
function isAllowedHost(hostHeader, port) {
|
|
2947
|
+
if (typeof hostHeader !== "string" || hostHeader.trim() === "") return false;
|
|
2948
|
+
const host = hostHeader.trim().toLowerCase();
|
|
2949
|
+
return host === `127.0.0.1:${port}` || host === `localhost:${port}` || host === `[::1]:${port}`;
|
|
2950
|
+
}
|
|
2951
|
+
|
|
2952
|
+
/**
|
|
2953
|
+
* S4:统一安全响应头。页面是内联 `<script>` + 内联 `<style>`,因此保留 `'unsafe-inline'`
|
|
2954
|
+
* (nonce 化要同时改三个页面模板 + 救援页,1.0.3 不做);其余全部收紧:
|
|
2955
|
+
* default-src 'none' —— 默认不许加载任何东西;
|
|
2956
|
+
* connect-src 'self' —— 3081 页只能同源 fetch(/plugins/api/*、/rescue/api/*);
|
|
2957
|
+
* img-src 'self' data: —— 内联 favicon 是 data: URI;
|
|
2958
|
+
* frame-ancestors 'none' / X-Frame-Options: DENY —— 禁止被嵌框(点劫持);
|
|
2959
|
+
* base-uri 'none' —— 防 <base> 改写相对地址;
|
|
2960
|
+
* nosniff / Referrer-Policy: no-referrer —— 防 MIME 嗅探与地址外泄。
|
|
2961
|
+
*/
|
|
2962
|
+
const SECURITY_HEADERS = {
|
|
2963
|
+
"X-Content-Type-Options": "nosniff",
|
|
2964
|
+
"X-Frame-Options": "DENY",
|
|
2965
|
+
"Referrer-Policy": "no-referrer",
|
|
2966
|
+
"Content-Security-Policy": "default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src 'self' data:; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'"
|
|
2967
|
+
};
|
|
2968
|
+
|
|
2969
|
+
/**
|
|
2970
|
+
* 给响应挂上安全头并返回可交给 writeHead 的 headers。
|
|
2971
|
+
* 也在请求入口先 `setHeader` 一次:这样 lib/rescue 的 handleRescueApi 自己 writeHead 的
|
|
2972
|
+
* JSON 响应(本仓库文件范围外)同样带上安全头。
|
|
2973
|
+
*/
|
|
2974
|
+
function applySecurityHeaders(res, extra = {}) {
|
|
2975
|
+
if (res && typeof res.setHeader === "function" && !res.headersSent) {
|
|
2976
|
+
for (const [name, value] of Object.entries(SECURITY_HEADERS)) res.setHeader(name, value);
|
|
2977
|
+
}
|
|
2978
|
+
return { ...SECURITY_HEADERS, ...extra };
|
|
2979
|
+
}
|
|
2980
|
+
|
|
2844
2981
|
function startServer(args) {
|
|
2845
2982
|
const startedAt = new Date().toISOString();
|
|
2846
2983
|
// 1.0.0-rc.1:每次启动器进程起来都重写 dsh-launcher.cmd(重装/升级后自愈,路径全部来自当前实况)
|
|
@@ -2853,7 +2990,7 @@ function startServer(args) {
|
|
|
2853
2990
|
const pluginsFlight = createSingleFlight("open-boot:plugins");
|
|
2854
2991
|
const rescueCtx = { profile: args.profile, dsh: args.dsh, cwd: args.cwd, port: args.port, flight };
|
|
2855
2992
|
const sendJson = (res, code, body) => {
|
|
2856
|
-
res.writeHead(code, { "Content-Type": "application/json; charset=utf-8" });
|
|
2993
|
+
res.writeHead(code, applySecurityHeaders(res, { "Content-Type": "application/json; charset=utf-8" }));
|
|
2857
2994
|
res.end(JSON.stringify(body));
|
|
2858
2995
|
};
|
|
2859
2996
|
/** L11 ①②:写接口的 Origin + 令牌检查。返回 true 表示已拒绝并应答。 */
|
|
@@ -2864,11 +3001,18 @@ function startServer(args) {
|
|
|
2864
3001
|
return true;
|
|
2865
3002
|
};
|
|
2866
3003
|
const sendHtml = (res, html) => {
|
|
2867
|
-
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" });
|
|
3004
|
+
res.writeHead(200, applySecurityHeaders(res, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" }));
|
|
2868
3005
|
res.end(html);
|
|
2869
3006
|
};
|
|
2870
3007
|
|
|
2871
3008
|
const server = createServer(async (req, res) => {
|
|
3009
|
+
// S4:先挂安全头(任何分支、含救援 API 自己的 writeHead 都继承)
|
|
3010
|
+
applySecurityHeaders(res);
|
|
3011
|
+
// S3:Host 校验放在**路由之前**——DNS 重绑定请求在这里就该被 421 挡掉
|
|
3012
|
+
if (!isAllowedHost(req.headers.host, args.port)) {
|
|
3013
|
+
sendJson(res, 421, { ok: false, code: 421, error: "Host 不被允许(仅接受回环地址)" });
|
|
3014
|
+
return;
|
|
3015
|
+
}
|
|
2872
3016
|
const url = new URL(req.url, "http://127.0.0.1");
|
|
2873
3017
|
const pathname = url.pathname;
|
|
2874
3018
|
if (pathname === "/") {
|
|
@@ -2897,6 +3041,9 @@ function startServer(args) {
|
|
|
2897
3041
|
return;
|
|
2898
3042
|
}
|
|
2899
3043
|
if (pathname === "/plugins/api/bundles" && req.method === "GET") {
|
|
3044
|
+
// S3 附带:已装插件清单不该裸奔给任意本地网页(DNS 重绑定后可读),GET 也过 Origin+令牌。
|
|
3045
|
+
// 页面侧 api() 对 GET 已经带 X-DSH-PM-Token(loadBundles 走 api()),不需要额外改动。
|
|
3046
|
+
if (rejectWrite(req, res)) return;
|
|
2900
3047
|
sendJson(res, 200, listBundles(args.profile));
|
|
2901
3048
|
return;
|
|
2902
3049
|
}
|
|
@@ -3033,7 +3180,7 @@ function startServer(args) {
|
|
|
3033
3180
|
});
|
|
3034
3181
|
return;
|
|
3035
3182
|
}
|
|
3036
|
-
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
|
|
3183
|
+
res.writeHead(404, applySecurityHeaders(res, { "Content-Type": "text/plain; charset=utf-8" }));
|
|
3037
3184
|
res.end("not found");
|
|
3038
3185
|
});
|
|
3039
3186
|
|
|
@@ -3062,7 +3209,8 @@ function startServer(args) {
|
|
|
3062
3209
|
console.log(`[open-boot] profile: ${args.profile};引擎端口: ${ENGINE_PORT};拉起引擎时${args.window ? "弹窗显示进度" : "静默启动"};cwd: ${process.cwd()}`);
|
|
3063
3210
|
console.log(`[open-boot] 救援能力(唯一网页入口):http://127.0.0.1:${args.port}/rescue(/rescue/api/*:verify|fix|start|stop|status);` +
|
|
3064
3211
|
`rescue-daemon 备份入口默认 ${RESCUE_DAEMON_PORT}(不再抢占 ${args.port})`);
|
|
3065
|
-
console.log(`[open-boot] 写接口防护:/api/boot 与 /rescue/api/* 的 POST 需同源 Origin +
|
|
3212
|
+
console.log(`[open-boot] 写接口防护:/api/boot 与 /rescue/api/* 的 POST 需同源 Origin + 一次性令牌(页面自动带上,不写日志/不落盘);` +
|
|
3213
|
+
`入口 Host 仅接受回环地址 127.0.0.1/localhost/[::1]:${args.port}(其余 421)`);
|
|
3066
3214
|
});
|
|
3067
3215
|
return server;
|
|
3068
3216
|
}
|
|
@@ -3138,6 +3286,8 @@ export {
|
|
|
3138
3286
|
// rc.2 测试面:单测直接驱动这些函数(不经过 HTTP)
|
|
3139
3287
|
CORE_BUNDLES, MANAGER_SELF_NAME, PKG_NAME_RE, runPnpm, writeFileAtomic, engineBusyGate,
|
|
3140
3288
|
uninstallPackageTransactional, updatePackage, checkUpdates, healthReport, restartEngine, ensureLauncherCmd,
|
|
3289
|
+
// 1.0.3 任务 C 测试面:Host 校验 / 安全响应头 / 有界缓冲 / updates 缓存
|
|
3290
|
+
isAllowedHost, SECURITY_HEADERS, PNPM_OUTPUT_CAP, createBoundedBuffer, lockfileStamp, resetUpdatesCacheForTests,
|
|
3141
3291
|
// U1 测试面:兜底页 HTML 静态断言(test-ux.mjs)直接检查页面结构,不用起浏览器
|
|
3142
3292
|
pluginsPageHtml
|
|
3143
3293
|
};
|
package/lib/aggregate.js
CHANGED
|
@@ -1,33 +1,56 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* aggregate.js — 多更新源版本聚合(纯函数,可单测)。
|
|
3
3
|
*
|
|
4
|
-
* 安全修复(CWE-494
|
|
4
|
+
* 安全修复(CWE-494 供应链投毒)——**权威层是显式的服务端信任判定,不再由源类型决定**。
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
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 → 下载器)。
|
|
10
14
|
*
|
|
11
|
-
*
|
|
12
|
-
* 1)
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
15
|
+
* 规则:
|
|
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" 视作非权威,保持旧断言语义,避免大面积改旧测试。
|
|
20
26
|
*/
|
|
21
27
|
import { compareVersions } from "./compare-versions.js";
|
|
22
28
|
|
|
23
|
-
/**
|
|
24
|
-
|
|
29
|
+
/**
|
|
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
|
+
});
|
|
25
39
|
|
|
26
40
|
/** 取条目声明的源类型;缺失/空串返回 null(= 老夹具,不参与分级)。 */
|
|
27
41
|
function entryType(entry) {
|
|
28
42
|
return typeof entry?.type === "string" && entry.type !== "" ? entry.type : null;
|
|
29
43
|
}
|
|
30
44
|
|
|
45
|
+
/**
|
|
46
|
+
* 条目是否属于权威层:优先看服务端字段 authoritative(boolean 才采信);
|
|
47
|
+
* 旧夹具只有 type 时按 1.0.2 语义退化(registry=权威、github=非权威)。
|
|
48
|
+
*/
|
|
49
|
+
function isAuthoritativeEntry(entry) {
|
|
50
|
+
if (typeof entry?.authoritative === "boolean") return entry.authoritative === true;
|
|
51
|
+
return entryType(entry) === "registry";
|
|
52
|
+
}
|
|
53
|
+
|
|
31
54
|
/**
|
|
32
55
|
* 在一组候选里取最高版本;并列随机(同一信任层内语义不变)。
|
|
33
56
|
* sourceUrl/sourceType 一并带出:调用方要按**刷新时命中的那个源**定位安装路径,
|
|
@@ -51,7 +74,7 @@ function pickHighest(entries) {
|
|
|
51
74
|
}
|
|
52
75
|
if (best === null) return null;
|
|
53
76
|
if (ties.length > 1) {
|
|
54
|
-
//
|
|
77
|
+
// 并列:随机挑选,同一信任层内源权重一致
|
|
55
78
|
const pick = ties[Math.floor(Math.random() * ties.length)];
|
|
56
79
|
best = { ...best, sourceName: pick.sourceName ?? null, sourceUrl: pick.sourceUrl ?? null, sourceType: pick.sourceType ?? null, repo: pick.repo ?? null };
|
|
57
80
|
}
|
|
@@ -71,26 +94,26 @@ function snapshotOf(entry) {
|
|
|
71
94
|
}
|
|
72
95
|
|
|
73
96
|
/**
|
|
74
|
-
* @param entries - [{version: string|null, sourceName, sourceUrl?, sourceType?, repo, official
|
|
75
|
-
*
|
|
97
|
+
* @param entries - [{version: string|null, sourceName, sourceUrl?, sourceType?, repo, authoritative?, official?, type?}]
|
|
98
|
+
* (已过滤禁用源)。authoritative 是服务端判定的信任层字段;type 只决定安装路径。
|
|
76
99
|
* @param [options] - {trustTiers?: boolean}:trustTiers:false 强制旧行为(全局取最高),仅兼容用。
|
|
77
100
|
* @returns {version, sourceName, sourceUrl, sourceType, repo, sourceTrust?, degraded?};
|
|
78
101
|
* 全部无版本时 version=null(sourceTrust/degraded 仅在分级生效时出现)。
|
|
79
102
|
*/
|
|
80
103
|
export function aggregateLatest(entries, options = {}) {
|
|
81
104
|
const list = Array.isArray(entries) ? entries : [];
|
|
82
|
-
const
|
|
83
|
-
//
|
|
84
|
-
if (!
|
|
105
|
+
const tiered = list.some((entry) => typeof entry?.authoritative === "boolean" || entryType(entry) !== null);
|
|
106
|
+
// 向后兼容:既无 authoritative 也无 type(旧调用/旧夹具)或显式关闭分级 → 原行为,全局取最高
|
|
107
|
+
if (!tiered || options?.trustTiers === false) {
|
|
85
108
|
return pickHighest(list) ?? { version: null, sourceName: null, sourceUrl: null, sourceType: null, repo: null };
|
|
86
109
|
}
|
|
87
|
-
const
|
|
88
|
-
if (
|
|
89
|
-
// 规则 1
|
|
90
|
-
const best = pickHighest(
|
|
91
|
-
if (best !== null) return { ...best,
|
|
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 };
|
|
92
115
|
}
|
|
93
|
-
// 规则 2
|
|
116
|
+
// 规则 2:权威源没给出确定版本 → 所有源里取最高作兜底,并标记「未经 npm 校验」
|
|
94
117
|
const fallback = pickHighest(list);
|
|
95
118
|
if (fallback === null) return { version: null, sourceName: null, sourceUrl: null, sourceType: null, repo: null, sourceTrust: null, degraded: false };
|
|
96
119
|
return { ...fallback, sourceTrust: SOURCE_TRUST.FALLBACK, degraded: true };
|