Marcio Cunha

How to block leaked API keys in Git with Gitleaks and CI/CD

Learn how to configure Gitleaks in local hooks and pipelines to prevent accidental leaks of API keys, passwords, and tokens in commits and repositories.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • Credential leaks frequently occur when developers mistakenly expose API keys in source code files.
  • Gitleaks acts as an automated code inspector using regular expressions and entropy to detect secrets.
  • Pre-commit hooks intercept the local save command before code uploads to the central repository.
  • Validating commits in CI/CD pipelines ensures a second layer of defense against human error or local bypass.
  • Keeping commit history clean requires automated tools, as manual password removal is highly inefficient.

The invisible danger of exposed API keys in source code

Anyone who has never accidentally pasted a password or access key directly into a configuration file should cast the first stone. In the rush of daily development, it is very easy to leave sensitive credentials inside the source code before pushing everything to GitHub or GitLab. In practice, this means any malicious crawler scanning the internet can find these keys within minutes and use your infrastructure to mine cryptocurrency or steal customer data. The financial damage and information security headache can be catastrophic for any company.

To prevent this type of disaster, relying solely on human attention simply does not work. We need intelligent automation that blocks errors even before they leave our development machine. This is precisely where Gitleaks comes in, a lightweight open-source tool designed to hunt down forgotten secrets in files and commit histories. When we integrate this scan with efficient CI/CD pipelines, we build an impenetrable barrier against corporate data leaks.

What is Gitleaks and how it uncovers hidden secrets

Gitleaks is a program that reads your code version history looking for patterns that resemble passwords, access tokens, private SSH keys, and cloud credentials. It works by combining two main techniques: regular expressions, which search for known formats like an AWS key prefix, and entropy analysis, which measures the degree of randomness in a character sequence. If a string appears to be a chaotic high-complexity mix of letters and numbers, the system immediately suspects it is not ordinary text, but rather a computer-generated secret key.

The great advantage of this approach is that it does not require you to configure complex rules from scratch for every service on the market. The tool comes out of the box with dozens of ready-to-use detections for popular providers like GitHub, Slack, Stripe, and Google Cloud. Furthermore, much like ensuring service integrity in automated certificate processes with Vault, we can run Gitleaks completely automated and silently in the background during our daily routine.

Implementing local hooks to block errors at the source

The best way to fix a security problem is to prevent it from happening in the very first second. In the Git world, pre-commit hooks are small scripts that run automatically every time you type the git commit command. If the script finds any sign of a secret key in your modified code, it immediately cancels the operation, preventing the commit from being recorded in your local history.

To set this up in practice, you can use the pre-commit utility combined with official Gitleaks rules. The basic step-by-step involves creating a configuration file at the root of your repository pointing to the secrets inspector:

  1. Install the hooks manager in your local development environment.
  2. Create a file named dot pre-commit config dot yaml at the project root.
  3. Add the official Gitleaks repository as a hook configured to run on every code change.
repos: - repo: https://github.com/gitleaks/gitleaks rev: v8.18.0 hooks: - id: gitleaks

With this file in place, whenever someone tries to save a secret in the local repository, the tool intercepts the action immediately and demands the removal of the sensitive data before proceeding.

Validating code in pipelines to ensure zero leaks

Although local hooks are excellent, rushed developers might accidentally bypass these validations using commands like git commit --no-verify. To close this security loophole, the second indispensable line of defense is embedding the scan directly into your project's continuous integration pipeline, whether in GitHub Actions, GitLab CI, or Jenkins. This way, even if code bypasses the programmer's machine, the central server performs a rigorous check before accepting the merge into the main branch.

In practice, the pipeline process downloads the source code, installs the Gitleaks binary, and executes a thorough scan across the entire recent change history. If any irregularity is found, the pipeline fails instantly, blocking code deployment to production and notifying the development team of the security flaw detected in the process.

Automating secret scanning is not just about installing software and forgetting it, but rather building a solid data protection culture within the engineering team. Tools like Gitleaks play a fundamental role by lifting the weight off human shoulders and automating error detection that could cost the company's reputation dearly. By combining local validations at commit time with rigid barriers on CI/CD servers, we ensure sensitive credentials remain strictly where they belong: safe and out of public reach.