Skip to content

Commit 3c9411f

Browse files
authored
Merge pull request alexrudall#517 from samwaree/hide-sensitive-info-inspect
Hide sensitive info when inspecting client
2 parents cc01eb5 + e6aae98 commit 3c9411f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/openai/client.rb

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module OpenAI
22
class Client
33
include OpenAI::HTTP
44

5+
SENSITIVE_ATTRIBUTES = %i[@access_token @organization_id @extra_headers].freeze
56
CONFIG_KEYS = %i[
67
api_type
78
api_version
@@ -107,5 +108,15 @@ def beta(apis)
107108
client.add_headers("OpenAI-Beta": apis.map { |k, v| "#{k}=#{v}" }.join(";"))
108109
end
109110
end
111+
112+
def inspect
113+
vars = instance_variables.map do |var|
114+
value = instance_variable_get(var)
115+
116+
SENSITIVE_ATTRIBUTES.include?(var) ? "#{var}=[REDACTED]" : "#{var}=#{value.inspect}"
117+
end
118+
119+
"#<#{self.class}:#{object_id} #{vars.join(', ')}>"
120+
end
110121
end
111122
end

spec/openai/client/client_spec.rb

+30
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,34 @@
133133
expect(connection.builder.handlers).to include Faraday::Response::Logger
134134
end
135135
end
136+
137+
context "when calling inspect" do
138+
let(:api_key) { "sk-123456789" }
139+
let(:organization_id) { "org-123456789" }
140+
let(:extra_headers) { { "Other-Auth": "key-123456789" } }
141+
let(:uri_base) { "https://example.com/" }
142+
let(:request_timeout) { 500 }
143+
let(:client) do
144+
OpenAI::Client.new(
145+
uri_base: uri_base,
146+
request_timeout: request_timeout,
147+
access_token: api_key,
148+
organization_id: organization_id,
149+
extra_headers: extra_headers
150+
)
151+
end
152+
153+
it "does not expose sensitive information" do
154+
expect(client.inspect).not_to include(api_key)
155+
expect(client.inspect).not_to include(organization_id)
156+
expect(client.inspect).not_to include(extra_headers[:"Other-Auth"])
157+
end
158+
159+
it "does expose non-sensitive information" do
160+
expect(client.inspect).to include(uri_base.inspect)
161+
expect(client.inspect).to include(request_timeout.inspect)
162+
expect(client.inspect).to include(client.object_id.to_s)
163+
expect(client.inspect).to include(client.class.to_s)
164+
end
165+
end
136166
end

0 commit comments

Comments
 (0)