@leeruu/agentguard 0.0.0-stage → 0.1.1
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/LICENSE +202 -0
- package/NOTICE +22 -0
- package/README.md +298 -2
- package/dist/audit/query.js +151 -0
- package/dist/audit/writer.js +174 -0
- package/dist/cli.js +624 -0
- package/dist/demo.js +246 -0
- package/dist/errors.js +28 -0
- package/dist/format.js +29 -0
- package/dist/limits.js +42 -0
- package/dist/logger.js +46 -0
- package/dist/main.js +239 -0
- package/dist/packs.js +51 -0
- package/dist/policy/bootstrap.js +127 -0
- package/dist/policy/decide.js +41 -0
- package/dist/policy/glob.js +80 -0
- package/dist/policy/load.js +203 -0
- package/dist/policy/match.js +431 -0
- package/dist/policy/types.js +15 -0
- package/dist/proxy.js +440 -0
- package/package.json +69 -4
- package/packs/core.yaml +104 -0
- package/packs/git.yaml +43 -0
- package/packs/net.yaml +48 -0
- package/packs/secrets.yaml +120 -0
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 匹配层:把 YAML 里的 match 块编译成可执行判定。
|
|
3
|
+
*
|
|
4
|
+
* 语义(方案第 4 章「必须说死的五条」):
|
|
5
|
+
* - 字段路径从 args 根开始点号分隔;`tool` 是特例
|
|
6
|
+
* - 五种操作符:glob / regex / equals / in / prefix
|
|
7
|
+
* - 同一个操作符的多值 → OR
|
|
8
|
+
* - 同一规则的多字段 → AND
|
|
9
|
+
* - 优先级 deny > ask > allow(在 decide.ts);同档按声明顺序取第一条
|
|
10
|
+
*
|
|
11
|
+
* 两个防呆设计:
|
|
12
|
+
* - `args.*` 字段必须写成操作符对象,裸字符串会被拒绝并给出提示。
|
|
13
|
+
* 裸值只能当 `equals` 用,而用户写 `args.path: "**\/.env"` 时 99% 想表达 glob。
|
|
14
|
+
* - 所有 regex / glob 在加载期预编译,写错就是启动失败,而不是第一次调用才炸。
|
|
15
|
+
*/
|
|
16
|
+
import { logger } from '../logger.js';
|
|
17
|
+
import { compileGlob, matchGlob } from './glob.js';
|
|
18
|
+
import { PolicyError, } from './types.js';
|
|
19
|
+
const DECISIONS = ['allow', 'deny', 'ask'];
|
|
20
|
+
const OPERATORS = ['glob', 'regex', 'equals', 'in', 'prefix'];
|
|
21
|
+
const RULE_KEYS = new Set(['id', 'match', 'decision', 'reason']);
|
|
22
|
+
const ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
23
|
+
function isRecord(value) {
|
|
24
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
function isScalar(value) {
|
|
27
|
+
return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean';
|
|
28
|
+
}
|
|
29
|
+
function toText(value) {
|
|
30
|
+
if (typeof value === 'string')
|
|
31
|
+
return value;
|
|
32
|
+
if (value === null)
|
|
33
|
+
return 'null';
|
|
34
|
+
if (typeof value === 'object') {
|
|
35
|
+
try {
|
|
36
|
+
return JSON.stringify(value) ?? '';
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return '';
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return String(value);
|
|
43
|
+
}
|
|
44
|
+
/** 从对象里按路径取值;支持数组下标(`args.files.0.path`) */
|
|
45
|
+
export function getPath(root, path) {
|
|
46
|
+
let cur = root;
|
|
47
|
+
for (const key of path) {
|
|
48
|
+
if (Array.isArray(cur) && /^\d+$/.test(key)) {
|
|
49
|
+
cur = cur[Number(key)];
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (!isRecord(cur) || !(key in cur))
|
|
53
|
+
return undefined;
|
|
54
|
+
cur = cur[key];
|
|
55
|
+
}
|
|
56
|
+
return cur;
|
|
57
|
+
}
|
|
58
|
+
function tokenize(source) {
|
|
59
|
+
const tokens = [];
|
|
60
|
+
let i = 0;
|
|
61
|
+
const markQuantifier = (unbounded) => {
|
|
62
|
+
const last = tokens[tokens.length - 1];
|
|
63
|
+
if (last === undefined)
|
|
64
|
+
return;
|
|
65
|
+
last.quantified = true;
|
|
66
|
+
last.unbounded = last.unbounded || unbounded;
|
|
67
|
+
};
|
|
68
|
+
while (i < source.length) {
|
|
69
|
+
const ch = source[i];
|
|
70
|
+
if (ch === '\\') {
|
|
71
|
+
tokens.push({ kind: 'literal', quantified: false, unbounded: false });
|
|
72
|
+
i += 2;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (ch === '[') {
|
|
76
|
+
let j = i + 1;
|
|
77
|
+
while (j < source.length && source[j] !== ']') {
|
|
78
|
+
if (source[j] === '\\')
|
|
79
|
+
j += 1;
|
|
80
|
+
j += 1;
|
|
81
|
+
}
|
|
82
|
+
tokens.push({ kind: 'class', quantified: false, unbounded: false });
|
|
83
|
+
i = j + 1;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if (ch === '(') {
|
|
87
|
+
let depth = 0;
|
|
88
|
+
let j = i;
|
|
89
|
+
for (; j < source.length; j++) {
|
|
90
|
+
if (source[j] === '\\') {
|
|
91
|
+
j += 1;
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (source[j] === '[') {
|
|
95
|
+
while (j < source.length && source[j] !== ']') {
|
|
96
|
+
if (source[j] === '\\')
|
|
97
|
+
j += 1;
|
|
98
|
+
j += 1;
|
|
99
|
+
}
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (source[j] === '(')
|
|
103
|
+
depth += 1;
|
|
104
|
+
if (source[j] === ')') {
|
|
105
|
+
depth -= 1;
|
|
106
|
+
if (depth === 0)
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
tokens.push({ kind: 'group', quantified: false, unbounded: false });
|
|
111
|
+
i = j + 1;
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (ch === '.') {
|
|
115
|
+
tokens.push({ kind: 'dot', quantified: false, unbounded: false });
|
|
116
|
+
i += 1;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (ch === '|') {
|
|
120
|
+
tokens.push({ kind: 'alt', quantified: false, unbounded: false });
|
|
121
|
+
i += 1;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (ch === '^' || ch === '$') {
|
|
125
|
+
tokens.push({ kind: 'anchor', quantified: false, unbounded: false });
|
|
126
|
+
i += 1;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (ch === '*' || ch === '+') {
|
|
130
|
+
markQuantifier(true);
|
|
131
|
+
i += 1;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
if (ch === '?') {
|
|
135
|
+
markQuantifier(false);
|
|
136
|
+
i += 1;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (ch === '{') {
|
|
140
|
+
const close = source.indexOf('}', i);
|
|
141
|
+
if (close !== -1) {
|
|
142
|
+
const body = source.slice(i + 1, close);
|
|
143
|
+
markQuantifier(body.includes(',') && body.trim().endsWith(','));
|
|
144
|
+
i = close + 1;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
tokens.push({ kind: 'literal', quantified: false, unbounded: false });
|
|
149
|
+
i += 1;
|
|
150
|
+
}
|
|
151
|
+
return tokens;
|
|
152
|
+
}
|
|
153
|
+
/** 去掉 `(?:` / `(?<name>` / `(?=` / `?!` / `?<=` / `?<!` 这些组前缀,只留真正的 body */
|
|
154
|
+
function stripGroupPrefix(body) {
|
|
155
|
+
if (!body.startsWith('?'))
|
|
156
|
+
return body;
|
|
157
|
+
if (body.startsWith('?:'))
|
|
158
|
+
return body.slice(2);
|
|
159
|
+
if (body.startsWith('?<=') || body.startsWith('?<!'))
|
|
160
|
+
return body.slice(3);
|
|
161
|
+
if (body.startsWith('?<')) {
|
|
162
|
+
const close = body.indexOf('>');
|
|
163
|
+
return close === -1 ? body.slice(2) : body.slice(close + 1);
|
|
164
|
+
}
|
|
165
|
+
if (body.startsWith('?=') || body.startsWith('?!'))
|
|
166
|
+
return body.slice(2);
|
|
167
|
+
return body.slice(1);
|
|
168
|
+
}
|
|
169
|
+
/** 返回告警文案;null 表示这条正则没有明显的回溯风险 */
|
|
170
|
+
export function lintRegex(pattern) {
|
|
171
|
+
const source = pattern;
|
|
172
|
+
let i = 0;
|
|
173
|
+
while (i < source.length) {
|
|
174
|
+
if (source[i] === '\\') {
|
|
175
|
+
i += 2;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
if (source[i] === '[') {
|
|
179
|
+
i += 1;
|
|
180
|
+
while (i < source.length && source[i] !== ']') {
|
|
181
|
+
if (source[i] === '\\')
|
|
182
|
+
i += 1;
|
|
183
|
+
i += 1;
|
|
184
|
+
}
|
|
185
|
+
i += 1;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (source[i] !== '(') {
|
|
189
|
+
i += 1;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
// 找到一个组,取它的 body 与紧随其后的量词
|
|
193
|
+
let depth = 0;
|
|
194
|
+
let j = i;
|
|
195
|
+
for (; j < source.length; j++) {
|
|
196
|
+
if (source[j] === '\\') {
|
|
197
|
+
j += 1;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (source[j] === '[') {
|
|
201
|
+
while (j < source.length && source[j] !== ']') {
|
|
202
|
+
if (source[j] === '\\')
|
|
203
|
+
j += 1;
|
|
204
|
+
j += 1;
|
|
205
|
+
}
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (source[j] === '(')
|
|
209
|
+
depth += 1;
|
|
210
|
+
if (source[j] === ')') {
|
|
211
|
+
depth -= 1;
|
|
212
|
+
if (depth === 0)
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
const rawBody = source.slice(i + 1, j);
|
|
217
|
+
const body = stripGroupPrefix(rawBody);
|
|
218
|
+
const after = source.slice(j + 1, j + 8);
|
|
219
|
+
const outerUnbounded = /^(?:[*+]|\{\d+,\})/.test(after);
|
|
220
|
+
if (outerUnbounded) {
|
|
221
|
+
const tokens = tokenize(body);
|
|
222
|
+
const hasInnerUnbounded = tokens.some((token) => token.unbounded);
|
|
223
|
+
const first = tokens[0];
|
|
224
|
+
const last = tokens[tokens.length - 1];
|
|
225
|
+
const headSolid = first !== undefined && first.kind === 'literal' && !first.quantified;
|
|
226
|
+
const tailSolid = last !== undefined && last.kind === 'literal' && !last.quantified;
|
|
227
|
+
if (hasInnerUnbounded && !headSolid && !tailSolid) {
|
|
228
|
+
return `存在嵌套量词且组内没有固定分隔符(如 (a+)+),恶意输入可能触发回溯爆炸把网关卡死`;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
i = j + 1;
|
|
232
|
+
}
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
/* ------------------------------------------------------------------ */
|
|
236
|
+
function parseFieldPath(field, origin, ruleId) {
|
|
237
|
+
if (field === 'tool')
|
|
238
|
+
return ['tool'];
|
|
239
|
+
if (field.startsWith('args.')) {
|
|
240
|
+
const rest = field.slice('args.'.length);
|
|
241
|
+
if (rest === '') {
|
|
242
|
+
throw new PolicyError('字段 "args." 后必须跟子路径', origin, ruleId);
|
|
243
|
+
}
|
|
244
|
+
return ['args', ...rest.split('.')];
|
|
245
|
+
}
|
|
246
|
+
throw new PolicyError(`不认识的字段 "${field}":只支持 "tool" 或 "args.<子路径>"`, origin, ruleId);
|
|
247
|
+
}
|
|
248
|
+
function asStringList(op, value, field, origin, ruleId) {
|
|
249
|
+
const list = Array.isArray(value) ? value : [value];
|
|
250
|
+
if (list.length === 0) {
|
|
251
|
+
throw new PolicyError(`字段 "${field}" 的 ${op} 取值不能是空数组`, origin, ruleId);
|
|
252
|
+
}
|
|
253
|
+
return list.map((item) => {
|
|
254
|
+
if (typeof item !== 'string' || item === '') {
|
|
255
|
+
throw new PolicyError(`字段 "${field}" 的 ${op} 只能是非空字符串`, origin, ruleId);
|
|
256
|
+
}
|
|
257
|
+
return item;
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
function asScalarList(value, field, origin, ruleId) {
|
|
261
|
+
const list = Array.isArray(value) ? value : [value];
|
|
262
|
+
if (list.length === 0) {
|
|
263
|
+
throw new PolicyError(`字段 "${field}" 的取值不能是空数组`, origin, ruleId);
|
|
264
|
+
}
|
|
265
|
+
return list.map((item) => {
|
|
266
|
+
if (!isScalar(item)) {
|
|
267
|
+
throw new PolicyError(`字段 "${field}" 的取值只能是字符串 / 数字 / 布尔`, origin, ruleId);
|
|
268
|
+
}
|
|
269
|
+
return item;
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
function compileMatcher(field, raw, origin, ruleId) {
|
|
273
|
+
const path = parseFieldPath(field, origin, ruleId);
|
|
274
|
+
let op;
|
|
275
|
+
let rawValue;
|
|
276
|
+
if (isRecord(raw)) {
|
|
277
|
+
const keys = Object.keys(raw);
|
|
278
|
+
const opKeys = keys.filter((key) => OPERATORS.includes(key));
|
|
279
|
+
if (opKeys.length === 0) {
|
|
280
|
+
throw new PolicyError(`字段 "${field}" 的对象里没有合法操作符(可用:${OPERATORS.join(' / ')})`, origin, ruleId);
|
|
281
|
+
}
|
|
282
|
+
if (opKeys.length > 1) {
|
|
283
|
+
throw new PolicyError(`字段 "${field}" 同时写了多个操作符(${opKeys.join(', ')}),只能选一个`, origin, ruleId);
|
|
284
|
+
}
|
|
285
|
+
const unknownKeys = keys.filter((key) => !OPERATORS.includes(key));
|
|
286
|
+
if (unknownKeys.length > 0) {
|
|
287
|
+
throw new PolicyError(`字段 "${field}" 出现未知键 "${unknownKeys.join(', ')}"`, origin, ruleId);
|
|
288
|
+
}
|
|
289
|
+
op = opKeys[0];
|
|
290
|
+
rawValue = raw[op];
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
// 裸值只允许用在 tool 上(`tool: bash` / `tool: [a, b]`),args 字段必须显式写操作符
|
|
294
|
+
if (field !== 'tool') {
|
|
295
|
+
throw new PolicyError(`字段 "${field}" 必须写成操作符对象,例如 { glob: "**/.env*" };` +
|
|
296
|
+
`裸值会被误读成 equals,写错了不会报错但永不命中`, origin, ruleId);
|
|
297
|
+
}
|
|
298
|
+
op = Array.isArray(raw) ? 'in' : 'equals';
|
|
299
|
+
rawValue = raw;
|
|
300
|
+
}
|
|
301
|
+
if (op === 'in' && !Array.isArray(rawValue)) {
|
|
302
|
+
throw new PolicyError(`字段 "${field}" 的 in 取值得是数组`, origin, ruleId);
|
|
303
|
+
}
|
|
304
|
+
switch (op) {
|
|
305
|
+
case 'glob': {
|
|
306
|
+
const patterns = asStringList(op, rawValue, field, origin, ruleId);
|
|
307
|
+
return {
|
|
308
|
+
field,
|
|
309
|
+
path,
|
|
310
|
+
op,
|
|
311
|
+
patterns,
|
|
312
|
+
regexes: [],
|
|
313
|
+
globs: patterns.map((pattern) => {
|
|
314
|
+
try {
|
|
315
|
+
return compileGlob(pattern);
|
|
316
|
+
}
|
|
317
|
+
catch (err) {
|
|
318
|
+
throw new PolicyError(`字段 "${field}" 的 glob 无效:${err.message}`, origin, ruleId);
|
|
319
|
+
}
|
|
320
|
+
}),
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
case 'regex': {
|
|
324
|
+
const patterns = asStringList(op, rawValue, field, origin, ruleId);
|
|
325
|
+
return {
|
|
326
|
+
field,
|
|
327
|
+
path,
|
|
328
|
+
op,
|
|
329
|
+
patterns,
|
|
330
|
+
// 大小写不敏感:安全方向上宁多拦不漏拦
|
|
331
|
+
regexes: patterns.map((pattern) => {
|
|
332
|
+
try {
|
|
333
|
+
new RegExp(pattern, 'i');
|
|
334
|
+
}
|
|
335
|
+
catch (err) {
|
|
336
|
+
throw new PolicyError(`字段 "${field}" 的 regex 无效:${err.message}(模式:${pattern})`, origin, ruleId);
|
|
337
|
+
}
|
|
338
|
+
const warning = lintRegex(pattern);
|
|
339
|
+
if (warning !== null) {
|
|
340
|
+
logger.warn(`规则 ${ruleId} 的正则可能有回溯风险:${warning}`, { pattern });
|
|
341
|
+
}
|
|
342
|
+
return new RegExp(pattern, 'i');
|
|
343
|
+
}),
|
|
344
|
+
globs: [],
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
case 'prefix':
|
|
348
|
+
case 'equals':
|
|
349
|
+
case 'in': {
|
|
350
|
+
if (op === 'prefix')
|
|
351
|
+
asStringList(op, rawValue, field, origin, ruleId);
|
|
352
|
+
return {
|
|
353
|
+
field,
|
|
354
|
+
path,
|
|
355
|
+
op,
|
|
356
|
+
patterns: asScalarList(rawValue, field, origin, ruleId),
|
|
357
|
+
regexes: [],
|
|
358
|
+
globs: [],
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
/* ------------------------------------------------------------------ */
|
|
364
|
+
function patternHits(matcher, value) {
|
|
365
|
+
const text = toText(value);
|
|
366
|
+
switch (matcher.op) {
|
|
367
|
+
case 'equals':
|
|
368
|
+
return matcher.patterns.some((pattern) => toText(pattern) === text);
|
|
369
|
+
case 'prefix':
|
|
370
|
+
return matcher.patterns.some((pattern) => text.startsWith(toText(pattern)));
|
|
371
|
+
case 'in':
|
|
372
|
+
return matcher.patterns.some((pattern) => toText(pattern) === text);
|
|
373
|
+
case 'regex':
|
|
374
|
+
return matcher.regexes.some((re) => re.test(text));
|
|
375
|
+
case 'glob':
|
|
376
|
+
return matcher.globs.some((glob) => matchGlob(glob, text));
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
function fieldHits(matcher, tool, args) {
|
|
380
|
+
const value = matcher.path[0] === 'tool' ? tool : getPath(args, matcher.path.slice(1));
|
|
381
|
+
if (value === undefined)
|
|
382
|
+
return false;
|
|
383
|
+
// 实参是数组时逐个判定:`read_multiple_files` 传 [safe.txt, .env] 必须能命中 .env
|
|
384
|
+
const values = Array.isArray(value) ? value : [value];
|
|
385
|
+
return values.some((item) => patternHits(matcher, item));
|
|
386
|
+
}
|
|
387
|
+
export function compileRule(raw, origin, index) {
|
|
388
|
+
if (!isRecord(raw)) {
|
|
389
|
+
throw new PolicyError(`rules[${index}] 必须是对象`, origin);
|
|
390
|
+
}
|
|
391
|
+
for (const key of Object.keys(raw)) {
|
|
392
|
+
if (!RULE_KEYS.has(key)) {
|
|
393
|
+
throw new PolicyError(`rules[${index}] 出现未知键 "${key}"(可用:id / match / decision / reason)`, origin);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
const id = raw.id;
|
|
397
|
+
if (typeof id !== 'string' || !ID_PATTERN.test(id)) {
|
|
398
|
+
throw new PolicyError(`rules[${index}] 的 id 缺失或不合法(只允许字母、数字、. _ -,且不能以符号开头)`, origin);
|
|
399
|
+
}
|
|
400
|
+
const decision = raw.decision;
|
|
401
|
+
if (typeof decision !== 'string' || !DECISIONS.includes(decision)) {
|
|
402
|
+
throw new PolicyError(`规则 "${id}" 的 decision 必须是 ${DECISIONS.join(' / ')}`, origin, id);
|
|
403
|
+
}
|
|
404
|
+
let reason = null;
|
|
405
|
+
if (raw.reason !== undefined) {
|
|
406
|
+
if (typeof raw.reason !== 'string') {
|
|
407
|
+
throw new PolicyError(`规则 "${id}" 的 reason 必须是字符串`, origin, id);
|
|
408
|
+
}
|
|
409
|
+
reason = raw.reason;
|
|
410
|
+
}
|
|
411
|
+
const match = raw.match;
|
|
412
|
+
if (!isRecord(match)) {
|
|
413
|
+
throw new PolicyError(`规则 "${id}" 缺少 match 块`, origin, id);
|
|
414
|
+
}
|
|
415
|
+
const fields = Object.keys(match);
|
|
416
|
+
if (fields.length === 0) {
|
|
417
|
+
throw new PolicyError(`规则 "${id}" 的 match 为空 —— 空 match 会命中一切调用,若确实要全放行请用 defaults.on_no_match: allow`, origin, id);
|
|
418
|
+
}
|
|
419
|
+
const matchers = fields.map((field) => compileMatcher(field, match[field], origin, id));
|
|
420
|
+
return {
|
|
421
|
+
id,
|
|
422
|
+
decision: decision,
|
|
423
|
+
reason,
|
|
424
|
+
matchers,
|
|
425
|
+
origin,
|
|
426
|
+
test: (tool, args) => matchers.every((matcher) => fieldHits(matcher, tool, args)),
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
export function compileRules(rawRules, origin) {
|
|
430
|
+
return rawRules.map((raw, index) => compileRule(raw, origin, index));
|
|
431
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 策略模块的共享类型。单独成文件是为了打断 load → match → load 的循环依赖:
|
|
3
|
+
* types.ts ← glob.ts / match.ts / load.ts / decide.ts
|
|
4
|
+
*/
|
|
5
|
+
/** 策略配置错误。必须带上出处(文件 / 规则 id / 字段),否则用户没法定位 */
|
|
6
|
+
export class PolicyError extends Error {
|
|
7
|
+
origin;
|
|
8
|
+
ruleId;
|
|
9
|
+
constructor(message, origin, ruleId) {
|
|
10
|
+
super(origin === undefined ? message : `${origin}: ${message}`);
|
|
11
|
+
this.origin = origin;
|
|
12
|
+
this.ruleId = ruleId;
|
|
13
|
+
this.name = 'PolicyError';
|
|
14
|
+
}
|
|
15
|
+
}
|