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:
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import com.microsoft.regorus.Engine;
|
||||
import com.microsoft.regorus.PolicyLengthConfig;
|
||||
import com.microsoft.regorus.PolicyModule;
|
||||
import com.microsoft.regorus.Program;
|
||||
import com.microsoft.regorus.Rvm;
|
||||
@@ -26,6 +27,9 @@ public class Test {
|
||||
// Enable coverage.
|
||||
engine.setEnableCoverage(true);
|
||||
|
||||
// Raise the default col limit to 2000
|
||||
engine.setPolicyLengthConfig(new PolicyLengthConfig(2000, 1048576, 20000));
|
||||
|
||||
// Evaluate rule.
|
||||
String valueJson = engine.evalRule("data.test.message");
|
||||
System.out.println(valueJson);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Licensed under the MIT License.
|
||||
|
||||
use anyhow::Result;
|
||||
use core::num::{NonZeroU32, NonZeroUsize};
|
||||
use jni::objects::{JBooleanArray, JByteArray, JClass, JObject, JObjectArray, JString};
|
||||
use jni::sys::{jboolean, jbooleanArray, jbyteArray, jlong, jobjectArray, jstring};
|
||||
use jni::JNIEnv;
|
||||
@@ -365,6 +366,39 @@ pub extern "system" fn Java_com_microsoft_regorus_Engine_getAstAsJson(
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Engine_nativeSetPolicyLengthConfig(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
engine_ptr: jlong,
|
||||
max_col: u32,
|
||||
max_file_bytes: jlong,
|
||||
max_lines: jlong,
|
||||
) {
|
||||
let _ = throw_err(env, |_env| {
|
||||
let engine = unsafe { &mut *(engine_ptr as *mut Engine) };
|
||||
engine.set_policy_length_config(regorus::PolicyLengthConfig {
|
||||
max_col: NonZeroU32::new(max_col)
|
||||
.ok_or_else(|| anyhow::anyhow!("maxCol must be non-zero"))?,
|
||||
max_file_bytes: NonZeroUsize::new(max_file_bytes as usize)
|
||||
.ok_or_else(|| anyhow::anyhow!("maxFileBytes must be non-zero"))?,
|
||||
max_lines: NonZeroUsize::new(max_lines as usize)
|
||||
.ok_or_else(|| anyhow::anyhow!("maxLines must be non-zero"))?,
|
||||
});
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Engine_nativeClearPolicyLengthConfig(
|
||||
_env: JNIEnv,
|
||||
_class: JClass,
|
||||
engine_ptr: jlong,
|
||||
) {
|
||||
let engine = unsafe { &mut *(engine_ptr as *mut Engine) };
|
||||
engine.clear_policy_length_config();
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Engine_nativeDestroyEngine(
|
||||
_env: JNIEnv,
|
||||
|
||||
@@ -39,6 +39,8 @@ public class Engine implements AutoCloseable, Cloneable {
|
||||
private static native void nativeClearCoverageData(long enginePtr);
|
||||
private static native void nativeSetGatherPrints(long enginePtr, boolean b);
|
||||
private static native String nativeTakePrints(long enginePtr);
|
||||
private static native void nativeSetPolicyLengthConfig(long enginePtr, int maxCol, long maxFileBytes, long maxLines);
|
||||
private static native void nativeClearPolicyLengthConfig(long enginePtr);
|
||||
private static native void nativeDestroyEngine(long enginePtr);
|
||||
|
||||
// Pointer to Engine allocated on Rust's heap, all native methods works on
|
||||
@@ -259,6 +261,22 @@ public class Engine implements AutoCloseable, Cloneable {
|
||||
return nativeTakePrints(enginePtr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the policy length limits used when loading policies.
|
||||
*
|
||||
* @param config Policy length configuration.
|
||||
*/
|
||||
public void setPolicyLengthConfig(PolicyLengthConfig config) {
|
||||
nativeSetPolicyLengthConfig(enginePtr, config.maxCol, config.maxFileBytes, config.maxLines);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the policy length configuration, reverting to defaults.
|
||||
*/
|
||||
public void clearPolicyLengthConfig() {
|
||||
nativeClearPolicyLengthConfig(enginePtr);
|
||||
}
|
||||
|
||||
long getPtr() {
|
||||
return enginePtr;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
**/
|
||||
|
||||
package com.microsoft.regorus;
|
||||
|
||||
/**
|
||||
* Policy source length limits enforced when loading policy files.
|
||||
*
|
||||
* All values must be positive (non-zero).
|
||||
*/
|
||||
public final class PolicyLengthConfig {
|
||||
/**
|
||||
* Maximum column width per line (default: 1024).
|
||||
*/
|
||||
public final int maxCol;
|
||||
|
||||
/**
|
||||
* Maximum policy file size in bytes (default: 1 MiB).
|
||||
*/
|
||||
public final long maxFileBytes;
|
||||
|
||||
/**
|
||||
* Maximum number of lines per policy file (default: 20000).
|
||||
*/
|
||||
public final long maxLines;
|
||||
|
||||
/**
|
||||
* Create a new policy length configuration.
|
||||
*
|
||||
* @param maxCol Maximum column width per line.
|
||||
* @param maxFileBytes Maximum policy file size in bytes.
|
||||
* @param maxLines Maximum number of lines per policy file.
|
||||
*/
|
||||
public PolicyLengthConfig(int maxCol, long maxFileBytes, long maxLines) {
|
||||
if (maxCol <= 0) {
|
||||
throw new IllegalArgumentException("maxCol must be positive");
|
||||
}
|
||||
if (maxFileBytes <= 0) {
|
||||
throw new IllegalArgumentException("maxFileBytes must be positive");
|
||||
}
|
||||
if (maxLines <= 0) {
|
||||
throw new IllegalArgumentException("maxLines must be positive");
|
||||
}
|
||||
this.maxCol = maxCol;
|
||||
this.maxFileBytes = maxFileBytes;
|
||||
this.maxLines = maxLines;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user