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

WebR Expose matrix-sdk-crypto-js bindings for KeyBackup #2196

Closed
wants to merge 9 commits into from

Conversation

BillCarsonFr
Copy link
Member

Expose matrix-sdk-crypto-js bindings for:

  • BackupMachine, including BackupMachine::backup_key::version, which is not currently exposed in the underlying crate
  • RoomKeyBackupInfo (? actually perhaps just pass the raw json into verify_backup).
  • MegolmV1BackupKey (this is the public part of the key)
  • RecoveryKey (this is the private part of the key)
  • SignatureVerification
  • Signatures::serialize (for signing the backup)

Fixes: https://github.com/vector-im/crypto-internal/issues/100

  • Public API changes documented in changelogs (optional)

Signed-off-by:

@BillCarsonFr BillCarsonFr marked this pull request as ready for review July 1, 2023 11:33
@BillCarsonFr BillCarsonFr requested a review from a team as a code owner July 1, 2023 11:33
@BillCarsonFr BillCarsonFr requested review from poljar and removed request for a team July 1, 2023 11:33
@codecov
Copy link

codecov bot commented Jul 4, 2023

Codecov Report

Patch coverage: 15.38% and project coverage change: -0.05 ⚠️

Comparison is base (7bcc886) 76.71% compared to head (d6b30ae) 76.67%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2196      +/-   ##
==========================================
- Coverage   76.71%   76.67%   -0.05%     
==========================================
  Files         164      164              
  Lines       17646    17658      +12     
==========================================
+ Hits        13538    13540       +2     
- Misses       4108     4118      +10     
Impacted Files Coverage Δ
crates/matrix-sdk-crypto/src/store/memorystore.rs 77.12% <15.38%> (-5.15%) ⬇️

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

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

There are a bunch of unwraps that we need to get rid of and a couple of more nits. Looks promising though, thanks.

.map(|(k, v)| (k.to_string(), v.into_iter().map(|(k, v)| (k.to_string(), v)).collect()))
.collect();

serde_wasm_bindgen::to_value(&signatures).unwrap()
Copy link
Contributor

Choose a reason for hiding this comment

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

Left an unwrap here.

Copy link
Member

Choose a reason for hiding this comment

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

I'm far from convinced that we need this method anyway. What is the application supposed to do with a list of signatures once it has one? It should be up to the rust side to validate the signatures.

Will remove this for now.

self.inner
.decrypt_v1(&ephemeral_key, &mac, &ciphertext)
.map_err(|e| e.into())
.map(|r| JSON::parse(&r).unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

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

Another one. Hmm, this now assumes that whatever you are decrypting is JSON, I guess that's currently true but worries me slightly.

Copy link
Member

Choose a reason for hiding this comment

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

let's just return the JSON. The javascript side can deserialise it more easily anyway.

recovery_key: inner.recovery_key.map(|k| k.to_base58()),
backup_version: inner.backup_version,
};
Ok(serde_wasm_bindgen::to_value(&backup_keys).unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

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

More unwraps.

pub fn as_json(&self) -> JsValue {
trace!(?self.inner, "The signature");
// can't use directly serde_wasm_bindgen as there is an issue with BTreeMap
// It's always returning an empty {} if I do:
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, but you collect into a BTreeMap a couple of lines bellow as well, in fact the collect is almost the same thing the Serialize implementation does for the Signatures struct.

If serde_wasm_bindgen didn't work, did serde_json::to_string() work correctly? I don't see how that would be different to what you're doing here.

// can't clone or copy it, so..
let key = export.recovery_key.as_ref().map(|e| serde_json::to_string(e).unwrap());
BackupKeys {
recovery_key: key.map(|s| serde_json::from_str(&s).unwrap()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Heh, I had to add this in #2227 as well. Though I added just a clone to the backup key.

So let's do that instead.


/// Create a new [`BackupRecoveryKey`] from the given passphrase.
#[wasm_bindgen(js_name = "newFromPassphrase")]
pub fn new_from_passphrase(passphrase: String) -> BackupRecoveryKey {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we certain that EW needs this? If so perhaps we should give in and put it into the crypto crate instead of having it twice in the bindings.

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't see it in web code, we can probably reove

@jplatte
Copy link
Collaborator

jplatte commented Jul 13, 2023

The bindings have moved to a separate repo, you'll have to re-open the PR there: https://github.com/matrix-org/matrix-rust-sdk-crypto-web. Sorry for the inconvenience.

@jplatte jplatte closed this Jul 13, 2023
@richvdh richvdh changed the title WebR Expose matrix-sdk-crypto-js bindings for KeyBackup#100 WebR Expose matrix-sdk-crypto-js bindings for KeyBackup Jul 19, 2023
@@ -763,6 +767,155 @@ impl OlmMachine {
}))
}

/// Store the recovery key in the crypto store.
Copy link
Member

Choose a reason for hiding this comment

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

IMHO we should not use the term "recovery key" here: matrix-org/matrix-spec#1571


let raw_string = serde_json::to_string(&map).unwrap();

JSON::parse(&raw_string).unwrap()
Copy link
Member

Choose a reason for hiding this comment

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

why is it called asJSON if it's not returning JSON?

Comment on lines +95 to +129
/// Get the json with all signatures
#[wasm_bindgen(js_name = "asJSON")]
pub fn as_json(&self) -> JsValue {
trace!(?self.inner, "The signature");
// can't use directly serde_wasm_bindgen as there is an issue with BTreeMap
// It's always returning an empty {} if I do:
// serde_wasm_bindgen::to_value(&self.inner).unwrap()

// Keep it like that for now as it's working
let map: BTreeMap<String, BTreeMap<String, String>> = self
.inner
.clone()
.into_iter()
.map(|(u, sign)| {
(
u.as_str().to_owned(),
sign.iter()
.map(|(device, maybe_sign)| {
(
device.as_str().to_owned(),
match maybe_sign {
Ok(s) => s.to_base64(),
Err(e) => e.source.to_owned(),
},
)
})
.collect(),
)
})
.collect();

let raw_string = serde_json::to_string(&map).unwrap();

JSON::parse(&raw_string).unwrap()
}
Copy link
Member

Choose a reason for hiding this comment

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

Again, I'm not really sure why we need this. What is the JS side supposed to do with it?

Copy link
Member

Choose a reason for hiding this comment

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

Answer: we have to assemble the POST /_matrix/client/v3/room_keys/version request on the application side, and we need the signature data for that.

richvdh added a commit to matrix-org/matrix-rust-sdk-crypto-wasm that referenced this pull request Jul 20, 2023
Import Valere's changes from
matrix-org/matrix-rust-sdk#2196, excluding the fixes to
MemoryStore.
@poljar poljar deleted the valere/js_backup_bindings branch November 5, 2024 11:25
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.

4 participants