Prevent panic on requests with invalid URIs

When `try_into_http_request` was used with an invalid base URI the code
was panicking. Acording to `http::request::Builder` documentation,
`headers_mut` returns `None` if the builder contains errors, which was
the case when an invalid URI was provided.

The new version only sets the additional headers in case that there are
no errors on the builder, preventing the panic. The conversion will
return an error when the builder is consumed on `body`.
This commit is contained in:
gnieto 2021-05-08 18:12:56 +02:00 committed by GitHub
parent a0f7e1b771
commit e2eb92b8ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 5 deletions

View File

@ -226,11 +226,9 @@ impl Request {
"application/json", "application/json",
); );
let mut req_headers = req_builder if let Some(mut req_headers) = req_builder.headers_mut() {
.headers_mut() #header_kvs
.expect("`http::RequestBuilder` is in unusable state"); }
#header_kvs
let http_request = req_builder.body(#request_body)?; let http_request = req_builder.body(#request_body)?;

View File

@ -21,6 +21,10 @@ Improvements:
* Add a new `MatrixError` type to the `error` module that consists of a HTTP status code and JSON * Add a new `MatrixError` type to the `error` module that consists of a HTTP status code and JSON
`body` and is the new default error type for `ruma_api!` `body` and is the new default error type for `ruma_api!`
Bug fixes:
* Prevent panic when building requests with an invalid URI
# 0.16.1 # 0.16.1
Bug fixes: Bug fixes:

View File

@ -62,6 +62,21 @@ fn request_serde() {
assert_eq!(req.user, req2.user); assert_eq!(req.user, req2.user);
} }
#[test]
fn invalid_uri_should_not_panic() {
let req = Request {
hello: "hi".to_owned(),
world: "test".to_owned(),
q1: "query_param_special_chars %/&@!".to_owned(),
q2: 55,
bar: "barVal".to_owned(),
user: user_id!("@bazme:ruma.io"),
};
let result = req.clone().try_into_http_request::<Vec<u8>>("invalid uri", SendAccessToken::None);
assert!(result.is_err());
}
#[test] #[test]
fn request_with_user_id_serde() { fn request_with_user_id_serde() {
let req = Request { let req = Request {