Type security model
Rule expressions are strings that come from a rules store, so they must not be able to call arbitrary code. RulesEngine (since 5.0.0) therefore lets an expression call methods and static members only on registered types.
What an expression can use
| Category | Available? |
|---|---|
Operators, literals, conditional ? :, lambdas and LINQ over inputs |
Yes |
| Properties and fields of input objects | Yes |
| Methods on a type that is registered (see below) | Yes |
The predefined System.Linq.Dynamic.Core types (such as string, Math, Convert, DateTime, Guid) |
Yes |
| Methods and static members of any other type | No (the expression fails to parse) |
How a type becomes registered
The set of registered types is built by CustomTypeProvider:
- Types you list in
ReSettings.CustomTypes. - The types of the inputs of the call, when
AutoRegisterInputTypeistrue(the default). Generic arguments, array element types and nullable underlying types are included. ExpressionUtils(which providesCheckContains),EnumerableandIEnumerable.- Every base type and interface of the types above.
- When
EnableAssemblyScanningistrue(the default), every type in the loaded assemblies that is marked with[DynamicLinqType].
var settings = new ReSettings
{
CustomTypes = new[] { typeof(MyHelpers) }, // static helpers callable from expressions
EnableAssemblyScanning = false // do not scan the AppDomain for [DynamicLinqType]
};
var engine = new RulesEngine.RulesEngine(workflows, settings);
{ "RuleName": "Allowed", "Expression": "MyHelpers.CheckContains(input1.country, \"india,usa\")" }
Recommendations
- Register the smallest set of types that your rules need, and prefer static helper classes that expose only safe operations.
- Treat rule authors as trusted only as far as the registered types allow. Never register types that can reach the file system, network or process APIs.
- Set
EnableAssemblyScanning = falsewhen you do not use[DynamicLinqType]. It removes an AppDomain scan and makes the set of callable types explicit. - Expression case-sensitivity (
IsExpressionCaseSensitive) does not change which types are allowed.