How-to › Release and secure packages

How to scan a repo and its published package for leaked secrets#

Scan the files npm pack would publish, run gitleaks on every commit and in CI, and revoke a leaked key, because a rotation commit leaves it in git history.

Audience
Library maintainer
Level
beginner
Topic
Secure the supply chain
Verified

A test fixture holds a working AWS key, and .gitignore keeps it out of git, so every scan of the repository passes. The package’s "files" field lists test, so npm publish puts the fixture in the tarball, and anyone who installs the package can read the key. A later commit that swaps the key for an environment variable leaves the replaced value in git history.

What you get

You will end up with a small scanner of your own for the files npm would publish and for git history, plus gitleaks hook and CI files written from its documentation. This is for you if you publish a package from a repository that holds test fixtures or a .env.

Short answer

Before npm publish, scan exactly the files npm pack --dry-run --json lists, because a files entry or an added .npmignore can ship a fixture that .gitignore kept out of git. Run gitleaks as a pre-commit hook and in CI over the whole history. When a key leaks, revoke it at the provider: a later commit leaves it in git log -p, and a history rewrite does not reach clones taken before it.

You will need

Node 22 or later and git. The sample’s scanner and both demos run offline. Catching the key before the first push is the subject of catch a leaked API key before it reaches the repository; this page adds the published package and the history.

Approaches compared

ApproachWhen it fitsWhat it costs youWhen to pick something else
GitHub secret scanning push protectionThe repository is on GitHub and you want pushes blocked with no setupSupported patterns only, on GitHub only, and repository-level blocking needs GitHub Secret Protection enabledThe code lives on another host, or the leak travels in an npm tarball
gitleaksA pre-commit hook and a CI job over full history, on any hostYou maintain its rules and its ignore file, and its GitHub Action needs a license key for organizationsYou need to know whether a key it found still works
TruffleHogTriage of a long-lived repository, where a live key matters more than a dead oneVerification sends each credential it finds to that provider’s API, unless you pass --no-verificationPolicy forbids sending a found credential anywhere

Push protection needs no setup for your own pushes to public repositories on GitHub, but it reads pushes, so it never sees a tarball built from files git ignores. gitleaks runs anywhere and puts the rules and the false-positive list in your hands. TruffleHog’s verification tells a live key from a dead one by calling the provider with it.

Scan what npm would publish

A scan of the repository reads what git tracks. npm reads something else: the files field, or .npmignore, or .gitignore when there is no .npmignore, as the files section of package.json documents. So ask npm for the list and scan that:

export function packFiles(dir) {
  const json = execFileSync('npm', ['pack', '--dry-run', '--json', '--ignore-scripts'], {
    cwd: dir, encoding: 'utf8', env: { ...process.env, npm_config_loglevel: 'silent' },
  })
  const files = JSON.parse(json)[0].files.map((f) => f.path).sort()
  if (files.length === 0) throw new Error(`${dir}: npm pack lists no files`)
  return files
}

npm pack --dry-run reports what it would pack and writes no tarball. demo-pack.mjs copies pkg/, writes test/fixtures/config.json and a .env that hold fake credentials, and scans four setups:

node demo-pack.mjs
"files": ["lib", "test"]: BLOCK
  tarball: lib/index.js package.json test/fixtures/config.json
  LEAK    aws-access-key-id test/fixtures/config.json:2 AKIA...(20 chars)
"files": ["lib"]: publish
  tarball: lib/index.js package.json
no "files", .gitignore only: publish
  tarball: lib/index.js package.json
no "files", a .npmignore added: BLOCK
  tarball: .env lib/index.js package.json test/fixtures/config.json
  LEAK    github-pat .env:1 ghp_...(40 chars)
  LEAK    aws-access-key-id test/fixtures/config.json:2 AKIA...(20 chars)

pkg/.gitignore lists .env* and test/fixtures/, and with no files field npm honors it. The fourth setup adds a one-line .npmignore for coverage/, and npm stops reading .gitignore, so the .env ships too. Run the scan as the last step before npm publish, in the same job.

Run gitleaks on every commit and in CI

For the repository itself, gitleaks reads staged changes in a pre-commit hook and every patch in CI. The hook comes from the gitleaks repository through the pre-commit framework:

repos:
  - repo: https://github.com/gitleaks/gitleaks
    rev: v8.30.1
    hooks:
      - id: gitleaks

The gitleaks hook runs gitleaks git --pre-commit --redact --staged --verbose, and SKIP=gitleaks bypasses it, so the CI job is the check that holds. The workflow runs the gitleaks Action over full history,1 then the package scan:

      - uses: gitleaks/gitleaks-action@v2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GITLEAKS_LICENSE: ${{ secrets.GITLEAKS_LICENSE }}
          GITLEAKS_VERSION: 8.30.1

The Action installs a gitleaks release hard-coded in the Action unless GITLEAKS_VERSION names one, so the workflow pins the release the hook uses. Neither file was executed for this page. Both follow the gitleaks README and its .pre-commit-hooks.yaml at tag v8.30.1.

Check it worked

node --test scan.test.mjs
1..7
# tests 7
# suites 0
# pass 7
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 3659.722038

The tests run both demos, match each rule against its fake and a near miss, and prove no report line holds a whole secret. One scans every file in the sample for the four rules and five more token formats, because push protection can refuse a push that holds a credential-shaped string.

When it goes wrong

The scan of the working tree passes after you replace the key. The value is still in an earlier commit, and git log -p prints it. demo-history.mjs commits a fake Stripe key and replaces it in a second commit. It scans a depth-1 clone, then squashes both commits into one:

node demo-history.mjs
working tree after the rotation commit: 0 leak(s)
history: 1 leak(s)
  LEAK    stripe-secret-key 7ca55b2:config/keys.json:3 sk_l...(32 chars)
a depth-1 clone, as a default CI checkout makes: 0 leak(s)
history after squashing the leak away: 0 leak(s)
a clone taken before the rewrite: 1 leak(s)
  LEAK    stripe-secret-key 7ca55b2:config/keys.json:3 sk_l...(32 chars)

Rewriting history cleaned one repository and left the clone untouched, as it leaves every fork, cache, and mirror. Revoke the key at the provider first, then rewrite if you want the noise gone.

The CI scan passes on a repository that leaked. A checkout with actions/checkout’s default fetch-depth of 1 holds one commit, so the depth-1 clone in that run saw only the rotated file. The sample workflow sets fetch-depth: 0 for the gitleaks job, and a test fails if that line goes.

The scanner flags a value that is not a secret, such as an example in documentation. Add its fingerprint to an allowlist with an owner and a reason, as the sample’s --allowlist file does, or to .gitleaksignore for gitleaks. An entry with no owner is refused, so somebody answers for each exception.

When not to do this

Do not hand-write a scanner for production use. The sample’s four rules exist to make the packaging failure visible, and gitleaks and TruffleHog maintain their rule sets for you. The gitleaks README documents gitleaks dir for files and directories, and --max-archive-depth to extract a tarball before scanning, so it can read the file npm pack writes.

Do not run TruffleHog with verification on a repository whose findings you may not send out. It tests a candidate against the provider’s API, which is the point and also a disclosure. Pass --no-verification there.

Do not rely on push protection for a private repository without checking that GitHub Secret Protection is enabled for it. The protection on by default covers your pushes to public repositories.

Last verified

Verified 2026-11-30 against Node 22.23.5, npm 10.9.9 and git 2.43. The scanner that ran is the sample’s own. gitleaks, TruffleHog and GitHub push protection were not run, and the gitleaks hook and workflow files were not executed. Their behavior on this page is read from the gitleaks and TruffleHog READMEs at v8.30.1 and v3.100.1, the gitleaks Action README at v2, and GitHub’s push protection page.

Footnotes

  1. The GitHub Action asks organizations for a free license key. Its README says what leaves the runner to check it: the key, the repository name and the owner, sent to a license validation service. The README adds that the code never leaves GitHub, because the scan runs inside the Actions container. ↩︎ Back to text

Read this page as markdown · All how-to guides

Generate the client instead of writing it#

Retries, timeouts, pagination and auth are the same problems in every client. Voxgig generates them from your OpenAPI description, in 23 languages, from one model.

Listen to Fireside, the Voxgig podcast

Since 2018, Richard Rodger has talked with developer advocates, event organizers, and the people who build developer-facing companies about the specifics of the work.