How to Check WebP Files for Potential Threats in Node.js

Cloudmersive
4 min readApr 11, 2024

Half a year ago, a critical heap overflow vulnerability (CVE 2023–4863) was discovered in Libwebp, a popular WebP rendering and processing library used in several web browser and other common applications.

Without requiring any user interaction, a threat actor could use a specially crafted WebP file to trigger an out-of-bounds memory write in Libwebp’s memory allocation process, resulting in a remote code execution or denial of service attack.

Zero-day application vulnerabilities like this pop up quite frequently, and they’re often difficult to keep track of. Sometimes, mitigating a threat like this one simply requires installing the latest software update, but if we aren’t aware of that update (or why we should install it) for even a brief period of time, we might fall victim to a sudden attack.

If we focus more keenly on verifying the files used in such attacks, however, we may be able to sidestep attacks before they occur.

In this example, if we verified/validated all incoming WebP files to ensure they rigorously conformed with stringent WebP formatting standards, we would be able to identify abnormalities intended to corrupt the Libwebp memory allocation process and prevent malicious files from being rendered and processed.

Using the below Node.js code examples, we can take advantage of a free API that combines robust virus & malware scanning with deep file format verification to provide 360-degree protection against multiple file object threats. We can authorize our API calls using a free Cloudmersive API key (this will allow us to make up to 800 API calls per month with no additional commitments).

To structure our API call, we can begin by installing the client SDK. To install with NPM install, let’s run the below command:

npm install cloudmersive-virus-api-client --save

Alternatively, we can add the following snippet to our package.json:

  "dependencies": {
"cloudmersive-virus-api-client": "^1.1.9"
}

We can then call the function using the below code:

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

We can set a custom threat rule in the API request body to specifically block invalid WebP files, and we can also identify if any scripts or other threats were hidden within the file. Even when viruses and malware aren’t detected, files that breach a custom threat rule will trigger a “CleanResult”: False response, making it easy for us to delete or quarantine the file in question.

Now we can take an important step towards protecting our potentially vulnerable applications from rendering or processing malicious WebP files.

--

--

Cloudmersive

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