Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adding check for the maximum payload size #346

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ pub enum Error {

#[error("tenant suspended due to invalid configuration")]
TenantSuspended,

#[error("Payload too large: {0}")]
PayloadTooLarge(String),
}

impl IntoResponse for Error {
Expand Down Expand Up @@ -587,6 +590,12 @@ impl IntoResponse for Error {
message: "Request Accepted, tenant suspended due to invalid configuration".to_string(),
},
], vec![]),
Error::PayloadTooLarge(message) => crate::handlers::Response::new_failure(StatusCode::BAD_REQUEST, vec![
ResponseError {
name: "payload_too_large".to_string(),
message: format!("Payload too large: {}", message),
},
], vec![]),
e => {
warn!("Error does not have response clause, {:?}", e);

Expand Down
12 changes: 12 additions & 0 deletions src/handlers/push_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ pub async fn handler_internal(
)
})?;

// Check request body size shouldn't exceed 4Kb (FCM limitations)
if serde_json::to_string(&body)
.map_err(|e| (Error::InternalSerializationError(e), None))?
.len()
> 4096
{
return Err((
Error::PayloadTooLarge("Request body size should not exceed 4Kb".to_string()),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not make that inner string part of the display implementation provided by thiserror and you just embed the size of the payload in kb?

None,
));
};

let cloned_body = body.clone();
let push_message = if client.always_raw {
if let Some(body) = body.raw {
Expand Down
Loading