Snyk Delta CLI

1.13.2 · maintenance · verified Wed Apr 22

snyk-delta is a command-line interface tool designed to identify and report only *newly introduced* security vulnerabilities, license issues, and code findings in Snyk projects. It functions by comparing a current Snyk scan snapshot against a predefined baseline snapshot, making it particularly useful for integration into CI/CD pipelines, pre-commit hooks, or local development workflows where only changes since a previous state are of interest. The tool is currently at version 1.13.2 and is in maintenance mode, meaning new features are not being actively developed, though bug fixes and security patches continue to be released. Key differentiators include its ability to filter out pre-existing issues, focus on dependency changes (added/removed direct and indirect dependencies), and provide specific exit codes for automated decision-making in build systems. It supports Snyk Open Source, Container, and Code (with specific feature requirements), but not IaC.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates programmatic usage of `getCodeDelta` to compare two local SARIF files, illustrating how to identify new Snyk Code findings and exit with appropriate status codes based on the comparison result. Requires `SNYK_TOKEN` and `SNYK_ORG_ID`.

import { getCodeDelta } from 'snyk-delta';
import * as fs from 'fs';
import * as path from 'path';

const SNYK_TOKEN = process.env.SNYK_TOKEN ?? ''; // Ensure SNYK_TOKEN is set
const ORG_ID = process.env.SNYK_ORG_ID ?? 'YOUR_SNYK_ORG_ID'; // Replace with your Snyk Organization ID

async function compareSnykCodeResults() {
  if (!SNYK_TOKEN || ORG_ID === 'YOUR_SNYK_ORG_ID') {
    console.error('Error: Please set SNYK_TOKEN and SNYK_ORG_ID environment variables or replace placeholder.');
    process.exit(1);
  }

  // Create dummy SARIF files for demonstration if they don't exist
  const baselineSarifPath = path.join(__dirname, 'baseline.sarif.json');
  const currentSarifPath = path.join(__dirname, 'current.sarif.json');

  if (!fs.existsSync(baselineSarifPath)) {
    fs.writeFileSync(baselineSarifPath, JSON.stringify({"runs": []}, null, 2));
  }
  if (!fs.existsSync(currentSarifPath)) {
    // In a real scenario, this would be generated by `snyk code test --sarif > current.sarif.json`
    fs.writeFileSync(currentSarifPath, JSON.stringify({"runs": []}, null, 2));
  }

  try {
    const options = {
      baselineOrg: ORG_ID,
      // Optional: target a specific baseline project, otherwise `snyk-delta` attempts to find one
      // baselineProject: 'your-baseline-project-uuid', 
      // Optional: recommended for Code Analysis projects
      // projectName: 'owner/repo', 
      // targetReference: 'main', 
      // api: 'https://api.snyk.io/api/v1', // Snyk API endpoint, default value
    };

    console.log('Comparing Snyk Code results...');
    const deltaReport = await getCodeDelta(baselineSarifPath, currentSarifPath, options, SNYK_TOKEN);

    if (deltaReport.newFindings.length > 0) {
      console.log(`\nFound ${deltaReport.newFindings.length} new code findings:\n`);
      deltaReport.newFindings.forEach(finding => {
        const location = finding.locations[0]?.physicalLocation;
        const filePath = location?.artifactLocation?.uri || 'unknown file';
        const lineNumber = location?.region?.startLine || 'unknown line';
        console.log(`  - [${finding.ruleId}] ${finding.message.text} (${filePath}:${lineNumber})`);
      });
      process.exit(1); // Exit with 1 if new findings are present
    } else {
      console.log('\nNo new code findings introduced. Exiting with 0.');
      process.exit(0); // Exit with 0 if no new findings
    }
  } catch (error) {
    console.error('An error occurred during delta comparison:', error);
    process.exit(2); // Exit with 2 on error
  }
}

compareSnykCodeResults();

view raw JSON →