Created
November 12, 2020 02:28
-
-
Save atesgoral/f04d99b3e116f8242c947f922efa1e7b to your computer and use it in GitHub Desktop.
A way to prevent accidental leaking of sensitive data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sensitive | |
@@being_responsible = false | |
def initialize(value) | |
@value = value | |
end | |
def to_s | |
@@being_responsible ? @value : '[REDACTED]' | |
end | |
def inspect | |
@@being_responsible ? super : '[REDACTED]' | |
end | |
def self.responsibly | |
@@being_responsible = true | |
yield | |
ensure | |
@@being_responsible = false | |
end | |
end | |
require 'json' | |
s = Sensitive.new('super secret') | |
puts s | |
puts s.to_json | |
puts s.inspect | |
Sensitive.responsibly do | |
puts s | |
puts s.to_json | |
puts s.inspect | |
end | |
puts s | |
puts s.to_json | |
puts s.inspect |
Output:
[REDACTED]
"[REDACTED]"
[REDACTED]
super secret
"super secret"
#<Sensitive:0x00007f7f7a069dd8 @value="super secret">
[REDACTED]
"[REDACTED]"
[REDACTED]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sensitive data such as credit card numbers or passwords can accidentally leak into logs (e.g. through diagnostic logging) when a log filtering mechanism is not in place. By wrapping sensitive data in a class that requires a special contract to return the actual data, these accidents can be reduced, if not completely prevented.