Skip to content

[PM-2572] Add new cipher encryption on attachments without key when moving cipher to an org#3238

Closed
LRNcardozoWDF wants to merge 3957 commits intomainfrom
vault/pm-2572
Closed

[PM-2572] Add new cipher encryption on attachments without key when moving cipher to an org#3238
LRNcardozoWDF wants to merge 3957 commits intomainfrom
vault/pm-2572

Conversation

@LRNcardozoWDF
Copy link
Member

Type of change

  • Bug fix
  • New feature development
  • Tech debt (refactoring, code cleanup, dependency upgrades, etc)
  • Build/deploy pipeline (DevOps)
  • Other

Objective

When an attachment file gets reencrypted and reuploaded, the new key is ignored by the server. This PR prepares the fix for the mobile side by adding the cipher.key in the attachment's metadata.

Code changes

  • Attachment.cs AttachmentView.cs: Added property CipherKey to store the cipher.Key
  • src/Core/Models/Domain/Cipher.cs: Added logic to use attachment.CipherKey for decryption if necessary
  • src/Core/Services/CipherService.cs: Update the cipher to use cipher.Key and save that value in the attachment object and metadata.

Before you submit

  • Please check for formatting errors (dotnet format --verify-no-changes) (required)
  • Please add unit tests where it makes sense to do so (encouraged but not required)
  • If this change requires a documentation update - notify the documentation team
  • If this change has particular deployment requirements - notify the DevOps team

andrebispo5 and others added 30 commits September 7, 2023 16:30
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
* Update AppResources.af.resx

* Update AnonAddy references

* Reverted AnonAddy to AddyIo refactor, keeping text and url changes

---------

Co-authored-by: Andre Rosado <arosado@bitwarden.com>
* [PM-3606] Fix 2FA for autofill

* [PM-3606] Fix autofill when user doesn't have a login method available.

* [PM-3606] PR fixes

* [PM-3606] Add logout logic to other extension projects

* [PM-3606] Move code to base class.

* Transform into property instead of field

Co-authored-by: Federico Maccaroni <fedemkr@gmail.com>

* Remove double ";"

Co-authored-by: Federico Maccaroni <fedemkr@gmail.com>

* [PM-3606] Fix iOS extension by changing base class of LockPasswordViewController

---------

Co-authored-by: Federico Maccaroni <fedemkr@gmail.com>
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
…cally log out TDE users (#2747)

* [PM-3393] Log user out on biometric exceed attempts

* [PM-3393] Move duplicated code to AppHelpers

* [PM-3393] Update copy on new pop up

* [PM-3393] Moved VaultTimeoutService to LazyResolve.

* [PM-3382] Change IVaultTimeoutService for messaging

* [PM-3393] Use default values.
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
* [PM-3726] prevent legacy user login

* [PM-3726] prevent unlock or auto key migration if legacy user

* [PM-3726] add legacy checks to lock page and refactor

* [PM-3726] rethrow exception from pin

* formatting

* [PM-3726] add changes to LockViewController, consolidate logout calls

* formatting

* [PM-3726] pr feedback

* generate resx

* formatting
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
* PM-3811 Unified passkeys view and moved both inside Login as an array of FIdo2Key

* PM-3811 Passkeys unification => updated cipher details view an helpers

* PM-3811 Updated passkeys creation date time format
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
…oard for autofill (#2764)

* [PM-3446] Check if user has mp and allow autofill to use items with mp re-prompt
* [PM-2658] Settings Reorganization Init (#2697)

* PM-2658 Started settings reorganization (settings main + vault + about)

* PM-2658 Added settings controls based on templates and implemented OtherSettingsPage

* PM-2658 Fix format

* [PM-3512] Settings Appearance (#2703)

* PM-3512 Implemented new Appearance Settings

* PM-3512 Fix format

* [PM-3510] Implement Account Security Settings view (#2714)

* PM-3510 Implemented Security settings view

* PM-3510 Fix format

* PM-3510 Added empty placeholder to pending login requests and also improved a11y on security settings view.

* PM-3511 Implemented autofill settings view (#2735)

* [PM-3695] Add Connect to Watch to Other settings (#2736)

* PM-3511 Implemented autofill settings view

* PM-3695 Add Connect to watch setting to other settings view

* [PM-3693] Clear old Settings approach (#2737)

* PM-3511 Implemented autofill settings view

* PM-3693 Remove old Settings approach

* PM-3845 Fix default dark theme description verbiage (#2759)

* PM-3839 Fix allow screen capture and submit crash logs to init their state when the page appears (#2760)

* PM-3834 Fix dialogs strings on settings (#2758)

* [PM-3834] Fix import items link (#2782)

* PM-3834 Fix import items link

* PM-3834 Fix import items link, removed old link.

* [PM-4092] Fix vault timeout policies on new Settings (#2796)

* PM-4092 Fix vault timeout policy on settings for disabling controls and reset timeout when surpassing maximum

* PM-4092 Removed testing hardcoding of policy data
* PM-115 Added new cipher key and encryption/decryption mechanisms on cipher

* PM-115 fix format

* PM-115 removed ForceKeyRotation from new cipher encryption model given that another approach will be taken

* [PM-1690] Added minimum server version restriction to cipher key encryption (#2463)

* PM-1690 added minimum server version restriction to cipher key encryption and also change the force key rotation flag

* PM-1690 Updated min server version for new cipher encryption key and fixed configService registration

* PM-1690 removed forcekeyrotation

* PM-115 Temporarily Changed cipher key new encryption config to help testing (this change should be reseted eventually)

* PM-2456 Fix attachment encryption on new cipher item encryption model (#2556)

* PM-2531 Fix new cipher encryption on adding attachments on ciphers with no item level key (#2559)

* PM-115 Changed temporarily cipher key encryption min server version to 2023.6.0 to test

* PM-115 Reseted cipher key encryption minimum server version to 2023.5.0 and disable new cipher key on local cipher creation

* Added Key value to the cipher export model (#2628)

* Update Constants.cs

Updated minimum encryption server version to 2023.9.0 so QA can test its behavior

* PM-115 Fix file format

* PM-115 Changed new encryption off and minimum new encryption server version to 2023.8.0 for testing purposes

* PM-115 Changed CIpher key encryption minimum server version to 2023.9.0

* PM-3737 Remove suffix on client version sent to server (#2779)

* PM-115 QA testing server min version and enable new cipher key encryption

* PM-115 Disable new cipher encryption creation and change minimum server encryption version to 2023.9.1

---------

Co-authored-by: aj-rosado <109146700+aj-rosado@users.noreply.github.com>
* [PM-868] Check for previous page before loading vault. Remove exception throw.

* [PM-868] Continue to throw exceptions
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
* [PM-3741] [PM-3750] Improvements to local storage handling

* Update src/Android/MainActivity.cs

Co-authored-by: Federico Maccaroni <fedemkr@gmail.com>

---------

Co-authored-by: Federico Maccaroni <fedemkr@gmail.com>
vgrassia and others added 5 commits May 17, 2024 00:15
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Comment on lines +579 to +588
if(cipherView.Key == null)
{
foreach (var attachment in cipher.Attachments)
cipher = await EncryptAsync(cipherView);
var putCipherRequest = new CipherRequest(cipher);
var putCipherResponse = await _apiService.PutCipherAsync(cipherView.Id, putCipherRequest);
var cipherData = new CipherData(putCipherResponse, await _stateService.GetActiveUserIdAsync());
await UpsertAsync(cipherData);
cipher = await GetAsync(cipherView.Id);
cipherView = await cipher.DecryptAsync();
}
Copy link
Member

Choose a reason for hiding this comment

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

💡 Nice idea to update the cipher key here if it doesn't have one before updating the attachments.
🤔 I think that doing this you don't need the CipherKey in the Attachment neither the cipherKey added in the StringContent for the MultipartFormDataContent. Because I don't see a situation where the cipher doesn't have an item level encryption key and the attachment is migrated with it. Did you find one and that's why you left the CipherKey in the attachment?
🎨 Could you extract the common parts into a method (can be local function as you want)? So it's shared with lines 604...609

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, that was my interpretation of the steps in the task. I honestly thought we were just being extra careful.

github-actions bot and others added 3 commits May 24, 2024 12:09
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
@LRNcardozoWDF LRNcardozoWDF requested a review from fedemkr May 28, 2024 10:18
if (cipher.Attachments != null)
Cipher cipher = null;
//If the cipher doesn't have a key, we update it
if(cipherView.Key == null)
Copy link
Member

Choose a reason for hiding this comment

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

🎨 I think we could improve this checking if we should use cipher key encryption by calling ShouldUseCipherKeyEncryptionAsync, so we save a sever call if that's not the case.

{
foreach (var attachment in cipher.Attachments)
cipher = await EncryptAsync(cipherView);
await UpdateAndUpsertAsync(async () => await _apiService.PutCipherAsync(cipherView.Id, new CipherRequest(cipher)));
Copy link
Member

Choose a reason for hiding this comment

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

⛏️ You can omit the async and await inside the lambda.

await UpsertAsync(data);
cipherView.OrganizationId = organizationId;
cipherView.CollectionIds = collectionIds;
cipher = await EncryptAsync(cipherView);
Copy link
Member

Choose a reason for hiding this comment

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

🎨 You can move encrypting into UpdateAndUpsertAsync as well.

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 do that because I need the cipher to create two different requests, CipherRequest and CipherShareRequest. I could add in the api call but I think there's already too much happening in one line of code.
await UpdateAndUpsertAsync(async () => await _apiService.PutShareCipherAsync(cipherView.Id, new CipherShareRequest(await EncryptAsync(cipherView))), collectionIds);

Copy link
Member

Choose a reason for hiding this comment

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

Oh I see, I think it's fine to pass the CipherView as a parameter to the func to construct such requests.

}
}

private async Task UpdateAndUpsertAsync(Func<Task<CipherResponse>> func, HashSet<string> collectionIds = null)
Copy link
Member

Choose a reason for hiding this comment

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

⛏️ I'd rename the func parameter to callPutCipherApi or something that indicates the purpose of such func.

@LRNcardozoWDF LRNcardozoWDF requested a review from fedemkr May 28, 2024 20:35
Copy link
Member

@fedemkr fedemkr left a comment

Choose a reason for hiding this comment

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

Great just one small thing and it's done 😄 🎉

if (cipher.Attachments != null)
Cipher cipher = null;
//If the cipher doesn't have a key, we update it
if(await ShouldUseCipherKeyEncryptionAsync() && cipherView.Key == null)
Copy link
Member

Choose a reason for hiding this comment

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

⛏️ Invert the order so it doesn't waste time firing the task when Key != null

Suggested change
if(await ShouldUseCipherKeyEncryptionAsync() && cipherView.Key == null)
if (cipherView.Key == null && await ShouldUseCipherKeyEncryptionAsync())

Copy link
Member

@fedemkr fedemkr left a comment

Choose a reason for hiding this comment

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

LGTM 🎉

github-actions bot and others added 8 commits May 29, 2024 20:07
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: bitwarden-devops-bot <106330231+bitwarden-devops-bot@users.noreply.github.com>
@vvolkgang vvolkgang closed this Jun 20, 2024
@vvolkgang vvolkgang deleted the vault/pm-2572 branch June 20, 2024 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.