matrix-org / matrix-org/matrix-rust-sdk
Race on account creation causing upload of 100 OTKs instead of 50
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 2.3k
- Forks
- 500
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 106
Description
Can be reproduced on EX.
After login, the sdk will schedule:
- The upload the current device keys
- The first encryption sync
We can see from the logs that the initial key/upload is handled first. This initial upload also contained 50 otks, so the one time key count is updated
https://github.com/matrix-org/matrix-rust-sdk/blob/fbc914f58636b3efb46663a0f3f7598fc6b3bfc2/crates/matrix-sdk-crypto/src/olm/account.rs#L1222-L1226
11-06 11:38:16.085 Updated uploaded one-time key count 0 -> 50. | crates/matrix-sdk-crypto/src/olm/account.rs:535 | spans: keys_upload{request_id="353c874435e841c294845723da7d190a"}
Then in a quick race the first sync response is handled, the sync response says:
"device_one_time_keys_count": { "signed_curve25519": 0 },
So the one time key count is updated again to 0 this time.
https://github.com/matrix-org/matrix-rust-sdk/blob/fbc914f58636b3efb46663a0f3f7598fc6b3bfc2/crates/matrix-sdk-crypto/src/machine/mod.rs#L1390-L1393
11-06 11:38:16.091 Updated uploaded one-time key count 50 -> 0. | crates/matrix-sdk-crypto/src/olm/account.rs:535 | spans: next_sync_with_lock > sync_once > handle_response > preprocess_to_device_events > receive_sync_changes
As a consequence the sdk considers that the intial 50 keys have been used, so it will generate new ones.
Not sure if this behavior could cause problems.
If I remind correctly on legacy sdk we never considered the response of key/upload to update the published count of one time key.
Instead we only track the count that we got from the sync.
That means that after the fisrt sync the current count was considered unknown, thus an empty key/upload call was made to have the current count.
Maybe could avoid this scenario?, but we expect the sync response to be valid (see https://github.com/element-hq/synapse/issues/17491)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the one-time-key count updates in crates/matrix-sdk-crypto/src/olm/account.rs and crates/matrix-sdk-crypto/src/machine/mod.rs, then trace the key/upload and initial sync handling shown in the logs. Reproduce the account-creation race on EX and verify that the initial 50 uploaded keys are not treated as consumed or lead to uploading 100 OTKs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- cryptography, distributed-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100