Skip to content

Commit

Permalink
Add buffer_size_limit config option for providers
Browse files Browse the repository at this point in the history
The buffer size limit can be used to cap the maximum allowed buffer size
sent in a response. Requests that ask for buffers larger than this value
will be automatically rejected.

Signed-off-by: Joe Ellis <[email protected]>
  • Loading branch information
Joe Ellis committed Aug 26, 2020
1 parent f3b12da commit 849f375
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
# such as key names or policies
#log_error_details = false

# Decide how large (in bytes) buffers inside responses from this provider can be. Requests that ask
# for buffers larger than this threshold will be rejected. Defaults to 1MB.
#buffer_size_limit = 1048576

# (Required) Configuration for the service IPC listener component.
[listener]
# (Required) Type of IPC that the service will support.
Expand Down
21 changes: 20 additions & 1 deletion src/utils/global_config.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// Copyright 2020 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::sync::atomic::{AtomicBool, AtomicUsize};

/// Configuration values that affect most or all the
/// components of the service.
#[derive(Default, Debug)]
pub struct GlobalConfig {
log_error_details: AtomicBool,
buffer_size_limit: AtomicUsize,
}

impl GlobalConfig {
const fn new() -> Self {
GlobalConfig {
log_error_details: AtomicBool::new(false),
buffer_size_limit: AtomicUsize::new(1 << 20), // 1 MB
}
}

Expand All @@ -22,18 +24,26 @@ impl GlobalConfig {
pub fn log_error_details() -> bool {
GLOBAL_CONFIG.log_error_details.load(Ordering::Relaxed)
}

/// Fetch the size limit for buffers within responses (in bytes).
/// information about the error
pub fn buffer_size_limit() -> usize {
GLOBAL_CONFIG.buffer_size_limit.load(Ordering::Relaxed)
}
}

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

pub(super) struct GlobalConfigBuilder {
log_error_details: bool,
buffer_size_limit: Option<usize>,
}

impl GlobalConfigBuilder {
pub fn new() -> Self {
GlobalConfigBuilder {
log_error_details: false,
buffer_size_limit: None,
}
}

Expand All @@ -43,9 +53,18 @@ impl GlobalConfigBuilder {
self
}

pub fn with_buffer_size_limit(mut self, buffer_size_limit: usize) -> Self {
self.buffer_size_limit = Some(buffer_size_limit);

self
}

pub fn build(self) {
GLOBAL_CONFIG
.log_error_details
.store(self.log_error_details, Ordering::Relaxed);
GLOBAL_CONFIG
.buffer_size_limit
.store(self.buffer_size_limit.unwrap_or(1 << 20), Ordering::Relaxed);
}
}
10 changes: 10 additions & 0 deletions src/utils/service_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const WIRE_PROTOCOL_VERSION_MAJOR: u8 = 1;
/// Default value for the limit on the request body size (in bytes) - equal to 1MB
const DEFAULT_BODY_LEN_LIMIT: usize = 1 << 19;

/// Default value for the limit on the buffer size for response (in bytes) - equal to 1MB
const DEFAULT_BUFFER_SIZE_LIMIT: usize = 1 << 20;

type KeyInfoManager = Arc<RwLock<dyn ManageKeyInfo + Send + Sync>>;
type Provider = Arc<dyn Provide + Send + Sync>;
type Authenticator = Box<dyn Authenticate + Send + Sync>;
Expand All @@ -66,6 +69,7 @@ pub struct CoreSettings {
pub body_len_limit: Option<usize>,
pub log_error_details: Option<bool>,
pub allow_root: Option<bool>,
pub buffer_size_limit: Option<usize>,
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -96,6 +100,12 @@ impl ServiceBuilder {
pub fn build_service(config: &ServiceConfig) -> Result<FrontEndHandler> {
GlobalConfigBuilder::new()
.with_log_error_details(config.core_settings.log_error_details.unwrap_or(false))
.with_buffer_size_limit(
config
.core_settings
.buffer_size_limit
.unwrap_or(DEFAULT_BUFFER_SIZE_LIMIT),
)
.build();

let key_info_managers =
Expand Down

0 comments on commit 849f375

Please sign in to comment.