Fix thread safety issue by avoiding mutation of proxy options hash (#1617)

This commit is contained in:
Hiroaki Osawa 2025-04-08 21:53:16 +09:00 committed by GitHub
parent 1551c32371
commit cd1c44a4aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 2 deletions

View File

@ -22,8 +22,10 @@ module Faraday
when URI when URI
value = { uri: value } value = { uri: value }
when Hash, Options when Hash, Options
if (uri = value.delete(:uri)) if value[:uri]
value[:uri] = Utils.URI(uri) value = value.dup.tap do |duped|
duped[:uri] = Utils.URI(duped[:uri])
end
end end
end end

View File

@ -27,6 +27,33 @@ RSpec.describe Faraday::ProxyOptions do
expect(options.inspect).to eq('#<Faraday::ProxyOptions (empty)>') expect(options.inspect).to eq('#<Faraday::ProxyOptions (empty)>')
end end
it 'works with hash' do
hash = { user: 'user', password: 'pass', uri: 'http://@example.org' }
options = Faraday::ProxyOptions.from(hash)
expect(options.user).to eq('user')
expect(options.password).to eq('pass')
expect(options.uri).to be_a_kind_of(URI)
expect(options.path).to eq('')
expect(options.port).to eq(80)
expect(options.host).to eq('example.org')
expect(options.scheme).to eq('http')
expect(options.inspect).to match('#<Faraday::ProxyOptions uri=')
end
it 'works with option' do
opt_arg = { user: 'user', password: 'pass', uri: 'http://@example.org' }
option = Faraday::ConnectionOptions.from(proxy: opt_arg)
options = Faraday::ProxyOptions.from(option.proxy)
expect(options.user).to eq('user')
expect(options.password).to eq('pass')
expect(options.uri).to be_a_kind_of(URI)
expect(options.path).to eq('')
expect(options.port).to eq(80)
expect(options.host).to eq('example.org')
expect(options.scheme).to eq('http')
expect(options.inspect).to match('#<Faraday::ProxyOptions uri=')
end
it 'works with no auth' do it 'works with no auth' do
proxy = Faraday::ProxyOptions.from 'http://example.org' proxy = Faraday::ProxyOptions.from 'http://example.org'
expect(proxy.user).to be_nil expect(proxy.user).to be_nil