Skip to content

[AppCheck] Populate iOS TokenChanged expiration from the JWT exp claim - #1961

Merged
AustinBenoit merged 4 commits into
mainfrom
fix/app-check-ios-token-expiry
Nov 11, 2026
Merged

AustinBenoit merged 4 commits into
mainfrom
fix/app-check-ios-token-expiry

Conversation

@AustinBenoit

@AustinBenoit AustinBenoit commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Provide details of the change, and generalize the change in the PR title above.

On iOS, AppCheckListener::OnAppCheckTokenChanged received an AppCheckToken whose expire_time_millis was never set, because the FIRAppCheck token-change notification carries only the token string. The field also had no default value, so listeners read an uninitialized value. The Unity SDK converts that value to a DateTime, which can throw ArgumentOutOfRangeException inside the TokenChanged callback (firebase/firebase-unity-sdk#1556).

This change:

  • Default-initializes AppCheckToken::expire_time_millis to 0 and documents 0 as "expiration time unknown".
  • Adds an internal helper, AppCheckTokenFromJwt, that reads the exp claim from the token's JWT payload using the Base64 and FlatBuffers code already built into firebase_app. If the token is not a JWT with a valid exp (positive, and no later than 9999-12-31T23:59:59Z), the expiration stays 0.
  • Uses the helper in the iOS token-change notification handler.
  • Adds unit tests for the helper, and extends the TestAddTokenChangedListener integration test to compare the listener's expiration with the one GetAppCheckToken returns for the same token. On desktop and Android the two must be identical. On iOS and tvOS they must agree within five minutes: the listener's value comes from the JWT exp (server clock), while GetAppCheckToken's is the local request time plus the token's TTL.
  • Adds a release note.

Android and desktop behavior is unchanged; they already pass an expiration to token-changed listeners.


Testing

Describe how you've tested these changes. Link any manually triggered Integration tests or CPP binary SDK Packaging Github Action workflows, if applicable.

  • Built firebase_app_check and the new firebase_app_check_jwt_util_test on Linux desktop (Debug, with ASan and UBSan). The unit tests pass.
  • Integration tests were triggered with tests-requested: quick on the current head (686b2d5) and all jobs passed: https://github.com/firebase/firebase-cpp-sdk/actions/runs/42869609104. TestAddTokenChangedListener passed on desktop (Linux, Windows), Android (device and emulator), iOS (real device and simulator) and the tvOS simulator.

Type of Change

Place an x the applicable box:

  • Bug fix. Add the issue # below if applicable.
  • New feature. A non-breaking change which adds functionality.
  • Other, such as a build process or documentation change.

Related to firebase/firebase-unity-sdk#1556. Unity users get the fix once the Unity SDK builds against a C++ SDK release that includes this change.


Notes

  • Bug fixes and feature changes require an update to the Release Notes section of release_build_files/readme.md.
  • Read the contribution guidelines CONTRIBUTING.md.
  • Changes to the public API require an internal API review. If you'd like to help us make Firebase APIs better, please propose your change in a feature request so that we can discuss it together.

- Default-initialize AppCheckToken::expire_time_millis to 0 so default-constructed tokens never hold uninitialized memory, and document that 0 means the expiration time is unknown.
- Add AppCheckTokenFromJwt(std::string_view) in app_check/src/common/jwt_util.{h,cc} using Base64Decode + FlatBuffers FlexBuffers to extract the numeric "exp" claim into milliseconds. An exp that is not positive, or is later than 9999-12-31T23:59:59Z, is treated as unknown.
- Use AppCheckTokenFromJwt in -[AppCheckNotificationCenterWrapper appCheckTokenDidChangeNotification:] on iOS (firebase/firebase-unity-sdk#1556).
- Add unit tests in app_check/tests/jwt_util_test.cc and assert positive expire_time_millis in TestAddTokenChangedListener.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces JWT utility functions to parse the expiration time (exp claim) from Firebase App Check tokens on iOS and tvOS. It default-initializes AppCheckToken::expire_time_millis to 0 and populates it using the new AppCheckTokenFromJwt helper during token change notifications on iOS, resolving an ArgumentOutOfRangeException crash in Unity. Additionally, comprehensive unit tests for the JWT utility have been added, and the release notes have been updated. I have no feedback to provide as there are no review comments.

TestAddTokenChangedListener now asserts that the expiration delivered to a
token-changed listener matches the expiration GetAppCheckToken returns for the
same token, within five minutes. On iOS and tvOS the two come from different
clocks (JWT exp vs local request time plus TTL), so they are not expected to
be identical.
@AustinBenoit AustinBenoit added the tests-requested: quick Trigger a quick set of integration tests. label Nov 10, 2026
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. and removed tests-requested: quick Trigger a quick set of integration tests. labels Nov 10, 2026
@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

✅  Integration test succeeded!

Requested by @AustinBenoit on commit 770677d
Last updated: Wed Nov 11 11:40 PST 2026
View integration test log & download artifacts

On desktop and Android the token-changed listener and GetAppCheckToken
receive the same token, so TestAddTokenChangedListener now requires their
expirations to be identical there. iOS and tvOS keep the five-minute
tolerance, since the two values come from different clocks.
@github-actions github-actions Bot added the tests: succeeded This PR's integration tests succeeded. label Nov 10, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 10, 2026
@AustinBenoit AustinBenoit added the tests-requested: quick Trigger a quick set of integration tests. label Nov 10, 2026
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. tests: succeeded This PR's integration tests succeeded. and removed tests-requested: quick Trigger a quick set of integration tests. tests: succeeded This PR's integration tests succeeded. labels Nov 10, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 10, 2026
@AustinBenoit
AustinBenoit marked this pull request as ready for review November 10, 2026 19:14
@AustinBenoit
AustinBenoit merged commit 770677d into main Nov 11, 2026
39 checks passed
@github-actions github-actions Bot added tests: in-progress This PR's integration tests are in progress. tests: succeeded This PR's integration tests succeeded. and removed tests: succeeded This PR's integration tests succeeded. labels Nov 11, 2026
@firebase-workflow-trigger firebase-workflow-trigger Bot removed the tests: in-progress This PR's integration tests are in progress. label Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests: succeeded This PR's integration tests succeeded.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants