Join our Newsletter — 33% off our NHI Course

What should security teams do first when hardcoded secrets are discovered in code or Git history?

The first step is to identify every place the secret appears, assign ownership, and revoke or rotate it before treating the finding as closed. Discovery without invalidation leaves the credential usable. Teams should also check dependent systems, because one leaked secret may authenticate to cloud, source control, CI/CD, or other downstream services.

Why the first move is containment, not cleanup

Hardcoded secrets are an active access path until they are invalidated. The first response should be to map every location where the secret exists, determine who owns the credential, and revoke or rotate it immediately. That sequence matters because a discovered secret can still authenticate in code, Git history, CI/CD, cloud consoles, or downstream systems even after the file is removed.

Discovery without invalidation is only visibility. If teams stop at finding the string, they leave the credential usable by anyone who can reach the repository, artifact, backup, or cloned history that still contains it. The practical goal is to collapse the credential’s blast radius before any further investigation.

  • Trace the secret across working code, commits, tags, branches, forks, build logs, and deployment artifacts.
  • Identify the system owner and the dependent services that trust the credential.
  • Rotate or revoke the secret before treating the issue as closed.

Why Git history makes the problem persistent

When a secret has landed in Git, deletion from the current branch does not remove it from history. That makes repository hygiene, history rewriting, and downstream dependency checks part of the response, not optional follow-up work. The remediation question is not only “where is the secret now?” but also “what still accepts it?”

Secrets found in source control often have wider reach than teams expect. A single token may authorize package publishing, infrastructure changes, cloud API calls, or access to internal services. NHIMG’s Guide to the Secret Sprawl Challenge and Millions of Misconfigured Git Servers Leaking Secrets both reinforce the same operational point: secret exposure in code or Git is usually a sprawl problem, not a single-file problem.

Long-lived credentials are especially dangerous because they stay valid long after the original leak. That is why rotation should be fast and why teams should verify whether the secret was shared across environments, copied into scripts, or embedded in automation.

What good response looks like in practice

The strongest response combines ownership, invalidation, and verification. A team should assign the finding to the system owner, rotate the credential at the source of trust, and then confirm that dependent systems have picked up the replacement or fail closed if they have not. If the secret was used broadly, the team may need to replace it in more than one environment.

When the leak involves CI/CD or repository automation, the response should also include pipeline review and commit-path cleanup. NHIMG’s CI/CD pipeline exploitation case study, Reviewdog GitHub Action supply chain attack, and GitHub Dependabot Breach show how exposed or abused tokens can extend from a leaked secret into repository manipulation and broader build compromise.

For teams that want the broader control model, the Ultimate Guide to NHIs is useful because it ties together discovery, rotation, offboarding, and lifecycle governance for machine-facing credentials. That matters here because hardcoded secrets usually belong to non-human actors or automation, and those identities often outlive the application code that exposed them.

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 MITRE ATT&CK address the attack and risk surface, while CIS Controls v8 and NIST CSF 2.0 set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
CIS Controls v8 CIS 5 — Account Management Hardcoded secrets are account material that must be revoked or rotated quickly.
CIS 16 — Application Software Security Secrets in code and Git history are a secure development failure that needs prevention and response.
Recommendation — Revoke exposed credentials and verify all downstream accounts no longer trust them. Scan source and pipelines for embedded secrets and block new exposures before release.
OWASP Non-Human Identity Top 10 NHI-01 — Secrets and Credential Exposure The question centers on leaked secrets that remain usable until invalidated.
NHI-02 — Lifecycle and Offboarding Cleanup requires ownership assignment, rotation, and retirement of the compromised credential.
NHI-03 — Privilege and Access Scope Leaked secrets often grant broader access than teams expect, increasing blast radius.
Recommendation — Rotate or revoke exposed secrets immediately and confirm every use path is closed. Assign an owner, retire the leaked credential, and remove all lingering trust relationships. Reduce exposed credential scope and reissue it with the minimum access needed.
MITRE ATT&CK T1552 — Unsecured Credentials Hardcoded secrets in code or Git history are exposed credentials that adversaries can harvest.
Recommendation — Hunt for exposed credentials and invalidate them before adversaries can reuse them.
NIST CSF 2.0 PR.AC — Identity Management, Authentication, and Access Control Leaked secrets require access control correction through revocation and replacement.
RS.MI — Mitigation The first response is mitigation through invalidation, not just detection.
Recommendation — Remove the compromised access path and re-establish trusted authentication for affected services. Execute rapid credential mitigation and confirm the old secret no longer works.

Practitioner Guidance

What to prioritise: Revoke or rotate first, then clean up the exposure sites. If the credential can still authenticate anywhere, the incident is still live even if the source line has been deleted.

What to verify: Confirm whether the secret was reused across repos, environments, pipelines, or vendor integrations, and verify that the replacement secret is actually deployed or that the old one is rejected everywhere that matters.

Common mistake: Treating secret scanning as remediation. Scanning finds the problem; invalidation closes it. Teams that skip the second step often rediscover the same credential later in logs, mirrors, or history snapshots.

Practitioner takeaway: A hardcoded secret should be handled as a live credential compromise until ownership is known, access is revoked, and dependent systems are checked for continued trust in the leaked value.