-
Notifications
You must be signed in to change notification settings - Fork 276
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
feat(bindings/crypto-nodejs): Implement an Attachment
API.
#818
Conversation
This patch provides a new API to encrypt and decrypt attachment, i.e. big buffer of type `Uint8Array`. It's based on `matrix_sdk_crypto::AttachmentEncryptor` and `AttachmentDecryptor`.
Codecov Report
@@ Coverage Diff @@
## main #818 +/- ##
=======================================
Coverage 77.92% 77.92%
=======================================
Files 92 92
Lines 13919 13930 +11
=======================================
+ Hits 10846 10855 +9
- Misses 3073 3075 +2
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise this works - just a small property name issue
…ning `Uint8Array`. The new `napi-rs` release includes a patch that avoids cloning and copying data inside a `Uint8Array` (napi-rs/napi-rs#1224), it now returns a “Node.js reference” of it. This new `napi-rs` release also includes one of our patch, napi-rs/napi-rs#1200, which means we no longer need to depend on our fork.
Based on the [Section 11.11.1.6.1 Extensions to `m.room.message` msgtypes](https://spec.matrix.org/v1.2/client-server-api/#extensions-to-mroommessage-msgtypes), the parameter for the JSON Web Key is named `key`, not `web_key`. This patch fixes that by renaming the field when serializing and deserializing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks mostly good, the Clone
is a bit of a shame if it can't be avoided.
…nInfo`. We don't want to clone a struct that contains a secret. However, on the Node.js side, we can only receive arguments by references. The problem we have is that we cannot transfer the ownership of `MediaEncryptionInfo` to `AttachmentDecryptor` because we don't own it. To simulate this behavior, we use `Option.take`. A new method then appears: `EncryptedAttachment.hasMediaEncryptionInfoBeenConsumed` to know if the media encryption info has been consumed by `Attachment.decrypt` already or not. That way, we can decrypt only once. It is possible to do a JSON-encoded backup of the media encryption info by calling `EncryptedAttachment.mediaEncryptionInfo` though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it works
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still think that we should let the decryptor take a borrow of the info, but I'll deal with this later on.
Adding proper zeroization for this is a bit trickier than expected.
Looks good, thanks.
This patch fixes #796.
This patch provides a new API to encrypt and decrypt attachment, i.e. big buffer of type
Uint8Array
.This patch also fixes a bug in
matrix_sdk_crypto::MediaEncryptedInfo
where itsweb_key
field should serialize to and deserialize fromkey
to respect the specification.It's based on
matrix_sdk_crypto::AttachmentEncryptor
andAttachmentDecryptor
.Note: I'm really not happy with the factThis is now fixed. Data are no longer copied!EncryptedAttachment.encryptedData
returns a copy. But we cannot return a&T
withnapi-rs
. I tried to create a new buffer that references data from elsewhere, like withUint8Array::with_external_data
, but the data are collected by the GC (which notifies us before dropping the data); that's not what we want. I then tried withReference
orRef
, butnapi-rs
doesn't allow to return them from a function, so it's useless. I'll investigate that. In the meantime, as mentionned in #796, right now, there is a copy in the previousmatrix-sdk-crypto-nodejs
API. This patch is actually better as it doesn't need to serialize and deserialize the entire data, it just computes a newUint8Array
, which is an improvement.