codeant-cli 0.1.1 → 0.1.3
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 +45 -0
- package/package.json +1 -1
- package/src/commands/securityAnalysis.js +284 -0
- package/src/commands/staticAnalysis.js +405 -0
- package/src/components/Welcome.js +143 -0
- package/src/index.js +92 -25
- package/src/utils/commonApiHelper.js +84 -0
- package/src/utils/secretsApiHelper.js +15 -73
- package/src/utils/securityAnalysisApiHelper.js +118 -0
- package/src/utils/staticAnalysisApiHelper.js +118 -0
package/README.md
CHANGED
|
@@ -115,6 +115,51 @@ Use `--include` and `--exclude` with glob patterns to filter files:
|
|
|
115
115
|
- `MEDIUM` - Medium confidence, may need review
|
|
116
116
|
- `FALSE_POSITIVE` - Detected but likely not a real secret (always ignored)
|
|
117
117
|
|
|
118
|
+
#### `static-analysis`
|
|
119
|
+
|
|
120
|
+
Run static code analysis to detect code quality issues, bugs, and code smells.
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
codeant static-analysis [options]
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**Options:**
|
|
127
|
+
|
|
128
|
+
| Option | Description |
|
|
129
|
+
|--------|-------------|
|
|
130
|
+
| `--staged` | Scan only staged files (default) |
|
|
131
|
+
| `--all` | Scan all changed files compared to base branch |
|
|
132
|
+
| `--uncommitted` | Scan all uncommitted changes |
|
|
133
|
+
| `--last-commit` | Scan files from the last commit |
|
|
134
|
+
| `--fail-on <level>` | Fail on issues at or above this level (default: CRITICAL) |
|
|
135
|
+
| `--auto-fix` | Automatically apply fixes when available |
|
|
136
|
+
| `--include <patterns>` | Comma-separated glob patterns to include files |
|
|
137
|
+
| `--exclude <patterns>` | Comma-separated glob patterns to exclude files |
|
|
138
|
+
|
|
139
|
+
**Issue Levels:** `BLOCKER` > `CRITICAL` > `MAJOR` > `MINOR` > `INFO`
|
|
140
|
+
|
|
141
|
+
#### `security-analysis`
|
|
142
|
+
|
|
143
|
+
Run security analysis to detect vulnerabilities in your code.
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
codeant security-analysis [options]
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**Options:**
|
|
150
|
+
|
|
151
|
+
| Option | Description |
|
|
152
|
+
|--------|-------------|
|
|
153
|
+
| `--staged` | Scan only staged files (default) |
|
|
154
|
+
| `--all` | Scan all changed files compared to base branch |
|
|
155
|
+
| `--uncommitted` | Scan all uncommitted changes |
|
|
156
|
+
| `--last-commit` | Scan files from the last commit |
|
|
157
|
+
| `--fail-on <level>` | Fail on issues at or above this level (default: HIGH) |
|
|
158
|
+
| `--include <patterns>` | Comma-separated glob patterns to include files |
|
|
159
|
+
| `--exclude <patterns>` | Comma-separated glob patterns to exclude files |
|
|
160
|
+
|
|
161
|
+
**Severity Levels:** `CRITICAL` > `HIGH` > `MEDIUM`
|
|
162
|
+
|
|
118
163
|
#### `set-base-url <url>`
|
|
119
164
|
|
|
120
165
|
Set a custom API base URL.
|
package/package.json
CHANGED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import React, { useEffect, useState } from 'react';
|
|
2
|
+
import { Box, Text, useApp } from 'ink';
|
|
3
|
+
import { getConfigValue } from '../utils/config.js';
|
|
4
|
+
import { fetchApi } from '../utils/fetchApi.js';
|
|
5
|
+
import SecurityAnalysisApiHelper from '../utils/securityAnalysisApiHelper.js';
|
|
6
|
+
|
|
7
|
+
export default function SecurityAnalysis({ scanType = 'staged-only', failOn = 'HIGH', include = [], exclude = [] }) {
|
|
8
|
+
const { exit } = useApp();
|
|
9
|
+
const [status, setStatus] = useState('initializing');
|
|
10
|
+
const [issues, setIssues] = useState([]);
|
|
11
|
+
const [error, setError] = useState(null);
|
|
12
|
+
const [fileCount, setFileCount] = useState(0);
|
|
13
|
+
|
|
14
|
+
const apiKey = getConfigValue('apiKey');
|
|
15
|
+
|
|
16
|
+
// Handle not logged in state
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (!apiKey) {
|
|
19
|
+
const id = setTimeout(() => exit(new Error('Not logged in')), 0);
|
|
20
|
+
return () => clearTimeout(id);
|
|
21
|
+
}
|
|
22
|
+
}, [apiKey, exit]);
|
|
23
|
+
|
|
24
|
+
// Check if logged in
|
|
25
|
+
if (!apiKey) {
|
|
26
|
+
return React.createElement(
|
|
27
|
+
Box,
|
|
28
|
+
{ flexDirection: 'column', padding: 1 },
|
|
29
|
+
React.createElement(Text, { color: 'red' }, '✗ Not logged in.'),
|
|
30
|
+
React.createElement(Text, { color: 'gray' }, 'Run "codeant login" to authenticate.'),
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Helper to check if an issue should cause failure based on failOn level
|
|
35
|
+
const shouldFailOn = (severity) => {
|
|
36
|
+
const score = severity?.toUpperCase();
|
|
37
|
+
if (score === 'FALSE_POSITIVE') return false;
|
|
38
|
+
if (failOn === 'CRITICAL') return score === 'CRITICAL';
|
|
39
|
+
if (failOn === 'HIGH') return score === 'HIGH' || score === 'CRITICAL';
|
|
40
|
+
if (failOn === 'MEDIUM') return score === 'HIGH' || score === 'MEDIUM';
|
|
41
|
+
return true; // 'all' - fail on any non-false-positive
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
useEffect(() => {
|
|
45
|
+
let cancelled = false;
|
|
46
|
+
|
|
47
|
+
async function scanForIssues() {
|
|
48
|
+
try {
|
|
49
|
+
if (cancelled) return;
|
|
50
|
+
setStatus('scanning');
|
|
51
|
+
|
|
52
|
+
// Initialize git helper and get staged files
|
|
53
|
+
const helper = new SecurityAnalysisApiHelper(process.cwd());
|
|
54
|
+
await helper.init();
|
|
55
|
+
|
|
56
|
+
if (cancelled) return;
|
|
57
|
+
const requestBody = await helper.buildSecurityAnalysisApiRequest(scanType, include, exclude);
|
|
58
|
+
if (cancelled) return;
|
|
59
|
+
|
|
60
|
+
if (requestBody.files.length === 0) {
|
|
61
|
+
if (!cancelled) setStatus('no_files');
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Call the security analysis API
|
|
66
|
+
const response = await fetchApi(
|
|
67
|
+
'/extension/pr-review/security-analysis',
|
|
68
|
+
'POST',
|
|
69
|
+
requestBody,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
if (cancelled) return;
|
|
73
|
+
|
|
74
|
+
// Transform security issues response to the expected format
|
|
75
|
+
const detectedIssues = (response.securityIssues || []).map(file => ({
|
|
76
|
+
file_path: file.file_path,
|
|
77
|
+
issues: (file.security_issues || []).map(issue => ({
|
|
78
|
+
line_number: issue.line_number,
|
|
79
|
+
end_line: issue.end_line,
|
|
80
|
+
type: issue.issue_text,
|
|
81
|
+
confidence_score: issue.false_positive ? 'FALSE_POSITIVE' : issue.severity,
|
|
82
|
+
test_id: issue.test_id,
|
|
83
|
+
confidence: issue.confidence,
|
|
84
|
+
likelihood: issue.likelihood,
|
|
85
|
+
cwe: issue.cwe,
|
|
86
|
+
owasp: issue.owasp,
|
|
87
|
+
})),
|
|
88
|
+
}));
|
|
89
|
+
|
|
90
|
+
// Filter to only include files with actual issues
|
|
91
|
+
const filesWithIssues = detectedIssues.filter(
|
|
92
|
+
file => file.issues && file.issues.length > 0,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
if (!cancelled) {
|
|
96
|
+
setIssues(filesWithIssues);
|
|
97
|
+
setStatus('done');
|
|
98
|
+
}
|
|
99
|
+
} catch (err) {
|
|
100
|
+
if (!cancelled) {
|
|
101
|
+
setError(err.message);
|
|
102
|
+
setStatus('error');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
scanForIssues();
|
|
108
|
+
|
|
109
|
+
return () => {
|
|
110
|
+
cancelled = true;
|
|
111
|
+
};
|
|
112
|
+
}, [scanType, include, exclude]);
|
|
113
|
+
|
|
114
|
+
// Handle exit after status changes
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
if (status === 'done') {
|
|
117
|
+
// Check if any issues should cause failure based on failOn level
|
|
118
|
+
const hasBlockingIssues = issues.some(file =>
|
|
119
|
+
file.issues.some(issue => issue.false_positive !== true &&shouldFailOn(issue.severity)),
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
if (hasBlockingIssues) {
|
|
123
|
+
setTimeout(() => {
|
|
124
|
+
process.exitCode = 1;
|
|
125
|
+
exit(new Error('Security issues detected'));
|
|
126
|
+
}, 100);
|
|
127
|
+
} else {
|
|
128
|
+
setTimeout(() => exit(), 100);
|
|
129
|
+
}
|
|
130
|
+
} else if (status === 'no_files') {
|
|
131
|
+
setTimeout(() => exit(), 100);
|
|
132
|
+
} else if (status === 'error') {
|
|
133
|
+
setTimeout(() => exit(new Error(error)), 100);
|
|
134
|
+
}
|
|
135
|
+
}, [status, issues]);
|
|
136
|
+
|
|
137
|
+
// Render: Initializing
|
|
138
|
+
if (status === 'initializing') {
|
|
139
|
+
return React.createElement(
|
|
140
|
+
Box,
|
|
141
|
+
{ flexDirection: 'column', padding: 1 },
|
|
142
|
+
React.createElement(Text, { color: 'cyan' }, 'Initializing...'),
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Render: Scanning
|
|
147
|
+
if (status === 'scanning') {
|
|
148
|
+
return React.createElement(
|
|
149
|
+
Box,
|
|
150
|
+
{ flexDirection: 'column', padding: 1 },
|
|
151
|
+
React.createElement(Text, { color: 'cyan' }, `Scanning ${fileCount} file(s) for security issues...`),
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Render: No files to scan
|
|
156
|
+
if (status === 'no_files') {
|
|
157
|
+
const noFilesMessages = {
|
|
158
|
+
'staged-only': 'Stage some changes first with "git add".',
|
|
159
|
+
'branch-diff': 'No changes found compared to the base branch.',
|
|
160
|
+
'uncommitted': 'No uncommitted changes found.',
|
|
161
|
+
'last-commit': 'No files found in the last commit.',
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
return React.createElement(
|
|
165
|
+
Box,
|
|
166
|
+
{ flexDirection: 'column', padding: 1 },
|
|
167
|
+
React.createElement(Text, { color: 'yellow' }, 'No files to scan.'),
|
|
168
|
+
React.createElement(Text, { color: 'gray' }, noFilesMessages[scanType] || 'No files found.'),
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Render: Error
|
|
173
|
+
if (status === 'error') {
|
|
174
|
+
return React.createElement(
|
|
175
|
+
Box,
|
|
176
|
+
{ flexDirection: 'column', padding: 1 },
|
|
177
|
+
React.createElement(Text, { color: 'red' }, '✗ Error: ', error),
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Render: Done with issues found
|
|
182
|
+
if (status === 'done' && issues.length > 0) {
|
|
183
|
+
const allIssues = issues.flatMap(file =>
|
|
184
|
+
file.issues.map(s => ({ ...s, file_path: file.file_path })),
|
|
185
|
+
);
|
|
186
|
+
|
|
187
|
+
const blockingIssues = allIssues.filter(s => s.false_positive !== true && shouldFailOn(s.severity));
|
|
188
|
+
const falsePositives = allIssues.filter(s => s.false_positive !== true && !shouldFailOn(s.severity));
|
|
189
|
+
|
|
190
|
+
const hasBlockingIssues = blockingIssues.length > 0;
|
|
191
|
+
|
|
192
|
+
// Group by file for display
|
|
193
|
+
const groupByFile = (issuesList) => {
|
|
194
|
+
const grouped = {};
|
|
195
|
+
issuesList.forEach(s => {
|
|
196
|
+
if (!grouped[s.file_path]) grouped[s.file_path] = [];
|
|
197
|
+
grouped[s.file_path].push(s);
|
|
198
|
+
});
|
|
199
|
+
return grouped;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const blockingByFile = groupByFile(blockingIssues);
|
|
203
|
+
const falsePositivesByFile = groupByFile(falsePositives);
|
|
204
|
+
|
|
205
|
+
const elements = [
|
|
206
|
+
// Header
|
|
207
|
+
React.createElement(
|
|
208
|
+
Text,
|
|
209
|
+
{ key: 'header', color: hasBlockingIssues ? 'red' : 'yellow', bold: true },
|
|
210
|
+
hasBlockingIssues
|
|
211
|
+
? `✗ ${blockingIssues.length} security issue(s) found!`
|
|
212
|
+
: `⚠ ${falsePositives.length} potential security issue(s) found (ignored)`,
|
|
213
|
+
),
|
|
214
|
+
React.createElement(Text, { key: 'spacer1' }, ''),
|
|
215
|
+
];
|
|
216
|
+
|
|
217
|
+
// Blocking issues
|
|
218
|
+
if (blockingIssues.length > 0) {
|
|
219
|
+
Object.entries(blockingByFile).forEach(([filePath, fileIssues]) => {
|
|
220
|
+
elements.push(
|
|
221
|
+
React.createElement(Text, { key: `file-${filePath}`, color: 'yellow', bold: true }, filePath),
|
|
222
|
+
);
|
|
223
|
+
fileIssues.forEach((issue, idx) => {
|
|
224
|
+
elements.push(
|
|
225
|
+
React.createElement(
|
|
226
|
+
Text,
|
|
227
|
+
{ key: `issue-${filePath}-${idx}`, color: 'red' },
|
|
228
|
+
` Line ${issue.line_number}: ${issue.type} (${issue.confidence_score})`,
|
|
229
|
+
),
|
|
230
|
+
);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// False positives / ignored
|
|
236
|
+
if (falsePositives.length > 0) {
|
|
237
|
+
if (blockingIssues.length > 0) {
|
|
238
|
+
elements.push(React.createElement(Text, { key: 'spacer2' }, ''));
|
|
239
|
+
}
|
|
240
|
+
elements.push(
|
|
241
|
+
React.createElement(Text, { key: 'fp-header', color: 'gray' }, 'Ignored (false positives):'),
|
|
242
|
+
);
|
|
243
|
+
Object.entries(falsePositivesByFile).forEach(([filePath, fileIssues]) => {
|
|
244
|
+
elements.push(
|
|
245
|
+
React.createElement(Text, { key: `fp-file-${filePath}`, color: 'gray' }, ` ${filePath}`),
|
|
246
|
+
);
|
|
247
|
+
fileIssues.forEach((issue, idx) => {
|
|
248
|
+
elements.push(
|
|
249
|
+
React.createElement(
|
|
250
|
+
Text,
|
|
251
|
+
{ key: `fp-issue-${filePath}-${idx}`, color: 'gray', dimColor: true },
|
|
252
|
+
` Line ${issue.line_number}: ${issue.type} (${issue.confidence_score})`,
|
|
253
|
+
),
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
elements.push(React.createElement(Text, { key: 'spacer3' }, ''));
|
|
260
|
+
|
|
261
|
+
if (hasBlockingIssues) {
|
|
262
|
+
elements.push(
|
|
263
|
+
React.createElement(Text, { key: 'footer', color: 'gray' }, 'Fix security issues before committing.'),
|
|
264
|
+
);
|
|
265
|
+
} else {
|
|
266
|
+
elements.push(
|
|
267
|
+
React.createElement(Text, { key: 'footer', color: 'green' }, '✓ Commit allowed (only false positives found)'),
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return React.createElement(
|
|
272
|
+
Box,
|
|
273
|
+
{ flexDirection: 'column', padding: 1 },
|
|
274
|
+
...elements,
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Render: Done with no issues
|
|
279
|
+
return React.createElement(
|
|
280
|
+
Box,
|
|
281
|
+
{ flexDirection: 'column', padding: 1 },
|
|
282
|
+
React.createElement(Text, { color: 'green' }, '✓ No security issues found'),
|
|
283
|
+
);
|
|
284
|
+
}
|