Securely Process Presentations
- 11 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.
When you create a Presentation object, you can pass a LoadOptions object to the constructor. This object can specify prohibited content types and limit resources the loading operation can consume.
Once you loaded a file, use inspection and sanitization APIs to detect and remove unwanted content.
- 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, custom XML parts, and restricted links.
- 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.SecurityLoadingLimits property to access and configure the following limits before you load the presentation:
| Property | Description |
|---|---|
| MaxFileSize | Specifies the maximum allowed presentation file size. |
| MaxSlideCount | Specifies the maximum number of slides in a presentation. |
| MaxShapeCountPerSlide | Specifies the maximum number of shapes on a slide. |
| MaxXmlElementCount | Specifies the maximum number of XML elements in a presentation. |
| MaxXmlElementDepth | Specifies the maximum nesting depth of XML elements in a presentation. |
If you attempt to load a presentation that exceeds any of the specified limits, the API throws a SecurityLoadingLimitExceededException exception.
The following code snippet limits presentation file size, number of slides, and number of shapes per slide:
using System.IO;
using DevExpress.Docs.Office;
using DevExpress.Docs.Presentation;
namespace PresentationApiSample;
public class Program {
public static void Main(string[] _) {
// Open the presentation file for reading.
using (FileStream inputStream = File.OpenRead(@"..\..\..\data\Presentation.pptx")) {
// Configure security loading limits.
LoadOptions loadOptions = new LoadOptions {
SecurityLoadingLimits = new PresentationSecurityLoadingLimits {
MaxFileSize = 50L * 1024 * 1024, // 50 MB
MaxSlideCount = 100,
MaxShapeCountPerSlide = 500
}
};
// Load the presentation with the configured limits.
using (Presentation presentation = new Presentation(inputStream, loadOptions)) {
// Process the loaded presentation.
// ...
}
}
}
}
Load Files: Specify Content Type Restrictions
Use the LoadOptions.SecurityLoadingOptions property to access and configure unsafe content removal options.
Note
Configure security loading options before you load the presentation.
Use the following properties to skip potentially unsafe content when the API loads a presentation:
- RemoveMacros
- RemoveEmbeddedChartData
- RemoveOleObjects
- RemoveActiveXContent
- RemoveCustomXMLParts
- RestrictedHyperlinkRemovalMode
The following code snippet specifies content type restrictions and loads the presentation with configured options:
using System.IO;
using DevExpress.Docs.Office;
using DevExpress.Docs.Presentation;
namespace PresentationApiSample;
public class Program {
public static void Main(string[] _) {
// Open the presentation file for reading.
using (FileStream inputStream = File.OpenRead(@"..\..\..\data\Presentation.pptx")) {
// Configure options that remove unsafe content during loading.
LoadOptions loadOptions = new LoadOptions {
SecurityLoadingOptions = new PresentationSecurityLoadingOptions {
RemoveActiveXContent = true,
RemoveCustomXMLParts = true,
RemoveEmbeddedChartData = true,
RemoveExternalImages = true,
RemoveMacros = true,
RemoveOleObjects = true,
RestrictedHyperlinkRemovalMode = RestrictedHyperlinkRemovalMode.Full
}
};
// Load the presentation with the configured security options.
using (Presentation presentation = new Presentation(inputStream, loadOptions)) {
// Process the loaded presentation.
// ...
}
}
}
}
Handle Security Violations
If you need additional flexibility when it comes to loading restrictions, implement a custom handler class. If the Presentation API 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 its methods, and assign the implementation to the SecurityLoadingHandler property.
The ISecurityLoadingHandler.HandleLimitExceeded method handles file size and element count limit violations.
The HandleLimitExceeded 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 ISecurityLoadingHandler.HandleOptionsViolation(SecurityOptionsViolationParams) method handles content type violations.
The HandleOptionsViolation 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. |
The following code snippet configures loading options, handles security violations, and loads a presentation with 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:
using System;
using System.IO;
using DevExpress.Docs.Office;
using DevExpress.Docs.Presentation;
namespace PresentationApiSample;
public class Program {
public static void Main(string[] _) {
// Open the presentation file for reading.
using (FileStream inputStream = File.OpenRead(@"..\..\..\data\Presentation.pptx")) {
// Configure security limits, removal options, and the violation handler.
LoadOptions loadOptions = new LoadOptions {
SecurityLoadingLimits = new PresentationSecurityLoadingLimits {
MaxFileSize = 50L * 1024 * 1024, // 50 MB
MaxSlideCount = 1,
MaxShapeCountPerSlide = 500
},
SecurityLoadingOptions = new PresentationSecurityLoadingOptions {
RemoveActiveXContent = true,
RemoveCustomXMLParts = true,
RemoveEmbeddedChartData = true,
RemoveExternalImages = true,
RemoveMacros = true,
RemoveOleObjects = true,
RestrictedHyperlinkRemovalMode = RestrictedHyperlinkRemovalMode.Full
},
SecurityLoadingHandler = new SecurityLoadingHandler()
};
// Load the presentation with the configured security settings.
using (Presentation presentation = new Presentation(inputStream, loadOptions)) {
// Process the loaded presentation.
// ...
}
}
}
}
// Handle security violations during loading.
public class SecurityLoadingHandler : ISecurityLoadingHandler {
public SecurityLimitAction HandleLimitExceeded(SecurityLimitExceededParams parameters) {
// Log the exceeded limit and stop loading.
Console.WriteLine($"Security limit exceeded: {parameters.PropertyName}");
return SecurityLimitAction.Throw;
}
public SecurityViolationAction HandleOptionsViolation(SecurityOptionsViolationParams parameters) {
// Log the violated option and stop loading.
Console.WriteLine($"Security option violated: {parameters.PropertyName}");
return SecurityViolationAction.Remove;
}
}
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 a 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 inspect all supported content types. The parameterless overload uses PresentationInspectOptions.All.
using System.IO;
using DevExpress.Docs.Office;
using DevExpress.Docs.Presentation;
namespace PresentationApiSample;
public class Program {
public static void Main(string[] _) {
using (FileStream inputStream = File.OpenRead(@"..\..\..\data\Presentation.pptx")) {
using (Presentation presentation = new Presentation(inputStream)) {
// Inspect the presentation for potentially dangerous and private content.
PresentationInspectResult result = presentation.Inspect();
// Log inspection results or mark the presentation for review.
// ...
}
}
}
}
Call the Presentation.Inspect(PresentationInspectOptions) method overload to look for specific content types.
The Inspect method returns 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.
using DevExpress.Docs.Office;
using DevExpress.Docs.Presentation;
using (Presentation presentation = new Presentation(inputStream)) {
PresentationInspectOptions inspectOptions = PresentationInspectOptions.AllMetadata
| PresentationInspectOptions.Macros
| PresentationInspectOptions.ActiveXContent
| PresentationInspectOptions.OleObjects
| PresentationInspectOptions.HiddenSlides
| PresentationInspectOptions.HiddenShapes
| PresentationInspectOptions.OffSlideContent;
PresentationInspectResult result = presentation.Inspect(inspectOptions);
}
Sanitize Presentation Content
After you call the Inspect method, you can sanitize the document as follows:
- Use the
Inspectmethod’s return value to call the PresentationInspectResult.CreateSanitizeOptions() method. - Store the return value: PresentationSanitizeOptions.
- Pass generated options to the Presentation.Sanitize(PresentationSanitizeOptions) method. This method removes content from a presentation and returns the operation log.
using System.Collections.Generic;
using System.IO;
using DevExpress.Docs.Presentation;
namespace PresentationApiSample;
public class Program {
public static void Main(string[] _) {
using FileStream inputStream = File.OpenRead(@"..\..\..\data\Presentation.pptx");
using 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.
IReadOnlyList<PresentationSanitizeResult> results = presentation.Sanitize(sanitizeOptions);
// Log or process sanitization results.
// ...
string outputDir = "output";
Directory.CreateDirectory(outputDir);
// Save the sanitized presentation.
string outputPath = Path.Combine(outputDir, "Presentation_Sanitized.pptx");
using FileStream fileStream = File.Create(outputPath);
presentation.Save(fileStream);
}
}
Call the parameterless Presentation.Sanitize() method to remove potentially dangerous or private content with default sanitization options.
Use the Presentation.Sanitize(PresentationSanitizeOptions) overload to specify content types to be sanitized. The following table lists properties available in the PresentationSanitizeOptions class:
| Property | Description |
|---|---|
| RemoveActiveXContent | Removes ActiveX content from the presentation. |
| HiddenShapes | Specifies how to process hidden shapes. |
| HiddenSlides | Specifies how to process hidden slides. |
| Metadata | Specifies which metadata to remove from a presentation. |
| RemoveComments | Removes comments from a presentation. |
| RemoveCustomXmlParts | Removes custom XML parts from a presentation. |
| RemoveEmbeddedChartData | Removes embedded chart data from a presentation. |
| RemoveMacros | Removes macros from a presentation. |
| RemoveNotes | Removes notes from a presentation. |
| RemoveOffSlideContent | Removes content outside slide boundaries from a presentation. |
| RemoveOleObjects | Removes OLE objects from a presentation. |
PresentationSanitizeOptions presentationSanitizeOptions = new PresentationSanitizeOptions {
RemoveActiveXContent = true,
HiddenShapes = HiddenContentSanitizeMode.Remove,
HiddenSlides = HiddenContentSanitizeMode.Remove,
Metadata = MetadataRemovalScope.AuthorInfo | MetadataRemovalScope.OrganizationInfo,
RemoveComments = true,
RemoveCustomXmlParts = true,
RemoveEmbeddedChartData = true,
RemoveMacros = true,
RemoveNotes = true,
RemoveOffSlideContent = true,
RemoveOleObjects = true,
};
IReadOnlyList<PresentationSanitizeResult> results = presentation.Sanitize(presentationSanitizeOptions);