From 3c552c5f72a2f563b178b2e6d0e834e8edae16ff Mon Sep 17 00:00:00 2001 From: austinbenoit Date: Thu, 26 Nov 2026 02:22:11 +0000 Subject: [PATCH 1/2] Firestore (Desktop): remove redundant IsSignedIn checks in GetToken() FirebaseCppCredentialsProvider::GetToken() checked IsSignedIn() and then called RequestToken(), which hard-asserted IsSignedIn() a second time before calling FnAuthGetTokenAsync (which itself checks `auth->current_user().is_valid()`). Because Auth::SignOut() clears the user without holding `contents_->mutex`, a concurrent sign-out landing between the two checks triggered SIMPLE_HARD_ASSERT and aborted the process. Inline RequestToken() into GetToken(), remove the redundant IsSignedIn() pre-checks, and have GetAuthTokenAsync() return an invalid Future (`kFutureStatusInvalid`) when FnAuthGetTokenAsync returns false (Auth unavailable or no signed-in user). Checking `kFutureStatusInvalid` in GetToken() handles both unauthenticated calls and an invalid Future from Auth without allocating a dummy SuccessfulFuture or letting OnCompletion() silently no-op, and returns `AuthToken::Unauthenticated()` immediately. Also guard ConvertToken() so a sign-out racing completion returns `AuthToken::Unauthenticated()` instead of a token with an unauthenticated user, and consume `force_refresh_` atomically via `exchange(false)`. References: - Previous `RequestToken()` contract (`credentials_provider_desktop.h:71-77`): https://github.com/firebase/firebase-cpp-sdk/blob/70aa1bc0e0e088f274c3198c4f1d356091e237cf/firestore/src/main/credentials_provider_desktop.h#L71-L77 - `Auth::GetAuthTokenAsyncForRegistry()` (`auth_desktop.cc:183-206`): https://github.com/firebase/firebase-cpp-sdk/blob/70aa1bc0e0e088f274c3198c4f1d356091e237cf/auth/src/desktop/auth_desktop.cc#L183-L206 Fixes #1974 --- .../src/main/credentials_provider_desktop.cc | 96 ++++++++----------- .../src/main/credentials_provider_desktop.h | 24 ++--- release_build_files/readme.md | 2 + 3 files changed, 49 insertions(+), 73 deletions(-) diff --git a/firestore/src/main/credentials_provider_desktop.cc b/firestore/src/main/credentials_provider_desktop.cc index 8182c34497..d1f3649039 100644 --- a/firestore/src/main/credentials_provider_desktop.cc +++ b/firestore/src/main/credentials_provider_desktop.cc @@ -16,15 +16,14 @@ #include "firestore/src/main/credentials_provider_desktop.h" +#include #include #include #include "Firestore/core/src/util/status.h" #include "app/src/function_registry.h" -#include "app/src/reference_counted_future_impl.h" #include "firebase/auth/types.h" #include "firebase/firestore/firestore_errors.h" -#include "firestore/src/common/futures.h" #include "firestore/src/common/hard_assert_common.h" namespace firebase { @@ -70,8 +69,8 @@ Error FirestoreErrorFromAuthError(int error) { /** * Returns a Future that, when completed, will contain the token for the - * current user or an error. An empty token means that the current user is - * unauthenticated. + * current user or an error, or an invalid Future (`kFutureStatusInvalid`) if + * Auth is unavailable or no user is currently signed in. */ Future GetAuthTokenAsync(App& app, bool force_refresh) { Future result; @@ -83,11 +82,9 @@ Future GetAuthTokenAsync(App& app, bool force_refresh) { return result; } - // If CallFunction does not succeed, it's because either Auth has not - // registered implementations for the FunctionId we're using or because there - // isn't an Auth instance for this App. In either case this means Auth is - // unavailable and we should treat the current user as unauthenticated. - return SuccessfulFuture(""); + // If CallFunction does not succeed, Auth is either unavailable for this App + // or no user is currently signed in. + return Future(); } User GetCurrentUser(App& app) { @@ -112,7 +109,11 @@ StatusOr ConvertToken(const Future& future, App& app) { std::to_string(future.error()) + ")"); } - return AuthToken(*future.result(), GetCurrentUser(app)); + User user = GetCurrentUser(app); + if (!user.is_authenticated()) { + return AuthToken::Unauthenticated(); + } + return AuthToken(*future.result(), std::move(user)); } // Converts the result of the given future into an @@ -181,11 +182,37 @@ void FirebaseCppCredentialsProvider::GetToken( TokenListener listener) { std::lock_guard lock(contents_->mutex); - if (IsSignedIn()) { - RequestToken(std::move(listener)); - } else { + // Take note of the current value of `token_generation` so that this request + // can fail if there is a token change while the request is outstanding. + int expected_generation = contents_->token_generation; + + bool force_refresh = force_refresh_.exchange(false); + Future future = GetAuthTokenAsync(contents_->app, force_refresh); + if (future.status() == kFutureStatusInvalid) { listener(AuthToken::Unauthenticated()); + return; } + + std::weak_ptr weak_contents(contents_); + + // Note: if the future happens to be already completed (either because the + // token was readily available, or theoretically because the Auth token + // request finished so quickly), this completion will be invoked + // synchronously. Because the mutex is recursive, it's okay to lock it again + // in that case. + future.OnCompletion([listener = std::move(listener), expected_generation, + weak_contents](const Future& future_token) { + auto contents = weak_contents.lock(); + // Auth may invoke the callback when credentials provider has already + // been destroyed. + if (!contents) { + return; + } + + std::lock_guard lock(contents->mutex); + OnToken(future_token, contents->app, contents->token_generation, listener, + expected_generation); + }); } void FirebaseCppCredentialsProvider::AddAuthStateListener() { @@ -218,48 +245,5 @@ void FirebaseCppCredentialsProvider::OnAuthStateChanged(void* context) { } } -// Private member functions - -void FirebaseCppCredentialsProvider::RequestToken( - TokenListener listener) { - SIMPLE_HARD_ASSERT(IsSignedIn(), - "Cannot get token when there is no signed-in user"); - - // Take note of the current value of `token_generation` so that this request - // can fail if there is a token change while the request is outstanding. - int expected_generation = contents_->token_generation; - - bool force_refresh = force_refresh_; - force_refresh_ = false; - auto future = GetAuthTokenAsync(contents_->app, force_refresh); - - std::weak_ptr weak_contents(contents_); - - // Note: if the future happens to be already completed (either because the - // token was readily available, or theoretically because the Auth token - // request finished so quickly), this completion will be invoked - // synchronously. Because the mutex is recursive, it's okay to lock it again - // in that case. - future.OnCompletion( - // TODO(c++14): move `listener` into the lambda. - [listener, expected_generation, - weak_contents](const Future& future_token) { - auto contents = weak_contents.lock(); - // Auth may invoke the callback when credentials provider has already - // been destroyed. - if (!contents) { - return; - } - - std::lock_guard lock(contents->mutex); - OnToken(future_token, contents->app, contents->token_generation, - std::move(listener), expected_generation); - }); -} - -bool FirebaseCppCredentialsProvider::IsSignedIn() const { - return GetCurrentUser(contents_->app).is_authenticated(); -} - } // namespace firestore } // namespace firebase diff --git a/firestore/src/main/credentials_provider_desktop.h b/firestore/src/main/credentials_provider_desktop.h index aed23b3c81..079e139265 100644 --- a/firestore/src/main/credentials_provider_desktop.h +++ b/firestore/src/main/credentials_provider_desktop.h @@ -68,16 +68,6 @@ class FirebaseCppCredentialsProvider // interface. static void OnAuthStateChanged(void* context); - // Requests an auth token for the currently signed-in user asynchronously; the - // given `listener` will eventually be invoked with the token (or an error). - // If there is no signed-in user, immediately invokes the `listener` with - // `AuthToken::Unauthenticated()`. - void RequestToken( - firestore::credentials::TokenListener - listener); - - bool IsSignedIn() const; - // Wraps the data that is used by `auth::User::GetToken` callback. This // credentials provider holds a shared pointer to `Contents`, while the // `GetToken` callback stores a weak pointer. This makes safe the case where @@ -87,13 +77,13 @@ class FirebaseCppCredentialsProvider explicit Contents(App& app) : app(app) {} // FirebaseCppCredentialsProvider may be used by more than one thread. The - // mutex is locked in all public member functions and none of the private - // member functions (with the exception of `RequestToken` that locks the - // mutex in a lambda that gets involved asynchronously later). Therefore, - // the invariant is that when a private member function is involved, the - // mutex is always already locked. The mutex is recursive to avoid one - // potential case of deadlock (attaching a continuation to a `Future` which - // may be invoked immediately or asynchronously). + // mutex is locked in all public member functions (and asynchronously in + // `GetToken`'s completion callback) and none of the private member + // functions. Therefore, the invariant is that when a private member + // function is invoked, the mutex is always already locked. The mutex is + // recursive to avoid one potential case of deadlock (attaching a + // continuation to a `Future` which may be invoked immediately or + // asynchronously). // // TODO(b/148688333): make sure not to hold the mutex while calling methods // on `app`. diff --git a/release_build_files/readme.md b/release_build_files/readme.md index 4988efa008..ff483a83d7 100644 --- a/release_build_files/readme.md +++ b/release_build_files/readme.md @@ -624,6 +624,8 @@ code. - Database (Desktop): Fixed cached objects with priorities set (via `SetPriority()` or `SetValueAndPriority()`) being read back from the offline persistence cache as corrupted or null values. + - Firestore (Desktop): Fixed an assertion abort when `Auth::SignOut()` + races a Firestore token request. ### 14.2.0 - Changes From 1ba1b7da0e61c7de67c66d2e4b84bb3aaaa2b3a2 Mon Sep 17 00:00:00 2001 From: austinbenoit Date: Fri, 27 Nov 2026 20:02:58 +0000 Subject: [PATCH 2/2] Firestore (Desktop): guard empty UID in GetCurrentUser() Treat an empty UID from FnAuthGetCurrentUserUid as unauthenticated so a concurrent Auth::SignOut() between Auth's is_valid() and uid() reads cannot trigger HARD_ASSERT(!uid_.empty()) in User(uid). Also update the Contents::mutex comment in credentials_provider_desktop.h. --- firestore/src/main/credentials_provider_desktop.cc | 5 +++-- firestore/src/main/credentials_provider_desktop.h | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/firestore/src/main/credentials_provider_desktop.cc b/firestore/src/main/credentials_provider_desktop.cc index d1f3649039..1878b56000 100644 --- a/firestore/src/main/credentials_provider_desktop.cc +++ b/firestore/src/main/credentials_provider_desktop.cc @@ -93,8 +93,9 @@ User GetCurrentUser(App& app) { ::firebase::internal::FnAuthGetCurrentUserUid, &app, /* args= */ nullptr, &uid); - // If Auth is unavailable, treat the user as unauthenticated. - if (!success) return {}; + // If Auth is unavailable or no user is signed in (including if sign-out + // raced with reading the UID), treat the user as unauthenticated. + if (!success || uid.empty()) return {}; return User(uid); } diff --git a/firestore/src/main/credentials_provider_desktop.h b/firestore/src/main/credentials_provider_desktop.h index 079e139265..f1c9c429ea 100644 --- a/firestore/src/main/credentials_provider_desktop.h +++ b/firestore/src/main/credentials_provider_desktop.h @@ -77,11 +77,11 @@ class FirebaseCppCredentialsProvider explicit Contents(App& app) : app(app) {} // FirebaseCppCredentialsProvider may be used by more than one thread. The - // mutex is locked in all public member functions (and asynchronously in - // `GetToken`'s completion callback) and none of the private member - // functions. Therefore, the invariant is that when a private member - // function is invoked, the mutex is always already locked. The mutex is - // recursive to avoid one potential case of deadlock (attaching a + // mutex guards `change_listener_` and `token_generation` in + // `SetCredentialChangeListener`, `GetToken`, `GetToken`'s completion + // callback, and `OnAuthStateChanged` (`AddAuthStateListener` and + // `RemoveAuthStateListener` do not acquire the mutex themselves). The mutex + // is recursive to avoid one potential case of deadlock (attaching a // continuation to a `Future` which may be invoked immediately or // asynchronously). //