According to GitHub’s secret scanning alerts, thousands of secrets are leaked every month. The scary part? Many of these leaks go unnoticed for days or even weeks.
It happens more often than we’d like to admit. I’ve seen AWS keys accidentally pushed to GitHub, API tokens sitting in plain text in config files, and private keys casually hanging out in documentation.
The cost of a leaked credential can be enormous - both in terms of security risks and engineering time spent rotating keys. Taking the time to set up proper safeguards now can save countless hours of incident response later.
That’s where Secretlint comes in. It’s an open-source tool I’ve been using extensively to catch these issues before they become problems.
Getting Started with Secretlint
You have two main options for installing Secretlint:
-
Using Docker (Recommended for CI/CD):
-
Using NPM (Better for local development):
I prefer using NPM for local development as it integrates better with other development tools. Let’s walk through that setup.
Next, we’ll initialize Secretlint in our project.
This creates a .secretlintrc.json
file in the root of our project.
Running Secretlint for trevorlasn.com to see if it catches any secrets.
Looks good, I don’t have any secrets in my codebase. To demonstrate how Secretlint works, I’ve created a controlled example below. This shows the type of sensitive information Secretlint is designed to catch:
Running Secretlint on this example triggers our security checks:
The error above shows how Secretlint identifies private keys in your codebase. In a real project, this would prevent the accidental commit of sensitive credentials. This example uses a non-functional key for demonstration purposes only.
What Secretlint Can Detect
Beyond private keys, Secretlint can identify various types of sensitive information:
- AWS access keys and secret keys
- Google Cloud credentials
- API tokens and keys
- Database connection strings
- Basic authentication credentials
- And more through custom rules
Setting Up Pre-commit Hooks
Pre-commit hooks are like security guards for your git repository. They run checks on your code before each commit, making sure no sensitive data slips through. Here’s how to set them up using Husky and lint-staged:
This creates a .husky
directory in your project with the basic hook setup. Next, we’ll configure it to run Secretlint.
Update your package.json
with these settings:
- The
prepare
script runs automatically afternpm install
, setting up Husky for anyone who clones your repository lint-staged
configuration tells Husky to run Secretlint on all files that are staged for commit
Finally, add the pre-commit hook:
Now, when you try to commit a file containing secrets, here’s what happens:
Want to temporarily skip these checks? (Use with caution!)
But seriously, if you find yourself wanting to skip these checks, it’s usually a sign that something needs fixing in your code, not in your commit process.
Remember to add these files to your .gitignore
Advanced Configuration
Secretlint’s real power comes from its customization options. Let’s dive into some advanced configurations I use in different scenarios:
Customizing Rules
The basic configuration is just the start. Here’s how I structure more sophisticated rule sets:
Handling False Positives
False positives can be frustrating. Here are three ways I handle them:
- Using allowMessageIds
- Using Rule-Specific Allows
- Using Inline Comments
While Secretlint is great at catching credentials before they make it into your codebase, it’s just one piece of the security puzzle. I’ve found it works best when combined with proper secret management systems, environment variables for configuration, regular security audits, and ongoing team education about security practices.