Skip to content

feat!: 4.0 Client Hints API: data-minimizing defaults, brand lists, Critical-CH and Vary, package validation - #25

Open
BenjaminAbt wants to merge 23 commits into
mainfrom
feat/v4-client-hints-api
Open

BenjaminAbt wants to merge 23 commits into
mainfrom
feat/v4-client-hints-api

Conversation

@BenjaminAbt

@BenjaminAbt BenjaminAbt commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

This PR also brings #24 into main. #24 was merged into the branch of #23 after #23 had already been merged, so its changes are not in main. The first three commits (114bdd8, 7e1bc8b, a333e16) are the reviewed commits of #24, unchanged; their tree is identical to the merge commit 92e9b45 of #24. The new changes start with fd9df97.

This PR contains the API changes for 4.0: privacy-friendly defaults, decoding of all User-Agent Client Hints including the brand lists, Critical-CH and Vary, package validation against 3.3.5, a smaller package icon, and labels for the release notes. It also fixes the coverage gate, which measured the core package with the ASP.NET Core tests only. An independent review of the parser and the options found five problems, which are fixed in this PR (see Review fixes).

Breaking changes

Change Migration
Platform, Architecture and Device are false by default. Without configuration, Accept-CH requests only Sec-CH-UA and Sec-CH-UA-Mobile. Set the options you need to true.
Accept-CH no longer contains User-Agent. It is not a Client Hint; browsers send it anyway. None.
HttpClientHintsMiddlewareConfig and HttpClientHintsRequestMiddleware are internal. Use AddHttpClientHints and UseHttpClientHints.
An entry of Additional or Critical that is not a valid header name (a space, a comma, quotes, a leading digit) makes AddHttpClientHints throw an ArgumentException. Before, it was sent as is, and browsers ignored the whole Accept-CH header. Entries are trimmed. Fix the entry.
UA, FullVersionList and FormFactors combine a header sent in several field lines, as RFC 9651 requires for lists. Before, they returned the first line. None.
HttpClientHintsOptions.Lifetime is [Obsolete]: Accept-CH-Lifetime was removed from the Client Hints specification, and browsers ignore it. It still sets the header, now in whole seconds independent of the culture. Remove the setting.
IsMobile ignores leading and trailing whitespace (" ?1 " → true, was null). None.

Package validation reports only the two internalized types; they are listed in src/HttpClientHints.AspNetCore/CompatibilitySuppressions.xml. The constructor of the HttpClientHints record does not change: the new values are init properties.

Features

  • Brand lists: HttpClientHintsInterpreter.DecodeBrandList and the properties DecodedUA / DecodedFullVersionList return HttpClientHintsBrand (Brand, Version, IsGrease). IsGrease recognizes the GREASE brands of current Chromium-based browsers, such as Not_A Brand, and of older versions, such as Not A;Brand or "Not\A;Brand.
  • New hints: Bitness/DecodedBitness, Wow64, FormFactors/DecodedFormFactors on the record and the view. Architecture also requests Sec-CH-UA-WoW64, Device also Sec-CH-UA-Form-Factors. The new option FullVersionList requests Sec-CH-UA-Full-Version-List.
  • Decoders: DecodeStructuredBoolean and DecodeStructuredStringList. An internal StructuredFieldReader implements the RFC 9651 parsing of strings, booleans, lists and parameters; DecodeStructuredString uses it with unchanged results.
  • Decoding once per request: the Decoded* properties of the HttpClientHints record decode on the first access and return the same result afterwards. GetClientHints(HttpContext) and the feature return one record per request. Record equality and with expressions are not affected. The view still decodes on every access.
  • Critical sets Critical-CH and adds the hints to Accept-CH and Vary. Vary adds all requested hints to Vary, appended to an existing header once per response.

The Accept-CH middleware stays async, as decided in b8aa25b.

Performance of the brand lists

Change ReadDecodedBrandLists (two lists, three brands each)
Before 266 ns, 744 B
Arrays of the exact size instead of List (a list has at most one member more than commas) 268 ns, 648 B
Scalar loops instead of vectorized searches for keys, tokens and white space, which are a few characters long 165 ns, 648 B
SearchValues lookup for the first character of a key 157 ns, 648 B

A vectorized search for the end of a string was measured as well (285 ns) and is not used. Reading both lists again from the snapshot of the request takes 10 ns and allocates nothing. The field for the decoded values adds 16 B to the record (104 B → 120 B).

Review fixes

Finding Fix
Decoding a list with many escaped strings allocated O(n²): a 16 KB brand list allocated 90 MB per decode, a 32 KB list 342 MB. One request header within Kestrel's limits was enough. The string builder starts with the default capacity. 8, 16 and 32 KB headers now allocate 241 KB, 481 KB and 961 KB. Regression test included.
IsGrease missed the GREASE forms of older Chromium versions. New rule: the letters NotABrand with any of ()-./:;=?_"\ around them.
Hint names were not trimmed or validated (duplicates with spaces, invalid Accept-CH). Trimmed and validated, see breaking changes.
Vary was appended again when the pipeline ran again for the same response (UseStatusCodePagesWithReExecute). Appended only if missing.
The coverage gate keyed lines by file and line, so a covered method hid an uncovered lambda on the same line; a report without packages stopped the script. Lines are keyed per class, branches per number of branches; empty reports are skipped.

Packages, CI and release notes

Verification

  • dotnet build -c Release: 0 warnings. Each commit builds and passes its tests.
  • dotnet test -c Release with coverage: 872 tests passed; line and branch coverage of both packages 100 % with the new gate.
  • Native AOT smoke test: no trimming or AOT warnings, 24 of 24 checks passed (locally win-x64, CI linux-x64, .NET 8 to 11).
  • dotnet pack -p:Version=4.0.0: package validation passes. Locally the assembly version is 1.0.0 without NBGV, which validation rejects (CP0003); CI builds with the NBGV version.
  • dotnet format --verify-no-changes: no changes.

Benchmarks (.NET 11, same command as in the readme)

Benchmark Before After
GetClientHintsFromHeaders 56.99 ns, 88 B 82.25 ns, 120 B
GetClientHintsFromContextUncached 71.01 ns, 88 B 95.80 ns, 120 B
UseHttpClientHints 17.60 ns, 0 B 12.49 ns, 0 B
ReadDecodedBrandLists (new) 157.88 ns, 648 B
ReadDecodedBrandListsFromCachedSnapshot (new) 10.08 ns, 0 B
UseHttpClientHintsWithCriticalAndVary (new) 36.32 ns, 0 B

The snapshot reads three more headers (Sec-CH-UA-Bitness, Sec-CH-UA-WoW64, Sec-CH-UA-Form-Factors) and has three more fields and the field for the decoded values. UseHttpClientHints no longer sets Accept-CH-Lifetime in the benchmark.

- Set ContinuousIntegrationBuild in GitHub Actions. The embedded PDBs of the
  published packages contained paths of the build agent
  (/home/runner/work/...); they are now mapped to /_/.
- Fix the product name of the core package ("HTTP Client Agents") and the
  descriptions of both packages. Client Hints do not identify bots.
- Use single-word tags: NuGet split "User Agent" into "User" and "Agent".
- Add Copyright and PackageReleaseNotes.
- Run dotnet format --verify-no-changes in every build.
- Pin dotnet/nbgv to v0.5.2 instead of the master branch.
- Update actions/download-artifact from v4 to v8, which runs on Node.js 24.
  The packages are downloaded by name, which v5 to v8 did not change.
- Add Dependabot for NuGet and GitHub Actions with grouped updates.
  Microsoft.Extensions.Primitives stays at the lowest version for .NET 8.
- SECURITY.md asked to report vulnerabilities in public issues. It now points
  to GitHub private vulnerability reporting.
- Replace the unrelated Azure rule in copilot-instructions.md with the build,
  test, code and documentation rules of this repository.
- Replace the solution item for the missing ci.yml with the existing
  workflow files and dependabot.yml.
The report that dotnet test merges keeps the hits of only one test project
for a package. The core package is covered by its own tests and partly by
the ASP.NET Core tests, so the gate measured the coverage of the ASP.NET Core
tests only. The script now reads the report of every test project and target
framework and takes, for every line, the highest coverage of any report.
Cobertura does not record which branches are covered, so the branch coverage
is a lower bound. Lines that are not covered are listed.
…64 and FormFactors

- HttpClientHintsInterpreter.DecodeBrandList parses Sec-CH-UA and
  Sec-CH-UA-Full-Version-List into HttpClientHintsBrand (Brand, Version,
  IsGrease). Parameters other than v are ignored.
- DecodeStructuredStringList decodes Sec-CH-UA-Form-Factors, and
  DecodeStructuredBoolean decodes Sec-CH-UA-Mobile and Sec-CH-UA-WoW64.
  IsMobile uses it and now ignores surrounding whitespace.
- HttpClientHints and HttpClientHintsView: DecodedUA,
  DecodedFullVersionList, Bitness, DecodedBitness, Wow64, FormFactors and
  DecodedFormFactors. The record gets the new values as init properties, so
  its constructor does not change.
- An internal StructuredFieldReader implements the RFC 9651 parsing;
  DecodeStructuredString uses it without changing its results.
… Vary

BREAKING CHANGES
- Platform, Architecture and Device are false by default. Only Sec-CH-UA and
  Sec-CH-UA-Mobile, which browsers send with every request, are requested
  without configuration.
- Accept-CH no longer contains User-Agent, which is not a Client Hint.
- HttpClientHintsMiddlewareConfig and HttpClientHintsRequestMiddleware are
  internal. Use AddHttpClientHints and UseHttpClientHints.

Features
- FullVersionList requests Sec-CH-UA-Full-Version-List. Architecture also
  requests Sec-CH-UA-WoW64, Device also Sec-CH-UA-Form-Factors.
- Critical sets Critical-CH and adds the hints to Accept-CH and Vary.
  Vary adds all requested hints to the Vary header.
- Empty entries and duplicates in Additional and Critical are ignored.
- Lifetime is obsolete: browsers ignore Accept-CH-Lifetime, which was
  removed from the Client Hints specification. It still sets the header.

The middleware stays async. The smoke test, benchmarks and sample use the
new options. The public key is on one line for InternalsVisibleTo.
- Package validation compares the API with the packages 3.3.5 on NuGet.org.
  CompatibilitySuppressions.xml lists the two types that are now internal.
- CI packs in every build, so pull requests are validated as well.
- The packages contain a 128x128 icon (29 KB) instead of the 512x512 logo
  (281 KB). The logo for the readme is compressed losslessly (244 KB).
- Options with requested hints, entropy and defaults; Critical, Vary and the
  obsolete Lifetime; resulting response headers.
- Decoded values for every header, the decoders of HttpClientHintsInterpreter
  and their rules, brand lists and GREASE brands.
- Coverage gate, package validation and the baseline update after a release.
- Benchmarks measured again with the new snapshot values, brand list
  decoding and the Critical-CH and Vary middleware.
@BenjaminAbt BenjaminAbt added enhancement New feature or request breaking-change Breaking change, listed first in the release notes github-actions Changes GitHub Actions workflows tests Changes tests and removed enhancement New feature or request labels Nov 24, 2026
The packages use res/httpclienthints-icon.png; no file references the old logo.
The autolabeler rules in release-drafter.yml never ran: Release Drafter runs
only on pushes to main, and the autolabeler needs pull request events. The
rules also expected feature/ branches and "feature" in the title, while this
repository uses Conventional Commits.

- pr-labeler.yml runs Release Drafter on pull_request_target with
  disable-releaser, so pull requests from forks are labeled as well. It does
  not check out code of the pull request.
- Every pull request gets one category label from the type in its title.
  Release Drafter v6 lists a pull request in every category whose labels it
  has, so a breaking change ("feat!:") gets only breaking-change, and the
  dependencies rule for project files is removed; Dependabot sets that label.
- New category "Breaking Changes", listed first.
The reader created the StringBuilder of an escaped string with the length of
the whole header. A list with many escaped members allocated O(n²): decoding
a 16 KB brand list twice allocated 180 MB. The builder now starts with the
default capacity: 8, 16 and 32 KB headers allocate 241 KB, 481 KB and
961 KB. Found in review.
Older Chromium versions put the special characters before "Not" and also use
quote and backslash, for example ";Not A Brand" or "\"Not\A;Brand". IsGrease
now accepts the letters NotABrand in this order with any of the characters
 ()-./:;=?_"\ around them. Found in review.
- Additional and Critical: entries are trimmed. An entry that is not a valid
  header name (a space, a comma, quotes, a leading digit) makes
  AddHttpClientHints throw an ArgumentException, because browsers ignore the
  whole Accept-CH header if one member is not a token.
- The middleware adds its Vary values once per response, also when the
  pipeline runs again, for example with UseStatusCodePagesWithReExecute.
- UA, FullVersionList and FormFactors combine a header sent in several
  field lines, as RFC 9651 requires for lists. Other headers keep using the
  first line.
- The obsolete Lifetime sends whole seconds independent of the current
  culture; de-DE sent "1,5".

Found in review.
- Lines are keyed per class: a covered method no longer hides an uncovered
  lambda on the same source line.
- Branches with a different number of branches per target framework are
  counted per number, so the branch coverage stays a lower bound.
- A report without packages or classes no longer stops the script.

Found in review.
- Options: trimming and validation of names, Vary once per response.
- Decoded values: list headers in several field lines; GREASE rule with the
  forms of older Chromium versions.
- Coverage gate: lines per class, empty results directory.
- Benchmarks measured again.
main accepts changes only through pull requests whose Build and Test and Native AOT checks have passed.
- The list decoders return arrays of the exact size instead of a List: a list
  has at most one member more than commas, so the number of commas gives a
  safe upper bound. An empty header returns an empty array without allocating.
- Keys, tokens and the white space between members are a few characters
  long. Scalar loops skip them faster than vectorized searches, whose setup
  dominated for these lengths. A vectorized search for the end of a string
  was measured as well and was slower, so strings keep the scalar loop.

ReadDecodedBrandLists (two lists with three brands): 266 ns, 744 B ->
158 ns, 648 B.
The Decoded* properties of HttpClientHints decode on the first access and
return the same result afterwards. GetClientHints(HttpContext) and the
feature return one record per request, so every value of a request is
decoded at most once: reading both brand lists again takes 10 ns and
allocates nothing, instead of 158 ns and 648 B.

The values are held in a field whose equality is always true, so record
equality and GetHashCode do not change. A record created with a with
expression detects that the values belong to the original and decodes its
own. The field adds 16 B to the record (104 B -> 120 B); the values object
is allocated only on the first access of a Decoded* property.
The record decodes each value once, the view on every access. Benchmarks
measured again, with ReadDecodedBrandListsFromCachedSnapshot.
feat(tests): add tests for Vary header restoration
fix(interpreter): validate long UTF-8 display strings
fix(tests): ensure headers are released before decoding
chore(deps): update Nerdbank.GitVersioning to 3.10.94
feat(tests): add unit tests for release draft updates

feat(middleware): enhance Vary header handling for caching

feat(docs): improve documentation for ASP.NET Core caching

fix(samples): update sample project for new configurations

refactor(python): implement release package download script

chore(release): add changelog configuration for releases

chore(release): create release notes for version 4.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change Breaking change, listed first in the release notes github-actions Changes GitHub Actions workflows tests Changes tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant