Skip to content

Commit 3ea3461

Browse files
authored
Merge pull request parallaxsecond#233 from joechrisellis/add_buffer_size_limit
Add `buffer_size_limit` config option for providers
2 parents f957949 + 7d6c148 commit 3ea3461

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

config.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
# such as key names or policies
3232
#log_error_details = false
3333

34+
# Decide how large (in bytes) buffers inside responses from this provider can be. Requests that ask
35+
# for buffers larger than this threshold will be rejected. Defaults to 1MB.
36+
#buffer_size_limit = 1048576
37+
3438
# (Required) Configuration for the service IPC listener component.
3539
[listener]
3640
# (Required) Type of IPC that the service will support.

src/utils/global_config.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
// Copyright 2020 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
3-
use std::sync::atomic::AtomicBool;
3+
use crate::utils::service_builder::DEFAULT_BUFFER_SIZE_LIMIT;
44
use std::sync::atomic::Ordering;
5+
use std::sync::atomic::{AtomicBool, AtomicUsize};
56

67
/// Configuration values that affect most or all the
78
/// components of the service.
89
#[derive(Default, Debug)]
910
pub struct GlobalConfig {
1011
log_error_details: AtomicBool,
12+
buffer_size_limit: AtomicUsize,
1113
}
1214

1315
impl GlobalConfig {
1416
const fn new() -> Self {
1517
GlobalConfig {
1618
log_error_details: AtomicBool::new(false),
19+
buffer_size_limit: AtomicUsize::new(DEFAULT_BUFFER_SIZE_LIMIT), // 1 MB
1720
}
1821
}
1922

@@ -22,18 +25,26 @@ impl GlobalConfig {
2225
pub fn log_error_details() -> bool {
2326
GLOBAL_CONFIG.log_error_details.load(Ordering::Relaxed)
2427
}
28+
29+
/// Fetch the size limit for buffers within responses (in bytes).
30+
/// information about the error
31+
pub fn buffer_size_limit() -> usize {
32+
GLOBAL_CONFIG.buffer_size_limit.load(Ordering::Relaxed)
33+
}
2534
}
2635

2736
static GLOBAL_CONFIG: GlobalConfig = GlobalConfig::new();
2837

2938
pub(super) struct GlobalConfigBuilder {
3039
log_error_details: bool,
40+
buffer_size_limit: Option<usize>,
3141
}
3242

3343
impl GlobalConfigBuilder {
3444
pub fn new() -> Self {
3545
GlobalConfigBuilder {
3646
log_error_details: false,
47+
buffer_size_limit: None,
3748
}
3849
}
3950

@@ -43,9 +54,19 @@ impl GlobalConfigBuilder {
4354
self
4455
}
4556

57+
pub fn with_buffer_size_limit(mut self, buffer_size_limit: usize) -> Self {
58+
self.buffer_size_limit = Some(buffer_size_limit);
59+
60+
self
61+
}
62+
4663
pub fn build(self) {
4764
GLOBAL_CONFIG
4865
.log_error_details
4966
.store(self.log_error_details, Ordering::Relaxed);
67+
GLOBAL_CONFIG.buffer_size_limit.store(
68+
self.buffer_size_limit.unwrap_or(DEFAULT_BUFFER_SIZE_LIMIT),
69+
Ordering::Relaxed,
70+
);
5071
}
5172
}

src/utils/service_builder.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ const WIRE_PROTOCOL_VERSION_MAJOR: u8 = 1;
5353
/// Default value for the limit on the request body size (in bytes) - equal to 1MB
5454
const DEFAULT_BODY_LEN_LIMIT: usize = 1 << 20;
5555

56+
/// Default value for the limit on the buffer size for response (in bytes) - equal to 1MB
57+
pub const DEFAULT_BUFFER_SIZE_LIMIT: usize = 1 << 20;
58+
5659
type KeyInfoManager = Arc<RwLock<dyn ManageKeyInfo + Send + Sync>>;
5760
type Provider = Arc<dyn Provide + Send + Sync>;
5861
type Authenticator = Box<dyn Authenticate + Send + Sync>;
@@ -70,6 +73,7 @@ pub struct CoreSettings {
7073
pub body_len_limit: Option<usize>,
7174
pub log_error_details: Option<bool>,
7275
pub allow_root: Option<bool>,
76+
pub buffer_size_limit: Option<usize>,
7377
}
7478

7579
/// Configuration of Parsec
@@ -104,6 +108,12 @@ impl ServiceBuilder {
104108
pub fn build_service(config: &ServiceConfig) -> Result<FrontEndHandler> {
105109
GlobalConfigBuilder::new()
106110
.with_log_error_details(config.core_settings.log_error_details.unwrap_or(false))
111+
.with_buffer_size_limit(
112+
config
113+
.core_settings
114+
.buffer_size_limit
115+
.unwrap_or(DEFAULT_BUFFER_SIZE_LIMIT),
116+
)
107117
.build();
108118

109119
let key_info_managers =

0 commit comments

Comments
 (0)