Removing a user from channel breaks security after document updates

If I repeat adding and removing a user from a document permissions after 2 updates security fails and the document does not get marked so that I can purge it from the local database.

Here are the steps I take:

  • Create new Document, adding users to doc.users and call channel() function to add users to the channel

  • Check user can view document

  • Remove user from doc.users and call channel() function

  • Check user can no longer view document

  • Purge document from local database

  • Re-add user to the doc.users and call channel() function to add user into channel

  • Check user can view document

  • Remove user from doc.users and call channel() function

  • User can still view document

Is this normal behaviour?
Is there something I am missing?

These steps are performed in isolation? The answer might be “no” for both, but “yes” for “are there other contributing factors?”

This is what my three components look like.

  1. CB server / data. Test bucket. Test documents have 3 attributes. doc.users, doc.channel_name, doc.data. doc.users contains an array of user names.

  2. Sync Gateway has 3 test users and the sync function contains only
    channel (doc.channel_name);
    access (doc.users, doc.channel_name);

  3. Test app only replicates data and I am using print/debug statements to view document values.

What appears to be happening is that if I add and remove a user twice from the doc.users array - they retain access to the target document and all the data in the document in the mobile app. If I delete the app from the device and then replicate fresh the user does not have access to the target document. It seems to be something in the app.

I wonder if I am missing some important step when replicating data and users loose access to a document.

Even if I purge documents that have no content and also compact the database I can remove/add the user twice. On the third attempt for the target document the target user is removed form the doc.users property and also from the document channel but the document remains available in CBLite with all it’s data. If I make further changes to the document content on the server this is not replicated back to my test app. So it appears that CBLite is getting confused and not clearing down data from the document.