Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/liftable_e2ee_provider_errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
livekit-datatrack: major
livekit-uniffi: major
livekit: patch
livekit-ffi: patch
livekit-capture: patch
---

`EncryptionError::Failed` and `DecryptionError::Failed` carry a `reason` string and are no longer `flat_error`, so a foreign `EncryptionProvider` or `DecryptionProvider` returning an error no longer aborts the process with "Can't lift flat errors" -- a failed data track decrypt (no E2EE manager, key mismatch, corrupt frame) now drops the frame and leaves the room connected.
28 changes: 22 additions & 6 deletions livekit-datatrack/src/e2ee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,35 @@ pub struct EncryptedPayload {
/// An error indicating a payload could not be encrypted.
#[derive(Debug, Error)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
#[cfg_attr(feature = "uniffi", uniffi(flat_error))]
pub enum EncryptionError {
#[error("Encryption failed")]
Failed,
#[error("Encryption failed: {reason}")]
Failed { reason: String },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

suggestion (non-blocking): Consider defining an enum case for each error type (e.g., UnexpectedIvLength, KeyIndexOutOfRange, and Failed).

}

// Required because foreign code implements `EncryptionProvider::encrypt`: an exception that is
// NOT an `EncryptionError` surfaces through this catch-all rather than aborting.
#[cfg(feature = "uniffi")]
impl From<uniffi::UnexpectedUniFFICallbackError> for EncryptionError {
fn from(error: uniffi::UnexpectedUniFFICallbackError) -> Self {
Self::Failed { reason: error.reason }
}
}

/// An error indicating a payload could not be decrypted.
#[derive(Debug, Error)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
#[cfg_attr(feature = "uniffi", uniffi(flat_error))]
pub enum DecryptionError {
#[error("Decryption failed")]
Failed,
#[error("Decryption failed: {reason}")]
Failed { reason: String },
}
Comment on lines 50 to +56

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It might also be worth adding something to the AGENTS.md saying something to the effect of "never use flat_error in contexts where an error needs to be lifted from client sdk -> rust"?


// Required because foreign code implements `DecryptionProvider::decrypt`: an exception that is
// NOT a `DecryptionError` surfaces through this catch-all rather than aborting.
#[cfg(feature = "uniffi")]
impl From<uniffi::UnexpectedUniFFICallbackError> for DecryptionError {
fn from(error: uniffi::UnexpectedUniFFICallbackError) -> Self {
Self::Failed { reason: error.reason }
}
}

/// Provider for encrypting payloads for E2EE.
Expand Down
14 changes: 10 additions & 4 deletions livekit/src/room/e2ee/data_track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
let encrypted = self
.manager
.encrypt_data(payload.into(), &self.sender_identity, key_index)
.map_err(|_| dt::EncryptionError::Failed)?;
.map_err(|e| dt::EncryptionError::Failed { reason: e.to_string() })?;

debug_assert_eq!(
encrypted.key_index as u32,
Expand All @@ -46,8 +46,12 @@
);

let payload = encrypted.data.into();
let iv = encrypted.iv.try_into().map_err(|_| dt::EncryptionError::Failed)?;
let key_index = encrypted.key_index.try_into().map_err(|_| dt::EncryptionError::Failed)?;
let iv = encrypted.iv.try_into().map_err(|iv: Vec<u8>| dt::EncryptionError::Failed {

Check warning on line 49 in livekit/src/room/e2ee/data_track.rs

View workflow job for this annotation

GitHub Actions / Check Formatting

Diff in /home/runner/work/rust-sdks/rust-sdks/livekit/src/room/e2ee/data_track.rs
reason: format!("unexpected IV length: {}", iv.len()),
})?;
let key_index = encrypted.key_index.try_into().map_err(|e| dt::EncryptionError::Failed {
reason: format!("key index out of range: {e}"),
})?;

Ok(dt::EncryptedPayload { payload, iv, key_index })
}
Expand Down Expand Up @@ -79,7 +83,9 @@
payload.key_index as u32,
&sender_identity,
)
.ok_or_else(|| dt::DecryptionError::Failed)?;
.ok_or_else(|| dt::DecryptionError::Failed {
reason: "the E2EE manager could not decrypt the payload".to_owned(),
})?;
Ok(Bytes::from(decrypted))
}
}
Loading