Skip to content
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
18 changes: 15 additions & 3 deletions crates/apollo_http_server_config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use apollo_config::converters::deserialize_milliseconds_to_duration;
use apollo_config::dumping::{prepend_sub_config_name, ser_param, SerializeConfig};
use apollo_config::{ParamPath, ParamPrivacyInput, SerializedParam};
use serde::{Deserialize, Serialize};
use validator::Validate;
use validator::{Validate, ValidationError};

const HTTP_SERVER_PORT: u16 = 8080;
pub const DEFAULT_MAX_SIERRA_PROGRAM_SIZE: usize = 4 * 1024 * 1024; // 4MB
Expand All @@ -17,6 +17,7 @@ const DEFAULT_DYNAMIC_CONFIG_POLL_INTERVAL_MS: u64 = 1_000; // 1 second.

/// The http server connection related configuration.
#[derive(Clone, Debug, Default, Serialize, Deserialize, Validate, PartialEq)]
#[validate(schema(function = "max_size_validations"))]
pub struct HttpServerConfig {
pub dynamic_config: HttpServerDynamicConfig,
pub static_config: HttpServerStaticConfig,
Expand Down Expand Up @@ -51,8 +52,6 @@ impl HttpServerConfig {
pub struct HttpServerStaticConfig {
pub ip: IpAddr,
pub port: u16,
// TODO(Arni): add a validation that this size is bigger than
// http_server_config.dynamic_config.max_sierra_program_size.
pub max_request_body_size: usize,
#[serde(deserialize_with = "deserialize_milliseconds_to_duration")]
pub dynamic_config_poll_interval: Duration,
Expand Down Expand Up @@ -122,3 +121,16 @@ impl Default for HttpServerDynamicConfig {
Self { accept_new_txs: true, max_sierra_program_size: DEFAULT_MAX_SIERRA_PROGRAM_SIZE }
}
}

fn max_size_validations(http_server_config: &HttpServerConfig) -> Result<(), ValidationError> {
let max_request_body_size = http_server_config.static_config.max_request_body_size;
let max_sierra_program_size = http_server_config.dynamic_config.max_sierra_program_size;
// This validation is not strict enough, as it does not account for the overhead of the other
// fields that appear in the request body.
if max_request_body_size <= max_sierra_program_size {
return Err(ValidationError::new(
"max_request_body_size must be greater than max_sierra_program_size",
));
}
Ok(())
}
21 changes: 21 additions & 0 deletions crates/apollo_http_server_config/src/config_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use validator::Validate;

use crate::config::{HttpServerConfig, HttpServerDynamicConfig, HttpServerStaticConfig};

#[test]
fn validate_config() {
let config = HttpServerConfig {
dynamic_config: HttpServerDynamicConfig {
max_sierra_program_size: 3,
..Default::default()
},
static_config: HttpServerStaticConfig { max_request_body_size: 2, ..Default::default() },
};

let error = config.validate().unwrap_err();
assert!(
error
.to_string()
.contains("max_request_body_size must be greater than max_sierra_program_size")
);
}
2 changes: 2 additions & 0 deletions crates/apollo_http_server_config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pub mod config;
#[cfg(test)]
mod config_test;
Loading