Use a two-layer approach. Stop secrets locally with pre-commit detection, then enforce server-side rejection with pre-receive hooks so bypassed local checks do not matter. The goal is to prevent credential material from entering Git history at all, because cleanup after publication is unreliable and incomplete.
Why This Matters for Security Teams
Shared Git repositories turn a single developer mistake into an organisation-wide exposure event. Once a secret lands in a commit, it can propagate through clones, forks, caches, CI logs, and backup systems faster than any manual cleanup can follow. That is why modern guidance treats secret prevention as a source-control control, not just a secrets-management problem. The OWASP Non-Human Identity Top 10 and NHIMG’s research on Guide to the Secret Sprawl Challenge both point to the same operational reality: secrets are routinely duplicated, copied into tickets, and committed before anyone notices.
The risk is not limited to human mistakes. CI systems, bots, and automation accounts often have broad write access to repositories, which means a leaked token can become a pathway into build pipelines, package registries, or cloud accounts. NHIMG’s CI/CD pipeline exploitation case study shows how quickly source-code exposure becomes a wider supply-chain problem when controls rely on cleanup after the fact. In practice, many security teams encounter secret exposure only after the repository history has already been mirrored, indexed, and reused elsewhere.
How It Works in Practice
The most effective pattern is a two-layer control model. First, block obvious leaks before a developer can commit them locally. Second, make the server reject any bypassed or missed secret at push time so the repository never accepts it. Local checks improve developer feedback, but they are not the control boundary because they can be skipped, misconfigured, or disabled during urgent work.
At the workstation layer, teams commonly use pre-commit scanning to detect API keys, tokens, certificates, and private keys in staged changes. This should be paired with clear developer guidance on using environment variables, secret managers, and ephemeral credentials instead of hardcoded values. At the server layer, pre-receive hooks or repository enforcement rules inspect incoming objects and reject the push if credential material is present. That distinction matters because Git history is immutable once distributed, and cleanup after publication is unreliable.
- Scan staged files before commit so the developer gets immediate feedback.
- Reject secrets at push time so local bypasses do not matter.
- Prefer short-lived credentials and dynamic delivery over static long-lived secrets.
- Route sensitive values through approved secret stores instead of code, docs, or tickets.
Operationally, teams should tune detection for the secrets they actually issue, including cloud access keys, signing keys, and tokens used by CI/CD automation. Current practice also benefits from incident response runbooks that revoke exposed credentials immediately, because leak response time is often slower than the speed of source-code replication. The 2025 State of NHIs and Secrets in Cybersecurity reports that 44% of NHI tokens are exposed in the wild, which reinforces how often secret material escapes normal boundaries. These controls tend to break down in monorepos with heavy automation because bots, merge tooling, and generated files can create noisy false positives and missed paths if policy is not maintained continuously.
Common Variations and Edge Cases
Tighter pre-commit and pre-receive enforcement often increases developer friction, requiring organisations to balance speed against the reduction in accidental leakage. That tradeoff is real, especially in fast-moving engineering teams that work across many languages and build systems.
There is no universal standard for this yet, but current guidance suggests the best results come from layering detection with process controls. For example, repositories with many contributors may need allowlists for test fixtures, while regulated environments may require mandatory push protection and audit logging. Shared libraries and sample code also need special handling because static examples can be mistaken for live credentials if detection rules are too aggressive.
Teams should also treat exposed non-human identities as a lifecycle issue, not just a repository issue. If a secret has already been copied into documentation, chat, or a deployment script, Git enforcement alone will not solve the problem. NHIMG’s research on 52 NHI Breaches Analysis shows how often exposure follows reuse and duplication across systems. Best practice is evolving toward prevention, immediate revocation, and replacement with short-lived credentials before the same value can be reused elsewhere.
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 and OWASP Agentic AI Top 10 address the attack and risk surface, while NIST CSF 2.0 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| OWASP Non-Human Identity Top 10 | NHI-03 | Secret sprawl and exposed tokens map directly to NHI credential handling failures. |
| OWASP Agentic AI Top 10 | Automation and AI agents often commit or move secrets through shared repos. | |
| NIST CSF 2.0 | PR.AC-1 | Git push protection supports access control by preventing unauthorized credential exposure. |
Treat agent write access as high risk and enforce runtime secret blocking plus revocation.
Related resources from NHI Mgmt Group
- How should security teams prevent a malicious npm package from stealing cloud credentials?
- How should security teams stop forked pull requests from reaching privileged GitHub Actions jobs?
- How should security teams handle reusable secrets in npm build and CI environments?
- How should security teams decide whether JIT access is safe for non-human identities?
Deepen Your Knowledge
Reviewed and updated by the NHIMG editorial team on June 11, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org