Skip to content

Commit 05f22af

Browse files
committed
Merge branch 'main' of github.com:bitwarden/sdk-internal into arch/bw-uuid
# Conflicts: # crates/bitwarden-uniffi/src/vault/ciphers.rs # crates/bitwarden-wasm-internal/src/vault/ciphers.rs
2 parents 4cccada + 1a31a1f commit 05f22af

File tree

36 files changed

+646
-22
lines changed

36 files changed

+646
-22
lines changed

.cargo/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ rustflags = ["--cfg", "aes_armv8"]
66

77
[target.wasm32-unknown-unknown]
88
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']
9+
runner = 'wasm-bindgen-test-runner'
910

1011
# Enable support for 16k pages on Android, JNA is using these same flags
1112
# https://android-developers.googleblog.com/2024/08/adding-16-kb-page-size-to-android.html

.github/workflows/build-wasm-internal.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ jobs:
6060
RUST_TOOLCHAIN="$(grep -oP '^channel.*"(\K.*?)(?=")' ../../rust-toolchain.toml)"
6161
echo "RUST_TOOLCHAIN=${RUST_TOOLCHAIN}" | tee -a "${GITHUB_OUTPUT}"
6262
63+
- name: NPM setup
64+
run: npm ci
65+
6366
- name: Install rust
6467
uses: dtolnay/rust-toolchain@56f84321dbccf38fb67ce29ab63e4754056677e0 # stable
6568
with:

.github/workflows/rust-test.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,37 @@ jobs:
4545
- name: Test
4646
run: cargo test --workspace --all-features
4747

48+
test-wasm:
49+
name: WASM
50+
runs-on: ubuntu-24.04
51+
52+
steps:
53+
- name: Checkout
54+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
55+
56+
- name: Set Rust Toolchain
57+
id: toolchain
58+
shell: bash
59+
run: |
60+
RUST_TOOLCHAIN="$(grep -oP '^channel.*"(\K.*?)(?=")' rust-toolchain.toml)"
61+
echo "RUST_TOOLCHAIN=${RUST_TOOLCHAIN}" | tee -a "${GITHUB_OUTPUT}"
62+
63+
- name: Install rust
64+
uses: dtolnay/rust-toolchain@56f84321dbccf38fb67ce29ab63e4754056677e0 # stable
65+
with:
66+
toolchain: "${{ steps.toolchain.outputs.RUST_TOOLCHAIN }}"
67+
targets: wasm32-unknown-unknown
68+
components: rust-src
69+
70+
- name: Cache cargo registry
71+
uses: Swatinem/rust-cache@f0deed1e0edfc6a9be95417288c0e1099b1eeec3 # v2.7.7
72+
73+
- name: Install wasm-bindgen-cli
74+
run: cargo install wasm-bindgen-cli --version 0.2.100
75+
76+
- name: Test WASM
77+
run: cargo test --target wasm32-unknown-unknown -p bitwarden-wasm-internal -p bitwarden-threading -p bitwarden-error --all-features
78+
4879
coverage:
4980
name: Coverage
5081
runs-on: ubuntu-24.04

Cargo.lock

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ bitwarden-fido = { path = "crates/bitwarden-fido", version = "=1.0.0" }
3131
bitwarden-generators = { path = "crates/bitwarden-generators", version = "=1.0.0" }
3232
bitwarden-ipc = { path = "crates/bitwarden-ipc", version = "=1.0.0" }
3333
bitwarden-send = { path = "crates/bitwarden-send", version = "=1.0.0" }
34+
bitwarden-threading = { path = "crates/bitwarden-threading", version = "=1.0.0" }
3435
bitwarden-sm = { path = "bitwarden_license/bitwarden-sm", version = "=1.0.0" }
3536
bitwarden-ssh = { path = "crates/bitwarden-ssh", version = "=1.0.0" }
3637
bitwarden-uuid = { path = "crates/bitwarden-uuid", version = "=1.0.0" }
@@ -68,6 +69,7 @@ uuid = { version = ">=1.3.3, <2.0", features = ["serde", "v4", "js"] }
6869
validator = { version = ">=0.18.1, <0.20", features = ["derive"] }
6970
wasm-bindgen = { version = ">=0.2.91, <0.3", features = ["serde-serialize"] }
7071
wasm-bindgen-futures = "0.4.41"
72+
wasm-bindgen-test = "0.3.45"
7173

7274
# There is an incompatibility when using pkcs5 and chacha20 on wasm builds. This can be removed once a new
7375
# rustcrypto-formats crate version is released since the fix has been upstreamed.

crates/bitwarden-core/src/auth/auth_request.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ mod tests {
243243
new_device
244244
.crypto()
245245
.initialize_user_crypto(InitUserCryptoRequest {
246+
user_id: Some(uuid::Uuid::new_v4()),
246247
kdf_params: kdf,
247248
email: email.to_owned(),
248249
private_key: private_key.to_owned(),

crates/bitwarden-core/src/auth/login/auth_request.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub(crate) async fn complete_auth_request(
115115
client
116116
.crypto()
117117
.initialize_user_crypto(InitUserCryptoRequest {
118+
user_id: None,
118119
kdf_params: kdf,
119120
email: auth_req.email,
120121
private_key: require!(r.private_key),

crates/bitwarden-core/src/client/client.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::{Arc, RwLock};
1+
use std::sync::{Arc, OnceLock, RwLock};
22

33
use bitwarden_crypto::KeyStore;
44
use reqwest::header::{self, HeaderValue};
@@ -75,6 +75,7 @@ impl Client {
7575

7676
Self {
7777
internal: Arc::new(InternalClient {
78+
user_id: OnceLock::new(),
7879
tokens: RwLock::new(Tokens::default()),
7980
login_method: RwLock::new(None),
8081
#[cfg(feature = "internal")]

crates/bitwarden-core/src/client/encryption_settings.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use thiserror::Error;
66
use uuid::Uuid;
77

88
use crate::{
9+
error::UserIdAlreadySetError,
910
key_management::{AsymmetricKeyId, KeyIds, SymmetricKeyId},
1011
MissingPrivateKeyError, VaultLockedError,
1112
};
@@ -27,6 +28,9 @@ pub enum EncryptionSettingsError {
2728

2829
#[error(transparent)]
2930
MissingPrivateKey(#[from] MissingPrivateKeyError),
31+
32+
#[error(transparent)]
33+
UserIdAlreadySetError(#[from] UserIdAlreadySetError),
3034
}
3135

3236
pub struct EncryptionSettings {}

crates/bitwarden-core/src/client/internal.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::sync::{Arc, RwLock};
1+
use std::sync::{Arc, OnceLock, RwLock};
22

33
use bitwarden_crypto::KeyStore;
44
#[cfg(any(feature = "internal", feature = "secrets"))]
@@ -13,6 +13,7 @@ use super::login_method::ServiceAccountLoginMethod;
1313
use crate::{
1414
auth::renew::renew_token,
1515
client::{encryption_settings::EncryptionSettings, login_method::LoginMethod},
16+
error::UserIdAlreadySetError,
1617
key_management::KeyIds,
1718
DeviceType,
1819
};
@@ -44,6 +45,7 @@ pub(crate) struct Tokens {
4445

4546
#[derive(Debug)]
4647
pub struct InternalClient {
48+
pub(crate) user_id: OnceLock<Uuid>,
4749
pub(crate) tokens: RwLock<Tokens>,
4850
pub(crate) login_method: RwLock<Option<Arc<LoginMethod>>>,
4951

@@ -172,6 +174,14 @@ impl InternalClient {
172174
&self.key_store
173175
}
174176

177+
pub fn init_user_id(&self, user_id: Uuid) -> Result<(), UserIdAlreadySetError> {
178+
self.user_id.set(user_id).map_err(|_| UserIdAlreadySetError)
179+
}
180+
181+
pub fn get_user_id(&self) -> Option<Uuid> {
182+
self.user_id.get().copied()
183+
}
184+
175185
#[cfg(feature = "internal")]
176186
pub(crate) fn initialize_user_crypto_master_key(
177187
&self,

crates/bitwarden-core/src/client/test_accounts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ pub struct TestAccount {
117117
pub fn test_bitwarden_com_account() -> TestAccount {
118118
TestAccount {
119119
user: InitUserCryptoRequest {
120+
user_id: Some(uuid::uuid!("060000fb-0922-4dd3-b170-6e15cb5df8c8")),
120121
kdf_params: Kdf::PBKDF2 {
121122
iterations: 600_000.try_into().unwrap(),
122123
},
@@ -174,6 +175,7 @@ pub fn test_bitwarden_com_account() -> TestAccount {
174175
pub fn test_legacy_user_key_account() -> TestAccount {
175176
TestAccount {
176177
user: InitUserCryptoRequest {
178+
user_id: Some(uuid::uuid!("060000fb-0922-4dd3-b170-6e15cb5df8c8")),
177179
kdf_params: Kdf::PBKDF2 {
178180
iterations: 600_000.try_into().unwrap(),
179181
},

crates/bitwarden-core/src/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ impl_bitwarden_error!(IdentityError, ApiError);
4848
#[error("The client is not authenticated or the session has expired")]
4949
pub struct NotAuthenticatedError;
5050

51+
/// Client's user ID is already set.
52+
#[derive(Debug, Error)]
53+
#[error("The client user ID is already set")]
54+
pub struct UserIdAlreadySetError;
55+
5156
/// Missing required field.
5257
#[derive(Debug, Error)]
5358
#[error("The response received was missing a required field: {0}")]

crates/bitwarden-core/src/mobile/crypto.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub enum MobileCryptoError {
3939
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
4040
#[cfg_attr(feature = "wasm", derive(Tsify), tsify(into_wasm_abi, from_wasm_abi))]
4141
pub struct InitUserCryptoRequest {
42+
pub user_id: Option<uuid::Uuid>,
4243
/// The user's KDF parameters, as received from the prelogin request
4344
pub kdf_params: Kdf,
4445
/// The user's email address
@@ -131,6 +132,10 @@ pub async fn initialize_user_crypto(
131132

132133
let private_key: EncString = req.private_key.parse()?;
133134

135+
if let Some(user_id) = req.user_id {
136+
client.internal.init_user_id(user_id)?;
137+
}
138+
134139
match req.method {
135140
InitUserCryptoMethod::Password { password, user_key } => {
136141
let user_key: EncString = user_key.parse()?;
@@ -564,6 +569,7 @@ mod tests {
564569
initialize_user_crypto(
565570
& client,
566571
InitUserCryptoRequest {
572+
user_id: Some(uuid::Uuid::new_v4()),
567573
kdf_params: kdf.clone(),
568574
email: "[email protected]".into(),
569575
private_key: priv_key.to_owned(),
@@ -583,6 +589,7 @@ mod tests {
583589
initialize_user_crypto(
584590
&client2,
585591
InitUserCryptoRequest {
592+
user_id: Some(uuid::Uuid::new_v4()),
586593
kdf_params: kdf.clone(),
587594
email: "[email protected]".into(),
588595
private_key: priv_key.to_owned(),
@@ -638,6 +645,7 @@ mod tests {
638645
initialize_user_crypto(
639646
& client,
640647
InitUserCryptoRequest {
648+
user_id: Some(uuid::Uuid::new_v4()),
641649
kdf_params: Kdf::PBKDF2 {
642650
iterations: 100_000.try_into().unwrap(),
643651
},
@@ -659,6 +667,7 @@ mod tests {
659667
initialize_user_crypto(
660668
&client2,
661669
InitUserCryptoRequest {
670+
user_id: Some(uuid::Uuid::new_v4()),
662671
kdf_params: Kdf::PBKDF2 {
663672
iterations: 100_000.try_into().unwrap(),
664673
},
@@ -701,6 +710,7 @@ mod tests {
701710
initialize_user_crypto(
702711
&client3,
703712
InitUserCryptoRequest {
713+
user_id: Some(uuid::Uuid::new_v4()),
704714
kdf_params: Kdf::PBKDF2 {
705715
iterations: 100_000.try_into().unwrap(),
706716
},

crates/bitwarden-core/tests/register.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ async fn test_register_initialize_crypto() {
2929
client
3030
.crypto()
3131
.initialize_user_crypto(InitUserCryptoRequest {
32+
user_id: Some(uuid::Uuid::new_v4()),
3233
kdf_params: kdf,
3334
email: email.to_owned(),
3435
private_key: register_response.keys.private.to_string(),

crates/bitwarden-error/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ workspace = true
3434
[dev-dependencies]
3535
serde.workspace = true
3636
trybuild = "1.0.101"
37-
wasm-bindgen-test = "0.3.45"
37+
wasm-bindgen-test = { workspace = true }

crates/bitwarden-fido/src/authenticator.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Mutex;
22

33
use bitwarden_core::{Client, VaultLockedError};
44
use bitwarden_crypto::CryptoError;
5-
use bitwarden_vault::{CipherError, CipherView};
5+
use bitwarden_vault::{CipherError, CipherView, EncryptionContext};
66
use itertools::Itertools;
77
use log::error;
88
use passkey::{
@@ -431,6 +431,8 @@ impl passkey::authenticator::CredentialStore for CredentialStoreImpl<'_> {
431431
) -> Result<(), StatusCode> {
432432
#[derive(Debug, Error)]
433433
enum InnerError {
434+
#[error("Client User Id has not been set")]
435+
MissingUserId,
434436
#[error(transparent)]
435437
VaultLocked(#[from] VaultLockedError),
436438
#[error(transparent)]
@@ -454,6 +456,12 @@ impl passkey::authenticator::CredentialStore for CredentialStoreImpl<'_> {
454456
rp: passkey::types::ctap2::make_credential::PublicKeyCredentialRpEntity,
455457
options: passkey::types::ctap2::get_assertion::Options,
456458
) -> Result<(), InnerError> {
459+
let user_id = this
460+
.authenticator
461+
.client
462+
.internal
463+
.get_user_id()
464+
.ok_or(InnerError::MissingUserId)?;
457465
let cred = try_from_credential_full(cred, user, rp, options)?;
458466

459467
// Get the previously selected cipher and add the new credential to it
@@ -481,7 +489,10 @@ impl passkey::authenticator::CredentialStore for CredentialStoreImpl<'_> {
481489

482490
this.authenticator
483491
.credential_store
484-
.save_credential(encrypted)
492+
.save_credential(EncryptionContext {
493+
cipher: encrypted,
494+
encrypted_for: user_id,
495+
})
485496
.await?;
486497

487498
Ok(())
@@ -498,6 +509,8 @@ impl passkey::authenticator::CredentialStore for CredentialStoreImpl<'_> {
498509
async fn update_credential(&mut self, cred: Passkey) -> Result<(), StatusCode> {
499510
#[derive(Debug, Error)]
500511
enum InnerError {
512+
#[error("Client User Id has not been set")]
513+
MissingUserId,
501514
#[error(transparent)]
502515
VaultLocked(#[from] VaultLockedError),
503516
#[error(transparent)]
@@ -521,6 +534,12 @@ impl passkey::authenticator::CredentialStore for CredentialStoreImpl<'_> {
521534
this: &mut CredentialStoreImpl<'_>,
522535
cred: Passkey,
523536
) -> Result<(), InnerError> {
537+
let user_id = this
538+
.authenticator
539+
.client
540+
.internal
541+
.get_user_id()
542+
.ok_or(InnerError::MissingUserId)?;
524543
// Get the previously selected cipher and update the credential
525544
let selected = this.authenticator.get_selected_credential()?;
526545

@@ -550,7 +569,10 @@ impl passkey::authenticator::CredentialStore for CredentialStoreImpl<'_> {
550569

551570
this.authenticator
552571
.credential_store
553-
.save_credential(encrypted)
572+
.save_credential(EncryptionContext {
573+
cipher: encrypted,
574+
encrypted_for: user_id,
575+
})
554576
.await?;
555577

556578
Ok(())

crates/bitwarden-fido/src/traits.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bitwarden_vault::{Cipher, CipherListView, CipherView, Fido2CredentialNewView};
1+
use bitwarden_vault::{CipherListView, CipherView, EncryptionContext, Fido2CredentialNewView};
22
use passkey::authenticator::UIHint;
33
use thiserror::Error;
44

@@ -43,7 +43,7 @@ pub trait Fido2CredentialStore: Send + Sync {
4343

4444
async fn all_credentials(&self) -> Result<Vec<CipherListView>, Fido2CallbackError>;
4545

46-
async fn save_credential(&self, cred: Cipher) -> Result<(), Fido2CallbackError>;
46+
async fn save_credential(&self, cred: EncryptionContext) -> Result<(), Fido2CallbackError>;
4747
}
4848

4949
#[derive(Clone)]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[target.wasm32-unknown-unknown]
2+
runner = 'wasm-bindgen-test-runner'

0 commit comments

Comments
 (0)