Skip to content

fix: apply custom_headers to API client requests - #131

Open
pcbeingused333 wants to merge 1 commit into
Flagsmith:mainfrom
pcbeingused333:fix/api-client-custom-headers
Open

pcbeingused333 wants to merge 1 commit into
Flagsmith:mainfrom
pcbeingused333:fix/api-client-custom-headers

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 18, 2026 •

Copy link
Copy Markdown

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.

View guided diff

@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 18, 2026 23:21
@pcbeingused333
pcbeingused333 requested review from emyller and removed request for a team November 18, 2026 23:21
@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Explain Change →

Walkthrough

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.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Merge Risk: Moderate · up to f0033

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.

Share

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

View guided diff | Re-trigger cubic

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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.

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)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant