From 187db2c333444270e57f7589be12ed61ac22b745 Mon Sep 17 00:00:00 2001 From: austinbenoit Date: Mon, 23 Nov 2026 23:14:03 +0000 Subject: [PATCH 1/2] App Check (Desktop): register function registry calls on every App AppCheckInternal used a process-wide counter to register its function registry entries only when the first instance was created and to unregister them only when the last one was destroyed. Each App has its own function registry, so a second App never got the entries: Auth, Database, Firestore, Functions and Storage on that App looked up FnAppCheckGetTokenAsync, found nothing, and sent their requests without an App Check token. Teardown had the mirror-image problem and unregistered from whichever App happened to bring the counter to zero. The registered callbacks already find the AppCheck instance from the App they are given, so drop the counter and register and unregister on each instance's own App. Fixes #1970 --- app_check/src/desktop/app_check_desktop.cc | 50 ++++---- app_check/tests/CMakeLists.txt | 1 + .../tests/desktop/app_check_desktop_test.cc | 113 ++++++++++++++++++ release_build_files/readme.md | 5 + 4 files changed, 141 insertions(+), 28 deletions(-) create mode 100644 app_check/tests/desktop/app_check_desktop_test.cc diff --git a/app_check/src/desktop/app_check_desktop.cc b/app_check/src/desktop/app_check_desktop.cc index 864d17e203..80fb455c88 100644 --- a/app_check/src/desktop/app_check_desktop.cc +++ b/app_check/src/desktop/app_check_desktop.cc @@ -230,38 +230,32 @@ void AppCheckInternal::RemoveAppCheckListener(AppCheckListener* listener) { } } -static int g_app_check_registry_count = 0; - void AppCheckInternal::InitRegistryCalls() { - if (g_app_check_registry_count == 0) { - app_->function_registry()->RegisterFunction( - ::firebase::internal::FnAppCheckGetTokenAsync, - AppCheckInternal::GetAppCheckTokenAsyncForRegistry); - app_->function_registry()->RegisterFunction( - ::firebase::internal::FnAppCheckGetLimitedUseTokenAsync, - AppCheckInternal::GetLimitedUseAppCheckTokenAsyncForRegistry); - app_->function_registry()->RegisterFunction( - ::firebase::internal::FnAppCheckAddListener, - AppCheckInternal::AddAppCheckListenerForRegistry); - app_->function_registry()->RegisterFunction( - ::firebase::internal::FnAppCheckRemoveListener, - AppCheckInternal::RemoveAppCheckListenerForRegistry); - } - g_app_check_registry_count++; + // Each App has its own function registry, so register on every App that + // has an AppCheck instance. + app_->function_registry()->RegisterFunction( + ::firebase::internal::FnAppCheckGetTokenAsync, + AppCheckInternal::GetAppCheckTokenAsyncForRegistry); + app_->function_registry()->RegisterFunction( + ::firebase::internal::FnAppCheckGetLimitedUseTokenAsync, + AppCheckInternal::GetLimitedUseAppCheckTokenAsyncForRegistry); + app_->function_registry()->RegisterFunction( + ::firebase::internal::FnAppCheckAddListener, + AppCheckInternal::AddAppCheckListenerForRegistry); + app_->function_registry()->RegisterFunction( + ::firebase::internal::FnAppCheckRemoveListener, + AppCheckInternal::RemoveAppCheckListenerForRegistry); } void AppCheckInternal::CleanupRegistryCalls() { - g_app_check_registry_count--; - if (g_app_check_registry_count == 0) { - app_->function_registry()->UnregisterFunction( - ::firebase::internal::FnAppCheckGetTokenAsync); - app_->function_registry()->UnregisterFunction( - ::firebase::internal::FnAppCheckGetLimitedUseTokenAsync); - app_->function_registry()->UnregisterFunction( - ::firebase::internal::FnAppCheckAddListener); - app_->function_registry()->UnregisterFunction( - ::firebase::internal::FnAppCheckRemoveListener); - } + app_->function_registry()->UnregisterFunction( + ::firebase::internal::FnAppCheckGetTokenAsync); + app_->function_registry()->UnregisterFunction( + ::firebase::internal::FnAppCheckGetLimitedUseTokenAsync); + app_->function_registry()->UnregisterFunction( + ::firebase::internal::FnAppCheckAddListener); + app_->function_registry()->UnregisterFunction( + ::firebase::internal::FnAppCheckRemoveListener); } // Static functions used by the internal registry tool diff --git a/app_check/tests/CMakeLists.txt b/app_check/tests/CMakeLists.txt index 95a0035b2c..a870c465ff 100644 --- a/app_check/tests/CMakeLists.txt +++ b/app_check/tests/CMakeLists.txt @@ -25,6 +25,7 @@ firebase_cpp_cc_test( firebase_cpp_cc_test( firebase_app_check_test SOURCES + desktop/app_check_desktop_test.cc desktop/debug_provider_desktop_test.cc ${FIREBASE_CPP_SDK_ROOT_DIR}/app/rest/transport_mock.cc DEPENDS diff --git a/app_check/tests/desktop/app_check_desktop_test.cc b/app_check/tests/desktop/app_check_desktop_test.cc new file mode 100644 index 0000000000..bed5aabd99 --- /dev/null +++ b/app_check/tests/desktop/app_check_desktop_test.cc @@ -0,0 +1,113 @@ +// Copyright 2026 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include +#include + +#include "app/src/function_registry.h" +#include "app/src/include/firebase/app.h" +#include "app/src/include/firebase/future.h" +#include "app/tests/include/firebase/app_for_testing.h" +#include "firebase/app_check.h" +#include "gtest/gtest.h" + +namespace firebase { +namespace app_check { +namespace { + +// Hands out a token that names the App the provider was created for. +class NamedTokenProvider : public AppCheckProvider { + public: + explicit NamedTokenProvider(App* app) + : token_(std::string("token-for-") + app->name()) {} + + void GetToken(std::function + completion_callback) override { + AppCheckToken token; + token.token = token_; + token.expire_time_millis = 4102444800000LL; // 2100-01-01. + completion_callback(token, kAppCheckErrorNone, ""); + } + + private: + std::string token_; +}; + +class NamedTokenProviderFactory : public AppCheckProviderFactory { + public: + AppCheckProvider* CreateProvider(App* app) override { + providers_.push_back(std::make_unique(app)); + return providers_.back().get(); + } + + private: + std::vector> providers_; +}; + +class AppCheckDesktopTest : public ::testing::Test { + protected: + void SetUp() override { AppCheck::SetAppCheckProviderFactory(&factory_); } + void TearDown() override { AppCheck::SetAppCheckProviderFactory(nullptr); } + + NamedTokenProviderFactory factory_; +}; + +// Regression tests for +// https://github.com/firebase/firebase-cpp-sdk/issues/1970. Other products look +// up App Check through the function registry of their own App, so every App +// with an AppCheck instance needs the functions registered. +TEST_F(AppCheckDesktopTest, RegistersRegistryFunctionsOnEveryApp) { + std::unique_ptr default_app(testing::CreateApp()); + std::unique_ptr second_app( + testing::CreateApp(testing::MockAppOptions(), "second")); + std::unique_ptr default_app_check( + AppCheck::GetInstance(default_app.get())); + std::unique_ptr second_app_check( + AppCheck::GetInstance(second_app.get())); + + for (::firebase::internal::FunctionId id : + {::firebase::internal::FnAppCheckGetTokenAsync, + ::firebase::internal::FnAppCheckGetLimitedUseTokenAsync, + ::firebase::internal::FnAppCheckAddListener, + ::firebase::internal::FnAppCheckRemoveListener}) { + EXPECT_TRUE(default_app->function_registry()->FunctionExists(id)) << id; + EXPECT_TRUE(second_app->function_registry()->FunctionExists(id)) << id; + } +} + +TEST_F(AppCheckDesktopTest, EachAppGetsItsOwnTokenThroughItsRegistry) { + std::unique_ptr default_app(testing::CreateApp()); + std::unique_ptr second_app( + testing::CreateApp(testing::MockAppOptions(), "second")); + std::unique_ptr default_app_check( + AppCheck::GetInstance(default_app.get())); + std::unique_ptr second_app_check( + AppCheck::GetInstance(second_app.get())); + + for (App* app : {default_app.get(), second_app.get()}) { + Future token; + ASSERT_TRUE(app->function_registry()->CallFunction( + ::firebase::internal::FnAppCheckGetTokenAsync, app, nullptr, &token)) + << app->name(); + ASSERT_EQ(token.status(), kFutureStatusComplete) << app->name(); + ASSERT_EQ(token.error(), kAppCheckErrorNone) << app->name(); + EXPECT_EQ(*token.result(), std::string("token-for-") + app->name()); + } +} + +} // namespace +} // namespace app_check +} // namespace firebase diff --git a/release_build_files/readme.md b/release_build_files/readme.md index 46566c6817..2e9a325db5 100644 --- a/release_build_files/readme.md +++ b/release_build_files/readme.md @@ -613,6 +613,11 @@ workflow use only during the development of your app, not for publicly shipping code. ## Release Notes +### Upcoming +- Changes + - App Check (Desktop): Fixed requests from any `App` other than the first + one created being sent without an App Check token. + ### 14.2.0 - Changes - General (Android): Update to Firebase Android BoM version 35.2.0. From d326365f2e888b6bc885f4997ccb0ce5afe945a6 Mon Sep 17 00:00:00 2001 From: austinbenoit Date: Wed, 25 Nov 2026 03:39:24 +0000 Subject: [PATCH 2/2] App Check (Desktop): guard InitRegistryCalls and CleanupRegistryCalls against null app_ --- app_check/src/desktop/app_check_desktop.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app_check/src/desktop/app_check_desktop.cc b/app_check/src/desktop/app_check_desktop.cc index 80fb455c88..2208dbe1ad 100644 --- a/app_check/src/desktop/app_check_desktop.cc +++ b/app_check/src/desktop/app_check_desktop.cc @@ -231,6 +231,9 @@ void AppCheckInternal::RemoveAppCheckListener(AppCheckListener* listener) { } void AppCheckInternal::InitRegistryCalls() { + if (app_ == nullptr) { + return; + } // Each App has its own function registry, so register on every App that // has an AppCheck instance. app_->function_registry()->RegisterFunction( @@ -248,6 +251,9 @@ void AppCheckInternal::InitRegistryCalls() { } void AppCheckInternal::CleanupRegistryCalls() { + if (app_ == nullptr) { + return; + } app_->function_registry()->UnregisterFunction( ::firebase::internal::FnAppCheckGetTokenAsync); app_->function_registry()->UnregisterFunction(