Skip to content

Commit 707b7e5

Browse files
committed
apollo_http_server: add dynamic config size validation
1 parent a9eb32e commit 707b7e5

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

crates/apollo_http_server/src/http_server.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ use apollo_gateway_types::gateway_types::{
1616
GatewayOutput,
1717
SUPPORTED_TRANSACTION_VERSIONS,
1818
};
19-
use apollo_http_server_config::config::{HttpServerConfig, HttpServerDynamicConfig};
19+
use apollo_http_server_config::config::{
20+
validate_dynamic_config_bounds,
21+
HttpServerConfig,
22+
HttpServerDynamicConfig,
23+
};
2024
use apollo_infra::component_definitions::ComponentStarter;
2125
use apollo_infra_utils::type_name::short_type_name;
2226
use apollo_proc_macros::sequencer_latency_histogram;
@@ -105,6 +109,7 @@ impl HttpServer {
105109
self.dynamic_config_tx.clone(),
106110
self.config_manager_client.clone(),
107111
self.config.static_config.dynamic_config_poll_interval,
112+
self.config.static_config.max_request_body_size,
108113
));
109114

110115
// TODO(Tsabary): update the http server struct to hold optional fields of the
@@ -354,13 +359,18 @@ async fn dynamic_config_poll(
354359
tx: Sender<HttpServerDynamicConfig>,
355360
config_manager_client: SharedConfigManagerClient,
356361
poll_interval: Duration,
362+
max_request_body_size: usize,
357363
) {
358364
let mut interval = time::interval(poll_interval);
359365
loop {
360366
interval.tick().await;
361367
let dynamic_config_result = config_manager_client.get_http_server_dynamic_config().await;
362368
// Make the config available if it was successfully updated.
363369
if let Ok(dynamic_config) = dynamic_config_result {
370+
let _ = validate_dynamic_config_bounds(&dynamic_config, max_request_body_size)
371+
.inspect_err(|err| {
372+
warn!("Found invalid config when updating the dynamic config: {err}")
373+
});
364374
let _ = tx.send(dynamic_config);
365375
}
366376
}

crates/apollo_http_server_config/src/config.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use apollo_config::converters::deserialize_milliseconds_to_duration;
66
use apollo_config::dumping::{prepend_sub_config_name, ser_param, SerializeConfig};
77
use apollo_config::{ParamPath, ParamPrivacyInput, SerializedParam};
88
use serde::{Deserialize, Serialize};
9-
use validator::Validate;
9+
use validator::{Validate, ValidationError};
1010

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

1818
/// The http server connection related configuration.
1919
#[derive(Clone, Debug, Default, Serialize, Deserialize, Validate, PartialEq)]
20+
#[validate(schema(function = "max_size_validations"))]
2021
pub struct HttpServerConfig {
2122
pub dynamic_config: HttpServerDynamicConfig,
2223
pub static_config: HttpServerStaticConfig,
@@ -51,8 +52,6 @@ impl HttpServerConfig {
5152
pub struct HttpServerStaticConfig {
5253
pub ip: IpAddr,
5354
pub port: u16,
54-
// TODO(Arni): add a validation that this size is bigger than
55-
// http_server_config.dynamic_config.max_sierra_program_size.
5655
pub max_request_body_size: usize,
5756
#[serde(deserialize_with = "deserialize_milliseconds_to_duration")]
5857
pub dynamic_config_poll_interval: Duration,
@@ -122,3 +121,20 @@ impl Default for HttpServerDynamicConfig {
122121
Self { accept_new_txs: true, max_sierra_program_size: DEFAULT_MAX_SIERRA_PROGRAM_SIZE }
123122
}
124123
}
124+
125+
fn max_size_validations(http_server_config: &HttpServerConfig) -> Result<(), ValidationError> {
126+
let max_request_body_size = http_server_config.static_config.max_request_body_size;
127+
validate_dynamic_config_bounds(&http_server_config.dynamic_config, max_request_body_size)
128+
}
129+
130+
pub fn validate_dynamic_config_bounds(
131+
dynamic_config: &HttpServerDynamicConfig,
132+
max_request_body_size: usize,
133+
) -> Result<(), ValidationError> {
134+
if max_request_body_size < dynamic_config.max_sierra_program_size {
135+
return Err(ValidationError::new(
136+
"max_request_body_size must be greater than max_sierra_program_size",
137+
));
138+
}
139+
Ok(())
140+
}

0 commit comments

Comments
 (0)