Skip to content

Malware Scanning with GuardDuty ​

The GuardDuty adapter reads malware scan results from S3 object tags. Use it to check an upload before your application makes the file available.

GuardDuty performs the scan in AWS. Glasswork reads the result; your application decides what users can upload and when a file can be published.

Setup ​

Enable GuardDuty Malware Protection for S3 on your upload bucket, with scan result tagging enabled. The adapter requires the S3 SDK:

bash
npm install @aws-sdk/client-s3

Import it from its own subpath:

typescript
import { S3Client } from '@aws-sdk/client-s3';
import { GuardDutyMalwareScanner } from 'glasswork/uploads/guardduty';

const client = new S3Client({ region: 'eu-west-1' });
const scanner = new GuardDutyMalwareScanner({
  client,
  bucketName: 'my-app-files',
});

This adapter does not require the URL presigner. To also use UploadsService, install @aws-sdk/s3-request-presigner as described in Getting Started.

Read a Scan Result ​

typescript
const scan = await scanner.getScanResult(stagingKey);
console.log(scan.status);

status keeps GuardDuty's result names. Two additional values describe a missing or unrecognized result:

StatusMeaning
NO_THREATS_FOUNDGuardDuty found no malware.
THREATS_FOUNDGuardDuty detected malware.
UNSUPPORTEDGuardDuty could not scan this object.
ACCESS_DENIEDGuardDuty could not access the object.
FAILEDThe scan failed.
pendingThe scan result tag is missing.
unknownThe tag contains an unrecognized value.

A missing tag may mean the scan has not finished, but it can also mean tagging failed. pending does not prove that a scan is running. Keep files unavailable unless the result is NO_THREATS_FOUND; your application decides how to report other results and when to retry.

scan.response is the original AWS GetObjectTagging response. It includes the object's VersionId, all returned tags, and AWS response metadata. AWS request errors are passed through unchanged.

Use the Same Version Throughout ​

In a versioned bucket, use the returned version ID for metadata checks, content validation, and finalization. Reading a clean tag and then copying the latest object could publish a different version.

typescript
const scan = await scanner.getScanResult(stagingKey);
if (scan.status !== 'NO_THREATS_FOUND') {
  throw new Error('The upload does not have a clean scan result');
}

const versionId = scan.response.VersionId;
if (!versionId || versionId === 'null') {
  throw new Error('Expected a versioned upload');
}

const metadata = await uploadsService.inspectFile(stagingKey, { VersionId: versionId });
// Check ownership, size, type, and file content according to your application's rules.
if (!metadata.ETag) throw new Error('The inspected file has no ETag');

const copied = await uploadsService.finalizeUpload(stagingKey, finalKey, metadata.ETag, {
  sourceVersionId: versionId,
  deleteSource: false,
  command: { TaggingDirective: 'REPLACE', Tagging: '' },
});
// Store copied.VersionId after checking that it is a non-null version ID.

The copy uses the checked source version and clears its tags. Leaving the source in place prevents reuse of a create-only upload URL while it is still valid. Use bucket lifecycle rules to clean up those temporary uploads later. See Finalizing a Checked Version for copy options and cleanup behavior.

You can also read the scan result for a specific version, for example in a separate content validation worker:

typescript
const scan = await scanner.getScanResult(stagingKey, { VersionId: versionId });

The options accept native AWS GetObjectTagging fields. Glasswork sets the bucket and object key.

Application and AWS Responsibilities ​

Configure versioning, the scan prefix, and IAM permissions in your infrastructure. Protect the scan result tag so uploaders cannot write their own approval, and prevent users from reading files before validation. GuardDuty's role needs permission to scan and tag objects; the application needs permission to read the result, including tags on specific versions.

Keep upload limits, file content checks, publication rules, database writes, and user messages in your application. A status shown in the UI is feedback; check the scan again on the server when finalizing the upload.

For AWS setup and scan result details, see How GuardDuty Malware Protection for S3 works.

Released under the MIT License.