Secretlint CLI for Secret Detection

12.2.0 · active · verified Wed Apr 22

Secretlint is a powerful CLI tool designed for scanning codebases to detect and prevent the leakage of sensitive data like API keys, credentials, and private information. The current stable version is 12.2.0, with minor and patch releases occurring frequently, and major versions introducing breaking changes like Node.js engine requirements. It offers a highly extensible architecture through pluggable rules and presets (e.g., `@secretlint/secretlint-rule-preset-recommend`), supporting various file formats and offering multiple output formatters including `stylish`, `mask-result`, and `github` annotations. Key differentiators include its focus on precise secret detection, a flexible configuration system using `.secretlintrc` files, and the ability to mask secrets in output, making it suitable for CI/CD pipelines and pre-commit hooks.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing a basic `.secretlintrc.json` and then running `secretlint` both via the command line (using `npx`) and programmatically using the `run` function, showing secret detection and output masking.

import { run } from 'secretlint';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';

const tempDir = path.join(process.cwd(), '.secretlint-temp');
const tempFile = path.join(tempDir, 'example.js');
const configFile = path.join(tempDir, '.secretlintrc.json');

async function quickstart() {
  await fs.mkdir(tempDir, { recursive: true });
  await fs.writeFile(tempFile, 'const secretKey = "sk_live_YOUR_SECRET_KEY_123";');
  await fs.writeFile(configFile, JSON.stringify({
    "rules": [
      {
        "id": "@secretlint/secretlint-rule-preset-recommend",
        "rule": "@secretlint/secretlint-rule-preset-recommend"
      }
    ]
  }, null, 2));

  try {
    console.log('Running secretlint CLI via npx:');
    // Using child_process for CLI demo, or `run` for programmatic
    const { execa } = await import('execa'); // Using dynamic import for execa
    const cliResult = await execa('npx', [
      'secretlint',
      tempFile,
      '--secretlintrc', configFile,
      '--format=stylish'
    ], { reject: false, cwd: tempDir });
    console.log(cliResult.stdout);
    if (cliResult.exitCode === 1) {
      console.log('CLI detected secrets and exited with code 1.');
    } else {
      console.log('CLI finished, no secrets detected or --output was used.');
    }

    console.log('\nRunning secretlint programmatically with `run` function:');
    const programmaticResult = await run([tempFile], {
      cwd: tempDir,
      secretlintrc: configFile,
      format: 'mask-result'
    });
    console.log(programmaticResult.output);

    if (programmaticResult.ok === false) {
      console.log('Programmatic run detected secrets.');
    } else {
      console.log('Programmatic run finished, no secrets detected.');
    }

  } catch (error) {
    console.error('An error occurred:', error);
  } finally {
    await fs.rm(tempDir, { recursive: true, force: true });
  }
}

quickstart();

view raw JSON →