This week’s tip of the week is a tool called Betterleaks.

A few months ago, I wrote about hk, a Git hook manager that can run checks like formatting, linting, and type checking before your code gets committed. One of the things I mentioned was catching leaked secrets.

Well, here’s how to actually do that.

Betterleaks is an open-source secrets scanner maintained by folks behind Gitleaks, including its original author.

GitHub can scan repositories for leaked secrets, and that’s a great safety net.

But I’d rather catch an API key or token before the commit ever leaves my machine.

Betterleaks can scan just your staged changes. For this example, I staged some source code with a fake API key and a custom detection rule so there’s no real credential involved.

$ betterleaks git . --pre-commit --staged

 + ○
   ▾
 betterleaks 1.8.1

┌─demo-api-key──○
│
│ 2 │   const apiKey = "REDACTED";
│   │                   ^^^^^^^^
│
│ attributes:
│   git.message ......
│   git.sha ..........
│   path ............. src/api.ts
│   resource ......... git.patch_content
└○

11:05PM INF scanned ~394 bytes (394 bytes) in 35.7ms
11:05PM WRN leaks found: 1

And if you’re already using hk from my previous tip, it now has Betterleaks built in. Add it as one of your hook steps:

["betterleaks"] = Builtins.betterleaks

Now secret scanning can happen automatically alongside the other checks you already run before committing.

GitHub can still be another layer of protection, but it doesn’t have to be the first place you find out you committed a secret.

Check out Betterleaks and let me know what you think!

That’s it! Short and sweet. Until the next one!