Skip to content

Commit

Permalink
Return "invalid request" if JSON parses, but isn't a Request
Browse files Browse the repository at this point in the history
  • Loading branch information
ebkalderon committed Mar 4, 2022
1 parent cb53902 commit 0850ebc
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/transport.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Generic server for multiplexing bidirectional streams through a transport.

#[cfg(feature = "runtime-agnostic")]
use std::error::Error as StdError;

#[cfg(feature = "runtime-agnostic")]
use async_codec_lite::{FramedRead, FramedWrite};
#[cfg(feature = "runtime-agnostic")]
Expand All @@ -15,7 +18,7 @@ use futures::{future, join, stream, FutureExt, Sink, SinkExt, Stream, StreamExt,
use log::error;
use tower::Service;

use crate::codec::LanguageServerCodec;
use crate::codec::{LanguageServerCodec, ParseError};
use crate::jsonrpc::{Error, Id, Message, Request, Response};
use crate::service::{ClientSocket, RequestStream, ResponseSink};

Expand Down Expand Up @@ -149,7 +152,7 @@ where
}
Err(err) => {
error!("failed to decode message: {}", err);
let res = Response::from_error(Id::Null, Error::parse_error());
let res = Response::from_error(Id::Null, to_jsonrpc_error(err));
responses_tx.send(Message::Response(res)).await.unwrap();
}
}
Expand All @@ -172,6 +175,24 @@ fn display_sources(error: &dyn std::error::Error) -> String {
}
}

#[cfg(feature = "runtime-tokio")]
#[inline]
fn to_jsonrpc_error(err: ParseError) -> Error {
match err {
ParseError::Body(err) if err.is_data() => Error::invalid_request(),
_ => Error::parse_error(),
}
}

#[cfg(feature = "runtime-agnostic")]
#[inline]
fn to_jsonrpc_error(err: impl StdError) -> Error {
match err.source().and_then(|e| e.downcast_ref()) {
Some(ParseError::Body(err)) if err.is_data() => Error::invalid_request(),
_ => Error::parse_error(),
}
}

#[cfg(test)]
mod tests {
use std::task::{Context, Poll};
Expand Down

0 comments on commit 0850ebc

Please sign in to comment.