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:
Generated
+12
@@ -941,6 +941,7 @@ dependencies = [
|
||||
"getrandom 0.3.4",
|
||||
"regorus",
|
||||
"serde",
|
||||
"serde-wasm-bindgen",
|
||||
"serde_json",
|
||||
"uuid",
|
||||
"wasm-bindgen",
|
||||
@@ -990,6 +991,17 @@ dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde-wasm-bindgen"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8302e169f0eddcc139c70f139d19d6467353af16f9fce27e8c30158036a1e16b"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
|
||||
@@ -42,6 +42,7 @@ regorus = { path = "../..", default-features = false, features = ["arc", "rvm"]
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0.140"
|
||||
wasm-bindgen = "0.2.100"
|
||||
serde-wasm-bindgen = "0.6"
|
||||
# Specify uuid as a mandatory dependency so as to enable `js` feature which is now required
|
||||
# when targeting wasm32-unknown-unknown.
|
||||
uuid = { version = "1.15.1", default-features = false, features = ["v4", "fast-rng", "js"]}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -9,6 +9,9 @@ var engine = new regorus.Engine();
|
||||
// Enable code coverage
|
||||
engine.setEnableCoverage(true);
|
||||
|
||||
// Raise the default col limit to 2000
|
||||
engine.setPolicyLengthConfig({ maxCol: 2000, maxFileBytes: 1048576, maxLines: 20000 });
|
||||
|
||||
// Add Rego policy.
|
||||
var pkg = engine.addPolicy(
|
||||
// Associate this file name with policy
|
||||
|
||||
Reference in New Issue
Block a user