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

e2ee: commit a store transaction just after generating one-time keys #2930

Closed
wants to merge 2 commits into from

Conversation

bnjbvr
Copy link
Member

@bnjbvr bnjbvr commented Dec 11, 2023

See commit messages for details. I've gotten several EX devices wedged, and this is my biggest guess so far why it has happened in the first place.

To make it easier to see it's a test function when looking up callers/callees.
@bnjbvr bnjbvr requested a review from a team as a code owner December 11, 2023 17:02
@bnjbvr bnjbvr requested review from jplatte and removed request for a team December 11, 2023 17:02
Copy link
Collaborator

@jplatte jplatte left a comment

Choose a reason for hiding this comment

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

Apart from the fact that I've never heard the term "applicative transaction", looks good.

@bnjbvr bnjbvr changed the title e2ee: commit the application transaction just after generating one-time keys e2ee: commit a store transaction just after generating one-time keys Dec 11, 2023
@bnjbvr
Copy link
Member Author

bnjbvr commented Dec 11, 2023

Apart from the fact that I've never heard the term "applicative transaction", looks good.

You're right, this is made up vocabulary as of today, to disambiguate from sqlite- (or indexeddb, for that matter) level transactions.

Thanks for the review!

@jplatte
Copy link
Collaborator

jplatte commented Dec 11, 2023

Maybe "application-level"? "applicative" has an existing meaning in functional programming.

@bnjbvr
Copy link
Member Author

bnjbvr commented Dec 12, 2023

Ah yeah, I would argue people would think more immediately of applicative as "relating to an application" :-)

@bnjbvr bnjbvr force-pushed the commit-transaction-after-generating-otk branch from 5f8ad7e to de7acb6 Compare December 12, 2023 15:28
We're using applicative transactions to make sure that the account is properly synchronized in the cache vs in the database.

Before this commit, the transaction would be committed only when *all* the operations in it succeeded. This was based on the
assumption that most encryption requests could be replayed, by re-sending them to the server. Unfortunately, this assumption
doesn't hold for when generating one-time keys: it could be that one time-keys would be generated by the client, then
the applicative transaction would fail, resulting in the client "forgetting" about the one time keys it uploaded. The server
rejects reuploads of existing one-time keys, so that would end up wedging a device, causing unable-to-decrypt events, without
a proper way out.

Here, we propose to commit the account just after one-time keys have been generated. One of a few things can occur:
- the transaction succeeds, or the transactions fails *after* handling the server response of an upload of OTK; all good.
- the transaction fails *before* uploading the one-time keys: we can upload them later.
- the transaction fails *after* handling the response from the server: we can re-upload the same one-time keys, because it's
the same set and it hasn't changed since the previous time.

This doesn't go against the spirit of using transactions, since the in-memory caches are always properly synchronized in
that case, and the lock used to make sure at most one process writes to the storage is still hold by the transaction object.
@bnjbvr bnjbvr force-pushed the commit-transaction-after-generating-otk branch from de7acb6 to 038acc2 Compare December 12, 2023 15:39
Copy link

codecov bot commented Dec 12, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (74091de) 82.75% compared to head (038acc2) 82.75%.
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2930      +/-   ##
==========================================
- Coverage   82.75%   82.75%   -0.01%     
==========================================
  Files         220      220              
  Lines       22462    22465       +3     
==========================================
+ Hits        18588    18590       +2     
- Misses       3874     3875       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@uhoreg uhoreg left a comment

Choose a reason for hiding this comment

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

Flushing a transaction seems to be contrary to how transactions are normally supposed to work (all-or-nothing), but I guess it works here.


// Flush the applicative transaction as soon as the keys have been generated. If
// we don't do it here, then it's possible that the
// `StoreTransaction` fails, but we've uploaded keys in the
Copy link
Member

Choose a reason for hiding this comment

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

How would it happen? If the store transaction fails, then the receive_sync_changes will fail, then the whole sync will fail and the keys wont be uploaded?

I think that in that case the sync token wouldn't be persisted, so the app will try again the same sync?

Copy link
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks!

@bnjbvr
Copy link
Member Author

bnjbvr commented Dec 14, 2023

Discussed on chat, it's probably not a fix to the issue I've observed, since the OTKs aren't uploaded at the same time a store transaction is alive. Instead, we'll add more logs to try to understand which keys are generated twice.

@bnjbvr bnjbvr closed this Dec 14, 2023
@bnjbvr bnjbvr deleted the commit-transaction-after-generating-otk branch December 14, 2023 11:36
@richvdh
Copy link
Member

richvdh commented Dec 14, 2023

This was an attempt to fix #1415

@bnjbvr bnjbvr restored the commit-transaction-after-generating-otk branch January 9, 2024 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants