firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Expose API to clear offline persistence (in IndexedDB).

Open merlinnot opened this issue 8 years ago • 56 comments

Please expose a way to clear offline persistence (in IndexedDB). It would be useful to clean up when the user signs out.

merlinnot avatar Jan 18 '18 14:01 merlinnot

There is not currently (other than poking into IndexedDB directly to delete the data).

mikelehen avatar Jan 22 '18 23:01 mikelehen

Can we make it a feature request then?

merlinnot avatar Jan 23 '18 02:01 merlinnot

I edited it into a feature request. Feel free to add more detail if you have more specific use cases in mind, etc.

mikelehen avatar Jan 25 '18 19:01 mikelehen

I also need this. When a user logs out we need to clear local caches of private data in our application.

scottlu avatar Mar 02 '18 19:03 scottlu

For any given instance of Firestore we have two kinds of data:

  • pending writes, which are specific to a user
  • cached documents, which are not

We could potentially clear either or both of these.

If user makes a change but signs out before the client has finished sending it currently they'd end up with a persistent pending write, which we'd resume when that specific user signed in again.

Are you intending that this kind of data would be cleared as well?

wilhuff avatar Mar 02 '18 20:03 wilhuff

At logoff time we'd want to know that there are pending writes.

If online we'd wait for them to complete before logging off, then we'd delete cached documents.

If offline we'd warn the user and if the user chooses to continue with logging off, we'd delete the pending writes and the cached documents.

scottlu avatar Mar 02 '18 20:03 scottlu

Same thing in my app, I'd just as a user if he wants to discard changes or not. Probably the best solution would be to expose all of these things separately:

  • check if there are pending writes (+events / promise interface would be nice, like flushWrites: () => Promise<void>)
  • clear persisted docs
  • clear persisted writes

Last two can be of course merged into one method with some options.

merlinnot avatar Mar 02 '18 20:03 merlinnot

Makes sense. We've discussed adding some way to detect if there are pending writes or wait to flush pending writes in order to make disableNetwork safer for apps that want to explicitly control sync.

Separately we're working on multi-tab support for offline persistence such that active tabs can see each other's pending writes, even while disconnected. We'll need to investigate how this intersects with that work. If signing out in one tab doesn't sign out the others then nuking and paving IndexedDb out from under the other tabs isn't going to end well.

wilhuff avatar Mar 02 '18 20:03 wilhuff

I think that this could also lead to other issues.

Let's say that we have an app with offline persistence. User A logs in, and fetches her/his profile from the database. We have security rules: match /profiles/{uid} { allow read: if request.auth.uid == uid }. The profile will be fetched and stored locally for offline access.

The problem is that if User A logs out, User B will have access to all local data of that user. For example, if User B logs in and tries to get all profiles, she/he will get a success response with the local data, including the profile of User A, even if she/he doesn't have permissions to do so.

Whenever there's local data from another user, security rules wont work because the first response always comes from all cached data. This could lead to data breaches or wrong results while being offline.

laljg avatar Mar 13 '18 10:03 laljg

I repeat this advice in every discussion related to this but I want to be clear: if you’re at all sensitive to the disclosure of cached information within the same operating system user account: do not enable persistence.

The Firestore client that exists today assumes that operating system user == actual user. Protecting within that boundary is a hard problem that we have not attempted to solve.

We don’t believe clearing IndexedDB on sign-out is sufficient to implement this: it’s trivially defeated by a user forgetting to do it, the browser crashing, or an attacker installing a browser extension to read the contents of the database while another user is signed in.

Clearing IndexedDB is still useful, especially for testing, and we are hoping to add this feature (PRs welcome, of course).

However for sensitive applications like you describe we’re implementing an LRU aspect to our in-memory cache. This will improve perceived performance to be on par with having persistence enabled without requiring the user to remember to sign out or any of the other risks of disclosure.

wilhuff avatar Mar 13 '18 14:03 wilhuff

@wilhuff I agree with you in terms of sensitive data. However, I see this issue has other implications. For example, security rules are not being taken into account when Firestore returns the first callback from cached data, so if two users use the same device with different accounts, the cached response will return information from the other user, so you could end up showing wrong information in offline mode.

In my opinion, it's a good idea to make cache data specific to each user. Clearing IndexedDB seems to be a simple solution to fix all the issues mentioned here as well.

laljg avatar Mar 13 '18 16:03 laljg

If two users use the same device with different operating system accounts, everything works as intended: separate browser caches and operating system permissions on them isolate the users. In this scenario clearing LevelDB doesn't help (or hurt).

If two users use the same device using the same operating system account/browser then separating the cache data within LevelDB doesn't actually protect you: it's trivial to get at the other user's data within the same IndexedDB. If you're willing to work around this by clearing the IndexedDB data in between sessions, just don't write it: disable persistence.

wilhuff avatar Mar 13 '18 17:03 wilhuff

Just FYI-

so if two users use the same device with different accounts, the cached response will return information from the other user, so you could end up showing wrong information in offline mode.

This won't actually be the case for most apps. Since security rules do not act as filters (they either allow or deny a query in its entirety), as long as your app doesn't perform broken queries (that will be rejected by the backend due to security rules), users won't see any locally-cached data that they don't have access to... I repeat Gil's warning that you can't rely on this to protect sensitive data (you should disable persistence instead), but you can expect your app to behave offline just fine even if multiple users have used the app, resulting in various locally-cached data... in fact, sharing the cache improves the offline experience and general performance of the app since there's a greater chance that data requested by the app will already be available locally.

mikelehen avatar Mar 13 '18 20:03 mikelehen

@mikelehen @wilhuff Thanks for your feedback.

laljg avatar Mar 14 '18 10:03 laljg

+1 we need this feature to clear cached data when user reach the maximum storage data quota thanks

mohshraim avatar May 27 '18 22:05 mohshraim

We need this feature since we need to be able to explicitly remove any data stored on the device by our PWA. Our data is allowed to survive several logins by several users, but at some point we need to clear everything (for all users) if any user explicitly requests this.

Our devices (usually laptops) are used at trusted locations by trusted users, but day by day the trusted locations and trusted users might change.

Endran avatar Jun 23 '18 21:06 Endran

We have been running on Firestore for about one year now and are seeing massive performance issues related to the IndexedDB storing large cached data sets and therefor urgently need this feature.

ghost avatar Jun 26 '18 07:06 ghost

@nuvoPoint I had to disable persistence in my app too because of https://github.com/firebase/firebase-js-sdk/issues/513. It might be related to your issue.

merlinnot avatar Jun 26 '18 08:06 merlinnot

@merlinnot Our app needs to be able to run in offline mode also and have persistence if it get restarted, so that's not an option for us. If the Firebase people could explain their IndexedDB structure we might be able to manually clear unnecessary data from the cache?

ghost avatar Jun 26 '18 10:06 ghost

@nuvoPoint
How big is your data stored at IndexedDB? Performance issue on normal PC or mobile devices?

mohshraim avatar Jun 26 '18 11:06 mohshraim