How to Scan A File for Viruses in Salesforce Apex

Cloudmersive
2 min readFeb 24, 2021

Is it just me, or do you feel like every day you hear about a new form of cyber attack? To guard against these threats, we will be discussing how to use an API to automatically scan files for viruses, malware, and other threats in Salesforce Apex. The best feature of this particular virus protection is that it will identify zero-day threats that aren’t listed in the database of known malware signatures.

To kick things off, you will need to download and copy the /client folder into your Apex project:

Download Apex Client

For the next step, you can configure your API key and call the function with the following code:

SwagScanApi api = new SwagScanApi();
SwagClient client = api.getClient();
// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) client.getAuthentication('Apikey');
Apikey.setApiKey('YOUR API KEY');
Map<String, Object> params = new Map<String, Object>{
'inputFile' => Blob.valueOf('Sample text file\nContents'),
'allowExecutables' => true,
'allowInvalidFiles' => true,
'allowScripts' => true,
'allowPasswordProtectedFiles' => true,
'allowMacros' => true,
'restrictFileTypes' => 'restrictFileTypes_example'
};
try {
// cross your fingers
SwagVirusScanAdvancedResult result = api.scanFileAdvanced(params);
System.debug(result);
} catch (Swagger.ApiException e) {
// ...handle your exceptions
}

And done! In sub-second time, you will have your result indicating if the file is clean; to block executables, invalid files, scripts, password protected files, and macros, be sure to set those parameters to false.

--

--

Cloudmersive

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