Skip to content

[PM-33298] Fix InvalidCastException when uploading attachments as admin#7175

Open
boris324 wants to merge 1 commit intobitwarden:mainfrom
boris324:fix/attachment-upload-cast-exception
Open

[PM-33298] Fix InvalidCastException when uploading attachments as admin#7175
boris324 wants to merge 1 commit intobitwarden:mainfrom
boris324:fix/attachment-upload-cast-exception

Conversation

@boris324
Copy link

@boris324 boris324 commented Mar 9, 2026

Fixes #7062

Problem

When uploading attachments via admin endpoints, GetOrganizationDetailsByIdAsync returns a CipherOrganizationDetails which is then passed into CreateAttachmentForDelayedUploadAsync / CreateAttachmentAsync. These methods cast the cipher to CipherDetails when calling ReplaceAsync, but CipherOrganizationDetails is a parent of CipherDetails, so the downcast fails with an InvalidCastException.

Fix

Use pattern matching (is CipherDetails) to call the appropriate ReplaceAsync overload. When the cipher is a CipherDetails, use the specific overload; otherwise fall back to the base ReplaceAsync(Cipher) overload.

Applied to all three occurrences in CipherService.cs:

  • CreateAttachmentForDelayedUploadAsync
  • CreateAttachmentAsync
  • DeleteAttachmentAsync

CipherOrganizationDetails cannot be cast to CipherDetails since
CipherDetails is a subclass, not a superclass. Use pattern matching
to call the appropriate ReplaceAsync overload based on runtime type.

Fixes bitwarden#7062
@boris324 boris324 requested a review from a team as a code owner March 9, 2026 12:40
@boris324 boris324 requested a review from Jingo88 March 9, 2026 12:40
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@bitwarden-bot
Copy link

Thank you for your contribution! We've added this to our internal tracking system for review.
ID: PM-33298
Link: https://bitwarden.atlassian.net/browse/PM-33298

Details on our contribution process can be found here: https://contributing.bitwarden.com/contributing/pull-requests/community-pr-process.

@bitwarden-bot bitwarden-bot changed the title Fix InvalidCastException when uploading attachments as admin [PM-33298] Fix InvalidCastException when uploading attachments as admin Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to upload attachments due to InvalidCastException

3 participants