unbound-cli 1.21.2 → 1.22.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 +1 -1
- package/package.json +1 -1
- package/src/commands/false-positive.js +54 -5
- package/test/false-positive.test.js +72 -0
package/README.md
CHANGED
|
@@ -318,7 +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 (the detected values and prompts are never shown) |
|
|
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 detected values are never shown) |
|
|
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
|
+
| `unbound false-positive list\|mark\|unmark <trace-id>` | Any user: show, mark or unmark a false positive on their own findings (alias `fp`; needs no login on a device set up through MDM; admins can act on any finding in their org) |
|
|
322
322
|
|
|
323
323
|
```bash
|
|
324
324
|
unbound flags terminal --action block,warn --time-window SEVEN_DAYS
|
package/package.json
CHANGED
|
@@ -29,14 +29,29 @@ function chooseFinding(findings, mark, findingId) {
|
|
|
29
29
|
return candidates[0];
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
// The login key first, then the device key MDM setup left in config.json, so a user can skip `unbound login`.
|
|
33
|
+
async function call(method, path, opts) {
|
|
34
|
+
const keys = [...new Set([config.getApiKey(), config.getDeviceKey()].filter(Boolean))];
|
|
35
|
+
if (keys.length === 0) throw new Error('Not logged in. Run `unbound login` first.');
|
|
36
|
+
let refused;
|
|
37
|
+
for (const apiKey of keys) {
|
|
38
|
+
try {
|
|
39
|
+
return await api[method](path, { ...opts, apiKey });
|
|
40
|
+
} catch (err) {
|
|
41
|
+
if (err.statusCode !== 401 && err.statusCode !== 403) throw err;
|
|
42
|
+
refused = err;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
throw refused;
|
|
46
|
+
}
|
|
47
|
+
|
|
32
48
|
async function fetchFindings(trace) {
|
|
33
|
-
const data = await
|
|
49
|
+
const data = await call('get', `${PATH}?trace_id=${trace}`);
|
|
34
50
|
return data.findings || [];
|
|
35
51
|
}
|
|
36
52
|
|
|
37
53
|
async function run(fn) {
|
|
38
54
|
try {
|
|
39
|
-
if (!config.isLoggedIn()) throw new Error('Not logged in. Run `unbound login` first.');
|
|
40
55
|
await fn();
|
|
41
56
|
} catch (err) {
|
|
42
57
|
output.error(err.message);
|
|
@@ -58,15 +73,47 @@ async function list(traceValue, opts) {
|
|
|
58
73
|
output.logTable(HEADERS, findings.map((f) => [String(f.id), f.type, f.action, statusOf(f)]));
|
|
59
74
|
}
|
|
60
75
|
|
|
76
|
+
// Marks every finding of the user's on the trace that is still markable; --mark-all overrides --finding.
|
|
77
|
+
async function markAll(trace, findings, opts) {
|
|
78
|
+
const targets = findings.filter((f) => f.markable);
|
|
79
|
+
if (targets.length === 0) throw new Error('Nothing on this trace is left to mark.');
|
|
80
|
+
if (!opts.yes && !(await confirm(`Mark ${targets.length} findings on trace ${trace} as false positives?`))) {
|
|
81
|
+
output.info('Cancelled.');
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const marked = [];
|
|
85
|
+
for (const finding of targets) {
|
|
86
|
+
try {
|
|
87
|
+
const data = await call('post', PATH, {
|
|
88
|
+
body: { trace_id: trace, guardrail_action_id: finding.id, is_false_positive: true },
|
|
89
|
+
});
|
|
90
|
+
marked.push({ id: finding.id, type: finding.type, message: data.message });
|
|
91
|
+
} catch (err) {
|
|
92
|
+
throw new Error(`Marked ${marked.length} of ${targets.length}; finding ${finding.id} failed: ${err.message}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (opts.json) {
|
|
96
|
+
output.json({ trace_id: trace, marked });
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
for (const m of marked) output.success(`${m.message}: ${m.type} finding ${m.id}.`);
|
|
100
|
+
}
|
|
101
|
+
|
|
61
102
|
async function setMark(traceValue, opts, mark) {
|
|
62
103
|
const trace = resolveTraceId(traceValue);
|
|
63
|
-
const
|
|
104
|
+
const findings = await fetchFindings(trace);
|
|
105
|
+
if (opts.markAll) {
|
|
106
|
+
if (!mark) throw new Error('--mark-all works with mark only.');
|
|
107
|
+
if (opts.finding !== undefined) throw new Error('Use either --finding or --mark-all, not both.');
|
|
108
|
+
return markAll(trace, findings, opts);
|
|
109
|
+
}
|
|
110
|
+
const finding = chooseFinding(findings, mark, opts.finding);
|
|
64
111
|
const verb = mark ? 'Mark' : 'Unmark';
|
|
65
112
|
if (!opts.yes && !(await confirm(`${verb} ${finding.type} finding ${finding.id} on trace ${trace} as a false positive?`))) {
|
|
66
113
|
output.info('Cancelled.');
|
|
67
114
|
return;
|
|
68
115
|
}
|
|
69
|
-
const data = await
|
|
116
|
+
const data = await call('post', PATH, {
|
|
70
117
|
body: { trace_id: trace, guardrail_action_id: finding.id, is_false_positive: mark },
|
|
71
118
|
});
|
|
72
119
|
if (opts.json) {
|
|
@@ -99,6 +146,7 @@ Examples:
|
|
|
99
146
|
.command('mark <trace-id>')
|
|
100
147
|
.description('Mark one of your findings on a trace ID as a false positive.')
|
|
101
148
|
.option('--finding <id>', 'Finding ID, when the trace has more than one (see "list")')
|
|
149
|
+
.option('--mark-all', 'Mark every finding of yours on the trace (overrides --finding)')
|
|
102
150
|
.option('-y, --yes', 'Skip the confirmation prompt')
|
|
103
151
|
.option('--json', 'Output the JSON response')
|
|
104
152
|
.addHelpText('after', `
|
|
@@ -109,6 +157,7 @@ any finding in their organization, or work by action ID with
|
|
|
109
157
|
Examples:
|
|
110
158
|
$ unbound false-positive mark 3f2a6c1e-8b7d-4e2f-9a1c-5d6e7f8a9b0c
|
|
111
159
|
$ unbound false-positive mark 3f2a6c1e-8b7d-4e2f-9a1c-5d6e7f8a9b0c --finding 5521 --yes
|
|
160
|
+
$ unbound false-positive mark 3f2a6c1e-8b7d-4e2f-9a1c-5d6e7f8a9b0c --mark-all --yes
|
|
112
161
|
`)
|
|
113
162
|
.action((trace, opts) => run(() => setMark(trace, opts, true)));
|
|
114
163
|
|
|
@@ -127,4 +176,4 @@ Examples:
|
|
|
127
176
|
.action((trace, opts) => run(() => setMark(trace, opts, false)));
|
|
128
177
|
}
|
|
129
178
|
|
|
130
|
-
module.exports = { register, chooseFinding };
|
|
179
|
+
module.exports = { register, chooseFinding, call };
|
|
@@ -114,3 +114,75 @@ test('an invalid trace ID never reaches the backend', async () => {
|
|
|
114
114
|
server.close();
|
|
115
115
|
}
|
|
116
116
|
});
|
|
117
|
+
|
|
118
|
+
function freshCall({ cliKey, deviceKey, refuse = [] }) {
|
|
119
|
+
for (const m of ['../src/commands/false-positive', '../src/api', '../src/config']) delete require.cache[require.resolve(m)];
|
|
120
|
+
const api = require('../src/api');
|
|
121
|
+
const config = require('../src/config');
|
|
122
|
+
const used = [];
|
|
123
|
+
config.getApiKey = () => cliKey || null;
|
|
124
|
+
config.getDeviceKey = () => deviceKey || null;
|
|
125
|
+
api.get = async (_path, opts) => {
|
|
126
|
+
used.push(opts.apiKey);
|
|
127
|
+
if (refuse.includes(opts.apiKey)) throw new api.ApiError(401, { error: 'no' });
|
|
128
|
+
return { findings: [] };
|
|
129
|
+
};
|
|
130
|
+
return { call: require('../src/commands/false-positive').call, used };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
test('a user with only the device key from MDM setup needs no login', async () => {
|
|
134
|
+
const { call, used } = freshCall({ deviceKey: 'device-key' });
|
|
135
|
+
await call('get', PATH);
|
|
136
|
+
assert.deepEqual(used, ['device-key']);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test('the login key is tried first, and the device key only if it is refused', async () => {
|
|
140
|
+
const ok = freshCall({ cliKey: 'cli-key', deviceKey: 'device-key' });
|
|
141
|
+
await ok.call('get', PATH);
|
|
142
|
+
assert.deepEqual(ok.used, ['cli-key']);
|
|
143
|
+
const expired = freshCall({ cliKey: 'cli-key', deviceKey: 'device-key', refuse: ['cli-key'] });
|
|
144
|
+
await expired.call('get', PATH);
|
|
145
|
+
assert.deepEqual(expired.used, ['cli-key', 'device-key']);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test('with no key at all it says to log in, and a refused key surfaces the refusal', async () => {
|
|
149
|
+
await assert.rejects(freshCall({}).call('get', PATH), /unbound login/);
|
|
150
|
+
await assert.rejects(freshCall({ deviceKey: 'd', refuse: ['d'] }).call('get', PATH), (e) => e.statusCode === 401);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('mark-all marks every markable finding and skips the ones already marked', async () => {
|
|
154
|
+
const second = { ...flagged, id: 13 };
|
|
155
|
+
const { server, seen, port } = await startServer((method) =>
|
|
156
|
+
(method === 'GET' ? [200, { findings: [flagged, marked, second] }] : [200, { message: 'False positive marked' }]));
|
|
157
|
+
try {
|
|
158
|
+
const r = await runCli(port, ['false-positive', 'mark', TRACE, '--mark-all', '--yes']);
|
|
159
|
+
assert.equal(r.code, 0, r.stderr);
|
|
160
|
+
const posts = seen.filter((s) => s.method === 'POST').map((s) => s.body.guardrail_action_id);
|
|
161
|
+
assert.deepEqual(posts, [11, 13]);
|
|
162
|
+
assert.match(r.stdout + r.stderr, /False positive marked/);
|
|
163
|
+
} finally {
|
|
164
|
+
server.close();
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test('mark-all refuses to combine with --finding and posts nothing', async () => {
|
|
169
|
+
const { server, seen, port } = await startServer(() => [200, { findings: [flagged] }]);
|
|
170
|
+
try {
|
|
171
|
+
const r = await runCli(port, ['false-positive', 'mark', TRACE, '--mark-all', '--finding', '11', '--yes']);
|
|
172
|
+
assert.equal(r.code, 1);
|
|
173
|
+
assert.equal(seen.filter((s) => s.method === 'POST').length, 0);
|
|
174
|
+
} finally {
|
|
175
|
+
server.close();
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test('mark-all with nothing left to mark exits 1 and posts nothing', async () => {
|
|
180
|
+
const { server, seen, port } = await startServer(() => [200, { findings: [marked] }]);
|
|
181
|
+
try {
|
|
182
|
+
const r = await runCli(port, ['false-positive', 'mark', TRACE, '--mark-all', '--yes']);
|
|
183
|
+
assert.equal(r.code, 1);
|
|
184
|
+
assert.equal(seen.filter((s) => s.method === 'POST').length, 0);
|
|
185
|
+
} finally {
|
|
186
|
+
server.close();
|
|
187
|
+
}
|
|
188
|
+
});
|