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
2 changes: 1 addition & 1 deletion lib/flagsmith/sdk/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def build_headers(faraday, config)
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)

@cubic-dev-ai cubic-dev-ai Bot Nov 18, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: A configured X-Environment-Key in custom_headers replaces config.environment_key, sending API requests under the wrong environment. Reapply the SDK-owned environment key after merging custom headers.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At lib/flagsmith/sdk/api_client.rb, line 35:

<comment>A configured `X-Environment-Key` in `custom_headers` replaces `config.environment_key`, sending API requests under the wrong environment. Reapply the SDK-owned environment key after merging custom headers.</comment>

<file context>
@@ -32,7 +32,7 @@ def build_headers(faraday, config)
       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)
     end
 
</file context>
Suggested change
faraday.headers.merge!(config.custom_headers)
faraday.headers.merge!(config.custom_headers)
faraday.headers['X-Environment-Key'] = config.environment_key
Fix with cubic

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.

end

def configure_logger(faraday, config)
Expand Down
13 changes: 13 additions & 0 deletions spec/sdk/api_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,18 @@
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
end
end