unbound-cli 1.9.0 → 1.10.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/package.json +1 -1
- package/src/commands/policy.js +46 -12
- package/test/policy-conditions.test.js +30 -1
package/package.json
CHANGED
package/src/commands/policy.js
CHANGED
|
@@ -312,6 +312,34 @@ function parseFieldSpec(spec) {
|
|
|
312
312
|
return [spec.slice(0, eq), spec.slice(eq + 1)];
|
|
313
313
|
}
|
|
314
314
|
|
|
315
|
+
// The `custom` command family has no match-against fields — its single condition is
|
|
316
|
+
// the reserved `command` key, matched as ONE pattern against the whole raw command
|
|
317
|
+
// regardless of the classified family. The families metadata reports it with
|
|
318
|
+
// is_custom:true and no fields (mirrors the dashboard), so surface how to author it.
|
|
319
|
+
const CUSTOM_FAMILY_NAME = 'custom';
|
|
320
|
+
const CUSTOM_FAMILY_HINT =
|
|
321
|
+
"matches ONE pattern against the whole command (exact text, *wildcard*, or regex). " +
|
|
322
|
+
"Author it with --field command='<pattern>', e.g. --field command='make release-prod'.";
|
|
323
|
+
|
|
324
|
+
// Indented detail lines shown under a family name in `policy tool families`. Pure so
|
|
325
|
+
// the custom-family guidance is unit-testable. A custom family (is_custom) reports no
|
|
326
|
+
// fields by design, so explain its single whole-command pattern instead of "(no fields)".
|
|
327
|
+
function familyFieldLines(f) {
|
|
328
|
+
// Custom has no fields by design — return before touching f.fields so its shape is
|
|
329
|
+
// irrelevant for the custom branch.
|
|
330
|
+
if (f.is_custom) {
|
|
331
|
+
return [CUSTOM_FAMILY_HINT];
|
|
332
|
+
}
|
|
333
|
+
const fields = Array.isArray(f.fields) ? f.fields : Object.keys(f.fields || {});
|
|
334
|
+
if (fields.length === 0) {
|
|
335
|
+
return ['(no fields)'];
|
|
336
|
+
}
|
|
337
|
+
return fields.map((field) => {
|
|
338
|
+
const desc = (f.field_descriptions && f.field_descriptions[field]) || '';
|
|
339
|
+
return `${field}${desc ? ` — ${desc}` : ''}`;
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
|
|
315
343
|
// "ANY" is a wildcard across all fields; it can't be ANDed with other conditions.
|
|
316
344
|
// Mirrors the backend rule so the CLI fails fast before the API call. The check is
|
|
317
345
|
// case-insensitive so `--field Any=*`/`--field any=*` are caught too.
|
|
@@ -1433,8 +1461,8 @@ Subcommands:
|
|
|
1433
1461
|
.option('--prompt <text>', 'Natural-language description; routes through Unbound AI assist. Mutually exclusive with --command-family/--field/--config. Compatible with --name/--description/--action overrides.')
|
|
1434
1462
|
.option('--no-ai', 'Opt out of the AI-assist guard and use raw classification flags. Mutually exclusive with --prompt.')
|
|
1435
1463
|
.option('--name <name>', 'Policy name (required unless --prompt is used)')
|
|
1436
|
-
.option('--command-family <family>', 'Command family (e.g. git, filesystem, network). See `policy tool families`.')
|
|
1437
|
-
.option('--field <key=pattern>', 'Match field: <key>=<pattern>. Repeatable — multiple --field are ANDed (all must match), one per field, for a more specific policy. At least one required unless --config is used.', (val, prev = []) => [...prev, val])
|
|
1464
|
+
.option('--command-family <family>', 'Command family (e.g. git, filesystem, network), or `custom` to match one pattern against the whole command. See `policy tool families`.')
|
|
1465
|
+
.option('--field <key=pattern>', 'Match field: <key>=<pattern>. Repeatable — multiple --field are ANDed (all must match), one per field, for a more specific policy. At least one required unless --config is used. For the `custom` family the key is `command`.', (val, prev = []) => [...prev, val])
|
|
1438
1466
|
.option('--action <action>', `Action to take: ${TOOL_ACTIONS.join(' | ')}`)
|
|
1439
1467
|
.option('--description <text>', 'Human-readable description')
|
|
1440
1468
|
.option('--custom-message <text>', 'Message shown to the user when the policy fires. Required when --action is BLOCK or WARN.')
|
|
@@ -1466,6 +1494,12 @@ Examples:
|
|
|
1466
1494
|
--field operation=push --field branch=main --field remote='*origin*' \\
|
|
1467
1495
|
--action BLOCK --custom-message "Pushing to main on origin is blocked."
|
|
1468
1496
|
|
|
1497
|
+
# 'custom' family: one pattern matched against the WHOLE command (exact / *wildcard* / regex),
|
|
1498
|
+
# regardless of the classified family. The field key is always 'command'.
|
|
1499
|
+
$ unbound policy tool create-terminal --no-ai --name "Block prod release" \\
|
|
1500
|
+
--command-family custom --field command='make release-prod' \\
|
|
1501
|
+
--action BLOCK --custom-message "Prod releases go through CI."
|
|
1502
|
+
|
|
1469
1503
|
Discover valid command families and their fields: unbound policy tool families
|
|
1470
1504
|
Learn more: ${DOCS_TOOL}
|
|
1471
1505
|
`)
|
|
@@ -1558,12 +1592,18 @@ Learn more: ${DOCS_TOOL}
|
|
|
1558
1592
|
throw new Error('Cannot combine --config with --field.');
|
|
1559
1593
|
}
|
|
1560
1594
|
|
|
1595
|
+
const isCustomFamily = (opts.commandFamily || '').toLowerCase() === CUSTOM_FAMILY_NAME;
|
|
1596
|
+
|
|
1561
1597
|
let configObj;
|
|
1562
1598
|
if (opts.config) {
|
|
1563
1599
|
configObj = parseJsonOrThrow(opts.config);
|
|
1564
1600
|
} else {
|
|
1565
1601
|
if (!opts.field || opts.field.length === 0) {
|
|
1566
|
-
throw new Error(
|
|
1602
|
+
throw new Error(
|
|
1603
|
+
isCustomFamily
|
|
1604
|
+
? `The 'custom' family ${CUSTOM_FAMILY_HINT}`
|
|
1605
|
+
: 'At least one --field <key>=<pattern> is required (or use --config).'
|
|
1606
|
+
);
|
|
1567
1607
|
}
|
|
1568
1608
|
configObj = {};
|
|
1569
1609
|
for (const spec of opts.field) {
|
|
@@ -1874,14 +1914,8 @@ Learn more: ${DOCS_TOOL}
|
|
|
1874
1914
|
for (const f of families) {
|
|
1875
1915
|
console.log('');
|
|
1876
1916
|
output.info(f.name);
|
|
1877
|
-
const
|
|
1878
|
-
|
|
1879
|
-
console.log(' (no fields)');
|
|
1880
|
-
} else {
|
|
1881
|
-
for (const field of fields) {
|
|
1882
|
-
const desc = (f.field_descriptions && f.field_descriptions[field]) || '';
|
|
1883
|
-
console.log(` ${field}${desc ? ` — ${desc}` : ''}`);
|
|
1884
|
-
}
|
|
1917
|
+
for (const line of familyFieldLines(f)) {
|
|
1918
|
+
console.log(` ${line}`);
|
|
1885
1919
|
}
|
|
1886
1920
|
if (f.risk_level) console.log(` risk: ${f.risk_level}`);
|
|
1887
1921
|
}
|
|
@@ -2082,4 +2116,4 @@ Learn more: ${DOCS_POLICIES}
|
|
|
2082
2116
|
registerLegacy(policy);
|
|
2083
2117
|
}
|
|
2084
2118
|
|
|
2085
|
-
module.exports = { register, assertAnyExclusive };
|
|
2119
|
+
module.exports = { register, assertAnyExclusive, familyFieldLines, CUSTOM_FAMILY_HINT };
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
const { test } = require('node:test');
|
|
4
4
|
const assert = require('node:assert');
|
|
5
5
|
|
|
6
|
-
const { assertAnyExclusive } = require('../src/commands/policy');
|
|
6
|
+
const { assertAnyExclusive, familyFieldLines, CUSTOM_FAMILY_HINT } = require('../src/commands/policy');
|
|
7
7
|
|
|
8
8
|
test('assertAnyExclusive: allows a single ANY condition', () => {
|
|
9
9
|
assert.doesNotThrow(() => assertAnyExclusive({ ANY: '*' }));
|
|
@@ -26,3 +26,32 @@ test('assertAnyExclusive: tolerates empty/undefined config', () => {
|
|
|
26
26
|
assert.doesNotThrow(() => assertAnyExclusive({}));
|
|
27
27
|
assert.doesNotThrow(() => assertAnyExclusive(undefined));
|
|
28
28
|
});
|
|
29
|
+
|
|
30
|
+
// `policy tool families` must teach an agent how to author a custom policy: the
|
|
31
|
+
// custom family reports is_custom + no fields, and must render the command-pattern
|
|
32
|
+
// hint (not "(no fields)", which reads as "unusable").
|
|
33
|
+
test('familyFieldLines: custom family shows the command-pattern hint, not "(no fields)"', () => {
|
|
34
|
+
const lines = familyFieldLines({ name: 'custom', is_custom: true, fields: [] });
|
|
35
|
+
assert.deepStrictEqual(lines, [CUSTOM_FAMILY_HINT]);
|
|
36
|
+
assert.match(lines[0], /--field command=/);
|
|
37
|
+
assert.match(lines[0], /exact text, \*wildcard\*, or regex/);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test('familyFieldLines: a normal family with fields lists field — description', () => {
|
|
41
|
+
const lines = familyFieldLines({
|
|
42
|
+
name: 'git_action',
|
|
43
|
+
fields: ['operation', 'branch'],
|
|
44
|
+
field_descriptions: { operation: 'push/pull/…', branch: 'branch name' },
|
|
45
|
+
});
|
|
46
|
+
assert.deepStrictEqual(lines, ['operation — push/pull/…', 'branch — branch name']);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test('familyFieldLines: a fieldless non-custom family still shows "(no fields)"', () => {
|
|
50
|
+
assert.deepStrictEqual(familyFieldLines({ name: 'misc', fields: [] }), ['(no fields)']);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('familyFieldLines: custom returns the hint even with no/malformed fields property', () => {
|
|
54
|
+
// The is_custom guard fires before f.fields is read, so its shape is irrelevant.
|
|
55
|
+
assert.deepStrictEqual(familyFieldLines({ name: 'custom', is_custom: true }), [CUSTOM_FAMILY_HINT]);
|
|
56
|
+
assert.deepStrictEqual(familyFieldLines({ is_custom: true, fields: null }), [CUSTOM_FAMILY_HINT]);
|
|
57
|
+
});
|