Skip to content

Add-userid-to-encryption-methods #278

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

Merged
merged 11 commits into from
May 22, 2025
Merged

Conversation

MGibson1
Copy link
Member

This is not the owning entity of the key, but the user that is unlocked that triggered the request

🎟️ Tracking

📔 Objective

⏰ 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

  • 👍 (:+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

MGibson1 added 2 commits May 19, 2025 08:28
The API changes would require an dwell time for the server to
populate the userId field in Identity success responses. However, these
API endpoints are not yet used by the SDK.
This is not the owning entity of the key, but the user that is unlocked that triggered the request
Copy link
Contributor

github-actions bot commented May 19, 2025

Logo
Checkmarx One – Scan Summary & Details5d96e661-6cb3-4271-b41d-2dcc50d3f948

Great job, no security vulnerabilities found in this Pull Request

Copy link

codecov bot commented May 19, 2025

Codecov Report

Attention: Patch coverage is 59.37500% with 26 lines in your changes missing coverage. Please review.

Project coverage is 69.93%. Comparing base (af7ae90) to head (468bcda).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
crates/bitwarden-fido/src/authenticator.rs 0.00% 20 Missing ⚠️
...ates/bitwarden-core/src/auth/login/auth_request.rs 0.00% 1 Missing ⚠️
crates/bitwarden-core/src/client/test_accounts.rs 50.00% 1 Missing ⚠️
crates/bitwarden-core/src/mobile/crypto.rs 87.50% 1 Missing ⚠️
crates/bitwarden-uniffi/src/platform/fido2.rs 0.00% 1 Missing ⚠️
crates/bitwarden-uniffi/src/vault/ciphers.rs 0.00% 1 Missing ⚠️
...rates/bitwarden-wasm-internal/src/vault/ciphers.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #278      +/-   ##
==========================================
+ Coverage   69.83%   69.93%   +0.10%     
==========================================
  Files         213      214       +1     
  Lines       16809    16989     +180     
==========================================
+ Hits        11738    11882     +144     
- Misses       5071     5107      +36     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

MGibson1 added 3 commits May 19, 2025 08:48
UserId must either be optional or pulled from the auth token. Since the auth token is meant to be opaque to the client, I'm making user id optional. Cipher decryption will fail if it is not included, though.
@MGibson1 MGibson1 force-pushed the add-userid-to-encryption-methods branch from 49dcf86 to 2f18679 Compare May 19, 2025 22:17
@MGibson1 MGibson1 force-pushed the add-userid-to-encryption-methods branch 3 times, most recently from 108a702 to 276a6cc Compare May 20, 2025 18:38
@MGibson1 MGibson1 force-pushed the add-userid-to-encryption-methods branch from 276a6cc to edf8961 Compare May 20, 2025 19:04
@MGibson1 MGibson1 marked this pull request as ready for review May 20, 2025 19:13
@MGibson1 MGibson1 requested a review from a team as a code owner May 20, 2025 19:13
Comment on lines 176 to 179
pub fn set_user_id(&self, user_id: Uuid) {
*self.user_id.write().expect("RwLock is not poisoned") = Some(user_id);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

issue?: it seems odd to have a public function that changes the user_id without any additional context required. I feel like the user_id should only ever change when the client is initialized and possibly never again after that. Maybe there's some edge case where we'd want a client without Crypto but tied to a user? If that is the case then I think this function should instead be called something like init_user_id() and only allow itself to be called once. However, it's probably more likely that we'll split up the client into user and non-user scoped clients and then just require the user_id on creation.

Either way, I think we should remove the ability to change the user_id. My opinion is that it should be bundled into the initialize_user_crypto functions below, but I'm not married to that solution

Copy link
Member

@dani-garcia dani-garcia May 21, 2025

Choose a reason for hiding this comment

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

Yeah I agree, separate user-bound and non-user bounds clients would allow us to require user id during construction and enforce no changes to it, until we have those we can use a OnceLock to ensure the ID is only initialized once:

    pub(crate) user_id: OnceLock<Uuid>,

    pub fn init_user_id(&self, user_id: Uuid) -> Result<(), UserIdAlreadySetError> {
        self.user_id
            .set(user_id)
            .map_err(|_| UserIdAlreadySetError)
    }

    pub fn get_user_id(&self) -> Option<Uuid> {
        self.user_id.get().map(|x| *x)
    }

Copy link
Member Author

Choose a reason for hiding this comment

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

There definitely are user-bound, but not crypto-inited clients -- that's a locked SDK.

The thought was, that I only need the UserId during encryption (for now) and I'm not 100% familiar with our current usages and I wanted to minimize the impact of the breaking change.

I can certainly revise. I agree it makes sense to init userId only once -- that's an easy change -- Actually making the pure, environment-bound, and user-bound client contexts feel out of scope here, though. Do you agree?

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh yes splitting them up is way out of scope, we're gonna be working on that with Auth if I remember correctly. I was just thinking ahead a little, I could've been a little clearer there :) I think OnceLock that Dani suggested is a perfectly sound solution until we eventually start splitting stuff

Copy link
Member Author

Choose a reason for hiding this comment

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

This is done (468bcda), but there is an unfortunate side effect that crypto initialization can now error with a userId already set.

This will have to be updated once the clients are split, but for now is in line with the expectation that UserId is sent into crypto initialization. Once I implemented it, I got to thinking that for the same reasons it makes sense to error should multiple init_user_crypto requests be sent into a single client, so hopefully this can just morph into a CryptoAlreadyInitialized error or some such.

@MGibson1 MGibson1 force-pushed the add-userid-to-encryption-methods branch from 3413a56 to 0d0b707 Compare May 22, 2025 14:12
@MGibson1 MGibson1 requested a review from coroiu May 22, 2025 14:12
@MGibson1 MGibson1 force-pushed the add-userid-to-encryption-methods branch from 0d0b707 to 468bcda Compare May 22, 2025 14:13
Copy link

@MGibson1 MGibson1 merged commit 1a31a1f into main May 22, 2025
43 checks passed
@MGibson1 MGibson1 deleted the add-userid-to-encryption-methods branch May 22, 2025 15:49
SaintPatrck added a commit to bitwarden/android that referenced this pull request Jun 2, 2025
This commit updates several functions to accept an `encryptedFor` parameter, which specifies the user ID for whom the cipher is encrypted. This property is used by the server to verify the cipher is encrypted by the correct user. If verification fails the server responds with an appropriate error message.

This change affects the following:
- `toEncryptedNetworkCipher` and `toEncryptedNetworkCipherResponse` extension functions for `Cipher` now require an `encryptedFor` parameter.
- `CipherJsonRequest` and `SyncResponseJson.Cipher` now include an `encryptedFor` field.
- SDK functions like `encryptCipher` and `decryptFile` have been updated to align with these changes.

Additionally, this update includes the following SDK related changes:
- Adding the `encryptedFor` related logic (bitwarden/sdk-internal#278)
- Update in protocol and objects naming to have ``{foo}Client[Protocol]`` instead of `Client{foo}[Protocol]` (bitwarden/sdk-internal#224)
- Update attachments decryption to use `AttachmentView` instead of `Attachment` (bitwarden/sdk-internal#255)
SaintPatrck added a commit to bitwarden/android that referenced this pull request Jun 2, 2025
This commit updates several functions to accept an `encryptedFor` parameter, which specifies the user ID for whom the cipher is encrypted. This property is used by the server to verify the cipher is encrypted by the correct user. If verification fails the server responds with an appropriate error message.

This change affects the following:
- `toEncryptedNetworkCipher` and `toEncryptedNetworkCipherResponse` extension functions for `Cipher` now require an `encryptedFor` parameter.
- `CipherJsonRequest` and `SyncResponseJson.Cipher` now include an `encryptedFor` field.
- SDK functions like `encryptCipher` and `decryptFile` have been updated to align with these changes.

Additionally, this update includes the following SDK related changes:
- Adding the `encryptedFor` related logic (bitwarden/sdk-internal#278)
- Update in protocol and objects naming to have ``{foo}Client[Protocol]`` instead of `Client{foo}[Protocol]` (bitwarden/sdk-internal#224)
- Update attachments decryption to use `AttachmentView` instead of `Attachment` (bitwarden/sdk-internal#255)
SaintPatrck added a commit to bitwarden/android that referenced this pull request Jun 2, 2025
This commit updates several functions to accept an `encryptedFor` parameter, which specifies the user ID for whom the cipher is encrypted. This property is used by the server to verify the cipher is encrypted by the correct user. If verification fails the server responds with an appropriate error message.

This change affects the following:
- `toEncryptedNetworkCipher` and `toEncryptedNetworkCipherResponse` extension functions for `Cipher` now require an `encryptedFor` parameter.
- `CipherJsonRequest` and `SyncResponseJson.Cipher` now include an `encryptedFor` field.
- SDK functions like `encryptCipher` and `decryptFile` have been updated to align with these changes.

Additionally, this update includes the following SDK related changes:
- Adding the `encryptedFor` related logic (bitwarden/sdk-internal#278)
- Update in protocol and objects naming to have ``{foo}Client[Protocol]`` instead of `Client{foo}[Protocol]` (bitwarden/sdk-internal#224)
- Update attachments decryption to use `AttachmentView` instead of `Attachment` (bitwarden/sdk-internal#255)
SaintPatrck added a commit to bitwarden/android that referenced this pull request Jun 2, 2025
This commit updates several functions to accept an `encryptedFor` parameter, which specifies the user ID for whom the cipher is encrypted. This property is used by the server to verify the cipher is encrypted by the correct user. If verification fails the server responds with an appropriate error message.

This change affects the following:
- `toEncryptedNetworkCipher` and `toEncryptedNetworkCipherResponse` extension functions for `Cipher` now require an `encryptedFor` parameter.
- `CipherJsonRequest` and `SyncResponseJson.Cipher` now include an `encryptedFor` field.
- SDK functions like `encryptCipher` and `decryptFile` have been updated to align with these changes.

Additionally, this update includes the following SDK related changes:
- Adding the `encryptedFor` related logic (bitwarden/sdk-internal#278)
- Update in protocol and objects naming to have ``{foo}Client[Protocol]`` instead of `Client{foo}[Protocol]` (bitwarden/sdk-internal#224)
- Update attachments decryption to use `AttachmentView` instead of `Attachment` (bitwarden/sdk-internal#255)
SaintPatrck added a commit to bitwarden/android that referenced this pull request Jun 3, 2025
This commit updates several functions to accept an `encryptedFor` parameter, which specifies the user ID for whom the cipher is encrypted. This property is used by the server to verify the cipher is encrypted by the correct user. If verification fails the server responds with an appropriate error message.

This change affects the following:
- `toEncryptedNetworkCipher` and `toEncryptedNetworkCipherResponse` extension functions for `Cipher` now require an `encryptedFor` parameter.
- `CipherJsonRequest` and `SyncResponseJson.Cipher` now include an `encryptedFor` field.
- SDK functions like `encryptCipher` and `decryptFile` have been updated to align with these changes.

Additionally, this update includes the following SDK related changes:
- Adding the `encryptedFor` related logic (bitwarden/sdk-internal#278)
- Update in protocol and objects naming to have ``{foo}Client[Protocol]`` instead of `Client{foo}[Protocol]` (bitwarden/sdk-internal#224)
- Update attachments decryption to use `AttachmentView` instead of `Attachment` (bitwarden/sdk-internal#255)
SaintPatrck added a commit to bitwarden/android that referenced this pull request Jun 3, 2025
This commit updates several functions to accept an `encryptedFor` parameter, which specifies the user ID for whom the cipher is encrypted. This property is used by the server to verify the cipher is encrypted by the correct user. If verification fails the server responds with an appropriate error message.

This change affects the following:
- `toEncryptedNetworkCipher` and `toEncryptedNetworkCipherResponse` extension functions for `Cipher` now require an `encryptedFor` parameter.
- `CipherJsonRequest` and `SyncResponseJson.Cipher` now include an `encryptedFor` field.
- SDK functions like `encryptCipher` and `decryptFile` have been updated to align with these changes.

Additionally, this update includes the following SDK related changes:
- Adding the `encryptedFor` related logic (bitwarden/sdk-internal#278)
- Update in protocol and objects naming to have ``{foo}Client[Protocol]`` instead of `Client{foo}[Protocol]` (bitwarden/sdk-internal#224)
- Update attachments decryption to use `AttachmentView` instead of `Attachment` (bitwarden/sdk-internal#255)
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.

3 participants