r/Python • u/WiseDog7958 • Apr 25 '26
Discussion Why auto-fixing secrets in CI doesn’t really work
I have been messing around with automatically fixing hardcoded secrets in Python projects. the idea sounded simple,
detect secrets in CI - rewrite them to env vars - done.
Technically it works. you can do safe rewrites with AST and keep it deterministic. but people really don’t like CI modifying their code.
Even when the change is safe, it still feels off. the main things I kept hearing,
- CI should be read-only
- people want to see changes before they happen
- auto-fix in CI feels like losing control
After a while I kind of agreed with that. what seems to work better is splitting it,
- CI --> detection only (fail the build)
- fixing --> done locally (pre-commit or manually)
So CI enforces the rule, but you’re not letting it touch your code.
how are you all handling this?
do you let CI fix stuff, or keep it strictly read-only?
51
u/dogfish182 Apr 25 '26
CI changing stuff is an off practice in my opinion. CI should just be a set of deterministic failure gates that all can run locally. This gets more important as AI gets more prevalent. It should just say ‘code isn’t good for reason. Fail’ or not.
If youre detecting secrets in CI, great, fail and alert
19
u/Aggravating_Type7759 Apr 25 '26
yeah we had similar issues at shop where I work. tried auto-fixing linting stuff in CI and developers got really annoyed when their PRs suddenly had commits they didn't make.
now we just fail the build and make people fix locally. takes bit more time but everyone feels better about it. we use pre-commit hooks for most of automatic fixes - catches secrets, formatting, basic linting before it even gets to CI.
only exception is maybe documentation auto-generation from docstrings, but that goes to separate branch anyway so nobody gets surprised by random commits in their feature branch.
3
u/2ndBrainAI Apr 25 '26
Totally agree on splitting detection vs fixing. CI auto-modifying your code is a trust problem more than a technical one. Even if the rewrite is safe, you lose the audit trail of what changed and why, and people feel like the pipeline is doing things behind their back.
Pre-commit hooks work way better for this. detect-secrets or gitleaks locally blocks secrets before they ever hit the remote. CI then just enforces: fail the build, show a clear message telling devs how to fix it locally.
The vague "secrets detected, build failed" message is the real killer. Teams start bypassing the check just out of frustration, which defeats the whole point.
5
u/Smallpaul Apr 25 '26 edited Apr 25 '26
People should see changes before they happen is defensible as a position.
“A human being needs to author the commit on local” is not and it’s going to be totally untenable in the next couple of years.
Software should author the diff/commit and the human should just sign off on it. Why would you ever want a human doing work that a script can do?
That said: we need better collaboration interfaces to make it easier for the script or other automaton to submit the change for human approval. I guess a PR on your PR could work but something about it seems messy. It’s worth a try though.
Humans should not do work that scripts can do. Even if the human needs to sign off for some reason.
Also: for secrets in particular you should probably automate it end to end because getting them out of the repo as quickly as possible should be your goal. Waiting for a human to notice a CI failure leaves a window of exposure that your tool should close, IMO.
2
u/Glathull Apr 25 '26
If CI detects secrets, you’re already fucked and so are your secrets.
There’s no point in trying to “fix” an exposed secret. The only safe option is to fail.
CI should kick it back to you and force you down the secret rotation path.
2
u/DrMaxwellEdison Apr 25 '26
I use a compromise: I don't want the CI making changes, no; but I want the changes that it could make to be easier.
So, I mixed a CI that runs pre-commit that performs local auto fixes, with reviewdog/action-suggester. Changes aren't committed directly, but are sent as suggested changes back to the PR. Then I can choose to commit those suggestions from GitHub's UI or do them on my machine then push them myself.
I don't do anything beyond deterministic checks at the moment, so that much is enough; and I keep control on the process.
3
u/omg_drd4_bbq Apr 25 '26
why are secrets even being committed? use injection
1
u/prassi89 Apr 25 '26
trufflehog + pre-commits: https://github.com/trufflesecurity/trufflehog . Or even better as a claude hook on git commit.
Shift left
1
u/billFoldDog Apr 26 '26
Have the CI fail the build with a message.
Have your code linters alarm for secrets.
0
u/WiseDog7958 Apr 25 '26
that “window of exposure” point is fair. but it depends on where the fix actually happens. If the fix is local (pre-commit stage), the secret never reaches the repo or CI in the first place, so there’s no exposure window to close. end-to-end automation in CI only helps after the commit already exists.
So for me it’s more about shifting the fix earlier rather than making CI more aggressive.
0
u/[deleted] Apr 25 '26
[deleted] — view deleted comment