Part of Hosting by frameworkHosting · Files

Scan uploaded files for viruses with ClamAV, from Node.js or Python.

To scan uploaded files for viruses, run ClamAV’s daemon, clamd, and stream each upload to it with INSTREAM before you store it. A Node.js or Python client sends the bytes over a socket and gets back stream: OK or a signature name. clamd needs over 1.2 GiB of memory, so on a PaaS it runs as its own service.

Facts checked

clamscan or clamd

ClamAV ships two ways to scan. clamscan is a command: each run builds a new engine and loads the whole signature database, then scans and exits. clamd is a daemon: it loads the database once and answers scan requests on a unix socket or TCP port 3310. For uploads, use clamd. On our test machine a one-file clamscan run took 7.2 seconds, nearly all of it loading 3.6 million signatures; the same scan through clamd answers in milliseconds.

ToolHow it worksUse it for
clamscan †Loads the database on every runCron jobs and one-off scans of a directory
clamd † † †Loads the database once, listens on a socket, scans what clients stream to itAnything per request: uploads, attachments
clamdscan †A command-line client for clamd; --stream sends file contents instead of a pathShell scripts that should use a running clamd

† Competitor details come from each vendor’s own published pages on the dates shown; prices and plans change, so check the vendor before you decide. `clamscan` (2026-11-25) · `clamd` (2026-11-25) · `clamd` (2026-11-25) · `clamd` (2026-11-25) · `clamdscan` (2026-11-25)

The INSTREAM protocol

Your app and clamd rarely share a file system, so don’t send clamd a path (SCAN /tmp/upload); send the bytes. INSTREAM takes the file in chunks, each prefixed with its length as a 4-byte unsigned integer in network byte order, and a zero-length chunk ends it. The z prefix makes clamd end its reply with a NUL byte; ClamAV recommends it over the newline form.

The stream may not exceed StreamMaxLength in clamd.conf (100 MB by default). Past it, clamd answers INSTREAM size limit exceeded and the file is not scanned.

# What a client sends to clamd (TCP 3310 or a unix socket)
zINSTREAM\0                     # "z" = commands end with a NUL byte
<4-byte length, big-endian><that many bytes of the file>
<4-byte length><more bytes>     # as many chunks as you like
\0\0\0\0                        # a zero-length chunk ends the stream

# What comes back, NUL-terminated
stream: OK
stream: Eicar-Test-Signature FOUND
INSTREAM size limit exceeded. ERROR     # past StreamMaxLength (default 100M)

Client libraries for Node.js and Python

You can write the protocol yourself in about 25 lines (Canner’s version is below), or use a client. Check what you pick is still maintained:

LibraryLatest releaseConnects overNotes
clamscan (npm) †2.4.0, October 2024Unix socket or TCP, or runs the binariesscanStream() for uploads. isInfected is true, false or null, and null means it could not scan
clamdjs (npm) †1.0.2, March 2019TCP onlyscanStream() and scanBuffer() resolve with clamd’s raw reply
clamav-client (PyPI) †0.7.2, March 2026Unix socket or TCPTyped fork of clamd; instream() returns {"stream": (status, signature)}
clamd (PyPI) †1.0.2, August 2014Unix socket or TCPThe original; no release in over a decade
pyclamd (PyPI) †0.4.0, August 2017Unix socket or TCPNo release since 2017

† Competitor details come from each vendor’s own published pages on the dates shown; prices and plans change, so check the vendor before you decide. `clamscan` (npm) (2026-11-25) · `clamdjs` (npm) (2026-11-25) · `clamav-client` (PyPI) (2026-11-25) · `clamd` (PyPI) (2026-11-25) · `pyclamd` (PyPI) (2026-11-25)

Scanning an upload: Node.js and Python

Both examples turn anything other than a clean or infected verdict into an error, so a scanner outage can’t let a file through as clean. We ran both against clamd 1.5.5 with clean files:

// Node.js: npm install clamscan
import NodeClam from "clamscan";

const clam = await new NodeClam().init({
  clamdscan: { host: process.env.CLAMD_HOST, port: 3310, localFallback: false },
  preference: "clamdscan",
});

export async function scanUpload(stream) {
  const { isInfected, viruses } = await clam.scanStream(stream);
  if (isInfected === null) throw new Error("not scanned"); // null: clamd could not scan it
  return { clean: !isInfected, viruses };
}

# Python: pip install clamav-client
import os
from clamav_client.clamd import ClamdNetworkSocket

clamd = ClamdNetworkSocket(host=os.environ["CLAMD_HOST"], port=3310)

def scan_upload(fileobj):
    status, signature = clamd.instream(fileobj)["stream"]  # ("OK", None) or ("FOUND", "...")
    if status == "OK":
        return None
    if status == "FOUND":
        return signature
    raise RuntimeError(f"not scanned: {status}")

Why clamd is hard to run on a PaaS

  • Memory. ClamAV’s documentation says clamd “uses upwards of 1.2 GiB of RAM simply to load the signature definitions” and recommends 3 GiB at minimum, 4 GiB preferred. On our test machine clamd 1.5.5 held about 960 MB after loading the databases. Most web dynos and functions have less to spare.
  • Reloads double it. When new signatures arrive, clamd builds a new engine before dropping the old one, so “clamd will use twice the amount of RAM for a brief period”. ConcurrentDatabaseReload no avoids that by pausing scans during the reload.
  • Signature updates. freshclam downloads the databases (about 113 MB for main.cvd, daily.cvd and bytecode.cvd today) and checks for updates 12 times a day by default. ClamAV’s FAQ warns that downloads from shared cloud IP ranges are likely to be rate limited (HTTP 429) and that scripted downloads with curl or wget are denied; for many hosts, run a private mirror with cvdupdate.
  • Startup. A new container with no database runs freshclam before clamd can start, and the official image waits up to 1,800 seconds for clamd by default. Every cold start pays the load again, which rules out scale-to-zero functions.
  • So run it as a separate service with its own memory and a volume for the databases, and have the app reach it over a private network. Don’t start clamd inside each web process.

Where clamd can run

Platformclamd inside the appWhat works
Vercel Functions † †No: 2 GB of memory on Hobby, 4 GB at most on Pro, and every cold instance would reload signaturesCall a clamd service you run elsewhere
Netlify Functions † †No: 1,024 MB by default, 4,096 MB at mostCall a clamd service you run elsewhere
AWS Lambda † † †Possible with up to 10,240 MB and a container image, but each cold start reloadsA clamd service on ECS or EC2, or GuardDuty Malware Protection for S3 for files already in a bucket
Render, Railway, Fly, Heroku, your own servers † †Only on a large instanceThe clamav/clamav image as a private service with 3 to 4 GiB and a volume

† Competitor details come from each vendor’s own published pages on the dates shown; prices and plans change, so check the vendor before you decide. Vercel Functions (2026-11-25) · Vercel Functions (2026-11-25) · Netlify Functions (2026-11-25) · Netlify Functions (2026-11-25) · AWS Lambda (2026-11-25) · AWS Lambda (2026-11-25) · AWS Lambda (2026-11-25) · Render, Railway, Fly, Heroku, your own servers (2026-11-25) · Render, Railway, Fly, Heroku, your own servers (2026-11-25)

Run clamd beside your app

Cisco Talos publishes clamav/clamav images. The stable tag includes the databases and runs freshclam in the container; mount a volume on /var/lib/clamav so restarts don’t download everything again. Keep port 3310 private: clamd has no authentication.

# clamd as its own service, with the signatures on a volume
docker volume create clam_db
docker run -d --name clamav --restart unless-stopped \
  --memory 4g \
  --mount source=clam_db,target=/var/lib/clamav \
  --publish 127.0.0.1:3310:3310 \
  clamav/clamav:stable

# The image starts freshclam too; the first start downloads the databases
docker logs -f clamav

# Smaller footprint, at the cost of pausing scans during each reload (clamd.conf)
ConcurrentDatabaseReload no

Where in the upload path to scan

  • Before you store, encrypt or pass the file on. Encrypted or re-compressed bytes can’t be scanned, and once a file is in a bucket other code may already be serving it.
  • Stream, don’t buffer. Pipe the upload to clamd as it arrives (or from a temporary file), with a size limit at or below StreamMaxLength. Holding a 100 MB upload in memory to scan it costs 100 MB per concurrent upload.
  • An error means not scanned. Timeouts, a refused connection, size limit exceeded or clamscan returning null must reject the file or queue it for a retry, never mark it clean.
  • Know what “OK” covers. Inside archives, files larger than MaxFileSize (100 MB by default) or beyond MaxScanSize and MaxRecursion are skipped silently and the archive still reports OK, unless you set AlertExceedsMax yes, which flags them as Heuristics.Limits.Exceeded.
  • Keep the other checks. A scan matches known signatures; it doesn’t make an upload safe to serve. Still check type and size, use random storage keys and serve user files as attachments (uploading and serving files).

Or skip clamd: the Canner scan service

On Canner, a Live project can turn on Virus scan in its Services tab and redeploy. The app gets CANNER_SCAN_SOCKET, a unix socket that speaks the standard ClamAV protocol, so there is no daemon to size, no freshclam to schedule and no memory to reserve. Signatures update daily. This is the Node.js client from the docs; the Python version is there too:

import net from "node:net";

export function scan(buffer) {
  return new Promise((resolve, reject) => {
    const socket = net.connect(process.env.CANNER_SCAN_SOCKET);
    const out = [];
    socket.on("data", (d) => out.push(d));
    socket.on("error", reject);
    socket.on("close", () => {
      const verdict = Buffer.concat(out).toString().replace(/\0$/, "").trim();
      if (verdict === "stream: OK") return resolve({ clean: true });
      const found = verdict.match(/^stream: (.+) FOUND$/);
      if (found) return resolve({ clean: false, signature: found[1] });
      reject(new Error(verdict || "scan failed")); // not scanned: do not treat as clean
    });
    socket.write("zINSTREAM\0");
    for (let i = 0; i < buffer.length; i += 65536) {
      const part = buffer.subarray(i, i + 65536);
      const size = Buffer.alloc(4);
      size.writeUInt32BE(part.length);
      socket.write(size);
      socket.write(part);
    }
    socket.end(Buffer.alloc(4)); // a zero-length chunk ends the stream
  });
}

What the scan service accepts

  • Any clamd client works when you give it the socket path instead of a host and port. Only INSTREAM, PING and VERSION are accepted.
  • Limits: 100 MB per file; 3 scans at once and 120 a minute per project.
  • Verdicts: stream: OK is clean and … FOUND names a signature. Anything else, including INSTREAM size limit exceeded. ERROR, too many scans at once or scanner unavailable, means the file was not scanned.
  • Files storage is already covered. Files uploaded to Canner’s Files storage are scanned automatically; this service is for files your app handles itself, such as before it encrypts them or sends them elsewhere.
  • Nothing leaves the server. The scanner runs on the same servers as your app, in Canada, with no network access, and Canner counts scans without recording file names or content.
  • When it isn’t the fit: files over 100 MB, more than 3 scans at once, or Starter projects (services are Live and above). Run your own clamd for those.

Questions

How much memory does ClamAV need?

ClamAV’s documentation says over 1.2 GiB just to load the signatures, about twice that briefly during a reload, and recommends 3 GiB minimum, 4 GiB preferred. ConcurrentDatabaseReload no lowers the peak by pausing scans during reloads.

Should I use clamscan or clamd for uploads?

clamd. clamscan loads the entire database on every run, which takes seconds; clamd keeps it in memory and scans a stream in milliseconds.

Which Node.js ClamAV library should I use?

clamscan is the most complete and supports unix sockets and TCP; its last release was 2.4.0 in October 2024. clamdjs is TCP-only and hasn’t had a release since 2019. The protocol is simple enough to write yourself.

Which Python library should I use?

clamav-client, a maintained fork of clamd with type hints. clamd and pyclamd still work but haven’t had a release since 2014 and 2017.

Can I run ClamAV on Vercel or Lambda?

Not inside a function in practice: memory is tight and every cold start reloads the signatures. Run clamd as a separate service and call it, or on AWS use GuardDuty Malware Protection for S3 for objects in a bucket.

Scan files without running clamd.

Live projects get a virus scan, media processing, document rendering, OCR and a cache as switches on the Services tab, on servers in Montréal.