AssemblyReferenceProvider
Controls which assemblies are available to compiled expressions. Used for sandboxing.
public class AssemblyReferenceProvider
Default Behavior
The default provider includes a safe whitelist of common assemblies (DefaultWhitelist) and excludes a set of known dangerous assemblies (KnownDangerousAssemblies). Matching is a case-insensitive substring (Contains) match against each loaded assembly’s name.
Included by default (DefaultWhitelist):
System.RuntimeSystem.Private.CoreLibmscorlibnetstandardSystem.CoreSystem.LinqSystem.Linq.ExpressionsSystem.CollectionsSystem.Text.JsonSystem.Text.RegularExpressionsSystem.ComponentModel.AnnotationsRoslynRulesMicrosoft.Extensions.Logging.AbstractionsMicrosoft.CodeAnalysisMicrosoft.CodeAnalysis.CSharpMicrosoft.CSharpMicrosoft.CodeAnalysis.CSharp.Scripting
Excluded (KnownDangerousAssemblies, always blocked):
System.IO— file system accessSystem.IO.FileSystem— file system accessSystem.Diagnostics.Process— process executionSystem.Net.Http— network accessSystem.Net.Sockets— network accessSystem.Net.Security— network accessSystem.Security.Cryptography— cryptographySystem.Reflection.Emit— runtime code generationSystem.Runtime.Loader— assembly loadingSystem.Data.SqlClient— database accessSystem.Data.OleDb— database accessSystem.Data.Odbc— database accessMicrosoft.Win32.Registry— registry access
Custom Provider
var provider = new AssemblyReferenceProvider();
provider.AllowAssembly("MyCompany.Models");
var del = compiler.Compile<Func<Customer, bool>>(
"customer.Name.Length > 0",
new[] { "customer" },
referenceProvider: provider
);
Security Note
Even with a whitelist, never compile expressions from untrusted sources without validation. See Security for full details.
Related
- ExpressionCompiler — Uses provider
- Security — Security guide