Malware Scanning

On this page 7

Stacks scans the bytes people send before it stores or uses them: uploads through Storage.put and UploadedFile.store, streamed uploads, presigned uploads, form and feedback files, inbound email attachments, AI audio and image inputs, and files the dashboard's file manager receives. A detection is refused with a neutral message and the file goes to quarantine. This page is for the person running the app; the Malware skill covers the API.

Pick a scanner

Stacks talks to a scanner over the clamd protocol, so any of these work:

  • zavd, the Guardian daemon. Point guardian.socket at its socket and set driver: 'guardian'.
  • clamd from ClamAV. Install ClamAV, run freshclam to fetch signatures, start clamd with a LocalSocket (or TCPSocket 3310), and set driver: 'clamd' with clamd.socket (or clamd.host and clamd.port).

The default driver is auto: it uses the first zavd or clamd socket that answers, and if none does it lets everything through and logs a warning. Name a driver in production, so a scanner that goes away refuses uploads instead of waving them through.

// config/malware.ts
import type { MalwareConfig } from '@stacksjs/types'

export default {
  driver: 'clamd',
  clamd: { socket: '/var/run/clamav/clamd.ctl' },
  failClosed: true,
} satisfies MalwareConfig

Check it

bun -e "import { health } from '@stacksjs/malware'; console.log(await health())"

ok: true and a version mean the app can reach the scanner. To test the whole path, upload the EICAR test file (a harmless string every scanner flags) and confirm the app refuses it and a copy appears under storage/quarantine.

When the scanner is down

With failClosed: true (the default) a scan that cannot finish refuses the file: uploads answer 503, inbound attachments are withheld, presigned uploads stay staged until a retry, and file-manager scans retry from the queue. Set failClosed: false only if accepting unscanned files is better for you than refusing them.

Limits

  • maxScanBytes (default 512 MiB) caps what the app sends to the scanner.
  • clamd's StreamMaxLength caps what clamd accepts; raise it for large video.
  • A file past either limit counts as unscanned and is refused while failing closed.

Quarantine

Refused files are kept under storage/quarantine/<date>/ as <id>.quarantined with an <id>.json record of the verdict, signature, checksum and where the file came from. The files are readable only by the app user. Review and delete them with listQuarantine() and deleteQuarantined(id) from @stacksjs/malware, or remove old days on a schedule.

Presigned uploads

While scanning is on, presigned upload URLs point at incoming/ instead of the final key. The app promotes an upload only after Storage.confirmUpload() (or the ScanStagedUploadJob queue job) scans it. Add a lifecycle rule to your bucket that expires objects under incoming/ after a day, so abandoned uploads do not pile up.

Turning a surface off

surfaces in config/malware.ts switches individual surfaces: storage, forms, email, ai, fileManager and validation. A surface left out scans.

Released under the MIT License.