Skip to content

Commit 3f8145b

Browse files
committed
Do not include sensitive information in the inspect
I'd like to add this functionality to filter out sensitive information because these are exposed when an exception occurs. In my case, I had to manually catch any exception that could occur and send them to an error tracker and noticed that all these attributes were exposed. Of course, the main problem was that my code resulted in throwing this exception, but to me it also felt like a good suggestion to filter these attributes so they aren't unintentionally being exposed.
1 parent bea805c commit 3f8145b

File tree

8 files changed

+65
-0
lines changed

8 files changed

+65
-0
lines changed

lib/oauth2.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# includes gem files
1212
require 'oauth2/version'
13+
require 'oauth2/filtered_attributes'
1314
require 'oauth2/error'
1415
require 'oauth2/authenticator'
1516
require 'oauth2/client'

lib/oauth2/access_token.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ class AccessToken # rubocop:disable Metrics/ClassLength
66
TOKEN_KEYS_SYM = %i[access_token id_token token accessToken idToken].freeze
77
TOKEN_KEY_LOOKUP = TOKEN_KEYS_STR + TOKEN_KEYS_SYM
88

9+
include FilteredAttributes
10+
911
attr_reader :client, :token, :expires_in, :expires_at, :expires_latency, :params
1012
attr_accessor :options, :refresh_token, :response
13+
filtered_attributes :token, :refresh_token
1114

1215
class << self
1316
# Initializes an AccessToken from a Hash

lib/oauth2/authenticator.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
module OAuth2
66
class Authenticator
7+
include FilteredAttributes
8+
79
attr_reader :mode, :id, :secret
10+
filtered_attributes :secret
811

912
def initialize(id, secret, mode)
1013
@id = id

lib/oauth2/client.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ module OAuth2
1616
class Client # rubocop:disable Metrics/ClassLength
1717
RESERVED_PARAM_KEYS = %w[body headers params parse snaky].freeze
1818

19+
include FilteredAttributes
20+
1921
attr_reader :id, :secret, :site
2022
attr_accessor :options
2123
attr_writer :connection
24+
filtered_attributes :secret
2225

2326
# Instantiate a new OAuth 2.0 client using the
2427
# Client ID and Client Secret registered to your

lib/oauth2/filtered_attributes.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module OAuth2
2+
module FilteredAttributes
3+
def self.included(base)
4+
base.extend(ClassMethods)
5+
end
6+
7+
module ClassMethods
8+
def filtered_attributes(*attributes)
9+
@filtered_attribute_names = attributes.map(&:to_sym)
10+
end
11+
12+
def filtered_attribute_names
13+
@filtered_attribute_names || []
14+
end
15+
end
16+
17+
def inspect
18+
filtered_attribute_names = self.class.filtered_attribute_names
19+
return super if filtered_attribute_names.empty?
20+
21+
inspected_vars = instance_variables.map do |var|
22+
if filtered_attribute_names.any? { |filtered_var| var.to_s.include?(filtered_var.to_s) }
23+
"#{var}=[FILTERED]"
24+
else
25+
"#{var}=#{instance_variable_get(var).inspect}"
26+
end
27+
end
28+
"#<#{self.class}:#{object_id} #{inspected_vars.join(', ')}>"
29+
end
30+
end
31+
end

spec/oauth2/access_token_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,4 +741,16 @@ def self.contains_token?(hash)
741741
expect(access_token.to_hash).to eq(hash)
742742
end
743743
end
744+
745+
describe '#inspect' do
746+
let(:inspect_result) { described_class.new(nil, 'secret-token', { refresh_token: 'secret-refresh-token' }).inspect }
747+
748+
it 'filters out the @token value' do
749+
expect(inspect_result).to include('@token=[FILTERED]')
750+
end
751+
752+
it 'filters out the @refresh_token value' do
753+
expect(inspect_result).to include('@refresh_token=[FILTERED]')
754+
end
755+
end
744756
end

spec/oauth2/authenticator_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,10 @@
123123
end
124124
end
125125
end
126+
127+
describe '#inspect' do
128+
it 'filters out the @secret value' do
129+
expect(subject.inspect).to include('@secret=[FILTERED]')
130+
end
131+
end
126132
end

spec/oauth2/client_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,4 +967,10 @@ def stubbed_client(params = {}, &stubs)
967967
expect(subject.connection.builder.handlers).to include(Faraday::Request::UrlEncoded)
968968
end
969969
end
970+
971+
describe '#inspect' do
972+
it 'filters out the @secret value' do
973+
expect(subject.inspect).to include('@secret=[FILTERED]')
974+
end
975+
end
970976
end

0 commit comments

Comments
 (0)