Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/flagsmith/sdk/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@ def initialize(config)
def build_headers(faraday, config)
faraday.headers['Accept'] = 'application/json'
faraday.headers['Content-Type'] = 'application/json'
faraday.headers['X-Environment-Key'] = config.environment_key
faraday.headers['User-Agent'] = Flagsmith::SDK::Utils.user_agent
faraday.headers.merge(config.custom_headers)
faraday.headers.merge!(config.custom_headers)
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major · Security & Privacy · Quick win

Keep custom credentials out of request logs.

With the default logger, a request now logs the configured Authorization and X-Proxy-Token values to stdout. Faraday 2.0.1 logs request headers at info level by default, and this client installs that logger without filters. Disable request-header logging or redact sensitive values while still sending them in requests. (raw.githubusercontent.com)


Minor · Data Integrity & Integration · Quick win

Apply one precedence rule to X-Environment-Key in both clients.

ApiClient#build_headers sets X-Environment-Key to config.environment_key, then merge!(config.custom_headers) replaces it with any custom value. EventProcessor#headers gives the environment key precedence over custom headers. If custom_headers sets a different X-Environment-Key, flag requests and event requests can target different environments, and events can be attributed to the wrong environment.

Choose one rule for conflicting X-Environment-Key values, and apply it in both lib/flagsmith/sdk/api_client.rb and lib/flagsmith/sdk/event_processor.rb. The current code uses opposite precedence in each client.

# Set last, so custom_headers cannot replace it (same as the events API).
faraday.headers['X-Environment-Key'] = config.environment_key
end

def configure_logger(faraday, config)
faraday.response :logger, config.logger
faraday.response :logger, config.logger do |formatter|
redact_custom_headers(formatter, config)
end
end

# custom_headers often carry credentials, keep their values out of the logs.
def redact_custom_headers(formatter, config)
Faraday::Utils::Headers.new.update(config.custom_headers).each_key do |name|
formatter.filter(/^(#{Regexp.escape(name)}: ).*$/i, '\1"[REDACTED]"')
end
end

def configure_retries(faraday, config)
Expand Down
50 changes: 50 additions & 0 deletions spec/sdk/api_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,55 @@
expected_user_agent = "flagsmith-ruby-sdk/#{Flagsmith::VERSION}"
expect(connection.headers['User-Agent']).to eq(expected_user_agent)
end

it 'applies custom_headers alongside the default headers' do
custom_config = Flagsmith::Config.new(
environment_key: 'test-key',
api_url: 'https://edge.api.flagsmith.com/api/v1/',
custom_headers: { 'Authorization' => 'Bearer proxy-token', 'X-Proxy-Token' => 'abc' }
)
connection = described_class.new(custom_config).instance_variable_get(:@conn)

expect(connection.headers['Authorization']).to eq('Bearer proxy-token')
expect(connection.headers['X-Proxy-Token']).to eq('abc')
expect(connection.headers['X-Environment-Key']).to eq('test-key')
end

it 'keeps the configured environment key over one in custom_headers' do
custom_config = Flagsmith::Config.new(
environment_key: 'test-key',
custom_headers: { 'x-environment-key' => 'other-key' }
)
connection = described_class.new(custom_config).instance_variable_get(:@conn)

expect(connection.headers['X-Environment-Key']).to eq('test-key')
end
end

describe 'logging' do
it 'sends custom_headers without writing their values to the log' do
log = StringIO.new
custom_config = Flagsmith::Config.new(
environment_key: 'test-key',
api_url: 'https://edge.api.flagsmith.com/api/v1/',
custom_headers: { 'Authorization' => 'Bearer proxy-token', x_proxy_token: 'secret "quoted" value' },
logger: Logger.new(log)
)
connection = described_class.new(custom_config).instance_variable_get(:@conn)
sent_headers = nil
connection.builder.adapter :test do |stub|
stub.get('/api/v1/flags/') do |env|
sent_headers = env.request_headers.dup
[200, {}, '[]']
end
end

connection.get('flags/')

expect(sent_headers['Authorization']).to eq('Bearer proxy-token')
expect(sent_headers['X-Proxy-Token']).to eq('secret "quoted" value')
expect(log.string).to include('Authorization: "[REDACTED]"', 'X-Proxy-Token: "[REDACTED]"')
expect(log.string).not_to include('proxy-token', 'quoted')
end
end
end