Skip to content

Commit 82f69c3

Browse files
authored
Accept AttachmentView in AttachmentFile (#255)
## 🎟️ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> ## 📔 Objective Previously, decrypting attachment content in clients required fetching the cipher, filtering to find the relevant attachment, and then passing the full encrypted attachment to the `decrypt_buffer` function, even when the decrypted `AttachmentView` was already available. This change reafctors `AttachmentFile` to uses the `AttachmentView` directly. <!-- Describe what the purpose of this PR is, for example what bug you're fixing or new feature you're adding. --> ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes
1 parent 60ce7ac commit 82f69c3

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

crates/bitwarden-uniffi/src/vault/attachments.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl AttachmentsClient {
4444
pub fn decrypt_buffer(
4545
&self,
4646
cipher: Cipher,
47-
attachment: Attachment,
47+
attachment: AttachmentView,
4848
buffer: Vec<u8>,
4949
) -> Result<Vec<u8>> {
5050
Ok(self
@@ -57,7 +57,7 @@ impl AttachmentsClient {
5757
pub fn decrypt_file(
5858
&self,
5959
cipher: Cipher,
60-
attachment: Attachment,
60+
attachment: AttachmentView,
6161
encrypted_file_path: String,
6262
decrypted_file_path: String,
6363
) -> Result<()> {

crates/bitwarden-vault/src/cipher/attachment.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub struct AttachmentEncryptResult {
4646

4747
pub struct AttachmentFile {
4848
pub cipher: Cipher,
49-
pub attachment: Attachment,
49+
pub attachment: AttachmentView,
5050

5151
/// There are three different ways attachments are encrypted.
5252
/// 1. UserKey / OrgKey (Contents) - Legacy
@@ -195,7 +195,7 @@ mod tests {
195195

196196
use crate::{
197197
cipher::cipher::{CipherRepromptType, CipherType},
198-
Attachment, AttachmentFile, AttachmentFileView, AttachmentView, Cipher,
198+
AttachmentFile, AttachmentFileView, AttachmentView, Cipher,
199199
};
200200

201201
#[test]
@@ -271,13 +271,13 @@ mod tests {
271271
let user_key: SymmetricCryptoKey = "w2LO+nwV4oxwswVYCxlOfRUseXfvU03VzvKQHrqeklPgiMZrspUe6sOBToCnDn9Ay0tuCBn8ykVVRb7PWhub2Q==".to_string().try_into().unwrap();
272272
let key_store = create_test_crypto_with_user_key(user_key);
273273

274-
let attachment = Attachment {
274+
let attachment = AttachmentView {
275275
id: None,
276276
url: None,
277277
size: Some("161".into()),
278278
size_name: Some("161 Bytes".into()),
279-
file_name: Some("2.M3z1MOO9eBG9BWRTEUbPog==|jPw0By1AakHDfoaY8UOwOQ==|eP9/J1583OJpHsSM4ZnXZzdBHfqVTXnOXGlkkmAKSfA=".parse().unwrap()),
280-
key: Some("2.r288/AOSPiaLFkW07EBGBw==|SAmnnCbOLFjX5lnURvoualOetQwuyPc54PAmHDTRrhT0gwO9ailna9U09q9bmBfI5XrjNNEsuXssgzNygRkezoVQvZQggZddOwHB6KQW5EQ=|erIMUJp8j+aTcmhdE50zEX+ipv/eR1sZ7EwULJm/6DY=".parse().unwrap())
279+
file_name: Some("Test.txt".into()),
280+
key: Some("2.r288/AOSPiaLFkW07EBGBw==|SAmnnCbOLFjX5lnURvoualOetQwuyPc54PAmHDTRrhT0gwO9ailna9U09q9bmBfI5XrjNNEsuXssgzNygRkezoVQvZQggZddOwHB6KQW5EQ=|erIMUJp8j+aTcmhdE50zEX+ipv/eR1sZ7EwULJm/6DY=".parse().unwrap()),
281281
};
282282

283283
let cipher = Cipher {
@@ -328,12 +328,12 @@ mod tests {
328328
let user_key: SymmetricCryptoKey = "w2LO+nwV4oxwswVYCxlOfRUseXfvU03VzvKQHrqeklPgiMZrspUe6sOBToCnDn9Ay0tuCBn8ykVVRb7PWhub2Q==".to_string().try_into().unwrap();
329329
let key_store = create_test_crypto_with_user_key(user_key);
330330

331-
let attachment = Attachment {
331+
let attachment = AttachmentView {
332332
id: None,
333333
url: None,
334334
size: Some("161".into()),
335335
size_name: Some("161 Bytes".into()),
336-
file_name: Some("2.qTJdrgQe+tLCHTlPHMJXLw==|0we9+uYJPEY3FU5SblX2hg==|+fL/wTF/WgpoV+BNzmsmi284O0QNdVBUYmfqqs0CG1E=".parse().unwrap()),
336+
file_name: Some("Test.txt".into()),
337337
key: None,
338338
};
339339

crates/bitwarden-vault/src/cipher/attachment_client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl AttachmentsClient {
6868
pub fn decrypt_buffer(
6969
&self,
7070
cipher: Cipher,
71-
attachment: Attachment,
71+
attachment: AttachmentView,
7272
encrypted_buffer: &[u8],
7373
) -> Result<Vec<u8>, DecryptError> {
7474
let key_store = self.client.internal.get_key_store();
@@ -82,7 +82,7 @@ impl AttachmentsClient {
8282
pub fn decrypt_file(
8383
&self,
8484
cipher: Cipher,
85-
attachment: Attachment,
85+
attachment: AttachmentView,
8686
encrypted_file_path: &Path,
8787
decrypted_file_path: &Path,
8888
) -> Result<(), DecryptFileError> {

crates/bitwarden-vault/src/cipher/cipher_client.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ mod tests {
233233
let attachment_view = attachments.first().unwrap().clone();
234234
assert!(attachment_view.key.is_none());
235235

236-
assert_eq!(attachment_view.file_name.unwrap(), "h.txt");
236+
assert_eq!(attachment_view.file_name.as_deref(), Some("h.txt"));
237237

238238
let buf = vec![
239239
2, 100, 205, 148, 152, 77, 184, 77, 53, 80, 38, 240, 83, 217, 251, 118, 254, 27, 117,
@@ -245,7 +245,7 @@ mod tests {
245245
let content = client
246246
.vault()
247247
.attachments()
248-
.decrypt_buffer(cipher, attachment, buf.as_slice())
248+
.decrypt_buffer(cipher, attachment_view.clone(), buf.as_slice())
249249
.unwrap();
250250

251251
assert_eq!(content, b"Hello");
@@ -267,7 +267,11 @@ mod tests {
267267
let new_cipher = client.vault().ciphers().encrypt(view).unwrap();
268268
assert!(new_cipher.key.is_some());
269269

270-
let view = client.vault().ciphers().decrypt(new_cipher).unwrap();
270+
let view = client
271+
.vault()
272+
.ciphers()
273+
.decrypt(new_cipher.clone())
274+
.unwrap();
271275
let attachments = view.clone().attachments.unwrap();
272276
let attachment_view = attachments.first().unwrap().clone();
273277
assert!(attachment_view.key.is_some());
@@ -278,7 +282,7 @@ mod tests {
278282
attachment_view.clone().key.unwrap().to_string()
279283
);
280284

281-
assert_eq!(attachment_view.file_name.unwrap(), "h.txt");
285+
assert_eq!(attachment_view.file_name.as_deref(), Some("h.txt"));
282286

283287
let buf = vec![
284288
2, 114, 53, 72, 20, 82, 18, 46, 48, 137, 97, 1, 100, 142, 120, 187, 28, 36, 180, 46,
@@ -290,7 +294,7 @@ mod tests {
290294
let content = client
291295
.vault()
292296
.attachments()
293-
.decrypt_buffer(cipher, attachment, buf.as_slice())
297+
.decrypt_buffer(new_cipher.clone(), attachment_view.clone(), buf.as_slice())
294298
.unwrap();
295299

296300
assert_eq!(content, b"Hello");
@@ -316,14 +320,14 @@ mod tests {
316320

317321
// Ensure attachment key is still the same since it's protected by the cipher key
318322
assert_eq!(
319-
attachment.clone().key.unwrap().to_string(),
320-
attachment_view.key.unwrap().to_string()
323+
attachment.clone().key.as_ref().unwrap().to_string(),
324+
attachment_view.key.as_ref().unwrap().to_string()
321325
);
322326

323327
let content = client
324328
.vault()
325329
.attachments()
326-
.decrypt_buffer(new_cipher, attachment, buf.as_slice())
330+
.decrypt_buffer(new_cipher, attachment_view, buf.as_slice())
327331
.unwrap();
328332

329333
assert_eq!(content, b"Hello");

crates/bitwarden-wasm-internal/src/vault/attachments.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bitwarden_vault::{Attachment, Cipher, DecryptError};
1+
use bitwarden_vault::{AttachmentView, Cipher, DecryptError};
22
use wasm_bindgen::prelude::wasm_bindgen;
33

44
#[wasm_bindgen]
@@ -16,7 +16,7 @@ impl AttachmentsClient {
1616
pub fn decrypt_buffer(
1717
&self,
1818
cipher: Cipher,
19-
attachment: Attachment,
19+
attachment: AttachmentView,
2020
encrypted_buffer: &[u8],
2121
) -> Result<Vec<u8>, DecryptError> {
2222
self.0.decrypt_buffer(cipher, attachment, encrypted_buffer)

0 commit comments

Comments
 (0)