How to Scan MP4 Files for Viruses and Malware in Node.js
Just like image files, MP4 video files can carry viruses in malware in their video frames.
Scanning video files for threats is a bit cumbersome, but it’s certainly worth checking video uploads for threats before storing them in a sensitive location.
Thankfully, using the Node.js code examples provided below, we can simplify our task by calling a free API to scan video uploads entering server-side applications.
We’ll be able to check each video file for millions of potential virus and malware threats, and we’ll also verify the MP4 file format at the same time. That way, we can avoid malware AND threats associated with rendering invalid media.
To structure our API call, we can begin by installing the SDK via NPM install:
npm install cloudmersive-virus-api-client --save
Alternatively, we could add the following snippet to our package.json:
"dependencies": {
"cloudmersive-virus-api-client": "^1.1.9"
}
Before we call the function, let’s grab a free Cloudmersive API key to authorize our API calls. This will allow us to make up to 800 API calls per month with no commitments.
Now let’s copy the below code to call the function, and let’s set the ‘allowInvalidFiles’
parameter to “false”
in our request body (this will treat invalid MP4s the same as virus- or malware-infected MP4s):
var CloudmersiveVirusApiClient = require('cloudmersive-virus-api-client');
var defaultClient = CloudmersiveVirusApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveVirusApiClient.ScanApi();
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // 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, 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).
'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).
'allowMacros': 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).
'allowXmlExternalEntities': 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).
'allowInsecureDeserialization': 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).
'allowHtml': 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].
'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);
That’s all there is to it — now we can easily perform a thorough virus and malware scan on MP4 files entering a Node.js form upload process.