XML How to Block XML External Entity (XXE) Threats using Ruby

Cloudmersive
3 min readJul 3, 2023

--

XML entities are a useful feature of the language, but they also represent a significant cyber-attack vector. It’s critical to identify external entities in XML files as they can contain links to malicious resources intended to steal information from your system or deny service to it entirely.

Thankfully, you can easily root out XML External Entity threats using a free Virus Scan API with 360-degree content protection capabilities. The underlying service will check files for 17+ million virus and malware signatures, and using the Ruby code examples provided, you can customize your request to categorically block XXE threats as well (along with a variety of other hidden content threats).

To take advantage of this API, you’ll first need to install the Ruby SDK, which you can do by adding the Ruby client to your Gemfile:

gem 'cloudmersive-virus-scan-api-client', '~> 2.0.3'

After that, copy and paste the below code, and provide a free-tier Cloudmersive API key to authenticate your request (this key will allow up to 800 API calls per month with no commitment):

# load the gem
require 'cloudmersive-virus-scan-api-client'
# setup authorization
CloudmersiveVirusScanApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end

api_instance = CloudmersiveVirusScanApiClient::ScanApi.new

input_file = File.new('/path/to/inputfile') # File | Input file to perform the operation on.

opts = {
allow_executables: true, # BOOLEAN | Set to false to block executable files (program code) from being allowed in the input file. Default is false (recommended).
allow_invalid_files: true, # BOOLEAN | 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).
allow_scripts: true, # BOOLEAN | 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).
allow_password_protected_files: true, # BOOLEAN | 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).
allow_macros: true, # BOOLEAN | 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).
allow_xml_external_entities: true, # BOOLEAN | 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).
allow_insecure_deserialization: true, # BOOLEAN | 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).
allow_html: true, # BOOLEAN | 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].
restrict_file_types: 'restrict_file_types_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.
}

begin
#Advanced Scan a file for viruses
result = api_instance.scan_file_advanced(input_file, opts)
p result
rescue CloudmersiveVirusScanApiClient::ApiError => e
puts "Exception when calling ScanApi->scan_file_advanced: #{e}"
end

That’s all there is to it — now you can apply dynamic security policies to your application’s file upload processes.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet