redacted_credential_diff 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 95809195c46f686b6bcb4538b1521a919156d31a322597f1db2fe71ddaa843b3
4
+ data.tar.gz: fede840e13b5a97a07d4a2f3acb8bec86c0911ad033026eb709d3c1d49b85afd
5
+ SHA512:
6
+ metadata.gz: 237dc762805b23f35c25e0bbd04801566926706d2222a6e6b8932fd2da68a9cfcf904b5760d246264517cc03c275334a3eefe20b295df6308975d04923433855
7
+ data.tar.gz: 85714e50a0448902f47a9e80d305a3609ef244065296b52f82c65e1de57eb871d59a7486c0e826f1361cb48b7832ec95b7405441f3a31b62421d97a9131449b0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Tim Tilberg
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # redacted_credential_diff
2
+
3
+ A git diff driver for Rails encrypted credentials that shows which keys changed without printing their values.
4
+
5
+ ```diff
6
+ top:
7
+ - secret: <redacted efb7dccb>
8
+ + secret: <redacted 75d634fb>
9
+ + token: <redacted 1c0e77a2>
10
+ ```
11
+
12
+ 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. That output ends up in terminal scrollback, CI logs and the transcripts of any tool that ran the command. This gem replaces the decrypted text with a redacted rendering of the same file.
13
+
14
+ ## Setup
15
+
16
+ ```ruby
17
+ # Gemfile
18
+ gem "redacted_credential_diff"
19
+ ```
20
+
21
+ ```
22
+ bin/rails redacted_credential_diff:enroll
23
+ git commit .gitattributes
24
+ ```
25
+
26
+ `enroll` is run once per project. Nothing is run per developer.
27
+
28
+ Requires Rails 7.0 or later and Ruby 3.2 or later.
29
+
30
+ ## How it works
31
+
32
+ Git splits a diff driver into two parts.
33
+
34
+ - `.gitattributes` is committed and names the driver for each credentials file. `enroll` assigns `diff=redacted_credentials`, replacing Rails' `diff=rails_credentials` entries.
35
+ - 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.
36
+
37
+ 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.
38
+
39
+ 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.
40
+
41
+ ### What a redacted file looks like
42
+
43
+ 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.
44
+
45
+ - A changed value produces a different fingerprint, so the line shows as changed.
46
+ - The same value produces the same fingerprint on both sides, so unchanged lines stay out of the diff.
47
+ - Without the encryption key, a fingerprint cannot be checked against guessed values.
48
+
49
+ 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.
50
+
51
+ 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.
52
+
53
+ ### What each checkout shows
54
+
55
+ | State of the checkout | `git diff` of a credentials file |
56
+ | --- | --- |
57
+ | Gem installed, app loaded at least once | Redacted |
58
+ | Fresh clone, app never loaded | Encrypted file as stored |
59
+ | No encryption key available | Encrypted file as stored |
60
+ | Branch that predates the gem | `fatal: unable to read files to diff` |
61
+ | Clone already holding Rails' driver, app not loaded since the gem was added, on a branch that predates the gem | Decrypted |
62
+
63
+ Once the app has loaded in a checkout, none of its branches print decrypted credentials. The Railtie also points Rails' `rails_credentials` driver at the redacting command; on a branch where that command does not exist, git aborts the diff. Having the driver defined also stops `bin/rails credentials:edit` from installing the decrypting one.
64
+
65
+ The last row is the gap: a clone that ran `credentials:edit` before the gem arrived keeps Rails' decrypting driver until the app next loads there, and that driver still applies on branches whose `.gitattributes` names it.
66
+
67
+ ## Limits
68
+
69
+ - This covers git's diff output only. `bin/rails credentials:show` and `credentials:edit` are unchanged.
70
+ - `git diff --no-textconv` and hosted diff views bypass the driver and show the encrypted file.
71
+ - Key names are printed. A file whose keys are themselves sensitive, such as a map keyed by customer, still shows them.
72
+ - Identical values under different keys share a fingerprint.
73
+ - Each side of a diff starts `bundle exec`, so `git log -p` across a long credentials history is slow.
74
+ - A key kept at a custom `config.credentials.key_path` is not found, so those files diff as stored.
75
+ - The command assumes the Gemfile is at the top of the git working tree, where git runs it.
76
+
77
+ ## Development
78
+
79
+ ```
80
+ bundle exec rake test # unit tests
81
+ bin/e2e 8.1 # generates an app on that Rails release and checks what git prints
82
+ ```
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # Git textconv filter: prints the credentials file at the given path with its
3
+ # values redacted. Git runs it from the top of the working tree.
4
+
5
+ require "redacted_credential_diff"
6
+
7
+ abort "usage: redacted_credential_diff <path>" if ARGV.empty?
8
+
9
+ puts RedactedCredentialDiff::CredentialsFile.new(ARGV.first).redacted
@@ -0,0 +1,62 @@
1
+ require "pathname"
2
+ require "active_support"
3
+ require "active_support/encrypted_configuration"
4
+
5
+ module RedactedCredentialDiff
6
+ # One side of a credentials diff as git hands it to the driver: the working
7
+ # tree file, or an old revision written to a temp file that keeps only the
8
+ # original basename.
9
+ #
10
+ # The key is found by Rails' file naming convention rather than by loading
11
+ # the app, so a key kept at a custom `config.credentials.key_path` is not found.
12
+ class CredentialsFile
13
+ def initialize(path, root: Dir.pwd)
14
+ @path = Pathname(path)
15
+ @root = Pathname(root)
16
+ end
17
+
18
+ # The decrypted contents with values redacted. A file that cannot be
19
+ # decrypted, for want of a key or of the right one, is returned as stored,
20
+ # which is what git diffs with no driver.
21
+ def redacted
22
+ return stored unless encrypted.key
23
+
24
+ Redaction.new(yaml, key: encrypted.key).to_s
25
+ rescue ActiveSupport::MessageEncryptor::InvalidMessage
26
+ stored
27
+ end
28
+
29
+ private
30
+ # Rails stores credentials as written by a binary read, so they decrypt
31
+ # as ASCII-8BIT. Redaction works in characters and needs them as text.
32
+ def yaml
33
+ @yaml ||= encrypted.read.dup.force_encoding(Encoding::UTF_8)
34
+ end
35
+
36
+ def stored
37
+ @path.read
38
+ end
39
+
40
+ def encrypted
41
+ @encrypted ||= ActiveSupport::EncryptedConfiguration.new(
42
+ config_path: @path,
43
+ key_path: key_path,
44
+ env_key: "RAILS_MASTER_KEY",
45
+ raise_if_missing_key: false
46
+ )
47
+ end
48
+
49
+ # config/credentials/production.yml.enc is encrypted with
50
+ # config/credentials/production.key when that exists; everything else,
51
+ # including config/credentials.yml.enc, with config/master.key.
52
+ def key_path
53
+ return environment_key_path if environment_key_path.exist?
54
+
55
+ @root.join("config/master.key")
56
+ end
57
+
58
+ def environment_key_path
59
+ @root.join("config/credentials", "#{@path.basename(".yml.enc")}.key")
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,71 @@
1
+ require "pathname"
2
+
3
+ module RedactedCredentialDiff
4
+ # The git diff driver that renders credentials files through
5
+ # `bundle exec redacted_credential_diff`.
6
+ #
7
+ # Git splits a diff driver in two. The .gitattributes file is committed and
8
+ # names the driver for each credentials file; the driver's command lives in
9
+ # each clone's own git config and cannot be committed. A checkout that has
10
+ # not installed the command diffs the encrypted file as-is.
11
+ class DiffDriver
12
+ NAME = "redacted_credentials"
13
+
14
+ # Rails' own driver prints decrypted credentials. It is pointed at the same
15
+ # command so that branches still naming it in .gitattributes are covered.
16
+ NAMES = [ NAME, "rails_credentials" ]
17
+
18
+ COMMAND = "bundle exec redacted_credential_diff"
19
+
20
+ ATTRIBUTES = <<~END
21
+ config/credentials/*.yml.enc diff=#{NAME}
22
+ config/credentials.yml.enc diff=#{NAME}
23
+ END
24
+
25
+ # A .gitattributes line assigning any diff driver to a credentials file.
26
+ ASSIGNMENT = %r{\Aconfig/credentials\S*\.yml\.enc\s+diff=}
27
+
28
+ def initialize(root)
29
+ @root = Pathname(root)
30
+ end
31
+
32
+ # Defines the driver in this clone's git config.
33
+ def install
34
+ return unless checkout?
35
+
36
+ NAMES.each { |name| git "config", "diff.#{name}.textconv", COMMAND }
37
+ end
38
+
39
+ def installed?
40
+ NAMES.all? { |name| git("config", "--get", "diff.#{name}.textconv").strip == COMMAND }
41
+ end
42
+
43
+ # Names the driver for the credentials files in .gitattributes, replacing
44
+ # any driver already assigned to them.
45
+ def enroll
46
+ gitattributes.write(unassigned_attributes + ATTRIBUTES)
47
+ end
48
+
49
+ private
50
+ def checkout?
51
+ @root.join(".git").exist?
52
+ end
53
+
54
+ # The existing .gitattributes without any diff driver for credentials files.
55
+ def unassigned_attributes
56
+ return "" unless gitattributes.file?
57
+
58
+ gitattributes.read.lines(chomp: true).grep_v(ASSIGNMENT).map { |line| "#{line}\n" }.join
59
+ end
60
+
61
+ def gitattributes
62
+ @root.join(".gitattributes")
63
+ end
64
+
65
+ def git(*arguments)
66
+ IO.popen([ "git", *arguments ], chdir: @root.to_s, err: File::NULL, &:read)
67
+ rescue Errno::ENOENT
68
+ ""
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,17 @@
1
+ require "rails/railtie"
2
+
3
+ module RedactedCredentialDiff
4
+ class Railtie < Rails::Railtie
5
+ # Runs as soon as the application is defined, which includes
6
+ # `credentials:edit`: that command installs Rails' decrypting driver unless
7
+ # one is already defined.
8
+ config.before_configuration do
9
+ driver = DiffDriver.new(Rails.root)
10
+ driver.install if (Rails.env.development? || Rails.env.test?) && !driver.installed?
11
+ end
12
+
13
+ rake_tasks do
14
+ load "redacted_credential_diff/tasks.rake"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,145 @@
1
+ require "openssl"
2
+ require "psych"
3
+
4
+ module RedactedCredentialDiff
5
+ # Credentials YAML with every value and comment replaced by a fingerprint.
6
+ # Keys, nesting and line layout are left as written, so a diff of two
7
+ # redactions shows which entries were added, removed or changed without
8
+ # printing what they hold.
9
+ class Redaction
10
+ MASK = /\A<redacted \h{8}>\z/
11
+ COMMENT_MASK = /\A# <redacted \h{8}>\z/
12
+
13
+ # Line breaks as libyaml counts them, which is more than "\n". Line numbers
14
+ # from the parser only map back to the text when both agree.
15
+ LINE_BREAK = /\r\n|[\r\n\u0085\u2028\u2029]/
16
+ COMMENT = /#[^\r\n\u0085\u2028\u2029]*/
17
+
18
+ # +key+ is the credentials encryption key. Fingerprints are keyed with it so
19
+ # they are stable across both sides of a diff but cannot be reversed by
20
+ # guessing values offline.
21
+ def initialize(yaml, key:)
22
+ @yaml = yaml
23
+ @key = key
24
+ end
25
+
26
+ def to_s
27
+ return masked_line_by_line unless verified?
28
+
29
+ masked
30
+ rescue Psych::SyntaxError
31
+ masked_line_by_line
32
+ end
33
+
34
+ protected
35
+ def key_names
36
+ keys.map(&:value)
37
+ end
38
+
39
+ # True when every value and comment is already a fingerprint.
40
+ def masked?
41
+ values.all? { |node| MASK.match?(node.value) } && comments.all? { |span| COMMENT_MASK.match?(@yaml[span]) }
42
+ end
43
+
44
+ private
45
+ # Masking depends on the parser's source positions lining up with the
46
+ # text. The result is checked rather than trusted: it must parse to the
47
+ # same keys and hold nothing else but fingerprints.
48
+ def verified?
49
+ result = Redaction.new(masked, key: @key)
50
+ result.key_names == key_names && result.masked?
51
+ end
52
+
53
+ def masked
54
+ @masked ||= masks.sort_by { |span, _| -span.begin }.each_with_object(@yaml.dup) do |(span, mask), text|
55
+ text[span] = mask
56
+ end
57
+ end
58
+
59
+ def masks
60
+ value_masks.merge(comment_masks)
61
+ end
62
+
63
+ def value_masks
64
+ values.to_h { |node| [ span_of(node), mask_of(node) ] }
65
+ end
66
+
67
+ # An anchor stays in front of its mask so that aliases to it still resolve.
68
+ def mask_of(node)
69
+ [ anchor_of(node), fingerprint(source_of(node)) ].compact.join(" ")
70
+ end
71
+
72
+ def comment_masks
73
+ comments.to_h { |span| [ span, "# #{fingerprint(@yaml[span])}" ] }
74
+ end
75
+
76
+ # Scalars holding credentials: everything that is not a mapping key.
77
+ def values
78
+ scalars.reject { |node| keys.include?(node) || span_of(node).size.zero? }
79
+ end
80
+
81
+ def keys
82
+ @keys ||= mappings.flat_map { |mapping| mapping.children.each_slice(2).map(&:first) }.grep(Psych::Nodes::Scalar).to_set
83
+ end
84
+
85
+ def scalars
86
+ @scalars ||= document.grep(Psych::Nodes::Scalar)
87
+ end
88
+
89
+ def mappings
90
+ document.grep(Psych::Nodes::Mapping)
91
+ end
92
+
93
+ def document
94
+ @document ||= Psych.parse_stream(@yaml)
95
+ end
96
+
97
+ # Comments can hold commented-out credentials, so they are masked too. A
98
+ # "#" inside a scalar is part of that scalar, not a comment.
99
+ def comments
100
+ hash_runs.reject { |run| scalar_spans.any? { |scalar| scalar.cover?(run.begin) } }
101
+ end
102
+
103
+ # Every "#" through to the end of its line, comment or not.
104
+ def hash_runs
105
+ @yaml.enum_for(:scan, COMMENT).map { Regexp.last_match.begin(0)...Regexp.last_match.end(0) }
106
+ end
107
+
108
+ def scalar_spans
109
+ @scalar_spans ||= scalars.map { |node| span_of(node) }
110
+ end
111
+
112
+ # Character range of a scalar in the source. Block scalars are reported as
113
+ # running to the start of the following line; the trailing whitespace is
114
+ # left in place so the line structure survives.
115
+ def span_of(node)
116
+ first = offset(node.start_line, node.start_column)
117
+ first...(first + @yaml[first...offset(node.end_line, node.end_column)].rstrip.length)
118
+ end
119
+
120
+ def source_of(node)
121
+ @yaml[span_of(node)]
122
+ end
123
+
124
+ def anchor_of(node)
125
+ "&#{node.anchor}" if node.anchor
126
+ end
127
+
128
+ def offset(line, column)
129
+ line_offsets.fetch(line, @yaml.length) + column
130
+ end
131
+
132
+ def line_offsets
133
+ @line_offsets ||= [ 0 ] + @yaml.enum_for(:scan, LINE_BREAK).map { Regexp.last_match.end(0) }
134
+ end
135
+
136
+ # Content that is not valid YAML has no keys to preserve.
137
+ def masked_line_by_line
138
+ @yaml.lines(chomp: true).map { |line| fingerprint(line) unless line.strip.empty? }.join("\n") + "\n"
139
+ end
140
+
141
+ def fingerprint(text)
142
+ "<redacted #{OpenSSL::HMAC.hexdigest("SHA256", @key, text)[0, 8]}>"
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,10 @@
1
+ namespace :redacted_credential_diff do
2
+ desc "Assign the redacting diff driver to the credentials files in .gitattributes"
3
+ task :enroll do
4
+ driver = RedactedCredentialDiff::DiffDriver.new(Rails.root)
5
+ driver.enroll
6
+ driver.install
7
+
8
+ puts "Credentials files now diff through `#{RedactedCredentialDiff::DiffDriver::COMMAND}`. Commit .gitattributes to enroll the rest of the team."
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module RedactedCredentialDiff
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "redacted_credential_diff/version"
2
+ require "redacted_credential_diff/redaction"
3
+ require "redacted_credential_diff/credentials_file"
4
+ require "redacted_credential_diff/diff_driver"
5
+ require "redacted_credential_diff/railtie" if defined?(Rails::Railtie)
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redacted_credential_diff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tim Tilberg
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activesupport
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '7.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '7.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: railties
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '7.0'
40
+ description: |
41
+ A git diff driver for Rails encrypted credentials. Replaces the decrypting driver Rails installs, so that
42
+ git diff, git show and git log -p render each credentials file with its keys intact and every value and
43
+ comment replaced by a keyed fingerprint.
44
+ executables:
45
+ - redacted_credential_diff
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - LICENSE.txt
50
+ - README.md
51
+ - exe/redacted_credential_diff
52
+ - lib/redacted_credential_diff.rb
53
+ - lib/redacted_credential_diff/credentials_file.rb
54
+ - lib/redacted_credential_diff/diff_driver.rb
55
+ - lib/redacted_credential_diff/railtie.rb
56
+ - lib/redacted_credential_diff/redaction.rb
57
+ - lib/redacted_credential_diff/tasks.rake
58
+ - lib/redacted_credential_diff/version.rb
59
+ homepage: https://github.com/ttilberg/redacted_credential_diff
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ source_code_uri: https://github.com/ttilberg/redacted_credential_diff
64
+ bug_tracker_uri: https://github.com/ttilberg/redacted_credential_diff/issues
65
+ rubygems_mfa_required: 'true'
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '3.2'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 4.0.20
81
+ specification_version: 4
82
+ summary: Git diffs of Rails encrypted credentials that show which keys changed without
83
+ printing their values.
84
+ test_files: []