[Bug] App Check (iOS): crash in TokenChangedMethod because AppCheckToken.expire_time_millis is left uninitialized

Open
#1,556 6 comments 1 reaction 1 assignee View on GitHub

Maintainers usually reply within 1 day

@AustinBenoit is already working on this.

Since Nov 10, 2026.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp, objective-c

Research direction

Start by tracing the iOS notification path in app_check/src/ios/app_check_ios.mm, the AppCheckToken declaration in app_check/src/include/firebase/app_check.h, and the conversion in FirebaseAppCheck.TokenChangedMethod. Check whether the token expiration field is initialized before the callback reaches C#. The report has no reproducer; done means token-change notifications no longer crash when expiration data is absent or invalid, with coverage for that case.

Written by the indexing model from the issue text.

Description

api: appcheck type: bug
Description

On iOS, our app crashes intermittently with Il2CppExceptionWrapper inside FirebaseAppCheck.TokenChangedMethod (the [MonoPInvokeCallback] for App Check token changes). The managed exception cannot propagate through the reverse P/Invoke boundary, so IL2CPP calls std::terminate and the app is killed.

We believe the cause is that AppCheckToken.expire_time_millis is never initialized in the iOS token-change notification path:

  1. app_check/src/ios/app_check_ios.mm (firebase-cpp-sdk, same code in v13.11.0 and v14.1.0), appCheckTokenDidChangeNotification: creates a local firebase::app_check::AppCheckToken cpp_token; and only sets cpp_token.token. The TODO there (b/263138261) notes the notification does not contain an expiration time.
  2. AppCheckToken declares int64_t expire_time_millis; without a default member initializer (app_check/src/include/firebase/app_check.h), so the field holds an indeterminate value.
  3. SwigAppCheckListener::OnAppCheckTokenChanged (app_check/src/swig/app_check.i) copies the token as-is and queues CallbackValue1String1<AppCheckToken> to call into C#.
  4. FirebaseAppCheck.TokenChangedMethod calls AppCheckToken.FromAppCheckTokenInternal, which runs s_unixEpoch.AddMilliseconds((double)tokenInternal.expire_time_millis). When the indeterminate value falls outside the range of DateTime (roughly -6.2e13 to +2.5e14 ms from the Unix epoch), AddMilliseconds throws ArgumentOutOfRangeException. This happens before any user handler is called, and nothing catches it, so the app crashes.

This explains why the crash is intermittent: it depends on whatever value happens to be in that memory.

Note: the crash report does not include the managed exception type or message, so ArgumentOutOfRangeException is our inference from reading the source. We have not reproduced the crash locally.

Expected: a token-change notification should never crash the app. Possible fixes we can think of:

  • Initialize expire_time_millis (for example int64_t expire_time_millis = 0;), or set it explicitly in appCheckTokenDidChangeNotification:.
  • Guard the conversion in FromAppCheckTokenInternal, and/or catch exceptions inside TokenChangedMethod so that nothing escapes the reverse P/Invoke callback.

Because the exception is thrown before the user's TokenChanged handler runs, we cannot work around it in app code other than by not subscribing to TokenChanged on iOS.

Reproducing the issue

We cannot reproduce it deterministically, because it depends on uninitialized memory.

  1. On iOS (IL2CPP), set an App Check provider factory (AppAttestProviderFactory in production) before initializing Firebase.
  2. Get FirebaseAppCheck.DefaultInstance, call SetTokenAutoRefreshEnabled(true), and subscribe to TokenChanged.
  3. Call GetAppCheckTokenAsync(false) at startup and keep the app running so that tokens are refreshed.
  4. Occasionally the app crashes in TokenChangedMethod when a token-change notification is delivered.
Firebase Unity SDK Version

13.15.0

Unity editor version

6000.0.58f2

Installation Method

Unity Package Manager

Problematic Firebase Component(s)

App Check

Other Firebase Component(s) in use

Analytics, Authentication, Crashlytics, Messaging

Additional SDKs you are using

YetAnotherHttpHandler (Rust native library), UniTask

Targeted Platform(s)

Apple Platforms

Unity editor platform

Mac

Scripting Runtime

IL2CPP

Release Distribution Type

Pre-built SDK from https://firebase.google.com/download/unity

Relevant Log Output
Fatal Exception: Il2CppExceptionWrapper
Crashed: com.google.firebase.crashlytics.ios.exception
7  UnityFramework   FIRCLSTerminateHandler() + 426 (FIRCLSException.mm:426)
8  libc++abi.dylib  std::__terminate(void (*)()) + 20
9  libc++abi.dylib  __cxa_get_exception_ptr + 34
10 libc++abi.dylib  __cxxabiv1::exception_cleanup_func(_Unwind_Reason_Code, _Unwind_Exception*) + 138
11 UnityFramework   il2cpp::vm::Exception::Rethrow(Il2CppException*) + 116 (Exception.cpp:116)
12 UnityFramework   il2cpp_codegen_get_generic_virtual_method_internal(MethodInfo const*, MethodInfo const*) + 317
13-15 (mis-symbolicated as core::panicking::panic_fmt from a linked Rust library)
16 LivlyIsland      ReversePInvokeWrapper.FirebaseAppCheck_TokenChangedMethod (Firebase.AppCheck.cpp)
17 UnityFramework   firebase::callback::CallbackValue1String1<firebase::app_check::AppCheckToken>::Run() + 3496
18 UnityFramework   firebase::callback::CallbackEntry::Execute() + 1564
19 UnityFramework   firebase::callback::CallbackDispatcher::DispatchCallbacks() + 300
20 UnityFramework   firebase::callback::PollCallbacks() + 48
21-22 (mis-symbolicated as core::panicking::panic_fmt)
23 UnityFramework   il2cpp::vm::Runtime::InvokeWithThrow(MethodInfo const*, void*, void**) + 624
27 UnityFramework   MonoBehaviour::CallUpdateMethod(int) + 598
If using CocoaPods for Apple platforms, the project's Podfile.lock

Not attached. Firebase Unity SDK 13.15.0, which targets Firebase iOS pod 12.17.0 (FIREBASE_IOS_POD_VERSION in cmake/firebase_unity_version.cmake).

Dominant language
C#
Stars
325
Forks
62
Avg merge
1d 17h
Merged PRs (30d)
13

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from firebase/firebase-unity-sdk

All issues in firebase/firebase-unity-sdk

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.