redacted_credential_diff
A git diff driver for Rails encrypted credentials that shows which keys changed without printing their values.
top:
- secret: <redacted efb7dccb>
+ secret: <redacted 75d634fb>
+ token: <redacted 1c0e77a2>Rails' built-in driver (bin/rails credentials:diff) decrypts both sides of a diff, so git diff, git show and git log -p print credentials in full. When an agent calls git diff, the beans are unexpectedly spilled. This gem meets in the middle: Attempt to decrypt the file (Rails current behavior), but redact the values. The values get hashed so that diffs still show.
Setup
# Gemfile
gem "redacted_credential_diff"In a project already enrolled in Rails' credentials diffing, where .gitattributes names rails_credentials, that is the whole setup. The driver Rails installed is pointed at the redacting command the next time the app loads.
In a project that is not, or to name the driver explicitly:
bin/rails redacted_credential_diff:enroll
git commit .gitattributes
Requires Rails 7.0 or later and Ruby 3.2 or later.
How it works
Git splits a diff driver into two parts.
-
.gitattributesis committed and names the driver for each credentials file.enrollassignsdiff=redacted_credentials, replacing Rails'diff=rails_credentialsentries. - The driver's command lives in each clone's
.git/config, which cannot be committed. A Railtie writes it whenever the app loads in a development or test checkout.
Git then calls bundle exec redacted_credential_diff <path> once for each side of a diff and compares the two outputs itself. The command never sees both sides; it only renders one file. Where it cannot run, the installed driver prints the file as stored, because git aborts the whole diff when a driver fails.
The command does not load the Rails app. It decrypts with ActiveSupport::EncryptedConfiguration and finds the key by Rails' naming convention: config/credentials/<name>.key for config/credentials/<name>.yml.enc when that key exists, otherwise config/master.key. RAILS_MASTER_KEY takes precedence, as in Rails.
What a redacted file looks like
Keys, nesting and line layout are kept. Every value and every comment is replaced by <redacted xxxxxxxx>, where the eight hex digits are a truncated HMAC-SHA256 of the original text, keyed with the credentials encryption key.
- A changed value produces a different fingerprint, so the line shows as changed.
- The same value produces the same fingerprint on both sides, so unchanged lines stay out of the diff.
- Without the encryption key, a fingerprint cannot be checked against guessed values.
Comments are masked because credentials files commonly hold commented-out entries. Values are located by source position from the YAML parser, so multi-line and block scalars are masked in full. Content that does not parse as YAML is masked line by line.
The result is checked before it is printed: it must parse to the same keys as the original and hold nothing but fingerprints. A result that fails the check is discarded and the file is masked line by line instead, without its keys.
What each checkout shows
| State of the checkout |
git diff of a credentials file |
|---|---|
| Gem installed, app loaded at least once | Redacted |
| Fresh clone, app never loaded | Encrypted file as stored |
| No encryption key available | Encrypted file as stored |
| Branch that predates the gem | Encrypted file as stored |
| Gem removed from the project | Encrypted file as stored |
Once the app has loaded in a clone, none of its branches print decrypted credentials. The Railtie also points Rails' rails_credentials driver at the redacting command, which covers branches whose .gitattributes still names it. Having that driver defined also stops bin/rails credentials:edit from installing the decrypting one.
Removing
bin/rails redacted_credential_diff:unenroll
git commit .gitattributes
Then remove the gem from the Gemfile. unenroll takes the driver out of .gitattributes and out of the current clone's git config, leaving credentials files with no diff driver, as Rails generates a project.
The driver's command stays in every other clone's git config, where it prints the encrypted file once the gem is gone. Rails does not replace it, so each clone removes it by hand:
git config --unset diff.redacted_credentials.textconv
git config --unset diff.rails_credentials.textconv
bin/rails credentials:diff --enroll then restores Rails' decrypting diffs.
Limits
- This covers git's diff output only.
bin/rails credentials:showandcredentials:editare unchanged. -
git diff --no-textconvand hosted diff views bypass the driver and show the encrypted file. - Until the app first loads in a clone with the gem installed, that clone keeps whatever driver it already had.
- Key names are printed. A file whose keys are themselves sensitive, such as a map keyed by customer, still shows them.
- Identical values under different keys share a fingerprint.
- Each side of a diff starts
bundle exec, sogit log -pacross a long credentials history is slow. - A key kept at a custom
config.credentials.key_pathis not found, so those files diff as stored. - The command assumes the Gemfile is at the top of the git working tree, where git runs it.
Development
bundle exec rake test # unit tests
bin/e2e 8.1 # generates an app on that Rails release and checks what git prints