Sitemap

How to Block Insecure .XLTX Uploads in a C#/.NET App

4 min readApr 30, 2025

--

Excel template documents (.xltx) aren't as common as standard spreadsheets, but they still pose a formidable risk to our .NET application.

What’s Scary About XLTX?

Because they're based on the complex Office Open XML archive format (same as any other Office document), .xltx files are capable of carrying embedded external links and other hidden content. Attackers can use this content to execute code or manipulate data in our environment.

Under the guise of a harmless template, it’s completely possible that malicious scripts, malformed document components (intended to exploit file parses), or even embedded executables could slip past inadequate file validation or AV defenses.

How to Mitigate XLTX Threats

For C# developers dealing with .xltx file uploads — whether intentionally or not — adequately securing an application requires going beyond simple file-type validation. It’s not enough to check for valid .xltx (or any other Open Office type) extensions — it’s necessary to verify in-depth that the contents within the file are completely risk-free.

Scanning and Verifying XLTX Contents with a Free API

A good approach to defend against .xltx threats is to implement an API that performs a deep inspection of Office files, flagging anything suspicious before the file is processed or stored.

This adds a critical layer of defense before insecure files reach more sensitive server locations, helping ensure that template uploads don’t introduce silent vulnerabilities into a system.

We can take advantage of a simple, low-code API solution using the C# examples provided below. This is a fully realized AV and content validation service which we’ll need an API key to use.

We can get started by installing the SDK with NuGet:

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

After that, we can add the namespace imports 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;

Then we can define our main method and configure the API client with our free API key:

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

Finally, we can create our API instance, read our input file, and set advanced scan options for threat types and file restrictions:

            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 );
}
}
}
}

The scanning options allow us to determine exactly which content we do and don’t want to allow in our file upload process. We’re looking deep under the hood of each file we scan, and if we want to categorically block things like scripts, macros, and executables, we can stringently apply those rules with simple Booleans.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet