Subscribe to the Non-Human & AI Identity Journal

How should teams stop secrets from entering code in the first place?

The most effective pattern is to scan before code is shared, not after. Use local pre-commit and pre-push checks, then reinforce them with CI scanning so secrets are blocked in both developer and pipeline paths. That reduces the chance a token reaches shared history, packaged artefacts, or deployment systems.

Why This Matters for Security Teams

Stopping secrets at source is more effective than chasing them after a leak because code review, artefact scanning, and repository cleanup all happen too late if the credential has already been copied into history, build logs, or deployment state. The practical goal is to make secret entry harder than correct handling, using developer-side controls first and pipeline enforcement second. That pattern matters because leaked credentials are often duplicated and reused across systems, which turns one mistake into a broad exposure path.

NHIMG research shows why the problem is worth treating as a prevention issue, not just a detection issue: The State of Secrets Sprawl 2026 reports that 64% of valid secrets leaked in 2022 are still valid and exploitable today. That means a token that slips into code can remain dangerous long after the original commit is forgotten. Guidance from the OWASP Non-Human Identity Top 10 also reinforces that secret handling is an identity control problem, not just a developer hygiene issue. In practice, many security teams only discover weak secret controls after a credential has already propagated into multiple repositories and environments.

How It Works in Practice

The strongest pattern is layered prevention. Start with local checks that run before a developer can share code, then backstop them with CI scanning so a missed secret is blocked before merge or release. Pair those checks with format-aware detectors for API keys, certificates, tokens, and cloud credentials, and tune them to ignore common false positives without weakening the policy. This is where Guide to the Secret Sprawl Challenge is useful: it frames secret sprawl as a lifecycle problem, not a single scanning problem.

  • Use pre-commit hooks for developer laptops so obvious secrets never leave the workstation.
  • Use pre-push checks to catch anything missed by local review before it enters shared history.
  • Run CI secret scanning on every branch and pull request, including dependency and generated files.
  • Block merges on high-confidence matches, then route exceptions through PAM and security review.
  • Pair prevention with rotation and revocation so a missed secret cannot remain usable for long.

For teams working on modern build chains, this is also a supply chain control. The CI/CD pipeline exploitation case study and the Reviewdog GitHub Action supply chain attack show why pipeline trust cannot be assumed once secrets are present in automation. These controls tend to break down in fast-moving monorepos with generated artefacts and many bypass paths because developers find one unchecked path and reuse it repeatedly.

Common Variations and Edge Cases

Tighter secret prevention often increases developer friction, requiring organisations to balance release speed against the risk of credential leakage. That tradeoff is real, and current guidance suggests the answer is not to weaken controls, but to make the safe path the easiest path. In some teams, the right exception model may allow approved test fixtures, synthetic credentials, or short-lived tokens, but those exceptions need clear ownership and expiry.

There is no universal standard for this yet in highly automated environments, especially where AI-assisted coding, generated config, or multiple CI runners are involved. The Shai Hulud npm malware campaign illustrates how quickly secrets can be harvested once they enter developer workflows, while the 52 NHI Breaches Analysis shows that exposure frequently spans code, tickets, chat, and configuration. In mature environments, prevention must extend beyond code editors into ticketing systems, chat exports, and secrets distributed through automation. The right policy is usually a blend of block, educate, and rapidly revoke, with exceptions tracked as security events rather than informal waivers.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10 address the attack and risk surface, while NIST CSF 2.0 and NIST AI RMF set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP Non-Human Identity Top 10 NHI-03 Secret leakage prevention depends on controlling NHI credential exposure and rotation.
NIST CSF 2.0 PR.AC-4 Least-privilege access supports blocking secrets from unnecessary code paths.
NIST AI RMF AI RMF supports governance for AI-assisted code workflows that can introduce secrets.

Block hardcoded secrets early, then rotate and revoke any credential that bypasses prevention.