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:
@@ -3,6 +3,7 @@
|
||||
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use core::num::{NonZeroU32, NonZeroUsize};
|
||||
use regorus::languages::rego::compiler::Compiler;
|
||||
use regorus::rvm::program::{
|
||||
generate_assembly_listing, generate_tabular_assembly_listing, AssemblyListingConfig,
|
||||
@@ -26,6 +27,14 @@ struct ModuleSpec {
|
||||
content: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct PolicyLengthSpec {
|
||||
max_col: u32,
|
||||
max_file_bytes: usize,
|
||||
max_lines: usize,
|
||||
}
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub struct Program {
|
||||
program: Arc<RvmProgram>,
|
||||
@@ -183,6 +192,29 @@ impl Engine {
|
||||
self.engine.set_gather_prints(b)
|
||||
}
|
||||
|
||||
/// Set the policy length limits used when loading policies.
|
||||
///
|
||||
/// Accepts a JS object: `{ maxCol, maxFileBytes, maxLines }`.
|
||||
pub fn setPolicyLengthConfig(&mut self, config: JsValue) -> Result<(), JsValue> {
|
||||
let spec: PolicyLengthSpec =
|
||||
serde_wasm_bindgen::from_value(config).map_err(error_to_jsvalue)?;
|
||||
self.engine
|
||||
.set_policy_length_config(regorus::PolicyLengthConfig {
|
||||
max_col: NonZeroU32::new(spec.max_col)
|
||||
.ok_or_else(|| JsValue::from_str("maxCol must be non-zero"))?,
|
||||
max_file_bytes: NonZeroUsize::new(spec.max_file_bytes)
|
||||
.ok_or_else(|| JsValue::from_str("maxFileBytes must be non-zero"))?,
|
||||
max_lines: NonZeroUsize::new(spec.max_lines)
|
||||
.ok_or_else(|| JsValue::from_str("maxLines must be non-zero"))?,
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Clear the policy length configuration, reverting to defaults.
|
||||
pub fn clearPolicyLengthConfig(&mut self) {
|
||||
self.engine.clear_policy_length_config();
|
||||
}
|
||||
|
||||
/// Take the gathered output of print statements.
|
||||
///
|
||||
/// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.take_prints
|
||||
|
||||
Reference in New Issue
Block a user