-
Notifications
You must be signed in to change notification settings - Fork 915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
app-check: getToken with forceRefresh:true swallows errors and returns cached token #8822
Comments
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight. |
The error should be thrown in the case of the 403. I tried:
The catch worked correctly and logged "error [the error message]" when I forced a 403 error by misspelling the projectId:
The recaptcha error (this one actually happens for me on the bad site key - did you get your examples reversed?) seems like it's going down the wrong code path. We added the I think this can probably be fixed just by having the public-facing firebase-js-sdk/packages/app-check/src/api.ts Line 210 in 51465ce
|
Thank you, @hsubox76, for your response and for explaining the reasoning behind implementing internalError; that makes the intent clearer. Regarding being able to catch the error correctly when misspelling the projectId, could you please try this while a valid cached token exists? The issue specifically occurs in this scenario—I should have made that clearer in the reproduction steps (I've now updated them). In this case, the error is silently suppressed instead of being surfaced. While the cached token may still be valid, the purpose of forceRefresh: true is to fetch a new token, so returning the cached token in case of failure misleads the caller. Regarding the error examples, I’ve double-checked and confirmed that they aren't reversed. However, in my examples, I misspelled the I agree with your suggested fix, and I’ve also opened a PR with an alternative approach. I’m happy to proceed with whichever direction you recommend as this issue is blocking us because our implementation requires a fresh token when forceRefresh: true is set, and without error propagation, we can't handle failures properly. Looking forward to your thoughts! Thanks again. |
We don't want to make this change in |
Operating System
macOS Sonoma 14.5
Environment (if applicable)
Chrome
Firebase SDK Version
10.14.1
Firebase SDK Product(s)
AppCheck
Project Tooling
React app with Webpack and jest
Detailed Problem Description
What I was trying to achieve
I expected
getToken(appCheckInstance, { forceRefresh: true })
to return a fresh App Check token or throw an error if the request failed (e.g., due to network issues or misconfiguration). This would allow proper error handling in my application.What actually happened
When an error occurs,
getToken
does not return or throw it. Instead, it silently returns the cached token (if a valid one exists), making failures undetectable programmatically.Unexpected behavior
Code Reference:
In the
getToken
function, when an error occurs andforceRefresh
istrue
, if a valid cached token exists, the error is assigned to theinternalError
property of the token result object. However, thisinternalError
is never surfaced to the caller, and the function silently returns the cached token instead of propagating the error. This appears to be an oversight in handling theforceRefresh
case, specifically in error scenarios:firebase-js-sdk/packages/app-check/src/internal-api.ts
Line 187 in 42cea48
Relevant logs / Console output (observed in DevTools Console)
No Network Scenario:
Despite this logged error, getToken still returns the last cached token instead of failing.
Invalid ReCAPTCHA Site Key Scenario
When testing an incorrect ReCAPTCHA site key, the following message appeared in the console:
Again, the error is logged internally but not returned to the caller, preventing proper handling of any errors.
Expected behavior
Errors should be returned or thrown instead of being logged silently. If throwing the error is not the preferred approach, I would suggest returning the error alongside the token to the third-party caller. This would provide the app with the necessary information to properly detect and handle the failure, ensuring that it is not silently overlooked.
Steps and code to reproduce issue
The text was updated successfully, but these errors were encountered: