@codacy/tools-trivy-0 0.1.0 → 0.2.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.
Files changed (2) hide show
  1. package/dist/index.js +72 -11
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -216,21 +216,33 @@ async function parseVulnerabilities(output, enabledPatterns, targetFilesSet, rep
216
216
  linesCache.set(relPath, await readFileLines(import_path.default.join(repositoryRoot, relPath)));
217
217
  }
218
218
  const lines = linesCache.get(relPath);
219
+ const pkgLineByID = /* @__PURE__ */ new Map();
220
+ if (result.Packages) {
221
+ for (const pkg of result.Packages) {
222
+ if (pkg.Locations && pkg.Locations.length > 0 && pkg.Locations[0].StartLine > 0) {
223
+ pkgLineByID.set(pkg.ID, pkg.Locations[0].StartLine);
224
+ }
225
+ }
226
+ }
219
227
  for (const vuln of result.Vulnerabilities) {
220
228
  const patternId = VULN_SEVERITY_TO_PATTERN[vuln.Severity];
221
229
  if (!patternId) continue;
222
230
  if (enabledPatterns.size > 0 && !enabledPatterns.has(patternId)) continue;
223
- const meta = PATTERN_META.get(patternId) ?? { category: "Security", severity: "Info" };
231
+ const meta = PATTERN_META.get(patternId) ?? {
232
+ category: "Security",
233
+ severity: "Info"
234
+ };
224
235
  const title = vuln.Title ? `: ${vuln.Title}` : "";
225
236
  const fix = vuln.FixedVersion ? `Update to ${vuln.FixedVersion}.` : "No fix available.";
237
+ const line = vuln.PkgID && pkgLineByID.get(vuln.PkgID) || 1;
226
238
  issues.push({
227
239
  filePath: relPath,
228
240
  patternId,
229
241
  toolId: TOOL_ID,
230
- line: 1,
242
+ line,
231
243
  column: 1,
232
244
  message: `Insecure dependency ${vuln.PkgName}@${vuln.InstalledVersion} (${vuln.VulnerabilityID}${title}). ${fix}`,
233
- lineContent: getLineContent(lines, 1),
245
+ lineContent: getLineContent(lines, line),
234
246
  category: meta.category,
235
247
  severity: meta.severity
236
248
  });
@@ -243,7 +255,10 @@ async function parseSecrets(output, enabledPatterns, targetFilesSet, repositoryR
243
255
  if (!output.Results) return issues;
244
256
  const patternId = "Trivy_secret";
245
257
  if (enabledPatterns.size > 0 && !enabledPatterns.has(patternId)) return issues;
246
- const meta = PATTERN_META.get(patternId) ?? { category: "Security", severity: "Error" };
258
+ const meta = PATTERN_META.get(patternId) ?? {
259
+ category: "Security",
260
+ severity: "Error"
261
+ };
247
262
  const linesCache = /* @__PURE__ */ new Map();
248
263
  for (const result of output.Results) {
249
264
  if (!result.Secrets) continue;
@@ -277,7 +292,20 @@ var adapter = {
277
292
  preferredVersion: "0.69.3",
278
293
  compatibleVersions: ">=0.50.0",
279
294
  website: "https://trivy.dev/",
280
- languages: ["Python", "JavaScript", "TypeScript", "Go", "Java", "Ruby", "Rust", "PHP", "C#", "Dart", "Elixir", "Swift"],
295
+ languages: [
296
+ "Python",
297
+ "JavaScript",
298
+ "TypeScript",
299
+ "Go",
300
+ "Java",
301
+ "Ruby",
302
+ "Rust",
303
+ "PHP",
304
+ "C#",
305
+ "Dart",
306
+ "Elixir",
307
+ "Swift"
308
+ ],
281
309
  filePatterns: ["*"],
282
310
  supportedOS: ["linux", "darwin"],
283
311
  supportedArch: ["x64", "arm64"],
@@ -299,13 +327,25 @@ var adapter = {
299
327
  const codacyBin = import_path.default.join(ctx.toolInstallDir, BINARY);
300
328
  const codacyVersion = await getVersionFromBinary(codacyBin);
301
329
  if (codacyVersion) {
302
- ctx.logger.debug("Found trivy in codacy install dir", { toolId: TOOL_ID, version: codacyVersion });
303
- return { available: true, version: codacyVersion, path: codacyBin, installation: "codacy" };
330
+ ctx.logger.debug("Found trivy in codacy install dir", {
331
+ toolId: TOOL_ID,
332
+ version: codacyVersion
333
+ });
334
+ return {
335
+ available: true,
336
+ version: codacyVersion,
337
+ path: codacyBin,
338
+ installation: "codacy"
339
+ };
304
340
  }
305
341
  const globalVersion = await getVersionFromBinary(BINARY);
306
342
  if (globalVersion) {
307
343
  ctx.logger.debug("Found trivy on global PATH", { toolId: TOOL_ID, version: globalVersion });
308
- return { available: true, version: globalVersion, installation: "global" };
344
+ return {
345
+ available: true,
346
+ version: globalVersion,
347
+ installation: "global"
348
+ };
309
349
  }
310
350
  ctx.logger.debug("trivy not found", { toolId: TOOL_ID });
311
351
  return {
@@ -433,7 +473,14 @@ var adapter = {
433
473
  }
434
474
  try {
435
475
  if (runVulnScan) {
436
- const vulnArgs = [...baseArgs, "--scanners", "vuln", "--pkg-types", "library"];
476
+ const vulnArgs = [
477
+ ...baseArgs,
478
+ "--scanners",
479
+ "vuln",
480
+ "--pkg-types",
481
+ "library",
482
+ "--list-all-pkgs"
483
+ ];
437
484
  const severities = [];
438
485
  if (enabledPatterns.size === 0) {
439
486
  severities.push("CRITICAL", "HIGH", "MEDIUM", "LOW");
@@ -450,14 +497,28 @@ var adapter = {
450
497
  vulnArgs.push(ctx.repositoryRoot);
451
498
  const vulnOutput = await runTrivyScan(binary, vulnArgs, ctx.repositoryRoot, errors);
452
499
  if (vulnOutput) {
453
- issues.push(...await parseVulnerabilities(vulnOutput, enabledPatterns, targetFilesSet, ctx.repositoryRoot));
500
+ issues.push(
501
+ ...await parseVulnerabilities(
502
+ vulnOutput,
503
+ enabledPatterns,
504
+ targetFilesSet,
505
+ ctx.repositoryRoot
506
+ )
507
+ );
454
508
  }
455
509
  }
456
510
  if (runSecretScan) {
457
511
  const secretArgs = [...baseArgs, "--scanners", "secret", ctx.repositoryRoot];
458
512
  const secretOutput = await runTrivyScan(binary, secretArgs, ctx.repositoryRoot, errors);
459
513
  if (secretOutput) {
460
- issues.push(...await parseSecrets(secretOutput, enabledPatterns, targetFilesSet, ctx.repositoryRoot));
514
+ issues.push(
515
+ ...await parseSecrets(
516
+ secretOutput,
517
+ enabledPatterns,
518
+ targetFilesSet,
519
+ ctx.repositoryRoot
520
+ )
521
+ );
461
522
  }
462
523
  }
463
524
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codacy/tools-trivy-0",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Trivy adapter — CLI-mode security scanner",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,7 +8,7 @@
8
8
  "dist"
9
9
  ],
10
10
  "dependencies": {
11
- "@codacy/tooling": "0.1.0"
11
+ "@codacy/tooling": "0.2.0"
12
12
  },
13
13
  "license": "MIT",
14
14
  "author": "Codacy <support@codacy.com> (https://www.codacy.com)",