mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
feat: make policy length limits configurable per engine (#624)
- Add PolicyLengthConfig struct with max_col, max_file_bytes, and max_lines fields, replacing hardcoded constants in the lexer. - Add Engine::set_policy_length_config and clear_policy_length_config to allow callers to override the default limits. - Add Source::from_contents_with_limits and from_file_with_limits for direct Source construction with custom limits; existing from_contents and from_file signatures are preserved using defaults. - Add tests for default rejection, custom limits, and engine plumbing. - Add bindings for C, C++, Python, WASM/JS, Java, Ruby, C#, Go
This commit is contained in:
@@ -6,6 +6,7 @@ use crate::common::{
|
||||
};
|
||||
use crate::compiled_policy::RegorusCompiledPolicy;
|
||||
use crate::limits::RegorusExecutionTimerConfig;
|
||||
use crate::limits::RegorusPolicyLengthConfig;
|
||||
use crate::lock::{new_handle, read, try_read, try_write, Handle, ReadGuard, WriteGuard};
|
||||
use crate::panic_guard::with_unwind_guard;
|
||||
use alloc::boxed::Box;
|
||||
@@ -491,6 +492,37 @@ pub extern "C" fn regorus_engine_clear_execution_timer_config(
|
||||
}())
|
||||
}
|
||||
|
||||
/// Set the policy length limits used when loading policies.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_engine_set_policy_length_config(
|
||||
engine: *mut RegorusEngine,
|
||||
config: RegorusPolicyLengthConfig,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.set_policy_length_config(config.to_policy_length_config()?);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Clear the policy length configuration, reverting to defaults.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_engine_clear_policy_length_config(
|
||||
engine: *mut RegorusEngine,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.clear_policy_length_config();
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Get pretty printed coverage report.
|
||||
///
|
||||
/// See https://docs.rs/regorus/latest/regorus/coverage/struct.Report.html#method.to_string_pretty
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
use crate::common::{to_regorus_result, RegorusResult, RegorusStatus};
|
||||
use alloc::format;
|
||||
use anyhow::{anyhow, Result};
|
||||
use core::num::NonZeroU32;
|
||||
use core::num::{NonZeroU32, NonZeroUsize};
|
||||
use core::time::Duration;
|
||||
use regorus::utils::limits::{self, ExecutionTimerConfig};
|
||||
|
||||
@@ -158,6 +158,31 @@ impl RegorusExecutionTimerConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/// FFI representation of [`regorus::PolicyLengthConfig`].
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct RegorusPolicyLengthConfig {
|
||||
/// Maximum column width per line (must be non-zero).
|
||||
pub max_col: u32,
|
||||
/// Maximum policy file size in bytes (must be non-zero).
|
||||
pub max_file_bytes: usize,
|
||||
/// Maximum number of lines per policy file (must be non-zero).
|
||||
pub max_lines: usize,
|
||||
}
|
||||
|
||||
impl RegorusPolicyLengthConfig {
|
||||
pub fn to_policy_length_config(self) -> Result<regorus::PolicyLengthConfig> {
|
||||
Ok(regorus::PolicyLengthConfig {
|
||||
max_col: NonZeroU32::new(self.max_col)
|
||||
.ok_or_else(|| anyhow!("max_col must be non-zero"))?,
|
||||
max_file_bytes: NonZeroUsize::new(self.max_file_bytes)
|
||||
.ok_or_else(|| anyhow!("max_file_bytes must be non-zero"))?,
|
||||
max_lines: NonZeroUsize::new(self.max_lines)
|
||||
.ok_or_else(|| anyhow!("max_lines must be non-zero"))?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_set_fallback_execution_timer_config(
|
||||
config: RegorusExecutionTimerConfig,
|
||||
|
||||
Reference in New Issue
Block a user