Skip to content

fix(dtls): replace panicking unwrap/expect in crypto hot paths#68

Open
nightness wants to merge 1 commit intowebrtc-rs:masterfrom
Brainwires:fix/dtls-crypto-panics
Open

fix(dtls): replace panicking unwrap/expect in crypto hot paths#68
nightness wants to merge 1 commit intowebrtc-rs:masterfrom
Brainwires:fix/dtls-crypto-panics

Conversation

@nightness
Copy link
Copy Markdown

Summary

  • crypto/mod.rs generate_self_signed_with_alg: replaces 3× .unwrap() with ? — function already returns Result<Self>
  • config.rs Config::build(): replaces .build().unwrap() on the rustls WebPkiServerVerifier builder with .map_err(|e| Error::Other(...))? — function already returns Result<HandshakeConfig>
  • flight/flight5.rs Flight5::generate(): extracts the certificate ref with ok_or_else() returning a proper Fatal/InternalError DTLS alert instead of panicking with .unwrap() — function already returns Result<_, (Option<Alert>, Option<Error>)>
  • crypto/mod.rs CryptoPrivateKey::Clone: the Clone trait cannot return Result; adds a // Safety: comment explaining why re-parsing serialized_der is sound, and upgrades bare .unwrap() to .expect() with descriptive messages so any unexpected panic is diagnosable

Why This Matters

Panics in DTLS crypto paths terminate the peer connection (and potentially the process) instead of propagating a negotiation error. The flight5 unwrap in particular can fire during an active handshake if the local certificate is unexpectedly absent.

Test Plan

  • cargo build -p rtc-dtls passes (verified)
  • cargo test -p rtc-dtls passes

🤖 Generated with Claude Code

- generate_self_signed_with_alg: replace 3x .unwrap() with ? operator
- Config::build(): replace .build().unwrap() on rustls verifier with ?
- flight5::generate(): extract certificate ref with ok_or_else() instead
  of .unwrap(), returning a proper Fatal/InternalError DTLS alert
- CryptoPrivateKey::Clone: add Safety comment explaining why re-parsing
  serialized_der is sound; upgrade bare .unwrap() to .expect() with
  descriptive message so panics are diagnosable if they ever occur

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@nightness nightness force-pushed the fix/dtls-crypto-panics branch from fdf2918 to c8122f9 Compare April 1, 2026 18:43
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.

1 participant