From 803acce7e9a36b307ccb74301c33ad77bb26b9be Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Mon, 3 Jun 2024 16:12:33 +0400 Subject: [PATCH] style: error on invalid utf8 seq --- src/web_server/app_error.rs | 5 +++++ src/web_server/old_device_sensor_api/mod.rs | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/web_server/app_error.rs b/src/web_server/app_error.rs index ae02817..9285dcc 100644 --- a/src/web_server/app_error.rs +++ b/src/web_server/app_error.rs @@ -44,6 +44,9 @@ pub enum AppError { min: Option, }, + #[display(fmt = "UTF8")] + Utf8Error(std::str::Utf8Error), + #[display(fmt = "IDK")] UnknownBody { json_err: Option, @@ -63,6 +66,7 @@ impl web::error::WebResponseError for AppError { AppError::RequestTooLarge => StatusCode::PAYLOAD_TOO_LARGE, AppError::ServerRedisError(_) => StatusCode::INTERNAL_SERVER_ERROR, AppError::ApiKeyInvalid { .. } => StatusCode::BAD_REQUEST, + AppError::Utf8Error(_) => StatusCode::BAD_REQUEST, AppError::UnknownBody { .. } => StatusCode::BAD_REQUEST, AppError::QSError(..) => StatusCode::BAD_REQUEST, AppError::DeviceNotFound(..) => StatusCode::BAD_REQUEST, @@ -77,6 +81,7 @@ impl web::error::WebResponseError for AppError { AppError::RequestTooLarge => "Request is too large", AppError::ServerRedisError(_) => "Internal server error", AppError::ApiKeyInvalid { .. } => "API Key invalid", + AppError::Utf8Error(_) => "Invalid UTF8 sequence", AppError::UnknownBody { .. } => { "Can't figure out where and in what encoding the main data is" } diff --git a/src/web_server/old_device_sensor_api/mod.rs b/src/web_server/old_device_sensor_api/mod.rs index ba258e7..33be26f 100644 --- a/src/web_server/old_device_sensor_api/mod.rs +++ b/src/web_server/old_device_sensor_api/mod.rs @@ -50,7 +50,13 @@ pub async fn device_handler<'a>( Err(error) => json_error = Some(error), }, "application/x-www-form-urlencoded" => { - match qs_parser::parse_nm_qs_format(&body.as_ref().to_str_lossy()).await { + let body = match std::str::from_utf8(body.as_ref()) { + Ok(body) => body, + Err(error) => { + return Err(AppError::Utf8Error(error)); + } + }; + match qs_parser::parse_nm_qs_format(body).await { Ok(qs_body) => { real_body = Some(NMJsonPacket { devices: Vec::from([qs_body]),