Skip to main content

Secure Presentation Processing

  • 8 minutes to read

If your application loads and processes files, you need to consider potential security issues. Files with unsafe content can exhaust your machine’s resources or initiate malicious actions. The DevExpress Presentation API implements protection measures that help you avoid harmful file content.

Use the LoadOptions class to configure security settings for the loading process. These settings restrict resources that the API can consume while loading a presentation and remove potentially unsafe content.

After the presentation is loaded, use inspection and sanitization features to detect and remove unwanted content.

The following security features are available:

Load Files: Limit File Size and Element Count/Hierarchy
Restrict file load/parse operations that may exhaust available resources.
Load Files: Specify Content Type Restrictions
Avoid loading potentially unsafe content such as macros, ActiveX content, OLE objects, and custom XML parts.
Handle Security Violations
Analyze security violations and indicate whether the file loading operation may resume.
Inspect Presentation Content
Find content that may pose security or privacy risks. Log inspection results and mark presentations that require additional review.
Sanitize Presentation Content
Remove metadata, revision history, and hidden content before you share or archive presentations.

Load Files: Limit File Size and Element Count/Hierarchy

You can restrict the size and complexity of files the Presentation API should handle. Set limits to protect applications from malicious or malformed files that contain excessive data or deeply nested XML structures.

Use the LoadOptions.setSecurityLoadingLimits() method to assign a PresentationSecurityLoadingLimits object. Configure the following limits before you load the presentation:

Method Description
setMaxFileSize() Sets the maximum allowed presentation file size.
setMaxSlideCount() Sets the maximum number of slides in the presentation.
setMaxShapeCountPerSlide() Sets the maximum number of shapes on a slide.
setMaxXmlElementCount() Sets the maximum number of XML elements in the presentation.
setMaxXmlElementDepth() Sets the maximum nesting depth of XML elements in the presentation.

Warning

If you attempt to load a presentation that exceeds any of the specified limits, the API throws a SecurityLoadingLimitExceeded exception.

The following code snippet limits the presentation file size, number of slides, and number of shapes per slide:

package presentation;

import com.devexpress.docs.presentation.*;

import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Create load options.
        LoadOptions loadOptions = new LoadOptions();

        // Configure security loading limits.
        PresentationSecurityLoadingLimits limits = new PresentationSecurityLoadingLimits();
        limits.setMaxFileSize(50L * 1024 * 1024); // 50 MB
        limits.setMaxSlideCount(100);
        limits.setMaxShapeCountPerSlide(500);

        loadOptions.setSecurityLoadingLimits(limits);

        try (FileInputStream inputStream = new FileInputStream("Presentation.pptx");
             Presentation presentation = new Presentation(inputStream, loadOptions)) {

            //...
        }
    }
}

Set a security limit to null to disable the corresponding limit.

Load Files: Specify Content Type Restrictions

Use the LoadOptions.setSecurityLoadingOptions() method to assign a PresentationSecurityLoadingOptions object. The object specifies content that the Presentation API should remove when it loads a presentation.

Note

Configure security loading options before you load the presentation.

Use the following methods to skip potentially unsafe content when the API loads a presentation:

Method Description
setRemoveMacros(true) Removes macros.
setRemoveActiveXContent(true) Removes ActiveX content.
setRemoveOleObjects(true) Removes OLE objects.
setRemoveCustomXMLParts(true) Removes custom XML parts.
setRestrictedHyperlinkRemovalMode() Specifies the action that the API applies to restricted hyperlinks.

The following code snippet specifies content type restrictions and loads the presentation with configured options:

package presentation;

import com.devexpress.docs.presentation.*;

import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {

        // Create load options.
        LoadOptions loadOptions = new LoadOptions();

        // Configure security loading options.
        PresentationSecurityLoadingOptions securityLoadingOptions = new PresentationSecurityLoadingOptions();
        securityLoadingOptions.setRemoveMacros(true);
        securityLoadingOptions.setRemoveOleObjects(true);
        securityLoadingOptions.setRemoveActiveXContent(true);
        securityLoadingOptions.setRemoveCustomXMLParts(true);

        loadOptions.setSecurityLoadingOptions(securityLoadingOptions);

        try(FileInputStream inputStream = new FileInputStream("Presentation.pptx");
            Presentation presentation = new Presentation(inputStream, loadOptions)) {

            //...
        }
    }
}

Handle Security Violations

If you need additional flexibility when it comes to loading restrictions, implement a custom handler class. If the Presentation API it detects a security violation, the handler can analyze each instance and decide whether to resume or cancel the file loading operation.

Implement the ISecurityLoadingHandler interface and pass the implementation to the LoadOptions.setSecurityLoadingHandler() method.

The following code snippet configures loading options, handles security violations, and loads a presentation with the specified security settings. The example throws an exception if the presentation exceeds a configured limit or contains restricted content, and logs the violation to the console:

package presentation;

import com.devexpress.docs.office.*;
import com.devexpress.docs.presentation.*;

import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {

        // Create load options.
        LoadOptions loadOptions = new LoadOptions();

        // Configure security loading limits.
        PresentationSecurityLoadingLimits limits = new PresentationSecurityLoadingLimits();
        limits.setMaxFileSize(50L * 1024 * 1024);
        limits.setMaxSlideCount(100);
        limits.setMaxShapeCountPerSlide(500);

        loadOptions.setSecurityLoadingLimits(limits);

        // Configure security loading options.
        PresentationSecurityLoadingOptions securityLoadingOptionsOptions = new PresentationSecurityLoadingOptions();
        securityLoadingOptionsOptions.setRemoveMacros(true);
        securityLoadingOptionsOptions.setRemoveOleObjects(true);
        securityLoadingOptionsOptions.setRemoveActiveXContent(true);
        securityLoadingOptionsOptions.setRemoveCustomXMLParts(true);

        loadOptions.setSecurityLoadingOptions(securityLoadingOptionsOptions);

        // Handle security loading violations.
        loadOptions.setSecurityLoadingHandler(new ISecurityLoadingHandler() {
            @Override
            public SecurityLimitAction handleLimitExceeded(
                    SecurityLimitExceededParams parameters) {

                System.out.println("Security limit exceeded: " + parameters.getPropertyName());
                return SecurityLimitAction.THROW;
            }

            @Override
            public SecurityViolationAction handleOptionsViolation(
                    SecurityOptionsViolationParams parameters) {

                System.out.println("Security option violated: " + parameters.getPropertyName());
                return SecurityViolationAction.REMOVE;
            }
        });

        try(FileInputStream inputStream = new FileInputStream("Presentation.pptx");
            Presentation presentation = new Presentation(inputStream, loadOptions)) {

            //...
        }
    }
}

The handleLimitExceeded() method handles violations of file size and element count limits.

The method returns a SecurityLimitAction value that specifies whether to resume or cancel the file load operation.

Value Description
THROW Stops loading the presentation and throws an exception.
CONTINUE Continues loading the presentation.

The handleOptionsViolation() method handles content type violations.

The method returns a SecurityViolationAction value that specifies the action that the API takes when it detects a security violation.

Value Description
KEEP Keeps content that violates a security restriction.
REMOVE Removes content that violates a security restriction.

Inspect a Presentation Before Sanitization

Inspect a presentation when you need to identify potentially dangerous or private content before you share, archive, or otherwise process the file. Inspection does not modify the presentation. You can use inspection results to log security and privacy issues, mark presentations for review, or determine which content to remove.

Call the Presentation.inspect() method to analyze all supported content types. The parameterless overload uses PresentationInspectOptions.ALL.

try (FileInputStream inputStream = new FileInputStream("Presentation.pptx");
    Presentation presentation = new Presentation(inputStream)) {

    // Inspect the presentation for potentially dangerous and private content.
    PresentationInspectResult inspectResult = presentation.inspect();

    // Log inspection results or mark the presentation for review.
    // ...
}

Use the Presentation.inspect(PresentationInspectOptions) overload to specify which content types to inspect:

// Configure inspection options.
PresentationInspectOptions inspectOptions =
    PresentationInspectOptions.fromValue(
        PresentationInspectOptions._ALL_METADATA
            | PresentationInspectOptions._MACROS
            | PresentationInspectOptions._ACTIVE_X_CONTENT
            | PresentationInspectOptions._OLE_OBJECTS
            | PresentationInspectOptions._HIDDEN_SLIDES
            | PresentationInspectOptions._HIDDEN_SHAPES
            | PresentationInspectOptions._OFF_SLIDE_CONTENT);

// Inspect the presentation.
PresentationInspectResult inspectResult = presentation.inspect(inspectOptions);

The inspect() methods return a PresentationInspectResult object that contains information about potentially dangerous and private content detected in the presentation. You can use the inspection result to log content issues or prevent presentation distribution. For example, an application can mark a presentation that contains potentially unsafe content and prevent further distribution.

To remove detected content, call the PresentationInspectResult.createSanitizeOptions() method to create a PresentationSanitizeOptions object based on inspection results. Pass the resulting options to the Presentation.sanitize(PresentationSanitizeOptions) method.

The following code snippet inspects a presentation, creates sanitization options based on the inspection results, sanitizes the presentation, and saves the sanitized presentation to a file:

package presentation;

import com.devexpress.docs.presentation.*;

import java.io.*;
import java.nio.file.*;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        try (FileInputStream inputStream = new FileInputStream("Presentation.pptx");
            Presentation presentation = new Presentation(inputStream)) {

            PresentationInspectResult inspectResult = presentation.inspect();

            // Log or process detected security and privacy issues.
            // ...

            // Create sanitization options based on inspection results.
            PresentationSanitizeOptions sanitizeOptions = inspectResult.createSanitizeOptions();

            // Remove detected content.
            List<PresentationSanitizeResult> results = presentation.sanitize(sanitizeOptions);

            // Log or process sanitization results.
            // ...

            Path outputDir = Path.of("output");
            Files.createDirectories(outputDir);

            // Save the sanitized presentation.
            try (FileOutputStream fileStream = new FileOutputStream(
                    outputDir.resolve("Presentation_Sanitized.pptx").toFile())) {

                presentation.saveDocument(fileStream);
            }
        }
    }
}

Sanitize Presentation Content

Call the Presentation.sanitize() method to remove potentially dangerous or private content with default sanitization options.

Use the Presentation.sanitize(PresentationSanitizeOptions options) overload to specify content types to be sanitized.

The sanitize() method returns a list of PresentationSanitizeResult objects. Each result describes a sanitization operation performed on the presentation.

Use the following methods to configure sanitize options:

Method Description
setRemoveNotes(true) Removes notes.
setHiddenSlides(true) Removes hidden slides.
setHiddenShapes(true) Removes hidden shapes.
setRemoveOffSlideContent(true) Removes content outside slide boundaries.
setMetadata(MetadataRemovalScope.ALL) Removes metadata.
setRemoveActiveXContent(true) Removes ActiveX content.
setRemoveCustomXmlParts(true) Removes custom XML parts.
setRemoveMacros(true) Removes macros.
setRemoveOleObjects(true) Removes OLE objects.

The following code snippet applies a predefined sanitization policy:

package presentation;

import com.devexpress.docs.office.*;
import com.devexpress.docs.presentation.*;

import java.io.*;
import java.nio.file.*;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        try (FileInputStream inputStream = new FileInputStream("Presentation.pptx");
            Presentation presentation = new Presentation(inputStream)) {

            PresentationSanitizeOptions sanitizeOptions = new PresentationSanitizeOptions();

            sanitizeOptions.setRemoveNotes(true);
            sanitizeOptions.setRemoveOffSlideContent(true);
            sanitizeOptions.setRemoveMacros(true);
            sanitizeOptions.setRemoveActiveXContent(true);
            sanitizeOptions.setRemoveOleObjects(true);
            sanitizeOptions.setMetadata(MetadataRemovalScope.ALL);
            sanitizeOptions.setHiddenSlides(HiddenContentSanitizeMode.REMOVE);
            sanitizeOptions.setHiddenShapes(HiddenContentSanitizeMode.IGNORE);

            List<PresentationSanitizeResult> results = presentation.sanitize(sanitizeOptions);

            Path outputDir = Path.of("output");
            Files.createDirectories(outputDir);

            // Save the sanitized presentation.
            try (FileOutputStream fileStream = new FileOutputStream(
                    outputDir.resolve("Presentation_Sanitized.pptx").toFile())) {

                presentation.saveDocument(fileStream);
            }
        }
    }
}
See Also