Skip to content
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

Re-establish streams when App Check token expires. #5902

Merged
merged 7 commits into from
Jan 26, 2022

Conversation

ehsannas
Copy link
Contributor

Fixes #5842

@changeset-bot
Copy link

changeset-bot bot commented Jan 18, 2022

🦋 Changeset detected

Latest commit: 27702c1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@firebase/firestore Patch
firebase Patch
@firebase/firestore-compat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ehsannas ehsannas removed their assignment Jan 18, 2022
@google-oss-bot
Copy link
Contributor

google-oss-bot commented Jan 18, 2022

Size Report 1

Affected Products

  • @firebase/auth-compat

    TypeBase (4bd3a88)Merge (17bd41f)Diff
    browser20.0 kB20.1 kB+83 B (+0.4%)
    esm526.9 kB26.9 kB+87 B (+0.3%)
    main29.4 kB29.5 kB+87 B (+0.3%)
    module20.0 kB20.1 kB+83 B (+0.4%)
  • @firebase/auth/cordova

    TypeBase (4bd3a88)Merge (17bd41f)Diff
    browser180 kB180 kB+28 B (+0.0%)
    module180 kB180 kB+28 B (+0.0%)
  • @firebase/auth/internal

    TypeBase (4bd3a88)Merge (17bd41f)Diff
    browser163 kB163 kB+12 B (+0.0%)
    esm5212 kB212 kB+28 B (+0.0%)
    main179 kB179 kB+28 B (+0.0%)
    module163 kB163 kB+12 B (+0.0%)
  • @firebase/auth/react-native

    TypeBase (4bd3a88)Merge (17bd41f)Diff
    browser163 kB164 kB+893 B (+0.5%)
    module163 kB164 kB+893 B (+0.5%)
  • @firebase/firestore

    TypeBase (4bd3a88)Merge (17bd41f)Diff
    browser228 kB229 kB+1.01 kB (+0.4%)
    esm5285 kB286 kB+1.09 kB (+0.4%)
    main453 kB455 kB+1.55 kB (+0.3%)
    module228 kB229 kB+1.01 kB (+0.4%)
    react-native228 kB229 kB+1.01 kB (+0.4%)
  • bundle

    TypeBase (4bd3a88)Merge (17bd41f)Diff
    firestore (Persistence)229 kB230 kB+274 B (+0.1%)
    firestore (Query Cursors)189 kB189 kB+274 B (+0.1%)
    firestore (Query)190 kB190 kB+274 B (+0.1%)
    firestore (Read data once)178 kB179 kB+274 B (+0.2%)
    firestore (Realtime updates)181 kB181 kB+274 B (+0.2%)
    firestore (Transaction)163 kB163 kB+274 B (+0.2%)
    firestore (Write data)162 kB163 kB+274 B (+0.2%)
  • firebase

    TypeBase (4bd3a88)Merge (17bd41f)Diff
    firebase-auth-compat.js123 kB123 kB-29 B (-0.0%)
    firebase-auth-cordova.js91 B462 kB+462 kB (+508079.1%)
    firebase-auth-react-native.js101 B486 kB+486 kB (+480885.1%)
    firebase-auth.js411 kB411 kB+12 B (+0.0%)
    firebase-compat.js753 kB754 kB+243 B (+0.0%)
    firebase-firestore-compat.js281 kB281 kB+272 B (+0.1%)
    firebase-firestore.js770 kB772 kB+2.36 kB (+0.3%)

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/aAZtPSO83l.html

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Jan 18, 2022

Size Analysis Report 1

This report is too large (335,472 characters) to be displayed here in a GitHub comment. Please use the below link to see the full report on Google Cloud Storage.

Test Logs

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/lTQs3ik2R4.html

"@firebase/firestore": patch
---

Re-establish streams when App Check token expires.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is technically correct, but doesn't quite tell the user what problem this fixes. Can you focus on the user impact?

packages/firestore/src/remote/remote_store.ts Outdated Show resolved Hide resolved
@schmidt-sebastian
Copy link
Contributor

As discussed offline, the fix is fine given the backend limitations. Can you update the changelog?

this.appCheckCredentials.start(asyncQueue, () => Promise.resolve());
this.appCheckCredentials.start(asyncQueue, async newAppCheckToken => {
logDebug(LOG_TAG, 'Received new app check token=', newAppCheckToken);
await this.appCheckCredentialListener(newAppCheckToken);
Copy link
Contributor

Choose a reason for hiding this comment

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

await adds a lot of code for older JS configurations. You can just use return here and remove async/await.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

remoteStoreImpl.onlineStateTracker.set(OnlineState.Unknown);
}
remoteStoreImpl.offlineCauses.delete(OfflineCause.CredentialChange);
await enableNetworkInternal(remoteStoreImpl);
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need a separate method here I think - we should be able to just re-use remoteStoreHandleCredentialChange

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment on lines 5 to 9
Fixed bug: Firestore listeners stopped working and received a "Permission Denied"
error when App Check token expired (listener was active longer than the App
Check token TTL configured in the Firebase console). The issue does not occur if
listeners were renewed for other reasons such as Authentication token renewal,
listener being idle for a long time, page refresh, etc.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe:

Suggested change
Fixed bug: Firestore listeners stopped working and received a "Permission Denied"
error when App Check token expired (listener was active longer than the App
Check token TTL configured in the Firebase console). The issue does not occur if
listeners were renewed for other reasons such as Authentication token renewal,
listener being idle for a long time, page refresh, etc.
Fixed an AppCheck issue that caused Firestore listeners to stop working and receive a
"Permission Denied" error. This issue only occurred for AppCheck users that set their
expiration time to under an hour

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -234,6 +246,9 @@ export async function setOnlineComponentProvider(
client.setCredentialChangeListener(user =>
remoteStoreHandleCredentialChange(onlineComponentProvider.remoteStore, user)
);
client.setAppCheckTokenChangeListener((appCheckToken, user) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
client.setAppCheckTokenChangeListener((appCheckToken, user) =>
client.setAppCheckTokenChangeListener((_, user) =>

Copy link
Contributor

Choose a reason for hiding this comment

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

Does the AppCheckTokenListener really return the user?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

AppCheckTokenListener only provides the new app check token, not the user. I take the new app check token, as well as the latest user from the firestore_client to call remoteStoreHandleCredentialChange.

remoteStoreHandleCredentialChange needs to know the latest user because if the user has changed, it'll do a bunch of things related to user changes. By passing it the current user, that logic will be bypassed and only the restarting of the streams occurs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, this is a bit of a strange API as the user has nothing to do with the AppCheck token. But I see that this simplifies things a bit, as you otherwise have to manage the current user in yet another place. Let's leave as is for now and remove once the backend fixes the underlying issue.

Co-authored-by: Sebastian Schmidt <mrschmidt@google.com>
@ehsannas ehsannas merged commit e28b0e4 into master Jan 26, 2022
@ehsannas ehsannas deleted the ehsann/firestore-appcheck-token-expiry branch January 26, 2022 18:57
@google-oss-bot google-oss-bot mentioned this pull request Jan 26, 2022
@firebase firebase locked and limited conversation to collaborators Feb 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[AppCheck] firestore missing or insufficient permissions after a while
3 participants