Support for configuring the browser fetch credentials mode in BrowserClient.#1937
Support for configuring the browser fetch credentials mode in BrowserClient.#1937mauriceraguseinit wants to merge 3 commits into
fetch credentials mode in BrowserClient.#1937Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
brianquinlan
left a comment
There was a problem hiding this comment.
It it possible to construct tests for this? Also update CHANGELOG.md.
| /// | ||
| /// Defaults to [BrowserCredentialsMode.sameOrigin], which matches the | ||
| /// previous behavior when [withCredentials] was `false`. | ||
| BrowserCredentialsMode credentialsMode; |
There was a problem hiding this comment.
Can we make this private? I think that mutating it after construction is error-prone in any case.
There was a problem hiding this comment.
I agree that mutating this after construction is error-prone. However, withCredentials was a mutable public field in the original code. Making requestCredentials final and removing or disabling the withCredentials setter would break backwards compatibility for users who configure the client after instantiation.
How do we want to deal with this?
There was a problem hiding this comment.
How about we make credentialsMode non-final but private. The withCredentials setter can mutate it. When withCredentials is removed, we can make credentialsMode final. WDYT?
| /// | ||
| /// See also: | ||
| /// - https://fetch.spec.whatwg.org/#requestcredentials | ||
| enum BrowserCredentialsMode { |
There was a problem hiding this comment.
Do we want to call this RequestCredentials to match the spec?
| /// This corresponds to the browser `fetch` credentials mode `omit`. | ||
| omit('omit'), | ||
|
|
||
| /// Send credentials for same-origin requests only. |
There was a problem hiding this comment.
and only include credentials in same-origin replies.
| /// This corresponds to the browser `fetch` credentials mode `same-origin`. | ||
| sameOrigin('same-origin'), | ||
|
|
||
| /// Always send credentials, even for cross-origin requests. |
There was a problem hiding this comment.
...and include them in all responses.
…ch credentials modes - Introduce `RequestCredentials` enum (`omit`, `same-origin`, `include`). - Deprecate `withCredentials` getter and setter while maintaining full backwards compatibility. - Update README.md with instructions on how to use the new parameter. - Add unit tests verifying correct constructor initialization and fallback behaviors.
|
I have taken the comments regarding the implementation into account and adjusted the code. |
PR Health
Unused Dependencies
|
| Package | Status |
|---|---|
| http | ❗ Show IssuesThese packages may be unused, or you may be using assets from these packages: |
For details on how to fix these, see dependency_validator.
This check can be disabled by tagging the PR with skip-unused-dependencies-check.
Breaking changes ✔️
| Package | Change | Current Version | New Version | Needed Version | Looking good? |
|---|---|---|---|---|---|
| http | Non-Breaking | 1.6.0 | 1.6.2-wip | 1.6.2-wip | ✔️ |
This check can be disabled by tagging the PR with skip-breaking-check.
API leaks ⚠️
The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.
| Package | Leaked API symbol | Leaking sources |
|---|---|---|
| http | RequestCredentials | browser_client.dart::BrowserClient::requestCredentials browser_client.dart::RequestCredentials::omit browser_client.dart::RequestCredentials::sameOrigin browser_client.dart::RequestCredentials::include browser_client.dart::RequestCredentials::values browser_client.dart::BrowserClient::new::requestCredentials |
This check can be disabled by tagging the PR with skip-leaking-check.
Changelog Entry ✔️
| Package | Changed Files |
|---|
Changes to files need to be accounted for in their respective changelogs.
This check can be disabled by tagging the PR with skip-changelog-check.
Coverage ⚠️
| File | Coverage |
|---|---|
| pkgs/http/lib/src/browser_client.dart | 💔 Not covered |
This check for test coverage is informational (issues shown here will not fail the PR).
This check can be disabled by tagging the PR with skip-coverage-check.
License Headers ✔️
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
| Files |
|---|
| no missing headers |
All source files should start with a license header.
Unrelated files missing license headers
| Files |
|---|
| pkgs/cupertino_http/example/example.dart |
| pkgs/http/example/main.dart |
| pkgs/http_multi_server/test/cert.dart |
This check can be disabled by tagging the PR with skip-license-check.
| @@ -1,3 +1,7 @@ | |||
| ## 1.6.2-wip | |||
There was a problem hiding this comment.
1.6.1-wip (the -wip means Work-In-Progress) has not yet been released so you don't need to add a new version - just add your note to the 1.6.1-wip section.
| @@ -1,3 +1,7 @@ | |||
| ## 1.6.2-wip | |||
|
|
|||
| * Add `BrowserCredentialsMode` and `BrowserClient.credentialsMode` to support the `omit` browser fetch credentials mode. Deprecate `withCredentials`. | |||
| @@ -1,5 +1,5 @@ | |||
| name: http | |||
| version: 1.6.1-wip | |||
| version: 1.6.2-wip | |||
There was a problem hiding this comment.
You can revert this.
| @@ -0,0 +1,47 @@ | |||
| // Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file | |||
There was a problem hiding this comment.
I was imagining that these tests could actually verify the client/server interaction by running an HTTP server. But now I'm not sure if that is possible without actually server the Dart-compiled Javascript from the same host/port as the test server. If it is not possible, then I don't think that these tests are valuable and you can delete them. Sorry for asking you to do this unnecessary work.
| | [`package:fetch_client`][fetch] — [`FetchClient`][fetchclient] | Web | Dart, Flutter | ✅︎ | ✅︎ | ✅︎ | | ||
|
|
||
| > [!NOTE] | ||
| > [`BrowserClient`][browserclient] allows you to configure the browser `fetch` credentials mode (e.g., `omit`, `same-origin`, `include`) by passing the `requestCredentials` parameter to its constructor. |
There was a problem hiding this comment.
80 columns
But this actually feels out of place to mention a client's feature here. Maybe add this to the dartdoc for BrowserClient?
| class BrowserClient extends BaseClient { | ||
| /// Create a [BrowserClient]. | ||
| /// | ||
| /// By default, credentials are sent for same-origin requests only, which |
There was a problem hiding this comment.
I would remove. Most people will not be aware of the past behavior.
This PR adds support for configuring the browser
fetchcredentials mode inBrowserClient.Currently
BrowserClientonly exposes a booleanwithCredentialsoption, which maps to eithersame-origin(whenfalse) orinclude(whentrue). The browserfetchAPI, however, supports a third credentials mode,omit, which cannot be expressed through the existing boolean API. The only way to useomittoday is to fork or copyBrowserClientand change theRequestInitmanually.Closes #1936.
What this changes
BrowserCredentialsModeenum with the valuesomit,sameOrigin, andinclude, matching the three credentials modes defined by the [Fetch standard](https://fetch.spec.whatwg.org/#requestcredentials).credentialsModeproperty and constructor parameter toBrowserClient, defaulting toBrowserCredentialsMode.sameOriginto preserve the existing default behavior.RequestInit.credentialsvalue is now derived fromcredentialsMode.Backward compatibility
withCredentialsproperty is kept and continues to work, but is now deprecated because a boolean cannot represent all three credentials modes.credentialsMode:trueonly whencredentialsModeisinclude;truetoincludeandfalsetosameOrigin.BrowserClient()..withCredentials = truekeeps producing the samefetchbehavior as before.New usage