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:
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
use anyhow::{anyhow, Result};
|
||||
use core::num::{NonZeroU32, NonZeroUsize};
|
||||
use pyo3::exceptions::PyTypeError;
|
||||
use pyo3::prelude::*;
|
||||
use pyo3::types::*;
|
||||
@@ -392,6 +393,35 @@ impl Engine {
|
||||
.add_extension(path, nargs, Box::new(extension_impl))
|
||||
}
|
||||
|
||||
/// Set the policy length limits used when loading policies.
|
||||
///
|
||||
/// * `max_col`: Maximum column width per line.
|
||||
/// * `max_file_bytes`: Maximum policy file size in bytes.
|
||||
/// * `max_lines`: Maximum number of lines per policy file.
|
||||
#[pyo3(signature = (*, max_col, max_file_bytes, max_lines))]
|
||||
pub fn set_policy_length_config(
|
||||
&mut self,
|
||||
max_col: u32,
|
||||
max_file_bytes: usize,
|
||||
max_lines: usize,
|
||||
) -> Result<()> {
|
||||
self.engine
|
||||
.set_policy_length_config(::regorus::PolicyLengthConfig {
|
||||
max_col: NonZeroU32::new(max_col)
|
||||
.ok_or_else(|| anyhow!("max_col must be non-zero"))?,
|
||||
max_file_bytes: NonZeroUsize::new(max_file_bytes)
|
||||
.ok_or_else(|| anyhow!("max_file_bytes must be non-zero"))?,
|
||||
max_lines: NonZeroUsize::new(max_lines)
|
||||
.ok_or_else(|| anyhow!("max_lines must be non-zero"))?,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Clear the policy length configuration, reverting to defaults.
|
||||
pub fn clear_policy_length_config(&mut self) {
|
||||
self.engine.clear_policy_length_config();
|
||||
}
|
||||
|
||||
/// Enable code coverage
|
||||
///
|
||||
/// * `enable`: Whether to enable coverage or not.
|
||||
|
||||
@@ -10,6 +10,8 @@ if hasattr(sys.stdout, "reconfigure"):
|
||||
engine = regorus.Engine()
|
||||
|
||||
engine.set_rego_v0(True)
|
||||
# Raise the default col limit to 2000
|
||||
engine.set_policy_length_config(max_col=2000, max_file_bytes=1048576, max_lines=20000)
|
||||
|
||||
# Load policies
|
||||
pkg = engine.add_policy_from_file('../../tests/aci/framework.rego')
|
||||
|
||||
Reference in New Issue
Block a user