How to Block Executable Uploads in Javascript

One of the most dangerous attacks against your site is the zero-day threat. It is immune to normal definition-based virus scanning, and is often customized for your site. One means of preventing this is to block executable files. Generally this is done by filtering files by their extension, eliminating .EXE files, for example. However, if the hacker is smart, they will simply mask the file by changing the file extension to something less threatening, like .JPG. To provide you some protection against this, we will be setting up a Cloudmersive API.

Let’s add this script tag to the head of your page to make our client available:

<script src="https://cdn.cloudmersive.com/jsclient/cloudmersive-virus-client.js"></script>

Now we can call this function:

var CloudmersiveVirusApiClient = require('cloudmersive-virus-api-client');var defaultClient = CloudmersiveVirusApiClient.ApiClient.instance;// Configure API key authorization: Apikeyvar Apikey = defaultClient.authentications['Apikey'];Apikey.apiKey = 'YOUR API KEY';// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)//Apikey.apiKeyPrefix = 'Token';var apiInstance = new CloudmersiveVirusApiClient.ScanApi();var inputFile = "/path/to/file"; // File | Input file to perform the operation on.var opts = {'allowExecutables': true, // Boolean | Set to false to block executable files (program code) from being allowed in the input file.  Default is false (recommended).'allowInvalidFiles': 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).'allowScripts': true, // Boolean | Set to false to block script files, such as a PHP files, Pythong 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).'allowPasswordProtectedFiles': 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).'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.};var callback = function(error, data, response) {if (error) {console.error(error);} else {console.log('API called successfully. Returned data: ' + data);}};apiInstance.scanFileAdvanced(inputFile, opts, callback);

In particular, we want to make sure that allowExecutables is set to false (the default), which will cause all incoming files to be identified regardless of their extension in a sandbox environment. Also note the other options, particularly allowScripts, which can block malicious scripts.

Alright, that’s all there is to it!

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store