unbound-cli 1.20.6 → 1.21.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/README.md CHANGED
@@ -318,6 +318,7 @@ Listing needs Admin, Manager or Analytics Viewer. `mark-fp` needs Admin.
318
318
  | `unbound flags data-security` | PII and secrets a guardrail found (values masked; `--reveal` shows them) |
319
319
  | `unbound flags data-security mark-fp <action-id>` | Mark a detection as a false positive |
320
320
  | `unbound trace <trace-id>` | The log, terminal runs and data security detections for one trace ID (prompts and values hidden; `--reveal` shows them) |
321
+ | `unbound false-positive list\|mark\|unmark <trace-id>` | Any user: show, mark or unmark a false positive on their own findings (alias `fp`; admins can act on any finding in their org) |
321
322
 
322
323
  ```bash
323
324
  unbound flags terminal --action block,warn --time-window SEVEN_DAYS
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unbound-cli",
3
- "version": "1.20.6",
3
+ "version": "1.21.0",
4
4
  "description": "CLI tool for Unbound - AI Gateway management",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -0,0 +1,130 @@
1
+ const api = require('../api');
2
+ const config = require('../config');
3
+ const output = require('../output');
4
+ const { confirm } = require('../utils');
5
+ const { resolveTraceId } = require('../lib/flags');
6
+
7
+ const PATH = '/api/v1/analytics/guardrails/my_false_positive/';
8
+ const HEADERS = ['ID', 'Type', 'Action', 'Status'];
9
+
10
+ function statusOf(finding) {
11
+ return finding.is_false_positive ? 'False positive' : 'Flagged';
12
+ }
13
+
14
+ // The one finding a mark or unmark acts on: the --finding id, or the only candidate on the trace.
15
+ function chooseFinding(findings, mark, findingId) {
16
+ if (findingId !== undefined) {
17
+ const id = Number(findingId);
18
+ const found = findings.find((f) => f.id === id);
19
+ if (!found) throw new Error(`Finding ${findingId} is not one of your findings on this trace.`);
20
+ return found;
21
+ }
22
+ const candidates = findings.filter((f) => (mark ? f.markable : f.unmarkable));
23
+ if (candidates.length === 0) {
24
+ throw new Error(mark ? 'Nothing on this trace is left to mark.' : 'No mark of yours on this trace can be unmarked.');
25
+ }
26
+ if (candidates.length > 1) {
27
+ throw new Error(`${candidates.length} findings match (IDs ${candidates.map((f) => f.id).join(', ')}). Pass --finding <id>.`);
28
+ }
29
+ return candidates[0];
30
+ }
31
+
32
+ async function fetchFindings(trace) {
33
+ const data = await api.get(`${PATH}?trace_id=${trace}`);
34
+ return data.findings || [];
35
+ }
36
+
37
+ async function run(fn) {
38
+ try {
39
+ if (!config.isLoggedIn()) throw new Error('Not logged in. Run `unbound login` first.');
40
+ await fn();
41
+ } catch (err) {
42
+ output.error(err.message);
43
+ process.exitCode = 1;
44
+ }
45
+ }
46
+
47
+ async function list(traceValue, opts) {
48
+ const trace = resolveTraceId(traceValue);
49
+ const findings = await fetchFindings(trace);
50
+ if (opts.json) {
51
+ output.json({ trace_id: trace, findings });
52
+ return;
53
+ }
54
+ if (findings.length === 0) {
55
+ output.info(`No findings of yours on trace ${trace}.`);
56
+ return;
57
+ }
58
+ output.logTable(HEADERS, findings.map((f) => [String(f.id), f.type, f.action, statusOf(f)]));
59
+ }
60
+
61
+ async function setMark(traceValue, opts, mark) {
62
+ const trace = resolveTraceId(traceValue);
63
+ const finding = chooseFinding(await fetchFindings(trace), mark, opts.finding);
64
+ const verb = mark ? 'Mark' : 'Unmark';
65
+ if (!opts.yes && !(await confirm(`${verb} ${finding.type} finding ${finding.id} on trace ${trace} as a false positive?`))) {
66
+ output.info('Cancelled.');
67
+ return;
68
+ }
69
+ const data = await api.post(PATH, {
70
+ body: { trace_id: trace, guardrail_action_id: finding.id, is_false_positive: mark },
71
+ });
72
+ if (opts.json) {
73
+ output.json(data);
74
+ return;
75
+ }
76
+ output.success(`${data.message}: ${finding.type} finding ${finding.id}.`);
77
+ }
78
+
79
+ function register(program) {
80
+ const fp = program
81
+ .command('false-positive')
82
+ .alias('fp')
83
+ .description('Mark or unmark a data security finding of yours as a false positive, by trace ID.');
84
+
85
+ fp
86
+ .command('list <trace-id>')
87
+ .description('Show your findings on a trace ID.')
88
+ .option('--json', 'Output JSON')
89
+ .addHelpText('after', `
90
+ The trace ID is the UUID shown under "Enforced by Unbound" on a blocked request.
91
+ Only findings on your own requests are listed. Detected values are never shown.
92
+
93
+ Examples:
94
+ $ unbound false-positive list 3f2a6c1e-8b7d-4e2f-9a1c-5d6e7f8a9b0c
95
+ `)
96
+ .action((trace, opts) => run(() => list(trace, opts)));
97
+
98
+ fp
99
+ .command('mark <trace-id>')
100
+ .description('Mark one of your findings on a trace ID as a false positive.')
101
+ .option('--finding <id>', 'Finding ID, when the trace has more than one (see "list")')
102
+ .option('-y, --yes', 'Skip the confirmation prompt')
103
+ .option('--json', 'Output the JSON response')
104
+ .addHelpText('after', `
105
+ You can only mark findings on your own requests. Admins can use this command on
106
+ any finding in their organization, or work by action ID with
107
+ \`unbound flags data-security mark-fp\`.
108
+
109
+ Examples:
110
+ $ unbound false-positive mark 3f2a6c1e-8b7d-4e2f-9a1c-5d6e7f8a9b0c
111
+ $ unbound false-positive mark 3f2a6c1e-8b7d-4e2f-9a1c-5d6e7f8a9b0c --finding 5521 --yes
112
+ `)
113
+ .action((trace, opts) => run(() => setMark(trace, opts, true)));
114
+
115
+ fp
116
+ .command('unmark <trace-id>')
117
+ .description('Unmark a false positive you marked.')
118
+ .option('--finding <id>', 'Finding ID, when the trace has more than one (see "list")')
119
+ .option('-y, --yes', 'Skip the confirmation prompt')
120
+ .option('--json', 'Output the JSON response')
121
+ .addHelpText('after', `
122
+ You can unmark only a mark you made. Admins can unmark any.
123
+
124
+ Examples:
125
+ $ unbound false-positive unmark 3f2a6c1e-8b7d-4e2f-9a1c-5d6e7f8a9b0c
126
+ `)
127
+ .action((trace, opts) => run(() => setMark(trace, opts, false)));
128
+ }
129
+
130
+ module.exports = { register, chooseFinding };
@@ -243,7 +243,8 @@ same value for everyone in your organization.
243
243
  Examples:
244
244
  $ unbound flags data-security mark-fp 5521
245
245
 
246
- Admin only.
246
+ Admin only. To mark a finding of your own without a role, use
247
+ \`unbound false-positive mark <trace-id>\`.
247
248
  `)
248
249
  .action((id, opts) => run(() => markDataSecurityFalsePositive(id, opts)));
249
250
 
package/src/index.js CHANGED
@@ -183,6 +183,11 @@ FLAGS AND FALSE POSITIVES (Admin/Analytics Viewer/Manager; mark-fp is Admin only
183
183
  Mark a detection as a false positive
184
184
  $ unbound trace <trace-id> Log, terminal runs and data security for one trace ID
185
185
 
186
+ FALSE POSITIVES ON YOUR OWN REQUESTS (any user)
187
+ $ unbound false-positive list <trace-id> Your findings on a blocked request
188
+ $ unbound false-positive mark <trace-id> Mark one as a false positive
189
+ $ unbound false-positive unmark <trace-id> Undo a mark you made
190
+
186
191
  CHAT (Admin/Analytics Viewer/Manager)
187
192
  $ unbound chat Interactive REPL (multi-turn, in-memory history)
188
193
  $ unbound chat -m "show cost by provider for the last 30 days"
@@ -228,6 +233,7 @@ require('./commands/tools').register(program);
228
233
  require('./commands/analytics').register(program);
229
234
  require('./commands/flags').register(program);
230
235
  require('./commands/trace').register(program);
236
+ require('./commands/false-positive').register(program);
231
237
  require('./commands/download').register(program);
232
238
  require('./commands/setup').register(program);
233
239
  require('./commands/discover').register(program);
@@ -0,0 +1,116 @@
1
+ const { test } = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+ const fs = require('fs');
4
+ const http = require('http');
5
+ const os = require('os');
6
+ const path = require('path');
7
+ const { spawn } = require('child_process');
8
+
9
+ const { chooseFinding } = require('../src/commands/false-positive');
10
+
11
+ const TRACE = '225c6413-26e4-4cd2-85a6-8c5ccfc3c5ef';
12
+ const PATH = '/api/v1/analytics/guardrails/my_false_positive/';
13
+ const flagged = { id: 11, type: 'API Key', action: 'BLOCK', is_false_positive: false, markable: true, unmarkable: false };
14
+ const marked = { id: 12, type: 'PII', action: 'AUDIT', is_false_positive: true, markable: false, unmarkable: true };
15
+
16
+ test('mark picks the only unmarked finding and unmark the only marked one', () => {
17
+ assert.equal(chooseFinding([flagged, marked], true).id, 11);
18
+ assert.equal(chooseFinding([flagged, marked], false).id, 12);
19
+ });
20
+
21
+ test('--finding picks that finding and refuses one that is not on the list', () => {
22
+ assert.equal(chooseFinding([flagged, marked], true, '12').id, 12);
23
+ assert.throws(() => chooseFinding([flagged], true, '99'), /not one of your findings/);
24
+ });
25
+
26
+ test('several candidates need --finding, and none is an error', () => {
27
+ const other = { ...flagged, id: 13 };
28
+ assert.throws(() => chooseFinding([flagged, other], true), /Pass --finding/);
29
+ assert.throws(() => chooseFinding([marked], true), /left to mark/);
30
+ assert.throws(() => chooseFinding([flagged], false), /can be unmarked/);
31
+ });
32
+
33
+ function startServer(routes) {
34
+ const seen = [];
35
+ const server = http.createServer((req, res) => {
36
+ let body = '';
37
+ req.on('data', (c) => { body += c; });
38
+ req.on('end', () => {
39
+ const url = new URL(req.url, 'http://x');
40
+ seen.push({ method: req.method, path: url.pathname, query: url.searchParams, body: body ? JSON.parse(body) : null });
41
+ const [status, payload] = routes(req.method) || [404, { error: 'no route' }];
42
+ res.writeHead(status, { 'Content-Type': 'application/json' });
43
+ res.end(JSON.stringify(payload));
44
+ });
45
+ });
46
+ return new Promise((resolve) => server.listen(0, '127.0.0.1', () => resolve({ server, seen, port: server.address().port })));
47
+ }
48
+
49
+ function runCli(port, args) {
50
+ const home = fs.mkdtempSync(path.join(os.tmpdir(), 'fp-test-'));
51
+ fs.mkdirSync(path.join(home, '.unbound'));
52
+ fs.writeFileSync(path.join(home, '.unbound', 'config.json'),
53
+ JSON.stringify({ api_key: 'test-key', base_url: `http://127.0.0.1:${port}` }));
54
+ return new Promise((resolve) => {
55
+ const child = spawn('node', [path.join(__dirname, '..', 'src', 'index.js'), ...args], {
56
+ env: { ...process.env, HOME: home, UNBOUND_TOKEN: 'test-key', UNBOUND_API_URL: '', UNBOUND_TELEMETRY: '0', UNBOUND_NO_UPDATE_CHECK: '1' },
57
+ });
58
+ let stdout = '';
59
+ let stderr = '';
60
+ child.stdout.on('data', (d) => { stdout += d; });
61
+ child.stderr.on('data', (d) => { stderr += d; });
62
+ child.on('close', (code) => {
63
+ fs.rmSync(home, { recursive: true, force: true });
64
+ resolve({ code, stdout, stderr });
65
+ });
66
+ });
67
+ }
68
+
69
+ test('mark looks the trace up, then posts the chosen finding', async () => {
70
+ const { server, seen, port } = await startServer((method) =>
71
+ (method === 'GET' ? [200, { findings: [flagged] }] : [200, { message: 'False positive marked' }]));
72
+ try {
73
+ const r = await runCli(port, ['false-positive', 'mark', TRACE.toUpperCase(), '--yes']);
74
+ assert.equal(r.code, 0, r.stderr);
75
+ assert.equal(seen[0].path, PATH);
76
+ assert.equal(seen[0].query.get('trace_id'), TRACE);
77
+ assert.deepEqual(seen[1].body, { trace_id: TRACE, guardrail_action_id: 11, is_false_positive: true });
78
+ assert.match(r.stdout + r.stderr, /False positive marked/);
79
+ } finally {
80
+ server.close();
81
+ }
82
+ });
83
+
84
+ test('unmark posts is_false_positive false', async () => {
85
+ const { server, seen, port } = await startServer((method) =>
86
+ (method === 'GET' ? [200, { findings: [marked] }] : [200, { message: 'False positive unmarked' }]));
87
+ try {
88
+ const r = await runCli(port, ['fp', 'unmark', TRACE, '-y']);
89
+ assert.equal(r.code, 0, r.stderr);
90
+ assert.deepEqual(seen[1].body, { trace_id: TRACE, guardrail_action_id: 12, is_false_positive: false });
91
+ } finally {
92
+ server.close();
93
+ }
94
+ });
95
+
96
+ test('a trace with none of your findings posts nothing and exits 1', async () => {
97
+ const { server, seen, port } = await startServer(() => [200, { findings: [] }]);
98
+ try {
99
+ const r = await runCli(port, ['false-positive', 'mark', TRACE, '--yes']);
100
+ assert.equal(r.code, 1);
101
+ assert.equal(seen.filter((s) => s.method === 'POST').length, 0);
102
+ } finally {
103
+ server.close();
104
+ }
105
+ });
106
+
107
+ test('an invalid trace ID never reaches the backend', async () => {
108
+ const { server, seen, port } = await startServer(() => [200, { findings: [] }]);
109
+ try {
110
+ const r = await runCli(port, ['false-positive', 'list', 'not-a-uuid']);
111
+ assert.equal(r.code, 1);
112
+ assert.equal(seen.length, 0);
113
+ } finally {
114
+ server.close();
115
+ }
116
+ });