@dommaker/harness 1.13.0 → 1.14.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 +11 -0
- package/dist/cli/commands/check.js +3 -3
- package/dist/cli/commands/check.js.map +1 -1
- package/dist/core/app-constraints-loader.d.ts +5 -3
- package/dist/core/app-constraints-loader.d.ts.map +1 -1
- package/dist/core/app-constraints-loader.js +20 -7
- package/dist/core/app-constraints-loader.js.map +1 -1
- package/dist/core/constraints/checker.d.ts +5 -3
- package/dist/core/constraints/checker.d.ts.map +1 -1
- package/dist/core/constraints/checker.js +31 -10
- package/dist/core/constraints/checker.js.map +1 -1
- package/dist/core/constraints/checkers/capability-sync.d.ts.map +1 -1
- package/dist/core/constraints/checkers/capability-sync.js +21 -8
- package/dist/core/constraints/checkers/capability-sync.js.map +1 -1
- package/dist/core/constraints/checkers/context-doc-sync.d.ts.map +1 -1
- package/dist/core/constraints/checkers/context-doc-sync.js +7 -1
- package/dist/core/constraints/checkers/context-doc-sync.js.map +1 -1
- package/dist/core/constraints/checkers/docs-freshness.d.ts.map +1 -1
- package/dist/core/constraints/checkers/docs-freshness.js +41 -26
- package/dist/core/constraints/checkers/docs-freshness.js.map +1 -1
- package/dist/core/constraints/checkers/governance-presence.d.ts +3 -0
- package/dist/core/constraints/checkers/governance-presence.d.ts.map +1 -1
- package/dist/core/constraints/checkers/governance-presence.js +7 -10
- package/dist/core/constraints/checkers/governance-presence.js.map +1 -1
- package/dist/core/constraints/checkers/index.d.ts +6 -6
- package/dist/core/constraints/checkers/index.d.ts.map +1 -1
- package/dist/core/constraints/checkers/index.js +14 -9
- package/dist/core/constraints/checkers/index.js.map +1 -1
- package/dist/core/constraints/checkers/iron-flags.js +1 -1
- package/dist/core/constraints/checkers/iron-flags.js.map +1 -1
- package/dist/core/constraints/checkers/no-hardcoded-credentials.d.ts +11 -4
- package/dist/core/constraints/checkers/no-hardcoded-credentials.d.ts.map +1 -1
- package/dist/core/constraints/checkers/no-hardcoded-credentials.js +117 -30
- package/dist/core/constraints/checkers/no-hardcoded-credentials.js.map +1 -1
- package/dist/core/constraints/checkers/no-test-simplification.d.ts +16 -2
- package/dist/core/constraints/checkers/no-test-simplification.d.ts.map +1 -1
- package/dist/core/constraints/checkers/no-test-simplification.js +115 -15
- package/dist/core/constraints/checkers/no-test-simplification.js.map +1 -1
- package/dist/core/constraints/checkers/templated/exec.d.ts +19 -0
- package/dist/core/constraints/checkers/templated/exec.d.ts.map +1 -0
- package/dist/core/constraints/checkers/templated/exec.js +124 -0
- package/dist/core/constraints/checkers/templated/exec.js.map +1 -0
- package/dist/core/constraints/checkers/templated/index.d.ts +1 -0
- package/dist/core/constraints/checkers/templated/index.d.ts.map +1 -1
- package/dist/core/constraints/checkers/templated/index.js +3 -1
- package/dist/core/constraints/checkers/templated/index.js.map +1 -1
- package/dist/core/constraints/checkers/templates-registry.d.ts +2 -1
- package/dist/core/constraints/checkers/templates-registry.d.ts.map +1 -1
- package/dist/core/constraints/checkers/templates-registry.js +3 -1
- package/dist/core/constraints/checkers/templates-registry.js.map +1 -1
- package/dist/core/constraints/checkers/types.d.ts +67 -6
- package/dist/core/constraints/checkers/types.d.ts.map +1 -1
- package/dist/core/constraints/checkers/types.js +51 -6
- package/dist/core/constraints/checkers/types.js.map +1 -1
- package/dist/core/constraints/definitions/guidelines.d.ts +1 -0
- package/dist/core/constraints/definitions/guidelines.d.ts.map +1 -1
- package/dist/core/constraints/definitions/guidelines.js +4 -0
- package/dist/core/constraints/definitions/guidelines.js.map +1 -1
- package/dist/core/constraints/definitions/iron-laws.d.ts +1 -0
- package/dist/core/constraints/definitions/iron-laws.d.ts.map +1 -1
- package/dist/core/constraints/definitions/iron-laws.js +6 -1
- package/dist/core/constraints/definitions/iron-laws.js.map +1 -1
- package/dist/core/constraints/definitions.d.ts +7 -0
- package/dist/core/constraints/definitions.d.ts.map +1 -1
- package/dist/core/constraints/definitions.js +10 -0
- package/dist/core/constraints/definitions.js.map +1 -1
- package/dist/core/constraints/git-evidence.d.ts +10 -0
- package/dist/core/constraints/git-evidence.d.ts.map +1 -1
- package/dist/core/constraints/git-evidence.js +2 -0
- package/dist/core/constraints/git-evidence.js.map +1 -1
- package/dist/core/constraints/index.d.ts +3 -1
- package/dist/core/constraints/index.d.ts.map +1 -1
- package/dist/core/constraints/index.js +8 -1
- package/dist/core/constraints/index.js.map +1 -1
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +6 -1
- package/dist/core/index.js.map +1 -1
- package/dist/gates/checker-gate.d.ts +5 -1
- package/dist/gates/checker-gate.d.ts.map +1 -1
- package/dist/gates/checker-gate.js +10 -3
- package/dist/gates/checker-gate.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -3
- package/dist/index.js.map +1 -1
- package/dist/types/constraint.d.ts +32 -3
- package/dist/types/constraint.d.ts.map +1 -1
- package/dist/types/constraint.js +6 -1
- package/dist/types/constraint.js.map +1 -1
- package/dist/types/trace.d.ts +7 -0
- package/dist/types/trace.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/CONTEXT.md +1 -1
- package/src/__tests__/app-constraints-loader.test.ts +86 -3
- package/src/__tests__/checker-extra.test.ts +80 -4
- package/src/__tests__/checker-primitives-sdk.test.ts +70 -0
- package/src/__tests__/exec-checker-full-chain.test.ts +118 -0
- package/src/__tests__/public-exports.test.ts +5 -0
- package/src/__tests__/public-type-surface.test.ts +22 -0
- package/src/__tests__/public-value-surface.test.ts +5 -0
- package/src/cli/commands/__tests__/check-skip-output.test.ts +4 -4
- package/src/cli/commands/__tests__/check.test.ts +2 -0
- package/src/cli/commands/check.ts +3 -3
- package/src/core/CONTEXT.md +6 -6
- package/src/core/app-constraints-loader.ts +23 -8
- package/src/core/constraints/__tests__/git-evidence.test.ts +20 -0
- package/src/core/constraints/__tests__/run-env.test.ts +2 -0
- package/src/core/constraints/checker.ts +35 -12
- package/src/core/constraints/checkers/__tests__/capability-sync.test.ts +21 -3
- package/src/core/constraints/checkers/__tests__/context-doc-sync.test.ts +20 -12
- package/src/core/constraints/checkers/__tests__/docs-freshness-degraded.test.ts +89 -0
- package/src/core/constraints/checkers/__tests__/docs-freshness.test.ts +4 -2
- package/src/core/constraints/checkers/__tests__/exec-checker.test.ts +99 -0
- package/src/core/constraints/checkers/__tests__/fixtures/public-repo-sanitization.mjs +40 -0
- package/src/core/constraints/checkers/__tests__/governance-presence.test.ts +23 -2
- package/src/core/constraints/checkers/__tests__/input-contract.test.ts +135 -0
- package/src/core/constraints/checkers/__tests__/iron-flags.test.ts +15 -2
- package/src/core/constraints/checkers/__tests__/no-hardcoded-credentials.test.ts +110 -5
- package/src/core/constraints/checkers/__tests__/no-test-simplification.test.ts +144 -21
- package/src/core/constraints/checkers/__tests__/three-state-consistency.test.ts +6 -2
- package/src/core/constraints/checkers/capability-sync.ts +24 -10
- package/src/core/constraints/checkers/context-doc-sync.ts +8 -1
- package/src/core/constraints/checkers/docs-freshness.ts +44 -26
- package/src/core/constraints/checkers/governance-presence.ts +7 -8
- package/src/core/constraints/checkers/index.ts +22 -10
- package/src/core/constraints/checkers/iron-flags.ts +1 -1
- package/src/core/constraints/checkers/no-hardcoded-credentials.ts +135 -29
- package/src/core/constraints/checkers/no-test-simplification.ts +128 -17
- package/src/core/constraints/checkers/templated/exec.ts +138 -0
- package/src/core/constraints/checkers/templated/index.ts +1 -0
- package/src/core/constraints/checkers/templates-registry.ts +4 -2
- package/src/core/constraints/checkers/types.ts +106 -9
- package/src/core/constraints/definitions/guidelines.ts +4 -0
- package/src/core/constraints/definitions/iron-laws.ts +6 -1
- package/src/core/constraints/definitions.ts +10 -0
- package/src/core/constraints/git-evidence.ts +13 -0
- package/src/core/constraints/index.ts +22 -0
- package/src/core/index.ts +16 -0
- package/src/gates/CONTEXT.md +1 -1
- package/src/gates/__tests__/checker-gate.test.ts +19 -2
- package/src/gates/checker-gate.ts +11 -4
- package/src/index.ts +26 -0
- package/src/types/constraint.ts +35 -3
- package/src/types/trace.ts +8 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* exec 模板(harness#181,ADR-0035 决策 3 探测器来源 exec 档)测试
|
|
3
|
+
*
|
|
4
|
+
* 执行协议:params.command 以 env.projectPath 为 cwd 执行,退出码 0 = 满足、非 0 = 违规,
|
|
5
|
+
* stdout/stderr 行进证据。保护面:超时即杀 + 输出截断(命令失控不拖死检查流程)。
|
|
6
|
+
* 范式同 templated-checkers.test.ts:测面是 ConstraintCheck.evaluate(env),
|
|
7
|
+
* exec 不吃 git 证据,env 经 buildCheckEnv(context, 'none') 构造。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, it, expect } from '@jest/globals';
|
|
11
|
+
import { execScript } from '../templated/exec';
|
|
12
|
+
import { TEMPLATES } from '../index';
|
|
13
|
+
import { buildCheckEnv, normalizeCheckOutcome, type CheckEnv, type CheckOutcome } from '../types';
|
|
14
|
+
import { createProjectFixture } from '../../../../test-setup/project-fixture';
|
|
15
|
+
import type { ConstraintContext } from '../../../../types/constraint';
|
|
16
|
+
|
|
17
|
+
function makeEnv(projectPath: string): CheckEnv {
|
|
18
|
+
const context: ConstraintContext = { operation: 'commit', projectPath };
|
|
19
|
+
return buildCheckEnv(context, 'none');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const passed = (outcome: CheckOutcome): boolean => normalizeCheckOutcome(outcome).satisfied;
|
|
23
|
+
const evidenceText = (outcome: CheckOutcome): string =>
|
|
24
|
+
normalizeCheckOutcome(outcome).evidence.join('\n');
|
|
25
|
+
|
|
26
|
+
describe('exec 模板注册与参数校验', () => {
|
|
27
|
+
it('已注册进 TEMPLATES', () => {
|
|
28
|
+
expect(TEMPLATES.get('exec')).toBe(execScript);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('validateParams:command 必填非空字符串', () => {
|
|
32
|
+
expect(execScript.validateParams({}).join()).toContain('command');
|
|
33
|
+
expect(execScript.validateParams({ command: '' }).join()).toContain('command');
|
|
34
|
+
expect(execScript.validateParams({ command: 1 }).join()).toContain('command');
|
|
35
|
+
expect(execScript.validateParams({ command: 'true' })).toEqual([]);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('validateParams:timeoutMs 可选,须为正数且不超上限', () => {
|
|
39
|
+
expect(execScript.validateParams({ command: 'true', timeoutMs: '1' }).join()).toContain('timeoutMs');
|
|
40
|
+
expect(execScript.validateParams({ command: 'true', timeoutMs: 0 }).join()).toContain('timeoutMs');
|
|
41
|
+
expect(execScript.validateParams({ command: 'true', timeoutMs: -5 }).join()).toContain('timeoutMs');
|
|
42
|
+
expect(execScript.validateParams({ command: 'true', timeoutMs: 999_999_999 }).join()).toContain('timeoutMs');
|
|
43
|
+
expect(execScript.validateParams({ command: 'true', timeoutMs: 5000 })).toEqual([]);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('exec evaluate', () => {
|
|
48
|
+
it('退出码 0 → 满足', async () => {
|
|
49
|
+
const dir = createProjectFixture({ name: 'exec-pass' });
|
|
50
|
+
const check = execScript.create('app_x', { command: 'node -e "process.exit(0)"' });
|
|
51
|
+
expect(passed(await check.evaluate(makeEnv(dir)))).toBe(true);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('非零退出 → 违规,证据含退出码与 stdout/stderr 行', async () => {
|
|
55
|
+
const dir = createProjectFixture({ name: 'exec-fail' });
|
|
56
|
+
const command = `node -e "console.log('out-line'); console.error('err-line'); process.exit(3)"`;
|
|
57
|
+
const outcome = await execScript.create('app_x', { command }).evaluate(makeEnv(dir));
|
|
58
|
+
expect(passed(outcome)).toBe(false);
|
|
59
|
+
const text = evidenceText(outcome);
|
|
60
|
+
expect(text).toContain('退出码 3');
|
|
61
|
+
expect(text).toContain('out-line');
|
|
62
|
+
expect(text).toContain('err-line');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('cwd = env.projectPath(脚本拿到的项目根即约束目标项目)', async () => {
|
|
66
|
+
const dir = createProjectFixture({ name: 'exec-cwd' });
|
|
67
|
+
const command = 'node -e "console.log(process.cwd()); process.exit(1)"';
|
|
68
|
+
const text = evidenceText(await execScript.create('app_x', { command }).evaluate(makeEnv(dir)));
|
|
69
|
+
expect(text).toContain(dir);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('超时 → 杀死进程并判违规(命令失控不拖死检查流程)', async () => {
|
|
73
|
+
const dir = createProjectFixture({ name: 'exec-timeout' });
|
|
74
|
+
const check = execScript.create('app_x', {
|
|
75
|
+
command: 'node -e "setInterval(() => {}, 1000)"',
|
|
76
|
+
timeoutMs: 500,
|
|
77
|
+
});
|
|
78
|
+
const outcome = await check.evaluate(makeEnv(dir));
|
|
79
|
+
expect(passed(outcome)).toBe(false);
|
|
80
|
+
expect(evidenceText(outcome)).toContain('超时');
|
|
81
|
+
}, 15000);
|
|
82
|
+
|
|
83
|
+
it('输出截断保护:超长 stdout/stderr 不整段进证据', async () => {
|
|
84
|
+
const dir = createProjectFixture({ name: 'exec-truncate' });
|
|
85
|
+
const command = `node -e "for (let i = 0; i < 2000; i++) console.log('line-' + i + '-'.repeat(50)); process.exit(1)"`;
|
|
86
|
+
const outcome = await execScript.create('app_x', { command }).evaluate(makeEnv(dir));
|
|
87
|
+
expect(passed(outcome)).toBe(false);
|
|
88
|
+
expect(evidenceText(outcome)).toContain('截断');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('命令不存在(shell 退出 127)→ 违规而非静默通过', async () => {
|
|
92
|
+
const dir = createProjectFixture({ name: 'exec-enoent' });
|
|
93
|
+
const outcome = await execScript
|
|
94
|
+
.create('app_x', { command: 'definitely-not-a-real-command-xyz' })
|
|
95
|
+
.evaluate(makeEnv(dir));
|
|
96
|
+
expect(passed(outcome)).toBe(false);
|
|
97
|
+
expect(evidenceText(outcome)).toContain('退出码 127');
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* 示例业务检查器(harness#181 验收示例):public-repo-sanitization
|
|
4
|
+
*
|
|
5
|
+
* 场景原型 = studio 侧安全底线 public_repo_sanitization(ADR-0032 ①):公开发布仓的
|
|
6
|
+
* 受跟踪文本不得残留开发机现场绝对路径(/Users/<name>/、/home/<name>/),文档示例的
|
|
7
|
+
* 占位符形态(/Users/<you>/ 等尖括号段)豁免。
|
|
8
|
+
*
|
|
9
|
+
* 模板填不了的点:命中判定带豁免谓词,regex-scan「命中即违规」表达不了——正是
|
|
10
|
+
* ADR-0035 决策 3 的 exec 档场景。
|
|
11
|
+
*
|
|
12
|
+
* 协议(与 exec 模板约定):argv = 待扫文件(相对项目根;exec 模板以项目根为 cwd 执行);
|
|
13
|
+
* 命中逐条打印到 stdout 并 exit 1;干净 exit 0。业务知识不出业务仓:本脚本即业务判定本体。
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { readFileSync } from 'node:fs';
|
|
17
|
+
import { join } from 'node:path';
|
|
18
|
+
|
|
19
|
+
const MACHINE_PATH = /\/(?:Users|home)\/([^/\s'"]+)\//g;
|
|
20
|
+
|
|
21
|
+
const hits = [];
|
|
22
|
+
for (const rel of process.argv.slice(2)) {
|
|
23
|
+
let content;
|
|
24
|
+
try {
|
|
25
|
+
content = readFileSync(join(process.cwd(), rel), 'utf-8');
|
|
26
|
+
} catch {
|
|
27
|
+
continue; // 文件不可读(已删除/二进制)→ 跳过
|
|
28
|
+
}
|
|
29
|
+
content.split('\n').forEach((line, i) => {
|
|
30
|
+
for (const m of line.matchAll(MACHINE_PATH)) {
|
|
31
|
+
if (m[1].startsWith('<')) continue; // 占位符豁免:/Users/<you>/
|
|
32
|
+
hits.push(`${rel}:${i + 1} 残留开发机现场路径 ${m[0]}`);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (hits.length > 0) {
|
|
38
|
+
for (const hit of hits) console.log(hit);
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
@@ -59,8 +59,29 @@ describe('governance_presence checker', () => {
|
|
|
59
59
|
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
-
it('未采用 harness 治理(无 .harness/config.yml)→ skip', async () => {
|
|
63
|
-
|
|
62
|
+
it('未采用 harness 治理(无 .harness/config.yml)→ 带原因的 skip(harness#182)', async () => {
|
|
63
|
+
const outcome = normalizeCheckOutcome(await governancePresence.evaluate(makeEnv(tempDir)));
|
|
64
|
+
expect(outcome.skipped).toBe(true);
|
|
65
|
+
expect(outcome.skipReason).toContain('未采用 harness 治理');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('config.yml YAML 损坏 → 仍按「已采用」评估(harness#182:不再恰在配置出问题时闭嘴)', async () => {
|
|
69
|
+
fs.mkdirSync(path.join(tempDir, '.harness'), { recursive: true });
|
|
70
|
+
fs.writeFileSync(path.join(tempDir, '.harness', 'config.yml'), 'preset: [broken\n - {unclosed');
|
|
71
|
+
// 两处都没有治理契约 → fail(旧行为:解析抛错被吞 → 误判未采用 → skip)
|
|
72
|
+
const outcome = normalizeCheckOutcome(await governancePresence.evaluate(makeEnv(tempDir)));
|
|
73
|
+
expect(outcome.skipped).toBe(false);
|
|
74
|
+
expect(outcome.satisfied).toBe(false);
|
|
75
|
+
expect(outcome.evidence.join('\n')).toContain('治理契约');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('config.yml YAML 损坏 + AGENTS.md 有 PRESERVE:governance → pass(损坏配置不影响在场判定)', async () => {
|
|
79
|
+
fs.mkdirSync(path.join(tempDir, '.harness'), { recursive: true });
|
|
80
|
+
fs.writeFileSync(path.join(tempDir, '.harness', 'config.yml'), 'preset: [broken\n - {unclosed');
|
|
81
|
+
fs.writeFileSync(path.join(tempDir, 'AGENTS.md'), `# AGENTS.md\n\n${GOVERNANCE_BLOCK}\n`);
|
|
82
|
+
const outcome = normalizeCheckOutcome(await governancePresence.evaluate(makeEnv(tempDir)));
|
|
83
|
+
expect(outcome.skipped).toBe(false);
|
|
84
|
+
expect(outcome.satisfied).toBe(true);
|
|
64
85
|
});
|
|
65
86
|
|
|
66
87
|
it('新模型:AGENTS.md 有非空 PRESERVE:governance 段 → pass', async () => {
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 检查器输入契约测试(harness#182)
|
|
3
|
+
*
|
|
4
|
+
* 「检查器失效 ≠ 对象合规」的接缝锁定:
|
|
5
|
+
* - findMissingInputs:checker 声明的 needs 与环境供给比对(缺证据 / flag 未接线)
|
|
6
|
+
* - 编排层降级:git 取证失败 → 带原因的 skipped,不进入 evaluate(不再对空 diff 假 pass)
|
|
7
|
+
* - skipReason 落 ConstraintResult 与 ExecutionTrace(可统计的失效信号)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { describe, it, expect } from '@jest/globals';
|
|
11
|
+
import { ConstraintChecker } from '../../checker';
|
|
12
|
+
import { createGitEvidence } from '../../git-evidence';
|
|
13
|
+
import {
|
|
14
|
+
buildCheckEnv,
|
|
15
|
+
findMissingInputs,
|
|
16
|
+
normalizeCheckOutcome,
|
|
17
|
+
type ConstraintCheck,
|
|
18
|
+
} from '../types';
|
|
19
|
+
import { noTestSimplification } from '../no-test-simplification';
|
|
20
|
+
import type { Constraint, ConstraintContext } from '../../../../types/constraint';
|
|
21
|
+
import type { MergedConstraintsConfig } from '../../../../types/project-config';
|
|
22
|
+
import type { ExecutionTrace } from '../../../../types/trace';
|
|
23
|
+
|
|
24
|
+
const NO_TEST_SIMPLIFICATION: Constraint = {
|
|
25
|
+
id: 'no_test_simplification',
|
|
26
|
+
kind: 'check',
|
|
27
|
+
severity: 'error',
|
|
28
|
+
rule: 'DO NOT DELETE TESTS',
|
|
29
|
+
message: '禁止简化测试绕过困难',
|
|
30
|
+
trigger: 'commit',
|
|
31
|
+
enforcement: 'restore-tests',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const CONTEXT: ConstraintContext = { operation: 'commit', projectPath: '/nonexistent' };
|
|
35
|
+
|
|
36
|
+
function envOf(context: ConstraintContext, available: Record<string, boolean>) {
|
|
37
|
+
return buildCheckEnv(context, {
|
|
38
|
+
stagedDiff: async () => '',
|
|
39
|
+
stagedDiffNames: async () => '',
|
|
40
|
+
srcScan: () => [],
|
|
41
|
+
available: (input) => available[input] ?? true,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('findMissingInputs — needs 与环境供给比对', () => {
|
|
46
|
+
it('未声明 needs → 无缺项', () => {
|
|
47
|
+
expect(findMissingInputs({ id: 'x', evaluate: () => true }, envOf(CONTEXT, {}))).toEqual([]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('evidenceAvailable 报不可得 → 缺项自描述', () => {
|
|
51
|
+
const missing = findMissingInputs(noTestSimplification, envOf(CONTEXT, { stagedDiff: false }));
|
|
52
|
+
expect(missing).toEqual(['staged diff 不可得(git 取证失败或未接线)']);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('contextFlags 为 undefined → 缺项点名标志', () => {
|
|
56
|
+
const check: ConstraintCheck = {
|
|
57
|
+
id: 'flag-check',
|
|
58
|
+
needs: { contextFlags: ['hasVerificationEvidence'] },
|
|
59
|
+
evaluate: () => true,
|
|
60
|
+
};
|
|
61
|
+
expect(findMissingInputs(check, envOf(CONTEXT, {}))).toEqual([
|
|
62
|
+
'证据标志 hasVerificationEvidence 未接线',
|
|
63
|
+
]);
|
|
64
|
+
expect(
|
|
65
|
+
findMissingInputs(check, envOf({ ...CONTEXT, hasVerificationEvidence: false }, {}))
|
|
66
|
+
).toEqual([]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('手写 env 未提供 evidenceAvailable → 按全部可得(旧形状兼容)', () => {
|
|
70
|
+
const env = buildCheckEnv(CONTEXT, {
|
|
71
|
+
stagedDiff: async () => '',
|
|
72
|
+
stagedDiffNames: async () => '',
|
|
73
|
+
srcScan: () => [],
|
|
74
|
+
});
|
|
75
|
+
expect(findMissingInputs(noTestSimplification, env)).toEqual([]);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('normalizeCheckOutcome — CheckSkip 归一', () => {
|
|
80
|
+
it('{ skip, reason } → skipped + skipReason,evidence 恒空(未评估不留证据行)', () => {
|
|
81
|
+
expect(normalizeCheckOutcome({ skip: true, reason: '输入不可得' })).toEqual({
|
|
82
|
+
satisfied: true,
|
|
83
|
+
skipped: true,
|
|
84
|
+
evidence: [],
|
|
85
|
+
skipReason: '输入不可得',
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
/** 只含 no_test_simplification 的生效集:一次 run 一条 trace */
|
|
91
|
+
function onlyNoTestSimplification(): MergedConstraintsConfig {
|
|
92
|
+
return { constraints: { no_test_simplification: NO_TEST_SIMPLIFICATION }, disabled: [] };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
describe('编排层输入契约降级(git 取证失败 → 带原因的 skipped)', () => {
|
|
96
|
+
const throwingRunner = () => {
|
|
97
|
+
throw new Error('not a git repository');
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
it('git 失败 → skipped + skipReason,不阻断(fail-open 但不假 pass)', async () => {
|
|
101
|
+
const checker = new ConstraintChecker();
|
|
102
|
+
const git = createGitEvidence('/nonexistent', throwingRunner);
|
|
103
|
+
|
|
104
|
+
const result = await checker.check(NO_TEST_SIMPLIFICATION, CONTEXT, git);
|
|
105
|
+
|
|
106
|
+
expect(result.skipped).toBe(true);
|
|
107
|
+
expect(result.satisfied).toBe(true);
|
|
108
|
+
expect(result.skipReason).toContain('staged diff 不可得');
|
|
109
|
+
expect(result.message).toContain('staged diff 不可得');
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('git 失败 → trace 记 result:skip 且携带 skipReason(失效可统计)', async () => {
|
|
113
|
+
const traces: ExecutionTrace[] = [];
|
|
114
|
+
const checker = new ConstraintChecker({ record: (t) => traces.push(t) });
|
|
115
|
+
const git = createGitEvidence('/nonexistent', throwingRunner);
|
|
116
|
+
|
|
117
|
+
const run = await checker.checkConstraints(CONTEXT, onlyNoTestSimplification(), git);
|
|
118
|
+
|
|
119
|
+
expect(run.passed).toBe(true);
|
|
120
|
+
expect(run.errors[0].skipped).toBe(true);
|
|
121
|
+
const trace = traces.find(t => t.constraintId === 'no_test_simplification')!;
|
|
122
|
+
expect(trace.result).toBe('skip');
|
|
123
|
+
expect(trace.skipReason).toContain('staged diff 不可得');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('git 正常 → 进入评估(对照:契约齐备不降级)', async () => {
|
|
127
|
+
const checker = new ConstraintChecker();
|
|
128
|
+
const git = createGitEvidence('/nonexistent', () => '');
|
|
129
|
+
|
|
130
|
+
const result = await checker.check(NO_TEST_SIMPLIFICATION, CONTEXT, git);
|
|
131
|
+
|
|
132
|
+
expect(result.skipped).toBeUndefined();
|
|
133
|
+
expect(result.satisfied).toBe(true);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
@@ -21,10 +21,23 @@ const evaluate = (
|
|
|
21
21
|
|
|
22
22
|
describe('iron flags — 三态与字段映射', () => {
|
|
23
23
|
it('no_completion_without_verification → 读 hasVerificationEvidence', async () => {
|
|
24
|
-
|
|
24
|
+
// 未接线 → 带原因的 skip(harness#182:undefined flag 不再静默 skip)
|
|
25
|
+
expect(await evaluate(noCompletionWithoutVerification, {})).toEqual({
|
|
26
|
+
skip: true,
|
|
27
|
+
reason: '证据标志 hasVerificationEvidence 未接线',
|
|
28
|
+
});
|
|
25
29
|
expect(await evaluate(noCompletionWithoutVerification, { hasVerificationEvidence: false })).toBe(false);
|
|
26
30
|
expect(await evaluate(noCompletionWithoutVerification, { hasVerificationEvidence: true })).toBe(true);
|
|
27
31
|
// 相邻字段不串台:hasTest 不能顶替
|
|
28
|
-
expect(await evaluate(noCompletionWithoutVerification, { hasTest: true })).
|
|
32
|
+
expect(await evaluate(noCompletionWithoutVerification, { hasTest: true })).toEqual({
|
|
33
|
+
skip: true,
|
|
34
|
+
reason: '证据标志 hasVerificationEvidence 未接线',
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('声明 contextFlags 输入契约(harness#182):编排层据此在 evaluate 前降级', () => {
|
|
39
|
+
expect(noCompletionWithoutVerification.needs).toEqual({
|
|
40
|
+
contextFlags: ['hasVerificationEvidence'],
|
|
41
|
+
});
|
|
29
42
|
});
|
|
30
43
|
});
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* no_hardcoded_credentials 检查器旁测(ADR-0001
|
|
2
|
+
* no_hardcoded_credentials 检查器旁测(ADR-0001;harness#184 收窄正则版 + fail 证据行)
|
|
3
3
|
*
|
|
4
4
|
* 注意:测试夹具中的假凭证一律用字符串拼接构造,
|
|
5
5
|
* 避免本文件自身被凭证扫描(含 harness 自身 staged diff)命中。
|
|
6
6
|
* 测试面 = evaluate(env)(架构评审候选4,自 src/__tests__/ 就近迁入)。
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import { mkdtempSync, writeFileSync, rmSync } from 'fs';
|
|
10
|
+
import { tmpdir } from 'os';
|
|
11
|
+
import { join } from 'path';
|
|
12
|
+
import { describe, it, expect, afterAll } from '@jest/globals';
|
|
10
13
|
import {
|
|
11
14
|
noHardcodedCredentials,
|
|
12
15
|
containsHardcodedCredential,
|
|
@@ -27,6 +30,8 @@ function makeEnv(stagedDiff: string): CheckEnv {
|
|
|
27
30
|
const AWS_KEY = 'AKIA' + '1234567890ABCDEF';
|
|
28
31
|
const PEM_HEADER = '-----BEGIN ' + 'RSA PRIVATE KEY-----';
|
|
29
32
|
const PASSWORD_ASSIGN = 'const dbPass' + 'word = "sup3r-s3cret-value";';
|
|
33
|
+
const JWT =
|
|
34
|
+
'eyJ' + 'hbGciOiJIUzI1NiJ9.' + 'eyJ' + 'zdWIiOiIxMjM0NTY3ODkwIn0.' + 'dozjgNryP4J3jVmNHl0w5N_XgL0n';
|
|
30
35
|
|
|
31
36
|
describe('containsHardcodedCredential', () => {
|
|
32
37
|
it('命中 PEM 私钥头', () => {
|
|
@@ -45,6 +50,38 @@ describe('containsHardcodedCredential', () => {
|
|
|
45
50
|
expect(containsHardcodedCredential('api' + '_key: "abcdef123456789"')).toBe(true);
|
|
46
51
|
});
|
|
47
52
|
|
|
53
|
+
it('命中 .env 裸值(无引号赋值)', () => {
|
|
54
|
+
expect(containsHardcodedCredential('DB_TO' + 'KEN=abc123def456ghi')).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('命中无引号 YAML 值', () => {
|
|
58
|
+
expect(containsHardcodedCredential('pass' + 'word: secret123')).toBe(true);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('命中 URL 内嵌凭证', () => {
|
|
62
|
+
expect(containsHardcodedCredential('const url = "postgres:/' + '/user:pw12345@db.host/app";')).toBe(true);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('命中 JSON 带引号键', () => {
|
|
66
|
+
expect(containsHardcodedCredential('"pass' + 'word": "realvalue123"')).toBe(true);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('命中非清单变量名 credential/privateKey/signingKey', () => {
|
|
70
|
+
expect(containsHardcodedCredential('cred' + 'ential = "realvalue123";')).toBe(true);
|
|
71
|
+
expect(containsHardcodedCredential('private' + 'Key = "realvalue123";')).toBe(true);
|
|
72
|
+
expect(containsHardcodedCredential('signing' + '_key: realvalue123')).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('命中 JWT', () => {
|
|
76
|
+
expect(containsHardcodedCredential(`const tok = "${JWT}";`)).toBe(true);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('行内出现 test 不再整行豁免真实值', () => {
|
|
80
|
+
expect(
|
|
81
|
+
containsHardcodedCredential('const pass' + 'word = "realvalue123"; // test fixture')
|
|
82
|
+
).toBe(true);
|
|
83
|
+
});
|
|
84
|
+
|
|
48
85
|
it('豁免环境变量引用', () => {
|
|
49
86
|
expect(
|
|
50
87
|
containsHardcodedCredential('const pass' + 'word = process.env.DB_PASSWORD;')
|
|
@@ -57,6 +94,16 @@ describe('containsHardcodedCredential', () => {
|
|
|
57
94
|
).toBe(false);
|
|
58
95
|
});
|
|
59
96
|
|
|
97
|
+
it('豁免 TS 类型注解(值是类型名而非字面量)', () => {
|
|
98
|
+
expect(containsHardcodedCredential('api' + 'Key: string|undefined;')).toBe(false);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('豁免 URL 中的变量插值占位', () => {
|
|
102
|
+
expect(
|
|
103
|
+
containsHardcodedCredential('postgres://user:' + '${' + 'DB_PASS}@host/db')
|
|
104
|
+
).toBe(false);
|
|
105
|
+
});
|
|
106
|
+
|
|
60
107
|
it('普通代码不误报', () => {
|
|
61
108
|
expect(
|
|
62
109
|
containsHardcodedCredential('export function add(a: number, b: number) { return a + b; }')
|
|
@@ -65,15 +112,22 @@ describe('containsHardcodedCredential', () => {
|
|
|
65
112
|
});
|
|
66
113
|
|
|
67
114
|
describe('noHardcodedCredentials checker', () => {
|
|
68
|
-
it('staged diff 新增行命中凭证 → fail', async () => {
|
|
115
|
+
it('staged diff 新增行命中凭证 → fail,证据带文件+行号', async () => {
|
|
69
116
|
const diff = [
|
|
70
117
|
'diff --git a/src/config.ts b/src/config.ts',
|
|
71
118
|
'--- a/src/config.ts',
|
|
72
119
|
'+++ b/src/config.ts',
|
|
73
|
-
'@@ -
|
|
120
|
+
'@@ -3,3 +3,4 @@',
|
|
121
|
+
' line3',
|
|
122
|
+
'+line4',
|
|
74
123
|
'+' + PASSWORD_ASSIGN,
|
|
75
124
|
].join('\n');
|
|
76
|
-
|
|
125
|
+
const outcome = await noHardcodedCredentials.evaluate(makeEnv(diff));
|
|
126
|
+
expect(outcome).not.toBe(true);
|
|
127
|
+
expect(typeof outcome).toBe('object');
|
|
128
|
+
const detail = outcome as { pass: boolean; evidence?: string[] };
|
|
129
|
+
expect(detail.pass).toBe(false);
|
|
130
|
+
expect(detail.evidence?.some(l => l.includes('src/config.ts:5'))).toBe(true);
|
|
77
131
|
});
|
|
78
132
|
|
|
79
133
|
it('staged diff 仅删除行含凭证 → pass(删除凭证是好事)', async () => {
|
|
@@ -89,4 +143,55 @@ describe('noHardcodedCredentials checker', () => {
|
|
|
89
143
|
it('staged diff 为空且无可读变更文件 → pass', async () => {
|
|
90
144
|
expect(await noHardcodedCredentials.evaluate(makeEnv(''))).toBe(true);
|
|
91
145
|
});
|
|
146
|
+
|
|
147
|
+
it('diff 路径按文件过滤:.env.example 的真实值形态不误报', async () => {
|
|
148
|
+
const diff = [
|
|
149
|
+
'diff --git a/.env.example b/.env.example',
|
|
150
|
+
'--- a/.env.example',
|
|
151
|
+
'+++ b/.env.example',
|
|
152
|
+
'@@ -0,0 +1 @@',
|
|
153
|
+
'+DB_TO' + 'KEN=abc123def456ghi',
|
|
154
|
+
].join('\n');
|
|
155
|
+
expect(await noHardcodedCredentials.evaluate(makeEnv(diff))).toBe(true);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('diff 路径按文件过滤:文档中的 PEM 头样例不误报', async () => {
|
|
159
|
+
const diff = [
|
|
160
|
+
'diff --git a/docs/keys.md b/docs/keys.md',
|
|
161
|
+
'--- a/docs/keys.md',
|
|
162
|
+
'+++ b/docs/keys.md',
|
|
163
|
+
'@@ -0,0 +1 @@',
|
|
164
|
+
'+' + PEM_HEADER,
|
|
165
|
+
].join('\n');
|
|
166
|
+
expect(await noHardcodedCredentials.evaluate(makeEnv(diff))).toBe(true);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe('diff 不可用时退化为变更文件内容扫描', () => {
|
|
170
|
+
const dir = mkdtempSync(join(tmpdir(), 'harness-cred-test-'));
|
|
171
|
+
afterAll(() => rmSync(dir, { recursive: true, force: true }));
|
|
172
|
+
|
|
173
|
+
function contentEnv(files: string[]): CheckEnv {
|
|
174
|
+
return {
|
|
175
|
+
...createRunEnv(dir),
|
|
176
|
+
context: { operation: 'commit', projectPath: dir, changedFiles: files },
|
|
177
|
+
stagedDiff: async () => '',
|
|
178
|
+
stagedDiffNames: async () => '',
|
|
179
|
+
srcScan: () => [],
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
it('内容路径命中 → fail,证据带文件+行号', async () => {
|
|
184
|
+
writeFileSync(join(dir, '.env'), '# comment\nDB_TO' + 'KEN=abc123def456ghi\n');
|
|
185
|
+
const outcome = await noHardcodedCredentials.evaluate(contentEnv(['.env']));
|
|
186
|
+
expect(typeof outcome).toBe('object');
|
|
187
|
+
const detail = outcome as { pass: boolean; evidence?: string[] };
|
|
188
|
+
expect(detail.pass).toBe(false);
|
|
189
|
+
expect(detail.evidence?.some(l => l.includes('.env:2'))).toBe(true);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('内容路径同样豁免模板文件', async () => {
|
|
193
|
+
writeFileSync(join(dir, '.env.example'), 'DB_TO' + 'KEN=abc123def456ghi\n');
|
|
194
|
+
expect(await noHardcodedCredentials.evaluate(contentEnv(['.env.example']))).toBe(true);
|
|
195
|
+
});
|
|
196
|
+
});
|
|
92
197
|
});
|