Skip to content

fix: apply custom_headers to API client requests - #131

Open
pcbeingused33 wants to merge 2 commits into
Flagsmith:mainfrom
pcbeingused33:fix/api-client-custom-headers
Open

pcbeingused33 wants to merge 2 commits into
Flagsmith:mainfrom
pcbeingused33:fix/api-client-custom-headers

Conversation

@pcbeingused33

@pcbeingused33 pcbeingused33 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 fix uses merge!. Because the headers are now really sent, two related points are handled too:

  • X-Environment-Key is set after the merge, so custom_headers cannot replace the configured environment key. This is the same precedence the events API already uses.
  • The Faraday response logger writes request headers by default, so the values of custom_headers are shown as [REDACTED] in the log. They are still sent unchanged.

Test

Specs in spec/sdk/api_client_spec.rb check that custom headers are applied, that a custom x-environment-key does not override the configured key, and that a request sends the custom values while the log does not contain them.

This change was prepared with the assistance of an AI coding agent.

@pcbeingused33
pcbeingused33 requested a review from a team as a code owner November 18, 2026 23:21
@pcbeingused33
pcbeingused33 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.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

Comment thread lib/flagsmith/sdk/api_client.rb Resolved

@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.

@pcbeingused33

Copy link
Copy Markdown
Author

Both points are addressed in the latest commit: the configured environment key is now set after custom_headers, and custom header values are redacted in the request log.

This comment was drafted with Claude Code.

@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.

0 issues found across 2 files (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Requires human review: Auto-approval skipped because this PR is from an external contributor.

Re-trigger cubic

@pcbeingused33

Copy link
Copy Markdown
Author

Friendly ping in case this slipped through the cracks. Happy to make any changes if needed, thanks!

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