dsh-git-push 2.20.11 → 2.20.17
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.md +10 -6
- package/cli-audit.mjs +141 -0
- package/cli-tool.mjs +86 -0
- package/cli.mjs +114 -335
- package/lib/app/command-registry.js +3 -2
- package/lib/app/constants.js +9 -0
- package/lib/app/handlers/clone.js +12 -3
- package/lib/app/handlers/http-constants.js +11 -2
- package/lib/app/handlers/npm.js +4 -2
- package/lib/app/handlers/repos.js +10 -1
- package/lib/app/handlers/settings.js +9 -1
- package/lib/app/http-handlers.js +24 -3
- package/lib/app/schema-audit.js +42 -0
- package/lib/app/schema.js +19 -19
- package/lib/app/settings-bridge.js +51 -0
- package/lib/app/tool-call.js +7 -5
- package/lib/audit-rules/audit-rules-paths.yml +7 -1
- package/lib/audit-ui-sdk/contract.js +117 -0
- package/lib/audit-ui-sdk/index.js +17 -0
- package/lib/checks/regex.js +6 -1
- package/lib/cli/commands-audit.mjs +12 -17
- package/lib/cli/commands-tool.mjs +93 -0
- package/lib/cli/commands-vcs.mjs +2 -1
- package/lib/cli/shared.mjs +448 -0
- package/lib/client-parts/{account.js → git/account.js} +1 -1
- package/lib/client-parts/{actions.js → git/actions.js} +26 -11
- package/lib/client-parts/{apply.js → git/apply.js} +1 -1
- package/lib/client-parts/{browser.js → git/browser.js} +1 -1
- package/lib/client-parts/{const.js → git/const.js} +6 -1
- package/lib/client-parts/{controller.js → git/controller.js} +23 -11
- package/lib/client-parts/{icons.js → git/icons.js} +1 -1
- package/lib/client-parts/{loader.js → git/loader.js} +1 -1
- package/lib/client-parts/{npm.js → git/npm.js} +1 -1
- package/lib/client-parts/{page.js → git/page.js} +11 -21
- package/lib/client-parts/{repo.js → git/repo.js} +1 -1
- package/lib/client-parts/{settings.js → git/settings.js} +1 -1
- package/lib/client-parts/{utils.js → git/utils.js} +1 -1
- package/lib/client.js +75 -354
- package/lib/commit-push/pre-commit-engine.js +388 -0
- package/lib/commit-push.js +42 -20
- package/lib/git/transport.js +31 -10
- package/lib/http/index.js +82 -10
- package/lib/npm/npmrc.js +22 -5
- package/lib/self/index.js +1 -1
- package/package.json +6 -2
- package/scripts/audit-gate.mjs +235 -0
- package/scripts/build-client.mjs +15 -8
- package/scripts/check.mjs +12 -3
- package/scripts/scan-file-io.mjs +5 -1
- package/scripts/scan-version.mjs +10 -7
- package/skills/dev/dsh-git-push.md +62 -0
- package/skills/guide/guide-audit.md +11 -11
- package/skills/guide/guide-cli-scripts.md +3 -3
- package/skills/guide/guide-commit-push.md +2 -2
- package/skills/guide/guide-inject-ui.md +0 -16
- package/assets/start.sh +0 -396
- package/scripts/scan-hardcoded-paths.mjs +0 -125
- /package/lib/client-parts/{audit.js → audit/audit.js} +0 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
// 提交前预处理引擎 · 子进程调用层
|
|
2
|
+
//
|
|
3
|
+
// 为什么要有这一层(解耦点):提交门禁此前直接 import 审计内核(auditWithScope)与评分
|
|
4
|
+
// (scoreQuality),换引擎/换语言重写就要改 git 侧代码。现在门禁只认一份**与语言无关的
|
|
5
|
+
// 进程契约**:跑一条命令、看退出码、解析一行 JSON。这样 Python / Go / Rust 写的引擎
|
|
6
|
+
// 只要满足契约就能顶替,git 侧一行不用改。
|
|
7
|
+
//
|
|
8
|
+
// 契约(完整定义见 scripts/audit-gate.mjs 头部与 docs/ 下的「提交前预处理与审计引擎解耦方案」):
|
|
9
|
+
// stdout : 一行 JSON { ok, scope, summary, quality, findings, files }
|
|
10
|
+
// 退出码 : 0 = 无 blocker / 1 = 有 blocker / 2 = 执行失败
|
|
11
|
+
// 超时 : 达到 timeoutMs 一律按「执行失败」处理
|
|
12
|
+
//
|
|
13
|
+
// 退出码 1 **不等于**「本次提交被拦」:示例目录豁免、临时豁免、私有库豁免都属于 git 侧判定
|
|
14
|
+
// (依赖仓库语义 / 调用方参数 / GitHub 网络),引擎不知道也不该知道。所以本层只把
|
|
15
|
+
// 「引擎说有 blocker」如实映射成 blocked 标志,**最终拦截判定仍由 runAudit 用自己的豁免规则做**。
|
|
16
|
+
//
|
|
17
|
+
// 失败一律 fail-closed(拒绝提交):引擎崩了 / 超时 / 退出码非 0/1 / stdout 解析不了,
|
|
18
|
+
// 都不能当成「审计通过」放行 —— 那是把红线交给「恰好没跑起来」。
|
|
19
|
+
|
|
20
|
+
import { spawn } from 'node:child_process';
|
|
21
|
+
import { existsSync, accessSync, constants as fsConstants } from 'node:fs';
|
|
22
|
+
import { delimiter, isAbsolute, join, resolve } from 'node:path';
|
|
23
|
+
import { fileURLToPath } from 'node:url';
|
|
24
|
+
|
|
25
|
+
import { readSettings } from '../app/settings-bridge.js';
|
|
26
|
+
// 超时默认值取单一来源:schema 默认值与运行期兜底必须是同一个数字(见 lib/app/constants.js)
|
|
27
|
+
import { PRE_COMMIT_TIMEOUT_MS_DEFAULT } from '../app/constants.js';
|
|
28
|
+
|
|
29
|
+
export { PRE_COMMIT_TIMEOUT_MS_DEFAULT };
|
|
30
|
+
|
|
31
|
+
/** 内置轻量入口(相对插件根)。配置留空时用它。 */
|
|
32
|
+
export const AUDIT_GATE_SCRIPT_REL = join('scripts', 'audit-gate.mjs');
|
|
33
|
+
|
|
34
|
+
/** 错误信息里保留的 stderr 尾部字符数:够定位(栈尾通常在最后),又不至于把上下文刷爆。 */
|
|
35
|
+
export const STDERR_TAIL_CHARS = 2000;
|
|
36
|
+
|
|
37
|
+
/** stdout 收集上限(64MB):findings 全量在大仓可达数 MB,超这个量级说明契约跑偏,宁可报错。 */
|
|
38
|
+
export const STDOUT_MAX_BYTES = 64 * 1024 * 1024;
|
|
39
|
+
|
|
40
|
+
/** 契约退出码(与 scripts/audit-gate.mjs 的 EXIT_* 常量对应)。 */
|
|
41
|
+
export const ENGINE_EXIT_OK = 0;
|
|
42
|
+
export const ENGINE_EXIT_BLOCKED = 1;
|
|
43
|
+
export const ENGINE_EXIT_FAILED = 2;
|
|
44
|
+
|
|
45
|
+
/** 插件根目录(本文件位于 <pluginRoot>/lib/commit-push/,向上两级)。 */
|
|
46
|
+
export function pluginRootDir() {
|
|
47
|
+
return resolve(fileURLToPath(new URL('../../', import.meta.url)));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 取 stderr 尾部(按字符数,不按行——栈尾常常没有换行)。
|
|
52
|
+
* @param {string} text
|
|
53
|
+
* @returns {string}
|
|
54
|
+
*/
|
|
55
|
+
export function stderrTail(text) {
|
|
56
|
+
const raw = String(text || '').trim();
|
|
57
|
+
return raw.length > STDERR_TAIL_CHARS ? raw.slice(raw.length - STDERR_TAIL_CHARS) : raw;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 配置解析顺序:`cfg.preCommit`(宿主注入)→ 插件 config.json 的 `preCommit` → 内置默认。
|
|
62
|
+
*
|
|
63
|
+
* 为什么还要读 config.json:`preCommit` 是**高级文件配置**,不进 UI 设置项
|
|
64
|
+
* (lib/client/index.js 的 SETTINGS_SCHEMA 是 UI 单一事实源,其 resolveConfig 会丢弃未知键),
|
|
65
|
+
* 所以运行期 cfg 里不会有它。同款先例:lib/app/apply.js 直接从 config.json 读 injectUsageText。
|
|
66
|
+
* 将来 UI 把 preCommit 加进 SETTINGS_SCHEMA 后,第一档(cfg.preCommit)会自动接管,本函数不用改。
|
|
67
|
+
*
|
|
68
|
+
* @param {object} [cfg] 运行期配置
|
|
69
|
+
* @returns {{command: string, args: string[], timeoutMs: number, source: 'cfg'|'config.json'|'default'}}
|
|
70
|
+
*/
|
|
71
|
+
export function resolvePreCommitConfig(cfg = {}) {
|
|
72
|
+
const { raw, source } = readPreCommitBlock(cfg);
|
|
73
|
+
const command = typeof raw?.command === 'string' ? raw.command.trim() : '';
|
|
74
|
+
const configuredArgs = Array.isArray(raw?.args)
|
|
75
|
+
? raw.args.map((rawArg) => String(rawArg)).filter((rawArg) => rawArg !== '')
|
|
76
|
+
: [];
|
|
77
|
+
// 内置默认只在「command 与 args 都没配」时生效:
|
|
78
|
+
// · command 空 + args 空 → node + 插件自带 scripts/audit-gate.mjs
|
|
79
|
+
// · command 空 + args 有值 → node + 配置里给的引擎脚本(args 里相对路径按插件根解析)
|
|
80
|
+
// · command 有值 + args 空 → 那是一个独立可执行文件(Go/Rust 单二进制就是这种形态,
|
|
81
|
+
// 此时**不能**塞我们自己的脚本路径进去)
|
|
82
|
+
const args = (!command && configuredArgs.length === 0)
|
|
83
|
+
? [join(pluginRootDir(), AUDIT_GATE_SCRIPT_REL)]
|
|
84
|
+
: configuredArgs;
|
|
85
|
+
const rawTimeout = Number(raw?.timeoutMs);
|
|
86
|
+
// 超时必须是正有限数:0 / 负数 / NaN 一律退回默认(0 表示「立刻超时」会让门禁永远失败)
|
|
87
|
+
const timeoutMs = Number.isFinite(rawTimeout) && rawTimeout > 0 ? Math.floor(rawTimeout) : PRE_COMMIT_TIMEOUT_MS_DEFAULT;
|
|
88
|
+
return { command, args, timeoutMs, source };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 取 preCommit 配置块(两档来源 + 来源标记)。
|
|
93
|
+
* @param {object} cfg
|
|
94
|
+
* @returns {{raw: object|null, source: 'cfg'|'config.json'|'default'}}
|
|
95
|
+
*/
|
|
96
|
+
function readPreCommitBlock(cfg = {}) {
|
|
97
|
+
if (cfg && typeof cfg.preCommit === 'object' && cfg.preCommit !== null) {
|
|
98
|
+
return { raw: cfg.preCommit, source: 'cfg' };
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
const fileSettings = readSettings({});
|
|
102
|
+
if (fileSettings && typeof fileSettings.preCommit === 'object' && fileSettings.preCommit !== null) {
|
|
103
|
+
return { raw: fileSettings.preCommit, source: 'config.json' };
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
106
|
+
// 配置读不到(无权限/无 DSH_HOME/坏 JSON)→ 落回内置默认,不影响提交门禁
|
|
107
|
+
}
|
|
108
|
+
return { raw: null, source: 'default' };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 判断某个路径是否可执行(POSIX 查 X_OK;Windows 只查存在,可执行性由扩展名决定)。
|
|
113
|
+
* @param {string} absPath
|
|
114
|
+
* @returns {boolean}
|
|
115
|
+
*/
|
|
116
|
+
function isExecutableFile(absPath) {
|
|
117
|
+
try {
|
|
118
|
+
if (!existsSync(absPath)) return false;
|
|
119
|
+
if (process.platform === 'win32') return true;
|
|
120
|
+
accessSync(absPath, fsConstants.X_OK);
|
|
121
|
+
return true;
|
|
122
|
+
} catch {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 在 PATH 里搜命令(Windows 还要按 PATHEXT 补扩展名)。
|
|
129
|
+
* @param {string} commandName 裸命令名(不含目录分隔符)
|
|
130
|
+
* @returns {{ok: true, command: string} | {ok: false, error: string}}
|
|
131
|
+
*/
|
|
132
|
+
function searchCommandOnPath(commandName) {
|
|
133
|
+
const pathValue = process.env.PATH || '';
|
|
134
|
+
const dirs = pathValue.split(delimiter).filter(Boolean);
|
|
135
|
+
const extensions = process.platform === 'win32'
|
|
136
|
+
? String(process.env.PATHEXT || '.EXE;.CMD;.BAT').split(';').filter(Boolean)
|
|
137
|
+
: [''];
|
|
138
|
+
for (const dir of dirs) {
|
|
139
|
+
for (const ext of extensions) {
|
|
140
|
+
const candidate = join(dir, `${commandName}${ext}`);
|
|
141
|
+
if (isExecutableFile(candidate)) return { ok: true, command: candidate };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
ok: false,
|
|
146
|
+
error: `找不到引擎命令「${commandName}」:已搜索 PATH(${dirs.length} 个目录,PATH=${pathValue || '(空)'})`,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 显式给了路径的命令:绝对路径原样校验;带分隔符的相对路径按**插件根**解析(换机不断链)。
|
|
152
|
+
* @param {string} pathLike
|
|
153
|
+
* @returns {{ok: true, command: string} | {ok: false, error: string}}
|
|
154
|
+
*/
|
|
155
|
+
function resolvePathLikeCommand(pathLike) {
|
|
156
|
+
const absPath = isAbsolute(pathLike) ? pathLike : resolve(pluginRootDir(), pathLike);
|
|
157
|
+
if (isExecutableFile(absPath)) return { ok: true, command: absPath };
|
|
158
|
+
return { ok: false, error: `引擎命令不可用:${pathLike}(解析为 ${absPath},文件不存在或没有可执行权限)` };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 把配置里的 command 解析成**可直接 spawn 的绝对路径**,找不到就显式失败。
|
|
163
|
+
*
|
|
164
|
+
* 不做「找不到就静默回退内置引擎」:配置把命令写错时静默回退,会让提交门禁悄悄地用另一套
|
|
165
|
+
* 引擎跑——审计口径变了却没人知道,比直接报错危险得多。
|
|
166
|
+
* @param {string} command 配置的 command(空 = 内置默认:当前 node 解释器)
|
|
167
|
+
* @returns {{ok: true, command: string, isDefault: boolean} | {ok: false, error: string}}
|
|
168
|
+
*/
|
|
169
|
+
export function resolveEngineCommand(command) {
|
|
170
|
+
const wanted = String(command || '').trim();
|
|
171
|
+
if (!wanted) return { ok: true, command: process.execPath, isDefault: true };
|
|
172
|
+
const looksLikePath = isAbsolute(wanted) || wanted.includes('/') || wanted.includes('\\');
|
|
173
|
+
const found = looksLikePath ? resolvePathLikeCommand(wanted) : searchCommandOnPath(wanted);
|
|
174
|
+
return found.ok ? { ok: true, command: found.command, isDefault: false } : found;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 解析配置里的 args:相对路径按**插件根**解析,其余原样保留。
|
|
179
|
+
*
|
|
180
|
+
* 判据用「解析后是否真存在」而不是「有没有斜杠」:args 里既可能是脚本路径,
|
|
181
|
+
* 也可能是命令行选项的取值(如 `--timing 200`),一刀切按路径解析会把选项值改成
|
|
182
|
+
* 一个不存在的绝对路径。存在才改写,不存在就原样交给引擎自己报错 —— 保守且可预测。
|
|
183
|
+
* @param {string[]} args
|
|
184
|
+
* @returns {string[]}
|
|
185
|
+
*/
|
|
186
|
+
export function resolveEngineArgs(args = []) {
|
|
187
|
+
const pluginRoot = pluginRootDir();
|
|
188
|
+
return (Array.isArray(args) ? args : []).map((rawArg) => {
|
|
189
|
+
const text = String(rawArg);
|
|
190
|
+
if (!text || text.startsWith('-') || isAbsolute(text)) return text;
|
|
191
|
+
const absPath = resolve(pluginRoot, text);
|
|
192
|
+
return existsSync(absPath) ? absPath : text;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 把槽位清单转成引擎要的逗号串(空清单返回空串 = 不传该参数)。
|
|
198
|
+
* @param {unknown} slots
|
|
199
|
+
* @returns {string}
|
|
200
|
+
*/
|
|
201
|
+
function toSlotCsv(slots) {
|
|
202
|
+
return (Array.isArray(slots) ? slots : [])
|
|
203
|
+
.map((slotName) => String(slotName).trim())
|
|
204
|
+
.filter(Boolean)
|
|
205
|
+
.join(',');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 组装传给引擎的完整参数表(**由本模块统一追加,调用方不拼**)。
|
|
210
|
+
* 排序固定:先配置里的 args,再契约参数 —— 契约参数在最后,任何引擎都能靠「尾部固定段」解析。
|
|
211
|
+
* @param {object} p
|
|
212
|
+
* @returns {string[]}
|
|
213
|
+
*/
|
|
214
|
+
export function buildEngineArgs({
|
|
215
|
+
args = [], repoPath = '', scope = 'diff', rulesetDir = '', weights = {}, maxScanFiles, slots = [], disabledSlots = [], blockersOnly = false,
|
|
216
|
+
} = {}) {
|
|
217
|
+
const argv = [...resolveEngineArgs(args)];
|
|
218
|
+
argv.push('--repo', String(repoPath || '.'));
|
|
219
|
+
argv.push('--scope', scope === 'full' ? 'full' : 'diff');
|
|
220
|
+
if (rulesetDir) argv.push('--ruleset', String(rulesetDir));
|
|
221
|
+
if (weights && typeof weights === 'object' && Object.keys(weights).length) argv.push('--weights', JSON.stringify(weights));
|
|
222
|
+
// maxScanFiles 只在拿到有限数时才传:0 有明确含义(不限),所以不能用 truthy 判断
|
|
223
|
+
if (Number.isFinite(Number(maxScanFiles))) argv.push('--max-scan-files', String(Number(maxScanFiles)));
|
|
224
|
+
// 槽位清单只在「确实有内容」时才传:空数组传成空字符串会被引擎当成一个空槽位名
|
|
225
|
+
const whiteListCsv = toSlotCsv(slots);
|
|
226
|
+
if (whiteListCsv) argv.push('--slots', whiteListCsv);
|
|
227
|
+
const blackListCsv = toSlotCsv(disabledSlots);
|
|
228
|
+
if (blackListCsv) argv.push('--disabled-slots', blackListCsv);
|
|
229
|
+
if (blockersOnly) argv.push('--blockers-only');
|
|
230
|
+
argv.push('--json');
|
|
231
|
+
return argv;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* 拼一条可复制的「人类可读命令行」,用于错误信息(用户要能直接粘到终端里复现)。
|
|
236
|
+
* @param {string} command
|
|
237
|
+
* @param {string[]} args
|
|
238
|
+
* @returns {string}
|
|
239
|
+
*/
|
|
240
|
+
export function formatCommandLine(command, args = []) {
|
|
241
|
+
return [command, ...args].map((part) => (/\s/.test(part) ? `'${part}'` : part)).join(' ');
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* 失败结果统一形状:ok=false + 明确原因(含命令、退出码、stderr 尾部)。
|
|
246
|
+
* blocked=false —— 引擎不可用不属于「审计判定拦截」,调用方据此显示不同文案
|
|
247
|
+
* (工具侧:blocked 显示「推送被拦截」,非 blocked 显示「未完成:<原因>」)。
|
|
248
|
+
* @param {string} message
|
|
249
|
+
* @returns {{ok: false, blocked: false, error: string, audit: null}}
|
|
250
|
+
*/
|
|
251
|
+
function failClosed(message) {
|
|
252
|
+
return { ok: false, blocked: false, error: message, audit: null };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* 从 stdout 里取出那一行契约 JSON。
|
|
257
|
+
* 只容忍「前后有空行」,不容忍夹杂其他文字:出现噪音说明引擎破坏了契约,
|
|
258
|
+
* 此时宁可失败也不要猜(挑错行会拿到过期/别人的数据)。
|
|
259
|
+
* @param {string} stdout
|
|
260
|
+
* @returns {{ok: true, value: object} | {ok: false, error: string}}
|
|
261
|
+
*/
|
|
262
|
+
export function parseEngineStdout(stdout) {
|
|
263
|
+
const text = String(stdout || '').trim();
|
|
264
|
+
if (!text) return { ok: false, error: '引擎没有输出任何内容(stdout 为空)' };
|
|
265
|
+
const lines = text.split('\n').map((line) => line.trim()).filter(Boolean);
|
|
266
|
+
if (lines.length !== 1) {
|
|
267
|
+
return { ok: false, error: `引擎 stdout 不是「单行 JSON」(共 ${lines.length} 行非空输出)` };
|
|
268
|
+
}
|
|
269
|
+
let parsed;
|
|
270
|
+
try {
|
|
271
|
+
parsed = JSON.parse(lines[0]);
|
|
272
|
+
} catch (e) {
|
|
273
|
+
return { ok: false, error: `引擎 stdout 不是合法 JSON:${e.message};首 200 字符:${lines[0].slice(0, 200)}` };
|
|
274
|
+
}
|
|
275
|
+
if (!parsed || typeof parsed !== 'object') return { ok: false, error: '引擎 JSON 顶层不是对象' };
|
|
276
|
+
if (!parsed.summary || typeof parsed.summary !== 'object') {
|
|
277
|
+
return { ok: false, error: '引擎 JSON 缺少 summary 字段(契约要求 { summary:{blocker,warning,notice,total} })' };
|
|
278
|
+
}
|
|
279
|
+
return { ok: true, value: parsed };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* 起子进程并收 stdout/stderr,只负责「跑完 + 收齐」,不做契约解释。
|
|
284
|
+
* 拆出来的理由:超时强杀、流上限、spawn 异常三条路径都不该混在门禁语义里,
|
|
285
|
+
* 否则「跑失败」和「判失败」会糊在一起,改一处就动全身。
|
|
286
|
+
* @param {object} p { command, args, timeoutMs }
|
|
287
|
+
* @returns {Promise<{ok: true, stdout: string, stderr: string, exitCode: number|null, signal: string|null}
|
|
288
|
+
* | {ok: false, stage: 'timeout'|'too-large'|'spawn', error: string, stderr: string}>}
|
|
289
|
+
*/
|
|
290
|
+
function runEngineProcess({ command, args, timeoutMs }) {
|
|
291
|
+
return new Promise((settle) => {
|
|
292
|
+
let stdout = '';
|
|
293
|
+
let stderr = '';
|
|
294
|
+
let stdoutBytes = 0;
|
|
295
|
+
let settled = false;
|
|
296
|
+
// timer 必须先声明再被 finish 引用:spawn 同步抛异常时 finish 会跑在 setTimeout 之前,
|
|
297
|
+
// 用 const + 后置赋值会命中 TDZ(ReferenceError)把「启动失败」变成「未捕获异常」。
|
|
298
|
+
let timer = null;
|
|
299
|
+
let child = null;
|
|
300
|
+
const finish = (result) => {
|
|
301
|
+
if (settled) return;
|
|
302
|
+
settled = true;
|
|
303
|
+
if (timer) clearTimeout(timer);
|
|
304
|
+
settle(result);
|
|
305
|
+
};
|
|
306
|
+
const killChild = () => { try { child?.kill('SIGKILL'); } catch { /* 进程可能已退出 */ } };
|
|
307
|
+
|
|
308
|
+
try {
|
|
309
|
+
child = spawn(command, args, { stdio: ['ignore', 'pipe', 'pipe'] });
|
|
310
|
+
} catch (e) {
|
|
311
|
+
finish({ ok: false, stage: 'spawn', error: String(e?.message || e), stderr });
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
timer = setTimeout(() => {
|
|
315
|
+
killChild();
|
|
316
|
+
finish({ ok: false, stage: 'timeout', error: `>${timeoutMs}ms 未返回,已强杀`, stderr });
|
|
317
|
+
}, timeoutMs);
|
|
318
|
+
|
|
319
|
+
child.stdout?.on('data', (chunk) => {
|
|
320
|
+
stdoutBytes += chunk.length;
|
|
321
|
+
if (stdoutBytes > STDOUT_MAX_BYTES) {
|
|
322
|
+
killChild();
|
|
323
|
+
finish({ ok: false, stage: 'too-large', error: `stdout 超过上限 ${STDOUT_MAX_BYTES} 字节(findings 过大?可改用 --blockers-only)`, stderr });
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
stdout += chunk.toString();
|
|
327
|
+
});
|
|
328
|
+
child.stderr?.on('data', (chunk) => { stderr += chunk.toString(); });
|
|
329
|
+
child.on('error', (e) => {
|
|
330
|
+
const reason = e?.code === 'ENOENT' ? '命令不存在(ENOENT)' : String(e?.message || e);
|
|
331
|
+
finish({ ok: false, stage: 'spawn', error: reason, stderr });
|
|
332
|
+
});
|
|
333
|
+
child.on('close', (exitCode, signal) => {
|
|
334
|
+
finish({ ok: true, stdout, stderr, exitCode: typeof exitCode === 'number' ? exitCode : null, signal });
|
|
335
|
+
});
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* 跑提交前预处理引擎。
|
|
341
|
+
* @param {object} p
|
|
342
|
+
* repoPath : 仓库绝对路径(相对路径按当前工作目录解析,与旧行为一致)
|
|
343
|
+
* scope : 'diff' | 'full'
|
|
344
|
+
* rulesetDir: 自定规则目录(透传,不解析——旧行为就是原样传给审计)
|
|
345
|
+
* weights : 已解析好的权重覆盖对象(空对象 = 默认权重表)
|
|
346
|
+
* maxScanFiles / slots / disabledSlots:来自插件配置的审计选项(不传则用引擎默认值)
|
|
347
|
+
* blockersOnly: findings 只留 blocker
|
|
348
|
+
* cfg : 运行期配置(读 preCommit)
|
|
349
|
+
* timeoutMs : 覆盖超时(测试用)
|
|
350
|
+
* @returns {Promise<{ok: boolean, blocked: boolean, error?: string, audit: object|null,
|
|
351
|
+
* engine?: {command: string, args: string[], exitCode: number|null, timedOut: boolean}}>}
|
|
352
|
+
*/
|
|
353
|
+
export async function runPreCommitEngine({
|
|
354
|
+
repoPath = '.', scope = 'diff', rulesetDir = '', weights = {}, maxScanFiles, slots = [], disabledSlots = [],
|
|
355
|
+
blockersOnly = false, cfg = {}, timeoutMs,
|
|
356
|
+
} = {}) {
|
|
357
|
+
const spec = resolvePreCommitConfig(cfg);
|
|
358
|
+
const resolved = resolveEngineCommand(spec.command);
|
|
359
|
+
if (!resolved.ok) return failClosed(`提交前预处理失败:${resolved.error}(未执行任何 git 操作;修复 preCommit.command 后重试)`);
|
|
360
|
+
const command = resolved.command;
|
|
361
|
+
const args = buildEngineArgs({ args: spec.args, repoPath, scope, rulesetDir, weights, maxScanFiles, slots, disabledSlots, blockersOnly });
|
|
362
|
+
const commandLine = formatCommandLine(command, args);
|
|
363
|
+
const effectiveTimeout = Number.isFinite(Number(timeoutMs)) && Number(timeoutMs) > 0
|
|
364
|
+
? Math.floor(Number(timeoutMs))
|
|
365
|
+
: spec.timeoutMs;
|
|
366
|
+
const run = await runEngineProcess({ command, args, timeoutMs: effectiveTimeout });
|
|
367
|
+
|
|
368
|
+
if (!run.ok) {
|
|
369
|
+
const label = run.stage === 'timeout' ? '超时' : run.stage === 'too-large' ? '输出超限' : '无法启动';
|
|
370
|
+
return failClosed(`提交前预处理失败:引擎${label}(${run.error})\n 命令:${commandLine}\n stderr: ${stderrTail(run.stderr) || '(空)'}\n (未执行任何 git 操作;修复引擎或临时关闭 auditEnabled 后重试)`);
|
|
371
|
+
}
|
|
372
|
+
const { exitCode, signal, stdout, stderr } = run;
|
|
373
|
+
if (exitCode !== ENGINE_EXIT_OK && exitCode !== ENGINE_EXIT_BLOCKED) {
|
|
374
|
+
const codeText = exitCode === null ? `被信号 ${signal || '?'} 终止` : `退出码 ${exitCode}`;
|
|
375
|
+
return failClosed(`提交前预处理失败:引擎 ${commandLine} ${codeText}\n stderr: ${stderrTail(stderr) || '(空)'}\n (未执行任何 git 操作;修复引擎或临时关闭 auditEnabled 后重试)`);
|
|
376
|
+
}
|
|
377
|
+
const parsed = parseEngineStdout(stdout);
|
|
378
|
+
if (!parsed.ok) {
|
|
379
|
+
return failClosed(`提交前预处理失败:${parsed.error}\n 命令:${commandLine}(退出码 ${exitCode})\n stderr: ${stderrTail(stderr) || '(空)'}`);
|
|
380
|
+
}
|
|
381
|
+
return {
|
|
382
|
+
ok: true,
|
|
383
|
+
// 退出码 1 = 引擎看到 blocker;是否真的拦,由 git 侧豁免规则最终决定
|
|
384
|
+
blocked: exitCode === ENGINE_EXIT_BLOCKED,
|
|
385
|
+
audit: parsed.value,
|
|
386
|
+
engine: { command, args, exitCode, timedOut: false },
|
|
387
|
+
};
|
|
388
|
+
}
|
package/lib/commit-push.js
CHANGED
|
@@ -8,13 +8,14 @@
|
|
|
8
8
|
* 自动推送带审计门禁(blocker 拦截 + 敏感扫描 + .gitignore),不再裸 commit+push
|
|
9
9
|
* 返回结构含 audit 摘要,调用方(工具/外部插件)可据此判断「被阻断」。
|
|
10
10
|
*/
|
|
11
|
-
import { join } from 'node:path';
|
|
12
11
|
import { buildAuditApiGuide } from './app/inject-text.js';
|
|
13
|
-
import { existsSync } from 'node:fs';
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
// 审计内核(auditWithScope / scoreQuality)不再在本文件直接 import —— 提交门禁改为
|
|
14
|
+
// 调「可配置的审计引擎子进程」(lib/commit-push/pre-commit-engine.js),这是解耦点:
|
|
15
|
+
// 换引擎(含换语言重写)只改配置,git 侧零改动。
|
|
16
|
+
// ⚠️ 文档漂移检查器的接线也随之搬进引擎入口(scripts/audit-gate.mjs 里 import 一次);
|
|
17
|
+
// 宿主自己的工具/HTTP 侧仍由 lib/app/tool-call.js 接线,不依赖本文件。
|
|
18
|
+
import { runPreCommitEngine } from './commit-push/pre-commit-engine.js';
|
|
18
19
|
import { commitAndPush } from './git/index.js';
|
|
19
20
|
import { defaultConfig } from './client/index.js';
|
|
20
21
|
import { MAX_MSG_PREVIEW } from './app/constants.js';
|
|
@@ -41,28 +42,46 @@ export async function runAudit({ repoPath = '', audit, rulesetDir, slots, cfg: e
|
|
|
41
42
|
// 把该字段置空后同一项目 60 条 → 197 条、python/* 命中 0 → 5 ✓。
|
|
42
43
|
// 现在只保留显式传入的 slots(API/工具级白名单);配置里的顺序不再当白名单(禁用请用 auditDisabledSlots ✓)。
|
|
43
44
|
const slotOrder = Array.isArray(slots) && slots.length ? slots : undefined;
|
|
44
|
-
const auditRes = await auditWithScope(repoPath || '.', {
|
|
45
|
-
scope: cfg.auditScanScope || 'diff',
|
|
46
|
-
maxScanFiles: cfg.maxScanFiles,
|
|
47
|
-
rulesetDir: rulesetDir || '', // 自定规则目录已从设置移除,只认显式传参(工具 ruleset 参数)
|
|
48
|
-
slots: slotOrder,
|
|
49
|
-
disabledSlots: Array.isArray(cfg.auditDisabledSlots) ? cfg.auditDisabledSlots : [],
|
|
50
|
-
});
|
|
51
45
|
// 修复:提交前审计同样消费侧边栏权重覆盖(cfg.weightOverrides,config.json 回读),
|
|
52
46
|
// 与 code_audit 工具同源——此前传空权重,评分恒用默认权重表(侧边栏权重设置对提交审计无效)。
|
|
47
|
+
// 权重在 git 侧解析(配置语义留在插件侧),再序列化传给引擎:引擎只做「解析 → 用」。
|
|
53
48
|
let auditWeights = {};
|
|
54
49
|
if (typeof cfg.weightOverrides === 'string' && cfg.weightOverrides.trim()) {
|
|
55
50
|
try { auditWeights = JSON.parse(cfg.weightOverrides) || {}; } catch { /* 非法 JSON 回退默认 */ }
|
|
56
51
|
}
|
|
52
|
+
// ── 解耦点:审计与评分不再在进程内跑,改为调配置化引擎子进程 ──
|
|
53
|
+
// 传给引擎的东西只有「本来就在传的选项」:scope / maxScanFiles / rulesetDir /
|
|
54
|
+
// slots / disabledSlots / weights。maxScanFiles 每次都必须传:它的默认值是 3000,
|
|
55
|
+
// 而加载层把 0 当「不限」,漏传会让大仓门禁退化成全量扫描。
|
|
56
|
+
const engineRes = await runPreCommitEngine({
|
|
57
|
+
repoPath: repoPath || '.',
|
|
58
|
+
scope: cfg.auditScanScope || 'diff',
|
|
59
|
+
rulesetDir: rulesetDir || '', // 自定规则目录已从设置移除,只认显式传参(工具 ruleset 参数)
|
|
60
|
+
weights: auditWeights,
|
|
61
|
+
maxScanFiles: cfg.maxScanFiles,
|
|
62
|
+
slots: slotOrder,
|
|
63
|
+
disabledSlots: Array.isArray(cfg.auditDisabledSlots) ? cfg.auditDisabledSlots : [],
|
|
64
|
+
cfg,
|
|
65
|
+
});
|
|
66
|
+
// 引擎不可用(崩/超时/退出码 2/输出不合契约)一律 fail-closed:拒绝提交并给出可诊断原因。
|
|
67
|
+
// 绝不在这里「当作没有 blocker 放行」——那等于把红线交给「恰好没跑起来」。
|
|
68
|
+
if (!engineRes.ok) return { ok: false, blocked: false, error: engineRes.error, audit: null };
|
|
69
|
+
const engineAudit = engineRes.audit || {};
|
|
70
|
+
const findings = Array.isArray(engineAudit.findings) ? engineAudit.findings : [];
|
|
57
71
|
const auditOut = {
|
|
58
|
-
summary:
|
|
59
|
-
quality:
|
|
60
|
-
|
|
61
|
-
|
|
72
|
+
summary: engineAudit.summary,
|
|
73
|
+
quality: engineAudit.quality,
|
|
74
|
+
// 引擎溯源(新增字段,只增不改):出问题时能一眼看到「用的哪个引擎、退出码几」,
|
|
75
|
+
// 不必去翻配置或日志。既有文案(apiGuide / 拦截 error)保持原样不动。
|
|
76
|
+
engine: {
|
|
77
|
+
command: engineRes.engine?.command || '',
|
|
78
|
+
exitCode: engineRes.engine?.exitCode ?? null,
|
|
79
|
+
blocked: engineRes.engine?.exitCode === 1,
|
|
80
|
+
},
|
|
62
81
|
};
|
|
63
82
|
// 审计输出改为「级别数量 + 评分 + API 查询用法」——审计明细由聚合 API
|
|
64
83
|
// (/api/git-push/audit)查询,不再内联 findings 全文;blocked 摘要保留(拦截必需)。
|
|
65
|
-
auditOut.apiGuide = buildAuditApiGuide(repoPath, { scope:
|
|
84
|
+
auditOut.apiGuide = buildAuditApiGuide(repoPath, { scope: engineAudit.scope, summary: engineAudit.summary, quality: auditOut.quality });
|
|
66
85
|
// 本次提交的按文件临时豁免(auditExemptPaths):列入的文件 finding 一律不参与拦截判定(含 blocker),
|
|
67
86
|
// 仅本次有效、不落盘、不改 .auditignore;评分仍用全量 findings(豁免不影响打分基数)。
|
|
68
87
|
// 语义按需求方定稿:豁免即豁免,不设「只允许 warning/notice」的限制,也不按规则类别过滤。
|
|
@@ -70,9 +89,9 @@ export async function runAudit({ repoPath = '', audit, rulesetDir, slots, cfg: e
|
|
|
70
89
|
.map((p) => String(p || '').replace(/\\/g, '/').replace(/^\.\//, '').trim())
|
|
71
90
|
.filter(Boolean));
|
|
72
91
|
const isExemptFile = (f) => exemptSet.size > 0 && exemptSet.has(String(f.file || '').replace(/\\/g, '/').replace(/^\.\//, ''));
|
|
73
|
-
const exemptFindings = exemptSet.size ?
|
|
92
|
+
const exemptFindings = exemptSet.size ? findings.filter((f) => isExemptFile(f)) : [];
|
|
74
93
|
// 只算非示例目录的 blocker(示例目录内的 blocker 是规则演示,不拦真实提交)
|
|
75
|
-
const blocked =
|
|
94
|
+
const blocked = findings.filter((f) => f.severity === 'blocker' && !isSampleExemptDir(repoPath || '.', f.file) && !isExemptFile(f));
|
|
76
95
|
auditOut.blocked = blocked.map((f) => ({ file: f.file, line: f.line, rule: f.rule, message: (f.message || '').slice(0, MAX_MSG_PREVIEW) }));
|
|
77
96
|
if (exemptSet.size) {
|
|
78
97
|
// 回显(记录而非判断):本次豁免了哪些文件、命中哪些规则、共几条 finding
|
|
@@ -150,7 +169,10 @@ export async function commitWithAudit({
|
|
|
150
169
|
} = {}) {
|
|
151
170
|
const cfg = defaultConfig();
|
|
152
171
|
const a = await runAudit({ repoPath, audit, rulesetDir, slots });
|
|
153
|
-
|
|
172
|
+
// blocked 用 runAudit 的真实结论(缺省仍按拦截处理,保持旧行为):
|
|
173
|
+
// 审计判定拦截 = true;引擎不可用 = false(那是「没跑起来」,不是「审计不通过」,
|
|
174
|
+
// 工具侧据此显示「未完成:<原因>」而不是「推送被拦截」)。
|
|
175
|
+
if (!a.ok) return { ok: false, blocked: a.blocked ?? true, error: a.error, audit: a.audit };
|
|
154
176
|
// 推送通道缺省取插件配置(默认 ssh:推本地 HEAD、远端 sha 与本地一致)
|
|
155
177
|
const r = await commitAndPush({ repoPath, message, push, dryRun, customIgnorePatterns, requirementsConfirmed, force, pushMethod: pushMethod || cfg.pushMethod || 'ssh', pushGate, pushConfirmed, paths });
|
|
156
178
|
return { ok: r.ok !== false, ...r, audit: a.audit };
|
package/lib/git/transport.js
CHANGED
|
@@ -466,33 +466,54 @@ async function uploadBlobsAndBuildTree({ repoPath, entries, remoteBlobShas, api
|
|
|
466
466
|
* (ControlMaster)把 N 仓总耗时压到接近单仓。
|
|
467
467
|
* @returns {Promise<{ok:boolean, sha:string, via?:string}>}
|
|
468
468
|
*/
|
|
469
|
-
export async function liveRemoteHeadAsync({ repoPath = '', branch = '', remote = '' } = {}) {
|
|
469
|
+
export async function liveRemoteHeadAsync({ repoPath = '', branch = '', remote = '', budgetMs = 0 } = {}) {
|
|
470
470
|
if (process.env.DSH_GIT_PUSH_OFFLINE === '1') return { ok: false, sha: '', via: 'offline' };
|
|
471
|
+
// 预算传播(2026-11-13):调用方给的**总预算**要逐级摊到每一步 git 调用上。
|
|
472
|
+
// 为什么必须这么做:本地列表的「整表预算」只在**每次探测前**检查一次,
|
|
473
|
+
// 单次探测内部若有一句不带超时的 git 调用(`remote get-url` / `branch --show-current`
|
|
474
|
+
// 在冷启动首次 SSH 建连、CIFS 大目录 stat 时可能挂很久),整个请求就会穿透预算
|
|
475
|
+
// —— 实测症状:DSH 启动后第一次打开「本地」页报 `读取失败: signal timed out`
|
|
476
|
+
// (前端 fetch 30s 超时先炸,而服务端 15s 预算形同虚设)。
|
|
477
|
+
// 做法:每一步的 timeoutMs = min(单步语义上限, 剩余预算),总时长因此被预算界定;
|
|
478
|
+
// 这不是「超时兜底」,是给外部 I/O 定可界定的语义上限(对端不可控)。
|
|
479
|
+
const startedAt = Date.now();
|
|
480
|
+
const budgetDeadline = budgetMs > 0 ? startedAt + budgetMs : Infinity;
|
|
481
|
+
// 每一步的可用时长:剩余预算与单步上限取小;预算耗尽返回 1ms(让调用立刻失败而不是继续等)
|
|
482
|
+
const stepTimeout = (capMs) => {
|
|
483
|
+
const remain = budgetDeadline - Date.now();
|
|
484
|
+
if (!Number.isFinite(remain)) return capMs;
|
|
485
|
+
if (remain <= 0) return 1;
|
|
486
|
+
return Math.max(1, Math.min(capMs, remain));
|
|
487
|
+
};
|
|
488
|
+
const META_STEP_TIMEOUT = 3_000; // 读 remote / 当前分支:本地读 config,3s 足够
|
|
489
|
+
const LS_TIMEOUT = 8_000; // 单次 ls-remote(含 SSH 建连)
|
|
471
490
|
// 指定 remote 时核验它(与推送同源);否则沿用 origin
|
|
472
491
|
const origin = remote
|
|
473
|
-
? (await runGitAsync(['remote', 'get-url', remote], { cwd: repoPath })).stdout
|
|
474
|
-
: (await runGitAsync(['remote', 'get-url', 'origin'], { cwd: repoPath })).stdout;
|
|
492
|
+
? (await runGitAsync(['remote', 'get-url', remote], { cwd: repoPath, timeoutMs: stepTimeout(META_STEP_TIMEOUT) })).stdout
|
|
493
|
+
: (await runGitAsync(['remote', 'get-url', 'origin'], { cwd: repoPath, timeoutMs: stepTimeout(META_STEP_TIMEOUT) })).stdout;
|
|
475
494
|
const pr = parseGithubOwnerRepo(origin);
|
|
476
|
-
const target = branch || (await runGitAsync(['branch', '--show-current'], { cwd: repoPath })).stdout;
|
|
495
|
+
const target = branch || (await runGitAsync(['branch', '--show-current'], { cwd: repoPath, timeoutMs: stepTimeout(META_STEP_TIMEOUT) })).stdout;
|
|
477
496
|
if (!pr || !target) return { ok: false, sha: '' };
|
|
478
497
|
const sshUrl = sshUrlOf(pr);
|
|
479
498
|
const known = knownHostsPath();
|
|
480
|
-
const LS_TIMEOUT = 8_000;
|
|
481
499
|
let tried = 0;
|
|
482
500
|
for (const { keyPath, kind } of resolveSshKeys()) {
|
|
483
501
|
if (tried >= 2) break; // 最多试 2 个 key,避免多 key 串行放大耗时
|
|
502
|
+
if (budgetDeadline - Date.now() <= 0) break; // 预算耗尽就别再启新的 SSH 尝试
|
|
484
503
|
tried += 1;
|
|
485
504
|
const sshCmd = buildLsRemoteSshCmd(keyPath, known);
|
|
486
|
-
const r = await runGitAsync(['ls-remote', sshUrl, `${REFS_HEADS_PREFIX}${target}`], { cwd: repoPath, env: { GIT_SSH_COMMAND: sshCmd }, timeoutMs: LS_TIMEOUT });
|
|
505
|
+
const r = await runGitAsync(['ls-remote', sshUrl, `${REFS_HEADS_PREFIX}${target}`], { cwd: repoPath, env: { GIT_SSH_COMMAND: sshCmd }, timeoutMs: stepTimeout(LS_TIMEOUT) });
|
|
487
506
|
if (r.ok) {
|
|
488
507
|
const sha = (r.stdout || '').trim().split(/\s+/)[0] || '';
|
|
489
508
|
if (sha) return { ok: true, sha, via: kind };
|
|
490
509
|
}
|
|
491
510
|
}
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
511
|
+
if (budgetDeadline - Date.now() > 0) {
|
|
512
|
+
const lr = await runGitAsync(['ls-remote', rr.remote || 'origin', `${REFS_HEADS_PREFIX}${target}`], { cwd: repoPath, timeoutMs: stepTimeout(LS_TIMEOUT) });
|
|
513
|
+
if (lr.ok) {
|
|
514
|
+
const sha = (lr.stdout || '').trim().split(/\s+/)[0] || '';
|
|
515
|
+
if (sha) return { ok: true, sha, via: 'origin' };
|
|
516
|
+
}
|
|
496
517
|
}
|
|
497
518
|
return { ok: false, sha: '' };
|
|
498
519
|
}
|