Skip to content

Commit 48e3a8e

Browse files
mantasclaude
andcommitted
fix: close two gaps in the credential-redaction contract
Config promises that a config object is safe to log, and the transport promises the same for its debug output. Both leaked. Config#inspect rendered proxy verbatim, and a proxy URL carries its credentials inline, so `http://user:pass@proxy:8080` printed the password in full. The userinfo is now blanked while the host stays visible, which is the part worth seeing in a bug report. Transport#redact matched payload keys against an exact list, so the keys Zammad and OAuth actually send went straight to the log in clear text: password_confirm (Zammad's own object attribute), access_token, refresh_token and client_secret. Matching a substring instead covers those and every key the old list held. Config also stored caller-supplied strings as-is. Data members are mutable in Ruby, so `config.url << "..."` worked and mutated the caller's own string object at the same time, and any Transport built from that config afterwards would pick up the change. Each string member is now a frozen copy — copied rather than interned with String#-@, so a credential does not outlive its config in the global fstring table. Reported by CodeRabbit on #53. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 2d60cb1 commit 48e3a8e

6 files changed

Lines changed: 125 additions & 21 deletions

File tree

‎lib/zammad_api/config.rb‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ class Config
7676

7777
URL_PATTERN = %r{\Ahttps?://}i
7878

79+
# The +user:password@+ part of a URL. A proxy URL carries its credentials
80+
# inline, so {#inspect} has to blank them while keeping the host visible.
81+
USERINFO_PATTERN = %r{(?<=://)[^/@]+(?=@)}
82+
7983
def initialize(
8084
url:,
8185
user: nil,
@@ -95,18 +99,18 @@ def initialize(
9599
# these keyword arguments are invisible to the type checker.
96100
# steep:ignore:start
97101
super(
98-
url: normalize_url(url),
99-
user: presence(user),
100-
password: presence(password),
101-
http_token: presence(http_token),
102-
oauth2_token: presence(oauth2_token),
103-
user_agent: user_agent,
102+
url: immutable(normalize_url(url)),
103+
user: immutable(presence(user)),
104+
password: immutable(presence(password)),
105+
http_token: immutable(presence(http_token)),
106+
oauth2_token: immutable(presence(oauth2_token)),
107+
user_agent: immutable(user_agent),
104108
timeout: timeout,
105109
open_timeout: open_timeout,
106110
retries: retries,
107111
retry_interval: retry_interval,
108112
ssl_verify: ssl_verify,
109-
proxy: presence(proxy),
113+
proxy: immutable(presence(proxy)),
110114
logger: logger || Logger.new(IO::NULL)
111115
)
112116
# steep:ignore:end
@@ -136,6 +140,7 @@ def render(key, value)
136140
# Loggers have verbose default inspect output that would drown out the
137141
# rest of the configuration.
138142
return "#<#{value.class}>" if key == :logger
143+
return value.sub(USERINFO_PATTERN, REDACTION).inspect if key == :proxy && value
139144

140145
value.inspect
141146
end
@@ -172,8 +177,16 @@ def presence(value)
172177
value
173178
end
174179

175-
def normalized_string(value)
176-
value.to_s
180+
# Ruby leaves the members of a Data object mutable, so a caller-supplied
181+
# String would otherwise stay writable through the config - and shared
182+
# with the caller's own variable. Freezing a copy keeps a Config, and any
183+
# Transport built from one, genuinely immutable.
184+
#
185+
# This deliberately copies rather than using String#-@: interning would
186+
# keep a credential in the global fstring table for the life of the
187+
# process, well past the config that held it.
188+
def immutable(value)
189+
value.is_a?(String) ? value.dup.freeze : value
177190
end
178191
end
179192
end

‎lib/zammad_api/transport.rb‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ class Transport
3535
Timeout::Error
3636
].freeze
3737

38-
# Request payload keys whose values must never reach the log.
39-
SENSITIVE_KEYS = %i[password token api_token http_token oauth2_token secret private_key].freeze
38+
# Substrings that mark a request payload key as carrying a credential.
39+
# Matching on a substring rather than the whole key covers the variants
40+
# Zammad and OAuth actually send - password_confirm, access_token,
41+
# refresh_token, client_secret - which an exact-match list silently let
42+
# through to the log.
43+
SENSITIVE_KEY_PATTERN = /password|token|secret|private_key/i
4044

4145
REDACTED = '[REDACTED]'
4246

@@ -195,7 +199,7 @@ def log_response(method, path, response, started)
195199

196200
def redact(value)
197201
case value
198-
when Hash then value.to_h { |key, nested| [key, SENSITIVE_KEYS.include?(key.to_s.to_sym) ? REDACTED : redact(nested)] }
202+
when Hash then value.to_h { |key, nested| [key, SENSITIVE_KEY_PATTERN.match?(key.to_s) ? REDACTED : redact(nested)] }
199203
when Array then value.map { redact(it) }
200204
else value
201205
end

‎sig/zammad_api/config.rbs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module ZammadAPI
88
REDACTED_ATTRIBUTES: Array[Symbol]
99
REDACTION: String
1010
URL_PATTERN: Regexp
11+
USERINFO_PATTERN: Regexp
1112

1213
def self.new: (**untyped) -> instance
1314

@@ -54,6 +55,6 @@ module ZammadAPI
5455
def validate_credentials!: () -> void
5556
def validate_numbers!: () -> void
5657
def presence: (untyped) -> untyped
57-
def normalized_string: (untyped) -> String
58+
def immutable: (untyped) -> untyped
5859
end
5960
end

‎sig/zammad_api/transport.rbs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module ZammadAPI
33
RETRIABLE_METHODS: Array[Symbol]
44
RETRIABLE_STATUSES: Array[Integer]
55
RETRIABLE_EXCEPTIONS: Array[untyped]
6-
SENSITIVE_KEYS: Array[Symbol]
6+
SENSITIVE_KEY_PATTERN: Regexp
77
REDACTED: String
88

99
attr_reader config: Config

‎spec/unit/zammad_api/config_spec.rb‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,52 @@ def build(url: 'https://zammad.example.com', http_token: 'token', **overrides)
149149
config = build(password: 'hunter2')
150150
expect(config.to_s).to eq(config.inspect)
151151
end
152+
153+
context 'with an authenticated proxy' do
154+
subject(:rendered) { build(proxy: 'http://puser:pproxy-s3cret@proxy.test:8080').inspect }
155+
156+
it 'redacts the proxy credentials' do
157+
expect(rendered).not_to include('pproxy-s3cret')
158+
end
159+
160+
it 'keeps the proxy host visible' do
161+
expect(rendered).to include('proxy.test:8080')
162+
end
163+
164+
it 'marks the redacted userinfo' do
165+
expect(rendered).to include('proxy="http://[REDACTED]@proxy.test:8080"')
166+
end
167+
end
168+
169+
it 'leaves a proxy without credentials alone' do
170+
expect(build(proxy: 'http://proxy.test:8080').inspect).to include('proxy="http://proxy.test:8080"')
171+
end
172+
end
173+
174+
describe 'immutability of string members' do
175+
it 'freezes the url' do
176+
expect(build.url).to be_frozen
177+
end
178+
179+
it 'does not share the url with the caller' do
180+
supplied = +'https://zammad.example.com/'
181+
config = described_class.new(url: supplied, http_token: 'tok')
182+
supplied << 'mutated'
183+
expect(config.url).to eq('https://zammad.example.com/')
184+
end
185+
186+
it 'freezes the credentials' do
187+
config = build(user: 'u', password: +'pw', http_token: nil)
188+
expect(config.password).to be_frozen
189+
end
190+
191+
it 'freezes the proxy' do
192+
expect(build(proxy: +'http://proxy.test:8080').proxy).to be_frozen
193+
end
194+
195+
it 'freezes the user agent' do
196+
expect(build(user_agent: +'custom/1.0').user_agent).to be_frozen
197+
end
152198
end
153199

154200
it 'is immutable' do

‎spec/unit/zammad_api/transport_spec.rb‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,10 @@
250250
end
251251

252252
describe 'logging' do
253-
subject(:log) { output.string }
253+
subject(:log) { log_device.string }
254254

255-
let(:output) { StringIO.new }
256-
let(:logger) { Logger.new(output, level: Logger::DEBUG) }
255+
let(:log_device) { StringIO.new }
256+
let(:logger) { Logger.new(log_device, level: Logger::DEBUG) }
257257

258258
before do
259259
stub_request(:post, url).to_return(json_response({ id: 1 }, status: 201))
@@ -282,11 +282,51 @@
282282
end
283283

284284
it 'stays silent by default' do
285-
quiet = StringIO.new
286-
allow(quiet).to receive(:write)
287285
stub_request(:get, url).to_return(json_response([]))
288-
unit_transport.get('api/v1/groups', operation: 'test')
289-
expect(quiet).not_to have_received(:write)
286+
287+
expect { unit_transport.get('api/v1/groups', operation: 'test') }
288+
.to output('').to_stdout.and output('').to_stderr
289+
end
290+
291+
context 'with credential-bearing payload keys' do
292+
subject(:log) { log_device.string }
293+
294+
let(:log_device) { StringIO.new }
295+
296+
before do
297+
stub_request(:post, url).to_return(json_response({ id: 1 }, status: 201))
298+
unit_transport(logger: Logger.new(log_device, level: Logger::DEBUG)).post(
299+
'api/v1/groups',
300+
operation: 'test',
301+
body: {
302+
login: 'jane',
303+
password_confirm: 'confirm-s3cret',
304+
access_token: 'access-s3cret',
305+
refresh_token: 'refresh-s3cret',
306+
client_secret: 'client-s3cret'
307+
}
308+
)
309+
end
310+
311+
it 'redacts a password_confirm' do
312+
expect(log).not_to include('confirm-s3cret')
313+
end
314+
315+
it 'redacts an access_token' do
316+
expect(log).not_to include('access-s3cret')
317+
end
318+
319+
it 'redacts a refresh_token' do
320+
expect(log).not_to include('refresh-s3cret')
321+
end
322+
323+
it 'redacts a client_secret' do
324+
expect(log).not_to include('client-s3cret')
325+
end
326+
327+
it 'still keeps a non-sensitive value' do
328+
expect(log).to include('login: "jane"')
329+
end
290330
end
291331
end
292332
end

0 commit comments

Comments
 (0)