Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Criteria String Parsing and Untrusted Input

    • 5 minutes to read

    DevExpress controls and libraries can use and store filter expressions (see Criteria Language Syntax). When an application parses a criteria string, the parser builds a CriteriaOperator tree. The application then processes this tree (for instance, compiles it, converts it to a LINQ, Entity Framework, or OData expression, or calls ToString, Clone, Equals, or GetHashCode methods).

    A maliciously constructed criteria string can specify an extremely large or deeply nested expression. For this reason, DevExpress libraries impose structural limits on parsed criteria strings and protect recursive processing of expression trees against stack overflow. These measures help our libraries avoid the following issues:

    • Significant CPU time and stack space consumption
    • Possible denial of service (the application becomes unresponsive or terminates unexpectedly)
    • User requests that affect other users in multi-user and server applications (such as Blazor Server or ASP.NET Core)

    Untrusted Criteria Sources

    Treat a criteria string as untrusted input if a user or client can create or modify it. Common sources include the following:

    • Filter criteria restored from persisted layouts (for instance, grid or filter layouts saved by a user or stored in a shared location).
    • Filter expressions entered in the Filter Builder/Filter Editor text editor.
    • Filter strings that clients send to the server and that the application passes to the CriteriaOperator.Parse method or other DevExpress APIs that accept criteria strings.

    This threat does not affect applications that build criteria from fixed strings or in code and do not parse user-controlled strings.

    Criteria Parser Limits

    The criteria parser rejects a string that exceeds any of the following limits. All limits are static properties of the CriteriaOperator class:

    Property Default Value Description
    Max⁠Parse⁠Depth 1024 (Default⁠Max⁠ParseDepth) The maximum depth of the criteria tree. Properties, constants, and parameters have depth 0. Each operator or function call adds one level. Parentheses and chains of the same logical operator ([A] = 1 And [B] = 2 And [C] = 3) do not add levels.
    Max⁠Parse⁠Operands 16384 (Default⁠Max⁠Parse⁠Operands) The maximum number of operands in a single And/Or group, In (...) list, Iif argument list, or function argument list. The parser counts nested groups and lists separately.
    Max⁠Parse⁠Tokens 262144 (Default⁠Max⁠Parse⁠Tokens) The maximum number of lexical tokens (names, numbers, strings, operators, brackets, and commas) in a criteria string. For instance, [A] = 1 consists of 3 tokens.

    Limits apply to the entire process. We recommend that you set limits once at application startup, before the application parses criteria strings. If necessary, you can change limit values during the application run, as the parser reads limit values each time it parses a string. You can set a property to zero or a negative value to disable the corresponding limit.

    Default values do not affect realistic filters. If your application accepts only short and simple filters, lower the limits: the smaller the allowed input, the lower the cost of processing it.

    Warning

    Raise or disable parser limits only after you verify that your application can safely process larger input. The default depth limit is designed to fit a standard thread stack. If you raise the MaxParseDepth value, verify the stack size of all threads that process criteria.

    Handle Limit Violations

    If a criteria string exceeds a limit, the parser throws a CriteriaParserLimitException. The CriteriaParserLimitException.Kind property identifies the exceeded limit: Depth, Operands, or Tokens.

    CriteriaParserLimitException derives from CriteriaParserException. Existing parser error handlers also handle limit violations.

    The following code snippet tightens parser limits and rejects criteria strings that exceed them:

    using DevExpress.Data.Filtering;
    using DevExpress.Data.Filtering.Exceptions;
    
    // Call at application startup.
    CriteriaOperator.MaxParseDepth = 128;
    CriteriaOperator.MaxParseOperands = 1000;
    CriteriaOperator.MaxParseTokens = 10000;
    
    // ...
    
    try {
        CriteriaOperator criteria = CriteriaOperator.Parse(userInput);
        // Use the criteria.
    }
    catch(CriteriaParserLimitException e) {
        // e.Kind returns CriteriaParserLimitKind.Depth, Operands, or Tokens.
        // The input exceeds a limit. Reject it.
    }
    catch(CriteriaParserException) {
        // The input is not a valid criteria string. Reject it.
    }
    

    Stack Overflow Protection

    Parser limits apply only to criteria strings. Criteria trees that you build in code are not subject to these limits.

    Code that processes criteria trees (compilation, conversion to LINQ, Entity Framework, or OData expressions, and the ToString, Clone, Equals, and GetHashCode methods) checks available stack space. If a tree is too deep to process on the current thread, the code throws an exception before the stack runs out (System.InsufficientExecutionStackException). You can catch this exception, unlike a StackOverflowException, and the process continues to run.

    If your application builds criteria trees in code from external data, catch InsufficientExecutionStackException where the application processes these trees.

    Best Practices

    • Always update to the most recent version of DevExpress libraries.
    • Treat layout files, saved filters, and criteria strings from users or clients as untrusted input. Allow only trusted parties to modify persisted layouts that other users load.
    • Lower parser limits if your application accepts only short and simple filters.
    • Raise or disable parser limits only after you verify that your application can safely process larger input.
    • Catch CriteriaParserException when you parse criteria strings from external sources, and treat the input as invalid.

    See Also