feat: add opt-in RefitSettings.ValidateHeaders#2225
Merged
Conversation
…alues - add `RefitSettings.ValidateHeaders` (default false); when true, header values are applied with `HttpHeaders.Add` so malformed values throw `FormatException` at request-build time instead of being sent verbatim - thread the flag through both request builders in parity: the generated `GeneratedRequestRunner.SetHeader`/`AddHeaderCollection` and the reflection `RequestBuilderImplementation.SetHeader` - keep CR/LF stripping in both modes and preserve the request/content header split and same-name removal guard - emit `refitSettings.ValidateHeaders` into generated header application - promote pending PublicAPI to shipped and record the new setting - document the opt-in in the README and breaking-changes notes Closes #1853
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2225 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 182 183 +1
Lines 9541 9553 +12
Branches 1795 1796 +1
=========================================
+ Hits 9541 9553 +12 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
glennawatson
marked this pull request as ready for review
July 14, 2026 14:43
…ders branches - Extract the validated/verbatim header-apply logic and the request/content InvalidOperationException fallback into a single internal HttpHeaderApplier in Refit, called by both GeneratedRequestRunner and the reflection RequestBuilderImplementation via ProjectReference + InternalsVisibleTo, removing the copy-pasted duplication. - Add TUnit tests covering the validated success path, the content-header fallback, and the no-content drop branches for both the generated runner and the reflection builder.
…validation # Conflicts: # docs/breaking-changes.md
…validation # Conflicts: # docs/breaking-changes.md # src/InterfaceStubGenerator.Shared/Emitter.Inline.cs
ChrisPulman
approved these changes
Jul 15, 2026
…validation # Conflicts: # docs/breaking-changes.md # src/tests/Refit.GeneratorTests/_snapshots/InterfaceStubGeneratorTests.FindInterfacesSmokeTest#IGitHubApi.g.verified.cs # src/tests/Refit.GeneratorTests/_snapshots/InterfaceStubGeneratorTests.FindInterfacesSmokeTest#INestedGitHubApi.g.verified.cs
…validation # Conflicts: # docs/breaking-changes.md
…validation # Conflicts: # docs/breaking-changes.md # src/Refit.Reflection/RequestBuilderImplementation.RequestBuilding.cs
…validation # Conflicts: # docs/breaking-changes.md
…validation # Conflicts: # docs/breaking-changes.md
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



What kind of change does this PR introduce?
Feature - a new opt-in
RefitSettings.ValidateHeaderssetting. Additive and off by default, so existing behaviour is unchanged.What is the new behavior?
RefitSettings.ValidateHeaders(defaultfalse).true, Refit applies header values withHttpHeaders.Add, which validates each value against its header parser. A malformed value (for example an invalidIf-Modified-Since, a badUser-Agent, or a date header that does not parse) now throwsFormatExceptionwhile the request is being built, instead of being sent as-is.GeneratedRequestRunner) and the reflection path (Refit.Reflection).What is the current behavior?
Header values are always added verbatim with
HttpHeaders.TryAddWithoutValidation, so malformed values are sent to the server untouched and are never surfaced as an exception.Closes #1853
What might this PR break?
false, so the current verbatim behaviour is preserved unless a caller explicitly setsValidateHeaders = true.FormatExceptionat request-build time for header values the framework parser rejects. This is the intended effect of enabling validation.Checklist
mainbranchAdditional information
FormatException; validation disabled sends the value verbatim. The pair used to prove validation throws isIf-Modified-Sincewith the valuenot a date- verified empirically:HttpHeaders.Add("If-Modified-Since", "not a date")throwsFormatException, whileTryAddWithoutValidationstores it verbatim.docs/breaking-changes.md.Addby default would reject many currently-valid header values with no security benefit (CR/LF is already stripped), so this is strictly opt-in.