Skip to content

Commit 6d7064b

Browse files
committed
backend: fix deserialization of chunked request.
1 parent e7667bb commit 6d7064b

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

backend/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ semver = "1.0.24"
5353
# This is a workaround until lettre and native-tls are updated
5454
openssl = "0.10.72"
5555
actix-ws = "0.3.0"
56+
serde_json = "1.0.140"
5657

5758
[dev-dependencies]
5859
libsodium-sys-stable = "1.20.4"

backend/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ pub enum Error {
5858
ChargerCredentialsWrong,
5959
#[display("Charger does not exist")]
6060
ChargerDoesNotExist,
61+
#[display("Invalid payload")]
62+
InvalidPayload,
6163
}
6264

6365
impl error::ResponseError for Error {
@@ -85,6 +87,7 @@ impl error::ResponseError for Error {
8587
Self::SessionDoesNotExist => StatusCode::UNAUTHORIZED,
8688
Self::ChargerCredentialsWrong => StatusCode::UNAUTHORIZED,
8789
Self::ChargerDoesNotExist => StatusCode::BAD_REQUEST,
90+
Self::InvalidPayload => StatusCode::BAD_REQUEST,
8891
}
8992
}
9093
}

backend/src/routes/send_chargelog_to_user.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use actix_web::{post, web, HttpRequest, HttpResponse, Responder};
2+
use futures_util::StreamExt;
23
use serde::{Deserialize, Serialize};
34
use utoipa::ToSchema;
45

56
use crate::{
67
error::Error,
78
rate_limit::ChargerRateLimiter,
8-
routes::charger::add::{get_charger_from_db, password_matches},
9+
routes::{charger::add::{get_charger_from_db, password_matches}, user::get_user},
910
utils::{parse_uuid, send_email_with_attachment},
1011
AppState,
1112
};
@@ -14,7 +15,7 @@ use crate::{
1415
pub struct SendChargelogSchema {
1516
pub charger_uuid: String,
1617
pub password: String,
17-
pub user_email: String,
18+
pub user_uuid: String,
1819
pub chargelog: Vec<u8>, // binary data
1920
}
2021

@@ -31,8 +32,20 @@ pub async fn send_chargelog(
3132
req: HttpRequest,
3233
state: web::Data<AppState>,
3334
rate_limiter: web::Data<ChargerRateLimiter>,
34-
payload: web::Json<SendChargelogSchema>,
35+
mut payload: web::Payload,
3536
) -> actix_web::Result<impl Responder> {
37+
let mut bytes = web::BytesMut::new();
38+
while let Some(chunk) = payload.next().await {
39+
let chunk = chunk.map_err(|_| Error::InternalError)?;
40+
let chunk = chunk.into_iter().filter(|b| *b != b'\r' && *b != b'\n').collect::<Vec<u8>>();
41+
bytes.extend_from_slice(&chunk);
42+
}
43+
let payload: SendChargelogSchema = serde_json::from_slice(&bytes)
44+
.map_err(|err| {
45+
log::error!("Failed to parse payload: {}", err);
46+
Error::InvalidPayload
47+
})?;
48+
3649
rate_limiter.check(payload.charger_uuid.clone(), &req)?;
3750

3851
let charger_id = parse_uuid(&payload.charger_uuid)?;
@@ -41,14 +54,17 @@ pub async fn send_chargelog(
4154
return Err(Error::ChargerCredentialsWrong.into());
4255
}
4356

57+
let user = parse_uuid(&payload.user_uuid)?;
58+
let user = get_user(&state, user).await?;
59+
4460
let subject = "Your Charger Log";
4561
let body = "Attached is your requested chargelog.".to_string();
4662
send_email_with_attachment(
47-
&payload.user_email,
63+
&user.email,
4864
subject,
4965
body,
5066
payload.chargelog.clone(),
51-
"chargelog.bin",
67+
"chargelog.pdf",
5268
&state,
5369
);
5470

@@ -73,7 +89,7 @@ mod tests {
7389
let payload = SendChargelogSchema {
7490
charger_uuid: charger.uuid.clone(),
7591
password: charger.password.clone(),
76-
user_email: user.mail.clone(),
92+
user_uuid: crate::routes::user::tests::get_test_uuid(&user.mail).unwrap().to_string(),
7793
chargelog: vec![1, 2, 3, 4, 5],
7894
};
7995

@@ -98,7 +114,7 @@ mod tests {
98114
let payload = SendChargelogSchema {
99115
charger_uuid: charger.uuid.clone(),
100116
password: "wrongpassword".to_string(),
101-
user_email: user.mail.clone(),
117+
user_uuid: crate::routes::user::tests::get_test_uuid(&user.mail).unwrap().to_string(),
102118
chargelog: vec![1, 2, 3, 4, 5],
103119
};
104120

0 commit comments

Comments
 (0)