Skip to content

Commit 6f9819e

Browse files
committed
Hide sensitive info when inspecting client
1 parent cc01eb5 commit 6f9819e

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/openai/client.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,17 @@ def beta(apis)
107107
client.add_headers("OpenAI-Beta": apis.map { |k, v| "#{k}=#{v}" }.join(";"))
108108
end
109109
end
110+
111+
def inspect
112+
sensitive_attributes = %i[@access_token @organization_id @extra_headers]
113+
114+
vars = instance_variables.map do |var|
115+
value = instance_variable_get(var)
116+
117+
sensitive_attributes.include?(var) ? "#{var}=[REDACTED]" : "#{var}=#{value.inspect}"
118+
end
119+
120+
"#<#{self.class}:#{object_id} #{vars.join(', ')}>"
121+
end
110122
end
111123
end

spec/openai/client/client_spec.rb

Lines changed: 30 additions & 0 deletions
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)