r/git • • Oct 31 '26

Devs, what happens in your team when someone pushes an API key to main?how do you keep API keys out of git history?

0 Upvotes

34 comments sorted by

80

u/tom-md Oct 31 '26
  1. Rotate the key. It was public and can not be made secret again after the fact.
  2. Place the replacement API key into AWS Secrets or wherever you put your keys.
  3. Ask yourself why the security mentality of the company is weak.

0

u/[deleted] Oct 31 '26

[deleted] — view deleted comment

18

u/patmorgan235 Oct 31 '26

If a secret is not rotateable then you need to make it rotateable

9

u/ImDevinC Oct 31 '26

What secrets can't be rotated?

5

u/Adorable_Character_6 Oct 31 '26

Compromised and not recoverable is a good reason to job hunt.

5

u/platinummyr Oct 31 '26

Even if you rewind and force push, you can't easily force anyone else to garbage collect the commit. Especially if it's a public repository. Train your employees on security practices, use CI to scan for secrets. Don't implement secret schemes that lack a rotation mechanism.

5

u/n9iels Oct 31 '26

That is the same as claiming emails needs a way to delete a message after sent. And in the end someone takes a screenshot or saves it outside their mailbox. When a secret is leaked it is not secret anymore. Never.

2

u/R3PTILIA Nov 01 '26

Sure with the same technology from Men in Black

27

u/marvinfuture Oct 31 '26

In my experience as a DevOps engineer, this type of stuff happens because organizations don't understand the best practices here.

What I mean is a couple things failed here:

  1. Developer had access to a sensitive key they shouldn't have
  2. The codebase and secrets implementation was built in a way that leverages secrets directly instead of leveraging integrations that make secrets a JIT automated system
  3. Your codebase didn't have a .gitignore file that would catch checking in a key file
  4. Your developer either made a mistake or didn't know better
  5. If this was caught by a person reviewing code, implement tooling that looks for secrets checked into code automatically

Personally I'd use this type of scenario to do two things

First remediate the issue. Treat the key as compromised and rotate it immediately. Undo the git commit as well if possible in your SCM solution.

Second, improve the process. Implement tooling that makes this harder to do and identify why this was checked in and not caught

26

u/OkLettuce338 Oct 31 '26

Rotate the key

14

u/pippin_go_round Oct 31 '26

No dev has access to any API keys. At least none that can cause any real damage. Those are put into the secret manager in prod only by a very select few people.

9

u/carsncode Oct 31 '26

You have to burn the key, period. You can't un-leak a secret. You treat it as a security incident, run a retro, correct the process that allowed it to happen in the first place, starting with people having access to and using real secrets.

6

u/Philluminati Oct 31 '26

Change the password/token so the key no longer works. Don't break your git history for a half chance of undoing this mistake. Changing the key is the right way forwards.

6

u/deZbrownT Oct 31 '26

How does it happen that devs are handling api keys in a professional dev environment?

1

u/swagrid003 Oct 31 '26

How else would you do it?

2

u/footsie Oct 31 '26

Seperation of duties - if you're contributing code to production you shouldn't be the one handling secrets for the code running production. Or deploying it.

In situations where that isn't possible (understaffed) the person doing multiple roles should have seperate accounts with role appropriate RBACs for each.

2

u/disoculated Nov 01 '26

Keys go in a secret store, not the source code repository.

1

u/deZbrownT Oct 31 '26

There is some kind of central repo, and they only refer to that repo value. You can build it in many ways. It depends on the rest of your infrastructure, but basically there is a secrets-handling tool for each size.

1

u/swagrid003 29d ago

Well yeah obviously, but at some point a dev has to go on a third party service, mint the API key by clicking a button, and bung it in the secret store like GCP secrets or whatever. I've never worked anywhere were this isn't the job of devs.

1

u/deZbrownT 29d ago

I see, yeah. Larger organisations have teams that only handle billing accounts, and they provide the dev with just the env. They also maintain and take care of keys for all environments. Expense tracking is one of the priorities for a large organisation, resources get lost very easily.

4

u/mxz117 Oct 31 '26

Happened once, guy pushed “env 3” in a PR

Just rotated the key

1

u/mmaug Oct 31 '26

And then he pushed "env 4"… 😏😂

3

u/mxz117 Oct 31 '26

It gets worse, it was the backend env in the frontend repo

4

u/unicornsandrainbowst Oct 31 '26

1 - Api keys (the ones that matter) are not accessible to devs. They are read from a vault as part of the pipeline.   2 - if they are exposed (by a compromised action for example), we just rotate the key in the vault, takes 1 minute. The next pipeline picks the new key.  

As simple as that. Anyone running production environments with .env files on each dev machine trusting .gitignore and the dev itself, needs to take a closer look at their security practices.

4

u/Adorable-Strangerx Oct 31 '26

You can but there is no point. Just rotate key.

1

u/disoculated Nov 01 '26

One zillion percent this. Having the capability to mess with history is a far worse poison than an invalidated key.

And even if you remove it, god knows where it’s already been copied to.

2

u/Gurnug Oct 31 '26

You will get so much paperwork to fill after that security incident that you will remember

2

u/the_helpdesk Oct 31 '26

You pay for tooling that prevents committing secrets. GitHub has some, datadog has some, and I'm sure there's 40 other options. Or you can write your own pre-commit hooks.

3

u/atarivcs Oct 31 '26

You keep API keys out of git by not committing files that contain an API key.

11

u/SamIAre Oct 31 '26

If your team’s answer to “what happens when there’s a security issue” is just “we don’t make mistakes to begin with” then frankly your planning is piss poor. Like obviously it’s better to not make a mistake but you have to have protocols in place for when things happen anyway.

1

u/mpersico 23d ago

You don't give them access to the key in the first place. You put the key in a vault , and the program has to be registered with the vault. The program goes and grabs the key when it needs it. Doesn't write it anywhere. Doesn't store it.

1

u/chriswaco Oct 31 '26

We’re a very small team that puts local keys into .key files and have git hooks to prevent checking them into any repo.

-1

u/Ok_Entrepreneur_8509 Oct 31 '26

If you can't retire that key and get a new one, you can force a history rewrite to remove the commit. This is not going to be 100% safe, because you don't know if the key got out, but if you are mainly trying to keep your repo clean, it works.

Assuming you are on main and the bad commit is the most recent one:

git reset --hard HEAD~1

git push origin main --force-with-lease

Force with lease just prevents you from accidentally clobbering anything that got committed after.

If you need to pull the bad commit out from deeper in the history, you have to do a more complicated rebase process.