You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: apply custom_headers to API client requests - #131
Flagsmith::ApiClient#build_headers ended with faraday.headers.merge(config.custom_headers). Faraday::Utils::Headers#merge returns a new object and does not modify the receiver, so the result was discarded and custom_headers (for example Authorization or a proxy token) never reached the API requests. The event processor already merges them correctly, so only the API client was affected.
The fix uses merge!, so configured headers are added after the default ones.
Test
New spec in spec/sdk/api_client_spec.rb builds the client with custom_headers: { 'Authorization' => ..., 'X-Proxy-Token' => ... } and checks both are on the connection while X-Environment-Key is kept. The spec asserts on headers that the old merge call discarded, so it fails before the change and passes after.
This change was prepared with the assistance of an AI coding agent.
Summary by cubic
Fixes the Flagsmith API client so configured custom_headers (such as Authorization or proxy tokens) are actually sent with API requests.
The header build previously used merge, which returns a new object without modifying the connection's headers, so custom headers were silently discarded. Switched to merge! so custom headers are applied after the defaults, and added a spec covering that behavior.
Written for commit f0033bd. Summary will update on new commits.
ApiClient#build_headers now uses merge! to add configured custom headers to Faraday’s headers. A spec checks that the configured Authorization and X-Proxy-Token headers are present alongside the default X-Environment-Key.
The change makes configured custom headers reach API requests, as intended. With the default logger, those headers, including credentials, will now also appear in stdout logs. A conflicting environment-key header can also make flag and event requests use different environments. Address the logging exposure before merging, and decide on a single precedence rule for the environment key.
- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
The reason will be displayed to describe this comment to others. Learn more.
1 issue found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. 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. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="lib/flagsmith/sdk/api_client.rb">
<violation number="1" location="lib/flagsmith/sdk/api_client.rb:35">
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.</violation>
</file>
Reply with feedback, questions, or to request a fix.
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>
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Organization UI
Review profile: ASSERTIVE
Plan: Advanced
Run ID: dad15c26-d523-4101-9939-daf0e8f21b00
Commits
Reviewing files that changed from the base of the PR and between 9a53b08 and f0033bd.
Files selected for processing: 2
lib/flagsmith/sdk/api_client.rb
spec/sdk/api_client_spec.rb
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Flagsmith::ApiClient#build_headersended withfaraday.headers.merge(config.custom_headers).Faraday::Utils::Headers#mergereturns a new object and does not modify the receiver, so the result was discarded andcustom_headers(for exampleAuthorizationor a proxy token) never reached the API requests. The event processor already merges them correctly, so only the API client was affected.The fix uses
merge!, so configured headers are added after the default ones.Test
New spec in
spec/sdk/api_client_spec.rbbuilds the client withcustom_headers: { 'Authorization' => ..., 'X-Proxy-Token' => ... }and checks both are on the connection whileX-Environment-Keyis kept. The spec asserts on headers that the oldmergecall discarded, so it fails before the change and passes after.This change was prepared with the assistance of an AI coding agent.
Summary by cubic
Fixes the Flagsmith API client so configured
custom_headers(such asAuthorizationor proxy tokens) are actually sent with API requests.The header build previously used
merge, which returns a new object without modifying the connection's headers, so custom headers were silently discarded. Switched tomerge!so custom headers are applied after the defaults, and added a spec covering that behavior.Written for commit f0033bd. Summary will update on new commits.