Sitemap

How to Protect Your C# Application from Malicious .XPS Files

4 min readMay 1, 2025

--

XPS (.xps) files — Microsoft’s PDF alternative — might not be as common today, but they can still show up in certain document workflows and legacy systems.

These files can carry rich content like images and embedded fonts — and they can also carry scripts, which makes them a surprising attack surface.

In fact, .xps can carry a variety of known threat types — including malformed structures, embedded payloads, and even exploits targeting vulnerabilities in the viewer software (.xps viewers have had known parsing vulnerabilities in the past).

Including Defense Against Legacy Documents

C# applications built for printing, previewing, or archiving might expect to deal with these older files somewhat regularly. Even without that expectation, it’s still beneficial to implement security policies capable of detecting malicious or malformed .xps content (not to mention other “forgotten about” legacy files with dangerous capabilities) on the server-side.

Identifying Threats with a Dynamic Threat-Scanning API

The challenge is that traditional file validation methods won’t catch threats hidden within .xps files’ XML structure — or inside their embedded resources. A content-aware virus scanning API helps bridge that gap.

Using the below C# code examples, you can take advantage of an API that inspects .xps files deeply (and dozens of other file types, including PDF and MS Office documents), flagging any suspicious patterns or recorded malware signatures before your app attempts to process or render them.

We can start structuring our API call by first running the below command:

Install-Package Cloudmersive.APIClient.NETCore.VirusScan -Version 2.0.4

Next, we can add the using statements to the top of our file:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.VirusScan.Api;
using Cloudmersive.APIClient.NETCore.VirusScan.Client;
using Cloudmersive.APIClient.NETCore.VirusScan.Model;

Following that, we can set our API key for authentication (we can get a free API key with 800 API calls/month) and prepare configuration for API requests:

namespace Example
{
public class ScanFileAdvancedExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

Lastly, we can initialize our virus scan with configurable threat filters, scan input files and log results:

            var apiInstance = new ScanApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.
var allowExecutables = true; // bool? | Set to false to block executable files (program code) from being allowed in the input file. Default is false (recommended). (optional)
var allowInvalidFiles = true; // bool? | Set to false to block invalid files, such as a PDF file that is not really a valid PDF file, or a Word Document that is not a valid Word Document. Default is false (recommended). (optional)
var allowScripts = true; // bool? | Set to false to block script files, such as a PHP files, Python scripts, and other malicious content or security threats that can be embedded in the file. Set to true to allow these file types. Default is false (recommended). (optional)
var allowPasswordProtectedFiles = true; // bool? | Set to false to block password protected and encrypted files, such as encrypted zip and rar files, and other files that seek to circumvent scanning through passwords. Set to true to allow these file types. Default is false (recommended). (optional)
var allowMacros = true; // bool? | Set to false to block macros and other threats embedded in document files, such as Word, Excel and PowerPoint embedded Macros, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended). (optional)
var allowXmlExternalEntities = true; // bool? | Set to false to block XML External Entities and other threats embedded in XML files, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended). (optional)
var allowInsecureDeserialization = true; // bool? | Set to false to block Insecure Deserialization and other threats embedded in JSON and other object serialization files, and other files that contain embedded content threats. Set to true to allow these file types. Default is false (recommended). (optional)
var allowHtml = true; // bool? | Set to false to block HTML input in the top level file; HTML can contain XSS, scripts, local file accesses and other threats. Set to true to allow these file types. Default is false (recommended) [for API keys created prior to the release of this feature default is true for backward compatability]. (optional)
var restrictFileTypes = restrictFileTypes_example; // string | Specify a restricted set of file formats to allow as clean as a comma-separated list of file formats, such as .pdf,.docx,.png would allow only PDF, PNG and Word document files. All files must pass content verification against this list of file formats, if they do not, then the result will be returned as CleanResult=false. Set restrictFileTypes parameter to null or empty string to disable; default is disabled. (optional)

try
{
// Advanced Scan a file for viruses
VirusScanAdvancedResult result = apiInstance.ScanFileAdvanced(inputFile, allowExecutables, allowInvalidFiles, allowScripts, allowPasswordProtectedFiles, allowMacros, allowXmlExternalEntities, allowInsecureDeserialization, allowHtml, restrictFileTypes);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ScanApi.ScanFileAdvanced: " + e.Message );
}
}
}
}

To better understand the bredth of different threats we’re able to identify with this API, we can refer to the below response model:

{
"CleanResult": true,
"ContainsExecutable": true,
"ContainsInvalidFile": true,
"ContainsScript": true,
"ContainsPasswordProtectedFile": true,
"ContainsRestrictedFileFormat": true,
"ContainsMacros": true,
"ContainsXmlExternalEntities": true,
"ContainsInsecureDeserialization": true,
"ContainsHtml": true,
"ContainsUnsafeArchive": true,
"ContainsOleEmbeddedObject": true,
"VerifiedFileFormat": "string",
"FoundViruses": [
{
"FileName": "string",
"VirusName": "string"
}
],
"ContentInformation": {
"ContainsJSON": true,
"ContainsXML": true,
"ContainsImage": true,
"RelevantSubfileName": "string",
"IsAuthenticodeSigned": true
}
}

We’re now able to identify malicious .xps files — just like we’re able to identify malicious PDFs, XLSX, DOCX, JPG, PNG, and dozens of additional file types.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet