@dommaker/harness 1.12.0 → 1.13.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.
- package/CHANGELOG.md +8 -0
- package/dist/cli/commands/security.d.ts +19 -0
- package/dist/cli/commands/security.d.ts.map +1 -1
- package/dist/cli/commands/security.js +105 -0
- package/dist/cli/commands/security.js.map +1 -1
- package/dist/gates/command.d.ts +5 -1
- package/dist/gates/command.d.ts.map +1 -1
- package/dist/gates/command.js +6 -2
- package/dist/gates/command.js.map +1 -1
- package/dist/gates/definitions.d.ts.map +1 -1
- package/dist/gates/definitions.js +6 -1
- package/dist/gates/definitions.js.map +1 -1
- package/dist/pretool-use-hook.d.ts +13 -4
- package/dist/pretool-use-hook.d.ts.map +1 -1
- package/dist/pretool-use-hook.js +89 -8
- package/dist/pretool-use-hook.js.map +1 -1
- package/package.json +1 -1
- package/src/CONTEXT.md +1 -1
- package/src/__tests__/layering.test.ts +3 -1
- package/src/__tests__/pretool-use-hook.test.ts +211 -4
- package/src/cli/commands/CONTEXT.md +1 -1
- package/src/cli/commands/__tests__/security.test.ts +89 -1
- package/src/cli/commands/security.ts +99 -0
- package/src/gates/CONTEXT.md +2 -2
- package/src/gates/command.ts +6 -2
- package/src/gates/definitions.ts +6 -1
- package/src/pretool-use-hook.ts +104 -10
package/src/pretool-use-hook.ts
CHANGED
|
@@ -9,39 +9,132 @@
|
|
|
9
9
|
*
|
|
10
10
|
* 语义(与生成版一致):
|
|
11
11
|
* - stdin 收 provider PreToolUse JSON(tool_input.command);
|
|
12
|
-
* - CommandGate
|
|
12
|
+
* - CommandGate 判定 block 级黑名单,命中 → stderr 写原因 + exit 2
|
|
13
13
|
* (codex/kimi 阻断语义:exit 2 + stderr 阻断,其余 exit code fail-open);
|
|
14
14
|
* - fail-open:stdin 非 JSON / 缺字段 / CommandGate 异常一律放行——宁可漏拦
|
|
15
15
|
* 也不全体 Bash 秒断,拦截层只是纵深防御的一道;
|
|
16
|
-
* - warn/audit
|
|
16
|
+
* - warn/audit 级命中同样放行(只拦 block 级),与生成版行为一致。
|
|
17
|
+
*
|
|
18
|
+
* P1-7(ADR-0031,wayfinder 票08):判定后对命中写审计 trace(只记账不加新拦截
|
|
19
|
+
* 能力)——此前拦下/放行的危险命令只写 stderr 不落盘,traces.log 里只有约束检查
|
|
20
|
+
* 在写。留痕失败经 try/catch 吞掉,fail-open 口径与 shim 主路径一致。
|
|
21
|
+
*
|
|
22
|
+
* P1-5 harness 半边(ADR-0031,wayfinder 票08):识别 tool_name 非 Bash 的事件
|
|
23
|
+
* (Edit/Write/apply_patch/MCP 工具,studio 侧补全 codex hooks matcher 后会到达),
|
|
24
|
+
* 只留痕不拦截——这些事件的 tool_input 没有 command 字段,旧形状会因缺字段
|
|
25
|
+
* fail-open 空转;拦截归 codex 沙箱,本 shim 对它们只记账。
|
|
17
26
|
*/
|
|
18
27
|
import { CommandGate } from './gates/command';
|
|
28
|
+
import type { CommandBlacklistRule } from './gates/types';
|
|
29
|
+
import { TraceCollector } from './monitoring/traces';
|
|
19
30
|
|
|
20
31
|
/** hook 标识:与 studio-agent 生成版同一 marker(配置幂等检测共用口径) */
|
|
21
32
|
export const HOOK_MARKER = 'harness-command-gate';
|
|
22
33
|
|
|
23
34
|
interface PreToolUseInput {
|
|
24
|
-
|
|
35
|
+
tool_name?: string;
|
|
36
|
+
tool_input?: { command?: string; file_path?: string };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** 一次 PreToolUse 的判定结果:放行结论 + 命中明细(hits 供审计留痕) */
|
|
40
|
+
export interface PreToolUseDecision {
|
|
41
|
+
allowed: boolean;
|
|
42
|
+
command: string;
|
|
43
|
+
hits: CommandBlacklistRule[];
|
|
44
|
+
/** provider 工具名(缺省按 Bash 兼容旧事件形状) */
|
|
45
|
+
toolName: string;
|
|
46
|
+
/** 非 Bash 事件的目标路径类字段(file_path;不记内容正文) */
|
|
47
|
+
targetPath?: string;
|
|
25
48
|
}
|
|
26
49
|
|
|
27
50
|
/**
|
|
28
51
|
* 判定一次 PreToolUse 是否放行。
|
|
29
52
|
* block 级命中 → allowed=false;warn/audit/干净命令/坏输入一律 allowed=true。
|
|
53
|
+
* 非 Bash 工具事件(Edit/Write/apply_patch/MCP)恒放行(P1-5:拦截归 codex 沙箱)。
|
|
30
54
|
*/
|
|
31
|
-
export function decidePreToolUse(rawStdin: string):
|
|
55
|
+
export function decidePreToolUse(rawStdin: string): PreToolUseDecision {
|
|
32
56
|
let input: PreToolUseInput = {};
|
|
33
57
|
try {
|
|
34
58
|
input = JSON.parse(rawStdin || '{}') as PreToolUseInput;
|
|
35
59
|
} catch {
|
|
36
60
|
// 非 JSON stdin:放行
|
|
37
61
|
}
|
|
38
|
-
const
|
|
62
|
+
const toolName = input.tool_name || 'Bash';
|
|
63
|
+
const toolInput = input.tool_input || {};
|
|
64
|
+
if (toolName !== 'Bash') {
|
|
65
|
+
return { allowed: true, command: '', hits: [], toolName, targetPath: toolInput.file_path };
|
|
66
|
+
}
|
|
67
|
+
const command = toolInput.command || '';
|
|
39
68
|
try {
|
|
40
69
|
const gate = new CommandGate();
|
|
41
|
-
|
|
70
|
+
// 与 isAllowed 同一谓词(match):allowed = 无 block 级命中
|
|
71
|
+
const hits = gate.match(command);
|
|
72
|
+
return { allowed: !hits.some((r) => r.level === 'block'), command, hits, toolName };
|
|
42
73
|
} catch {
|
|
43
74
|
// CommandGate 加载/判定异常:fail-open
|
|
44
|
-
return { allowed: true, command };
|
|
75
|
+
return { allowed: true, command, hits: [], toolName };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** 黑名单级别 → trace severity 映射(词表不动 types/trace.ts) */
|
|
80
|
+
const SEVERITY_BY_LEVEL = {
|
|
81
|
+
block: 'error',
|
|
82
|
+
warn: 'warning',
|
|
83
|
+
audit: 'info',
|
|
84
|
+
} as const;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 判定留痕:非 Bash 工具事件一条 `tool-event:<tool>`(P1-5),Bash 命中一条
|
|
88
|
+
* 汇总 trace(P1-7)。
|
|
89
|
+
*
|
|
90
|
+
* 口径选择记理由:
|
|
91
|
+
* - constraintId 用汇总的 'command-gate' 而非每规则 'command-gate:<ruleId>'——
|
|
92
|
+
* usage-report 按 constraintId 聚合,逐规则会碎成几十个近零样本 id,
|
|
93
|
+
* 污染退役候选诊断;规则明细进 evidence。
|
|
94
|
+
* - evidence 只记规则 id + 命令首 token,不记完整命令文本——命令可能含敏感参数
|
|
95
|
+
* (token/路径),首 token 足以定位命令类别。
|
|
96
|
+
* - 隐式依赖:TraceCollector 缺省按 cwd 解析 projectPath,hook 进程 cwd 通常是
|
|
97
|
+
* 项目根(provider 在项目根起会话),可接受;写错地方也不拦主路径。
|
|
98
|
+
*/
|
|
99
|
+
function recordHookTrace(decision: PreToolUseDecision): void {
|
|
100
|
+
try {
|
|
101
|
+
// P1-5:非 Bash 工具事件只留痕不拦截(拦截归 codex 沙箱)——constraintId
|
|
102
|
+
// 按工具名分(tool-event:<tool>),工具集合有界、不会碎片化聚合;evidence
|
|
103
|
+
// 记工具名 + 目标路径类字段(file_path),不记内容正文。
|
|
104
|
+
if (decision.toolName !== 'Bash') {
|
|
105
|
+
new TraceCollector().record({
|
|
106
|
+
constraintId: `tool-event:${decision.toolName}`,
|
|
107
|
+
severity: 'info',
|
|
108
|
+
timestamp: Date.now(),
|
|
109
|
+
result: 'pass',
|
|
110
|
+
operation: 'pretool-use-hook',
|
|
111
|
+
evidence: [
|
|
112
|
+
`tool:${decision.toolName}`,
|
|
113
|
+
...(decision.targetPath ? [`path:${decision.targetPath}`] : []),
|
|
114
|
+
],
|
|
115
|
+
});
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (decision.hits.length === 0) return;
|
|
119
|
+
const maxLevel = decision.hits.some((r) => r.level === 'block')
|
|
120
|
+
? 'block'
|
|
121
|
+
: decision.hits.some((r) => r.level === 'warn')
|
|
122
|
+
? 'warn'
|
|
123
|
+
: 'audit';
|
|
124
|
+
const firstToken = decision.command.trim().split(/\s+/)[0] || '';
|
|
125
|
+
new TraceCollector().record({
|
|
126
|
+
constraintId: 'command-gate',
|
|
127
|
+
severity: SEVERITY_BY_LEVEL[maxLevel],
|
|
128
|
+
timestamp: Date.now(),
|
|
129
|
+
result: decision.allowed ? 'pass' : 'fail',
|
|
130
|
+
operation: 'pretool-use-hook',
|
|
131
|
+
evidence: [
|
|
132
|
+
...decision.hits.map((r) => `rule:${r.id}`),
|
|
133
|
+
...(firstToken ? [`cmd:${firstToken}`] : []),
|
|
134
|
+
],
|
|
135
|
+
});
|
|
136
|
+
} catch {
|
|
137
|
+
// 留痕失败不影响拦截主路径(fail-open 口径与 shim 一致)
|
|
45
138
|
}
|
|
46
139
|
}
|
|
47
140
|
|
|
@@ -50,9 +143,10 @@ export function decidePreToolUse(rawStdin: string): { allowed: boolean; command:
|
|
|
50
143
|
* 阻断时原因写 stderr(provider 把 stderr 作为阻断原因回填模型)。
|
|
51
144
|
*/
|
|
52
145
|
export function runPreToolUseHook(rawStdin: string): number {
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
146
|
+
const decision = decidePreToolUse(rawStdin);
|
|
147
|
+
recordHookTrace(decision);
|
|
148
|
+
if (!decision.allowed) {
|
|
149
|
+
console.error(`[${HOOK_MARKER}] blocked: ${decision.command}`);
|
|
56
150
|
return 2;
|
|
57
151
|
}
|
|
58
152
|
return 0;
|