Skip to main content
All docs
V26.1
  • SVG Image Limits

    • 5 minutes to read

    DevExpress controls and libraries load and render SVG images in UI controls, reports, dashboards, and Office documents. SVG elements can refer to other elements:

    • <use> elements -fill, stroke, and clip-path attributes with values set to url(#id)

    A malicious SVG image can utilize such references. Possible effects include app termination with a stack overflow exception or CPU/memory overload for an extended period of time (denial of service).

    DevExpress libraries limit the size of SVG images and references between SVG elements, and detect reference cycles.

    Untrusted SVG Sources

    Treat an SVG image as untrusted input if a user or another external party can create or modify the image. Common sources include the following:

    • Images that users upload to a web application (for instance, to XAF image property editors).
    • SVG images in report layouts and SVG images bound to data in reports and dashboards.
    • SVG images in Word, Excel, and PowerPoint documents (DOCX, XLSX, PPTX).
    • SVG images that an application loads from files, streams, or databases.

    SVG Image Limits

    All limits are static properties of the SvgImageLimits class:

    Property Default Value Description
    Max⁠Element⁠Depth 64 (Default⁠Max⁠Element⁠Depth) The maximum nesting depth of elements in an SVG image. If an image exceeds this limit, the application does not load the image.
    Max⁠Element⁠Count 65,536 (Default⁠Max⁠Element⁠Count) The maximum number of elements in an SVG image. The limit counts an element (together with its child elements) again each time a <use> element, clip path, pattern fill, or pattern stroke refers to this element. If an image exceeds this limit, the application does not load the image.
    Max⁠Reference⁠Depth 16 (Default⁠Max⁠Reference⁠Depth) The maximum depth of nested references that the renderer follows. The renderer does not follow deeper references and renders the image partially.
    Max⁠Referenced⁠Element⁠Count 16,384 (Default⁠Max⁠Referenced⁠Element⁠Count) The maximum number of referenced elements that the renderer draws in one image. The renderer skips elements over the limit.
    Max⁠Referenced⁠Point⁠Count 131,072 (Default⁠Max⁠Referenced⁠Point⁠Count) The maximum number of points in referenced paths that the renderer draws in one image. The renderer skips paths over the limit.
    Max⁠Clip⁠Path⁠Referenced⁠Point⁠Count 8,192 (Default⁠Max⁠Clip⁠Path⁠Referenced⁠Point⁠Count) The maximum number of points in referenced shapes that the renderer adds to a clip path. The renderer does not add shapes over the limit to the clip path.

    The application does not load an image that contains a reference cycle (for instance, a <use> element that refers to its own parent).

    The renderer ignores <use> elements that refer to a clip path (the SVG specification does not allow these references).

    When an image exceeds a reference limit, the renderer draws the image partially and writes the following warning to trace listeners: “The SVG image is rendered partially: …”.

    Limits apply to the entire process. Set limits once at application startup, before the application loads SVG images. Set a property to 0 to disable the corresponding limit. If you set MaxElementCount to 0, you also disable the check for reference cycles.

    Warning

    Raise or disable SVG image limits only if all SVG images in your application come from trusted sources. If you raise or disable the MaxElementDepth limit, an image with deeply nested elements may terminate the application process with a StackOverflowException. If you raise or disable the MaxReferenceDepth limit, deeply nested references may exhaust the thread stack and cause an InsufficientExecutionStackException.

    Handle Limit Violations

    If an image exceeds the MaxElementDepth or MaxElementCount limit, or contains a reference cycle, the application throws an SvgImageLimitsExceededException when it loads the image. The SvgImageLimitsExceededException.Kind property identifies the reason: ElementDepth, ElementCount, or ReferenceCycle.

    SvgImageLimitsExceededException derives from System.Security.SecurityException.

    The following example raises element limits and displays a placeholder for an image that exceeds them:

    using DevExpress.Utils.Svg;
    
    // Call at application startup.
    SvgImageLimits.MaxElementCount = 256 * 1024;
    SvgImageLimits.MaxReferencedElementCount = 64 * 1024;
    
    // ...
    
    try {
        SvgImage image = SvgImage.FromStream(stream);
        // Use the image.
    }
    catch(SvgImageLimitsExceededException e) {
        // e.Kind returns SvgImageLimitKind.ElementDepth, ElementCount, or ReferenceCycle.
        // Show a placeholder instead of the image.
    }
    

    Stack Overflow Protection

    If an application renders SVG images in a thread with a small stack (for instance, an IIS worker thread), the renderer throws a System.InsufficientExecutionStackException for deeply nested references before the stack runs out. You can catch this exception (unlike a StackOverflowException), and the process continues to run.

    Best Practices

    • Always update DevExpress libraries to the most recent version.
    • Treat SVG images from users and other external sources as untrusted input.
    • Handle SvgImageLimitsExceededException where the application loads SVG images from untrusted sources.
    • Raise or disable SVG image limits only if all SVG images in your application come from trusted sources.

    See Also