Skip to content

Commit ac2171e

Browse files
committed
fixup! feat: add request body size configuration
abort startup if request size > maximum
1 parent c46f38b commit ac2171e

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

rust/server/src/main.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ fn main() {
3737
eprintln!("Failed to load configuration: {}", e);
3838
std::process::exit(-1);
3939
});
40+
let vss_service_config = match &config.maximum_request_body_size {
41+
Some(size) => match VssServiceConfig::new(*size) {
42+
Ok(config) => Arc::new(config),
43+
Err(e) => {
44+
eprintln!("Configuration validation error: {}", e);
45+
return;
46+
},
47+
},
48+
None => Arc::new(VssServiceConfig::default()),
49+
};
4050

4151
let runtime = match tokio::runtime::Builder::new_multi_thread().enable_all().build() {
4252
Ok(runtime) => Arc::new(runtime),
@@ -132,10 +142,7 @@ fn main() {
132142
match res {
133143
Ok((stream, _)) => {
134144
let io_stream = TokioIo::new(stream);
135-
let vss_service_config = if let Some(req_body_size) = &config.maximum_request_body_size {
136-
VssServiceConfig::new(*req_body_size)
137-
} else {VssServiceConfig::default()};
138-
let vss_service = VssService::new(Arc::clone(&store), Arc::clone(&authorizer), vss_service_config);
145+
let vss_service = VssService::new(Arc::clone(&store), Arc::clone(&authorizer), Arc::clone(&vss_service_config));
139146
runtime.spawn(async move {
140147
if let Err(err) = http1::Builder::new().serve_connection(io_stream, vss_service).await {
141148
eprintln!("Failed to serve connection: {}", err);

rust/server/src/vss_service.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@ pub(crate) struct VssServiceConfig {
2626
}
2727

2828
impl VssServiceConfig {
29-
pub fn new(maximum_request_body_size: usize) -> Self {
30-
Self { maximum_request_body_size: maximum_request_body_size.min(MAXIMUM_REQUEST_BODY_SIZE) }
29+
pub fn new(maximum_request_body_size: usize) -> Result<Self, String> {
30+
if maximum_request_body_size > MAXIMUM_REQUEST_BODY_SIZE {
31+
return Err(format!(
32+
"Request body size {} exceeds maximum {}",
33+
maximum_request_body_size, MAXIMUM_REQUEST_BODY_SIZE
34+
));
35+
}
36+
37+
Ok(Self { maximum_request_body_size })
3138
}
3239
}
3340

@@ -41,12 +48,12 @@ impl Default for VssServiceConfig {
4148
pub struct VssService {
4249
store: Arc<dyn KvStore>,
4350
authorizer: Arc<dyn Authorizer>,
44-
config: VssServiceConfig,
51+
config: Arc<VssServiceConfig>,
4552
}
4653

4754
impl VssService {
4855
pub(crate) fn new(
49-
store: Arc<dyn KvStore>, authorizer: Arc<dyn Authorizer>, config: VssServiceConfig,
56+
store: Arc<dyn KvStore>, authorizer: Arc<dyn Authorizer>, config: Arc<VssServiceConfig>,
5057
) -> Self {
5158
Self { store, authorizer, config }
5259
}

0 commit comments

Comments
 (0)