Skip to content

Commit dfe0e08

Browse files
Add timeout for cbor_send, cbor_recv
1 parent 4abffd4 commit dfe0e08

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

libwebauthn/src/transport/cable/channel.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::time::Duration;
33

44
use async_trait::async_trait;
55
use tokio::sync::mpsc;
6-
use tokio::task;
6+
use tokio::{task, time};
77
use tracing::error;
88

99
use crate::proto::{
@@ -76,18 +76,23 @@ impl<'d> Channel for CableChannel<'d> {
7676
Err(Error::Transport(TransportError::TransportUnavailable))
7777
}
7878

79-
async fn cbor_send(&mut self, request: &CborRequest, _timeout: Duration) -> Result<(), Error> {
80-
self.cbor_sender
81-
.send(request.clone())
82-
.await
83-
.or(Err(Error::Transport(TransportError::TransportUnavailable)))
79+
async fn cbor_send(&mut self, request: &CborRequest, timeout: Duration) -> Result<(), Error> {
80+
match time::timeout(timeout, self.cbor_sender.send(request.clone())).await {
81+
Ok(Ok(_)) => Ok(()),
82+
Ok(Err(error)) => {
83+
error!(%error, "CBOR request send failure");
84+
Err(Error::Transport(TransportError::TransportUnavailable))
85+
}
86+
Err(_) => Err(Error::Transport(TransportError::Timeout)),
87+
}
8488
}
8589

86-
async fn cbor_recv(&mut self, _timeout: Duration) -> Result<CborResponse, Error> {
87-
self.cbor_receiver
88-
.recv()
89-
.await
90-
.ok_or(Error::Transport(TransportError::TransportUnavailable))
90+
async fn cbor_recv(&mut self, timeout: Duration) -> Result<CborResponse, Error> {
91+
match time::timeout(timeout, self.cbor_receiver.recv()).await {
92+
Ok(Some(response)) => Ok(response),
93+
Ok(None) => Err(Error::Transport(TransportError::TransportUnavailable)),
94+
Err(_) => Err(Error::Transport(TransportError::Timeout)),
95+
}
9196
}
9297

9398
fn get_state_sender(&self) -> &mpsc::Sender<UxUpdate> {

0 commit comments

Comments
 (0)