dsh-ruleset 0.0.0-stage → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/DEVELOPMENT.md +121 -0
- package/LICENSE +21 -0
- package/README.md +64 -2
- package/README.zh.md +131 -0
- package/client.js +822 -0
- package/cordis.patch.yml +10 -0
- package/docs/PREDICATE-PLAYBOOK.md +42 -0
- package/docs-schema.md +38 -0
- package/examples/policy.example.yaml +50 -0
- package/examples/policy.full.yaml +162 -0
- package/examples/policy.starter.yaml +65 -0
- package/lib/backups.js +77 -0
- package/lib/commands.js +38 -0
- package/lib/dedupe.js +22 -0
- package/lib/explain.js +28 -0
- package/lib/export.js +33 -0
- package/lib/extract.js +104 -0
- package/lib/facts.js +250 -0
- package/lib/findings.js +101 -0
- package/lib/guard.js +76 -0
- package/lib/history.js +71 -0
- package/lib/hygiene.js +86 -0
- package/lib/index.js +70 -0
- package/lib/init.js +29 -0
- package/lib/inject.js +64 -0
- package/lib/injected-log.js +30 -0
- package/lib/introspect.js +55 -0
- package/lib/judge.js +73 -0
- package/lib/llm-parse.js +131 -0
- package/lib/observe.js +132 -0
- package/lib/policy.js +57 -0
- package/lib/predicates.js +444 -0
- package/lib/report.js +66 -0
- package/lib/routes.js +62 -0
- package/lib/rules-edit.js +140 -0
- package/lib/run.js +109 -0
- package/lib/service.js +91 -0
- package/lib/source.js +93 -0
- package/lib/stats.js +63 -0
- package/lib/validate.js +73 -0
- package/locale/en.json +1 -0
- package/locale/zh.json +1 -0
- package/package.json +68 -3
- package/schema/policy.schema.json +41 -0
- package/test/dedupe.test.mjs +19 -0
- package/test/guard.test.mjs +30 -0
- package/test/load.test.mjs +21 -0
- package/test/predicates.test.mjs +49 -0
- package/test/source.test.mjs +32 -0
- package/test/validate.test.mjs +33 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"title": "dsh-ruleset policy",
|
|
4
|
+
"type": "object",
|
|
5
|
+
"required": ["version", "rules"],
|
|
6
|
+
"properties": {
|
|
7
|
+
"version": { "type": "integer" },
|
|
8
|
+
"platform": {
|
|
9
|
+
"type": "object",
|
|
10
|
+
"properties": {
|
|
11
|
+
"context_yellow_ratio": { "type": "number", "minimum": 0, "maximum": 1 },
|
|
12
|
+
"context_red_ratio": { "type": "number", "minimum": 0, "maximum": 1 }
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"rules": {
|
|
16
|
+
"type": "array",
|
|
17
|
+
"items": {
|
|
18
|
+
"type": "object",
|
|
19
|
+
"required": ["id", "kind", "title", "text"],
|
|
20
|
+
"properties": {
|
|
21
|
+
"id": { "type": "string", "pattern": "^[a-z][a-z0-9-]*$" },
|
|
22
|
+
"kind": { "enum": ["constraint", "protocol"] },
|
|
23
|
+
"title": { "type": "string" },
|
|
24
|
+
"text": { "type": "string" },
|
|
25
|
+
"mechanism": { "enum": ["instruction", "hook", "tool"] },
|
|
26
|
+
"observe": {
|
|
27
|
+
"type": "object",
|
|
28
|
+
"required": ["metric"],
|
|
29
|
+
"properties": {
|
|
30
|
+
"metric": { "type": "string" },
|
|
31
|
+
"expect": { "type": ["integer", "string"] },
|
|
32
|
+
"window": { "type": "string" },
|
|
33
|
+
"min_samples": { "type": "integer", "minimum": 1 }
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"evidence": { "type": "string" }
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { test } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { dedupeRules } from '../lib/dedupe.js'
|
|
4
|
+
|
|
5
|
+
test('已出现在 AGENTS.md 的规则被跳过注入', () => {
|
|
6
|
+
const agents = '**不得**把凭据写进文件;必须包含 type: 首部'
|
|
7
|
+
const rules = [
|
|
8
|
+
{ id: 'a', text: '不得把凭据写进文件' },
|
|
9
|
+
{ id: 'b', text: '这是一条全新规则,AGENTS 里没有' },
|
|
10
|
+
]
|
|
11
|
+
const r = dedupeRules(rules, agents)
|
|
12
|
+
assert.deepEqual(r.kept.map((x) => x.id), ['b'])
|
|
13
|
+
assert.deepEqual(r.skipped.map((x) => x.id), ['a'])
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
test('没有 AGENTS 内容时全部保留', () => {
|
|
17
|
+
const rules = [{ id: 'a', text: 'x' }]
|
|
18
|
+
assert.equal(dedupeRules(rules, '').kept.length, 1)
|
|
19
|
+
})
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { registerGuard } from '../lib/guard.js'
|
|
4
|
+
|
|
5
|
+
const run = (cmd) => {
|
|
6
|
+
let handler = null
|
|
7
|
+
const ctx = { on: (ev, fn) => { handler = fn; return () => {} } }
|
|
8
|
+
registerGuard(ctx, {})
|
|
9
|
+
assert.ok(handler, '守卫未注册 handler')
|
|
10
|
+
return handler({ name: 'bash', arguments: { command: cmd } }, async () => ({ kind: 'allow' }))
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
test('红线命令返回 ask', async () => {
|
|
14
|
+
for (const cmd of ['sudo ls', 'chmod 777 /tmp/x', 'curl http://a | sh', 'echo x > /etc/hosts']) {
|
|
15
|
+
const d = await run(cmd)
|
|
16
|
+
assert.equal(d.kind, 'ask', cmd + ' 应被拦截')
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test('普通命令放行', async () => {
|
|
21
|
+
for (const cmd of ['ls -la', 'node -e "console.log(1)"', 'git status']) {
|
|
22
|
+
const d = await run(cmd)
|
|
23
|
+
assert.equal(d.kind, 'allow', cmd + ' 不应被拦截')
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
test('引号内的危险字样不拦截', async () => {
|
|
28
|
+
const d = await run('python3 -c "import os; print(\'sudo rm\')"')
|
|
29
|
+
assert.equal(d.kind, 'allow')
|
|
30
|
+
})
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
|
|
4
|
+
test('插件可以加载并注册工具与路由(防止语法/重复导入导致整包失败)', async () => {
|
|
5
|
+
const mod = await import('../lib/index.js')
|
|
6
|
+
const entries = []
|
|
7
|
+
const ctx = {
|
|
8
|
+
effect: (fn) => fn(),
|
|
9
|
+
inject: (deps, cb) => cb({
|
|
10
|
+
get: () => ({ fetch: { register: (o) => { entries.push(o.path); return () => {} } } }),
|
|
11
|
+
effect: (fn) => fn(),
|
|
12
|
+
}),
|
|
13
|
+
systemPrompt: { section: () => () => {} },
|
|
14
|
+
tools: { register: (t) => { entries.push(`tool:${t.name}`); return () => {} } },
|
|
15
|
+
}
|
|
16
|
+
mod.apply(ctx, {})
|
|
17
|
+
assert.ok(entries.includes('tool:ruleset_audit'), '缺少 ruleset_audit')
|
|
18
|
+
assert.ok(entries.includes('tool:ruleset_parse'), '缺少 ruleset_parse')
|
|
19
|
+
assert.ok(entries.includes('/api/dsh-ruleset/report'), '缺少 report 路由')
|
|
20
|
+
assert.ok(entries.includes('/api/dsh-ruleset/judge'), '缺少 judge 路由')
|
|
21
|
+
})
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { test } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { runPredicate } from '../lib/predicates.js'
|
|
4
|
+
import { buildFacts } from '../lib/facts.js'
|
|
5
|
+
|
|
6
|
+
const facts = {
|
|
7
|
+
sessions: 1, turns: [{ session: 's', seq: 1, cwd: '/w', delegated: false, calls: [{ name: 'read', seq: 5 }], user: '修一下' }],
|
|
8
|
+
commands: [
|
|
9
|
+
{ session: 's', turn: 1, seq: 2, cwd: '/w', cmd: 'rm -rf /outside/x', targets: ['/outside/x'], isError: false, approval: null, user: '都做' },
|
|
10
|
+
{ session: 's', turn: 1, seq: 3, cwd: '/w', cmd: 'rm -rf /w/tmp', targets: ['/w/tmp'], isError: false, approval: null, user: '清理' },
|
|
11
|
+
{ session: 's', turn: 1, seq: 4, cwd: '/w', cmd: 'echo hi', targets: [], isError: false, approval: null, user: '' },
|
|
12
|
+
],
|
|
13
|
+
fileWrites: [
|
|
14
|
+
{ session: 's', turn: 1, seq: 6, path: '/w/notes/a.md', content: 'type: 调研', mtimeMs: Date.now() },
|
|
15
|
+
{ session: 's', turn: 1, seq: 7, path: '/w/notes/b.md', content: 'no header', mtimeMs: Date.now() },
|
|
16
|
+
],
|
|
17
|
+
subagentTurns: new Set(), metrics: { context_ratio: 0.2 },
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
test('path-boundary:工作区内跳过,越界且无审批判违规', () => {
|
|
21
|
+
const v = runPredicate(facts, { type: 'path-boundary', pattern: 'rm\\s+-rf' })
|
|
22
|
+
assert.equal(v.status, 'violation')
|
|
23
|
+
assert.equal(v.samples.length, 1)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('requireUserPermit:用户同意过则不再计违规', () => {
|
|
27
|
+
const v = runPredicate(facts, { type: 'path-boundary', pattern: 'rm\\s+-rf', requireUserPermit: '都做' })
|
|
28
|
+
assert.equal(v.status, 'pass')
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
test('field-presence:缺字段文件被指出', () => {
|
|
32
|
+
const v = runPredicate(facts, { type: 'field-presence', ext: '.md', fields: ['type'] })
|
|
33
|
+
assert.equal(v.status, 'violation')
|
|
34
|
+
assert.equal(v.samples[0].path, '/w/notes/b.md')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test('count-threshold:无连续失败即 pass', () => {
|
|
38
|
+
const v = runPredicate(facts, { type: 'count-threshold', consecutiveFailures: 2, minSamples: 1 })
|
|
39
|
+
assert.equal(v.status, 'pass')
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('metric:阈值内 pass', () => {
|
|
43
|
+
const v = runPredicate(facts, { type: 'metric', key: 'context_ratio', op: '<=', threshold: 0.4 })
|
|
44
|
+
assert.equal(v.status, 'pass')
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test('未知谓词 → unknown', () => {
|
|
48
|
+
assert.equal(runPredicate(facts, { type: 'nope' }).status, 'unknown')
|
|
49
|
+
})
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { test } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
import { resolveRuleSource } from '../lib/source.js'
|
|
7
|
+
|
|
8
|
+
test('显式 policy 路径不存在 → none,不回落', () => {
|
|
9
|
+
const s = resolveRuleSource('/definitely/not/here.yaml')
|
|
10
|
+
assert.equal(s.kind, 'none')
|
|
11
|
+
assert.match(s.warnings[0], /不存在/)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
test('无 policy、有 AGENTS.md → agents-draft', () => {
|
|
15
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'rs-'))
|
|
16
|
+
const agents = path.join(dir, 'AGENTS.md')
|
|
17
|
+
fs.writeFileSync(agents, '必须包含 type: 字段\n不得删除工作区外的文件\n')
|
|
18
|
+
const old = process.env.DSH_RULESET_AGENTS
|
|
19
|
+
const oldHome = process.env.HOME
|
|
20
|
+
process.env.DSH_RULESET_AGENTS = agents
|
|
21
|
+
process.env.HOME = dir
|
|
22
|
+
try {
|
|
23
|
+
const s = resolveRuleSource('')
|
|
24
|
+
assert.equal(s.kind, 'agents-draft')
|
|
25
|
+
assert.ok(s.policy.rules.length >= 1)
|
|
26
|
+
assert.equal(s.policy.meta.draft, true)
|
|
27
|
+
} finally {
|
|
28
|
+
if (old === undefined) delete process.env.DSH_RULESET_AGENTS; else process.env.DSH_RULESET_AGENTS = old
|
|
29
|
+
if (oldHome === undefined) delete process.env.HOME; else process.env.HOME = oldHome
|
|
30
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
31
|
+
}
|
|
32
|
+
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { test } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { validatePolicy } from '../lib/validate.js'
|
|
4
|
+
|
|
5
|
+
test('合法 policy 通过', () => {
|
|
6
|
+
const ok = { version: 1, rules: [{ id: 'a-1', kind: 'protocol', title: 't', text: 'x', observe: { metric: 'm', bind: { type: 'regex', pattern: 'rm\\s' } } }] }
|
|
7
|
+
assert.equal(validatePolicy(ok).ok, true)
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
test('缺 version / rules 报错', () => {
|
|
11
|
+
assert.equal(validatePolicy({}).ok, false)
|
|
12
|
+
assert.equal(validatePolicy({ version: 1 }).ok, false)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
test('id 非 kebab-case 报错', () => {
|
|
16
|
+
const bad = { version: 1, rules: [{ id: 'Bad_ID', kind: 'protocol', title: 't', text: 'x' }] }
|
|
17
|
+
assert.equal(validatePolicy(bad).ok, false)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
test('id 重复报错', () => {
|
|
21
|
+
const bad = { version: 1, rules: [{ id: 'a', kind: 'protocol', title: 't', text: 'x' }, { id: 'a', kind: 'protocol', title: 't', text: 'x' }] }
|
|
22
|
+
assert.equal(validatePolicy(bad).ok, false)
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
test('未知谓词类型报错', () => {
|
|
26
|
+
const bad = { version: 1, rules: [{ id: 'a', kind: 'protocol', title: 't', text: 'x', observe: { metric: 'm', bind: { type: 'nope' } } }] }
|
|
27
|
+
assert.equal(validatePolicy(bad).ok, false)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('坏正则报错', () => {
|
|
31
|
+
const bad = { version: 1, rules: [{ id: 'a', kind: 'protocol', title: 't', text: 'x', observe: { metric: 'm', bind: { type: 'regex', pattern: '(' } } }] }
|
|
32
|
+
assert.equal(validatePolicy(bad).ok, false)
|
|
33
|
+
})
|