Appearance
The antivirus service
ClamAV runs as a service of its own, with 1..N replicas, reached over a socket. No API instance embeds the engine. Why ClamAV, and what was refused in its place, is in ADR 0010; the contract is §9 of the specification.
The antivirus on one page
- One read, three consumers. The scan job opens the object once and streams it to
clamdwithINSTREAM— nothing is written to disk. The same bytes feed thesha256and the content-type detector on the way, so the hash, the type and the verdict are about exactly the same content. - Four answers, one verdict.
stream: OK,<signature> FOUND,ERRORandINSTREAM size limit exceeded. OnlyFOUNDsays something about the file; the other failures say something about the infrastructure and never release it — see The scan contract. - The limits, decided (D13).
StreamMaxLength/MaxFileSize64M,MaxScanSize400M,MaxRecursion16,MaxFiles10000, andAlertExceedsMaxon. clamd's default of 25M is exactly the largest profile in use and leaves no room for theINSTREAMframing — see clamd.conf. - Retries (D14). An engine error is retried after 1, 5 and 15 minutes — the first pass and three more, 120 seconds each — and the file stays unreleased throughout; see Quarantine, retries and the two alerts.
- The official probe is
GET /files/scanner-status, backed by the same reading as the health check and the two alerts (the queue, and a signature database older than 48 hours) — see Watching the scan. - A false positive has a path, and it is not a button. The file stays in quarantine; the answer is the corrected signature and a new upload.
The pass, step by step
FileScanJob decides nothing itself: it runs the registered IFileScanSteps in the order of FileScanStepOrder (Pipeline / Chain of Responsibility), and each step hands the pass on, ends it or wraps the rest of it.
| Order | Step | What it does |
|---|---|---|
| 100 | SelectScanCandidateStep | Reads which tenant the queued record belongs to — the only read with the tenant filter off, closed before the next step runs |
| 200 | OpenInTenantStep | Puts the record's tenant in session (no tenant for a product file) and loads it through the filter; wraps every step after it |
| 250 | RecordVerdictStep | Wraps the rest of the pass and writes the verdict notice (tenant, owner, outcome) into the outbox in the very save that commits each verdict, so the owner learns it after the commit, at least once (RN-GF-13). A file with no expected owner writes nothing |
| 300 | AdmitScanStep | Closes an attempt a dead worker left open, decides whether a pass may start, resolves the profile |
| 350 | PromoteFromTransitStep | Moves the upload off the transit key onto the final key, copying on the version it described; a different size or a refused copy purges the file with FILE_OBJECT_CHANGED_UNPROCESSABLE (RN-GF-18) |
| 400 | ReadAndScanStep | The single read: digest, content-type check and engine, capped at sizeBytes + 1 bytes |
| 420 | ReadIntegrityStep | A read longer or shorter than the confirmed size is refused the same way, unless the engine detected something |
| 500 | ApplyVerdictStep | Quarantine, type rejection, failure with retry, or release |
RN-GF-18 holds by construction too: the upload URL writes only under _transit/, and everything from step 350 on — the read, the quarantine copy, the download — touches only the final key, which no URL is ever signed to write. The refusal goes to the trail as stored_file.integrity_incident, and the tombstone (job:objeto-alterado) leaves without the file name (RN-GF-21).
RN-GF-08 holds by construction: the system scope exists only for the selection, and the verdict, the trail and the retry run in the record's own tenant with the filter on. A later story adds a step with an order between two of these and one registration line, without rewriting the job.
Why a daemon, and why not in the API
clamscan, the command-line scanner, reloads the entire signature database on every run — fine on a laptop, impossible on a server. clamd keeps the signatures in memory and answers over a socket.
That memory is also the reason the daemon is not a library call. The database is expensive enough that replicating it per process is out of the question: every API replica would carry its own copy for no gain in throughput. One shared clamd serves every tenant, because it sees bytes, not context — isolating it per tenant would multiply 4 GiB by N and buy nothing.
Memory is the resource that matters
| Request | 3 GiB minimum |
| Limit | 4 GiB |
The database sits in memory, and ConcurrentDatabaseReload — which is on, so scanning does not stop during an update — holds two copies of it while it swaps. That is what turns 3 GiB from "enough" into "enough until it reloads".
Short of memory, the kernel kills clamd and the container stays up. This is the nastiest failure the module has: the orchestrator sees a healthy service while the scan queue grows and not a single file is released.
Liveness and readiness: PING → PONG
The probe sends PING on the socket and requires PONG. It is the only signal that separates "the container is up" from "the engine answers" — the exact distinction the OOM kill erases. In the official image, clamdcheck.sh is literally echo PING | nc localhost 3310 compared against PONG, so the health check and the manual check are the same thing.
Readiness needs nothing extra: clamd binds its sockets only after the database is loaded, so a replica that answers PONG has a database, and one that does not answer takes no traffic.
Reporting and reacting are different jobs. The probe reports; restarting the replica or pulling it out of rotation belongs to the orchestrator. docker compose marks the container unhealthy and leaves it running — enough to see the failure on a developer's machine, and not a self-healing deployment. Scaling is the same kind of statement: the application dials an address, never a replica, so 1..N is a decision made where the service is deployed and nothing in the API changes with it.
The signature database lives in a volume
- The image is the
_basevariant, which ships no signatures. The database comes from the volume, never from an image layer. freshclamupdates it in place, daily by default (FRESHCLAM_CHECKS).- The volume persists across restarts and recreations. Rebuilding the container and downloading the whole database on every deploy is the fastest way to get blocked by the project's free CDN, and it is explicitly out of bounds.
VERSION is how the engine and the database version are read back. Those two values are what the module stores in scanEngine and scanDatabaseVersion on each scanned file, and the age of the database is what the 48-hour alert and GET /files/scanner-status are computed from — the alert that matters most, because everything looks fine while the engine quietly goes blind to anything catalogued since the last update.
The configuration is rendered, not written
clamd.conf is produced from the GrydFiles:Scanner section — see clamd.conf for every parameter and why it holds the value it holds. The file is mounted read-only, and nothing inside the container may rewrite the limits the application validated the profile ceilings against.
Bucket policy (D5)
Two things belong to the bucket or the CDN, not to the module:
X-Content-Type-Options: nosniff cannot be expressed by a presigned URL. The set of headers a presigned URL may override is closed, and none of the three providers admits this one. It therefore left the download contract and became a recommendation on the bucket or the CDN in front of it — where it applies to every response, including the ones the module does not issue. Where to set it on each CDN, and what defends the download without it, is in Download.
The _quarantine/ prefix must be closed. Infected content is kept as evidence, not as something retrievable: no presigned URL is ever issued for an object under that prefix, and the bucket policy should deny reads on it outside the credentials the module itself uses. A prefix that is merely "never linked to" is not a policy.
The _transit/ prefix needs a lifecycle rule. The scan deletes each transit object once it is copied, but the upload URL stays valid until it expires and a late PUT recreates the object, now with no record pointing at it. Expire everything under _transit/ a day after it was written; no record needs it for longer than its upload window and one scan. (With a GrydStorage:DefaultPrefix the physical prefix is {DefaultPrefix}/_transit/.) The rule assumes the scan queue does not stay backed up for longer than a day: an object expired before it is copied is gone, and the file ends in Failed with nothing to re-scan. The queue alert fires long before; raise the rule if you tolerate more.
Development
tools/GrydFiles.DevHost brings up PostgreSQL, MinIO and clamd together, so the whole flow runs on a developer's machine with no cloud. Its README.md has the commands; up.sh renders the configuration and then calls docker compose up, in that order.
The API is not one of those containers — it runs from the developer's machine against them, the same way tools/GrydAuth.AdminDevHost works.