Upload scanning
Every check in an upload handler trusts something the uploader chose. The Content-Type header is whatever the client typed, the filename is whatever the client typed, and the extension is part of the filename. Rename a program to holiday.jpg and all three agree. This reads byte 0.
What it stops
The scanner answers one question: is this file the kind of thing it says it is, and is it a kind of thing we accept at all? That covers the attacks a web application actually receives.
a renamed executable payload.exe as holiday.jpg, Content-Type: image/jpega web shell a .php, .jsp or .asp payload, whatever it is calleda polyglot a real image with code welded on after the end markera macro document .docx carrying vbaProject.binan app package .apk, .jar, .waran SVG a document that can carry script, not an imagea disk image or archive on a field that did not ask for onea decompression bomb refused from the header, before any pixels are allocatedknown malware only with ClamAV configured, below
Why the resize is not enough on its own
Grit re-encodes uploaded images, and that destroys an embedded payload for free: the output is encoded from decoded pixels, so nothing of the original file survives, not appended data and not metadata. It is a real defence and it covers exactly three types.
image/jpeg re-encoded = trueimage/png re-encoded = trueimage/webp re-encoded = trueimage/gif re-encoded = false <- and GIF is in the default allowlistimage/avif re-encoded = falseapplication/pdf re-encoded = falsevideo/mp4 re-encoded = false
A GIF is deliberately not re-encoded, because decoding and re-encoding one throws away the animation. That leaves a hole where nobody looks: a field declared file:image accepts GIFs, so a GIF with a payload appended after its terminator would be stored byte for byte. The same applies to any type the pipeline declines, and to any file on the OnError: StoreOriginal fallback path. The polyglot check is what covers all of them.
The two verdicts, and what each one does
malicious always refused, whatever the configuration says.An executable, an identified payload on an image, a macrodocument, an app package, known malware.suspicious recorded and allowed, by default.An archive on a field that did not ask for one, a PDF thatruns script when opened, an image with unrecognised bytesappended, an SVG.
UPLOAD_BLOCK_SUSPICIOUS=true once you have seen what your own traffic produces.Both upload paths
A file gets in two ways, and a check on one of them is not a check. Multipart uploads are scanned before anything is stored. A presigned upload goes from the browser straight to the bucket and never passes through the API at all, so there the object is fetched back, streamed to a temporary file, scanned, and deleted if it fails.
// internal/handlers/upload.go// Create: scan, then store.if h.scanOrRefuse(c, file, mimeType, header.Filename, acceptsList) {return}// CompleteUpload: already stored, so scan and delete on refusal.if stop := h.scanStoredOrRefuse(c, req.Key, req.ContentType, req.Filename, req.Accepts); stop {return}
The scan runs before the type allowlist, which is not an accident. With the allowlist first, an executable sent as application/octet-stream is refused as an unsupported type and nothing is recorded, so the most interesting upload anybody attempted leaves no trace.
What gets recorded
A refusal that is only a log line is a refusal nobody reads. Every verdict writes a threat_events row, visible at /system/threats in the admin, and a security event into the hash-chained activity log beside it so the record is tamper-evident.
Two fingerprints, answering different questions:
sha256 the file. "Have these exact bytes been tried before?"One refusal is somebody's mistake. The same payload undereleven names is somebody working.fingerprint the client: a hash of address, user agent and languages."Is this one actor?" Not an identity, and not treated asone: addresses are shared. It is for grouping, and twentyrefusals under one fingerprint is visible here when it isinvisible in a table keyed by account.
Plus the route, the request id, the user if there was one, the size, the name it arrived under, the rule that matched and what the bytes actually were.
Adding ClamAV
The signature layer knows categories. It does not know that a particular file is Win.Trojan.Agent-1234, and it never will, because that needs a signature database updated daily. ClamAV is the opt-in layer that does, spoken over its socket so the API stays a single static binary with no cgo.
# docker-compose.ymlclamav:image: clamav/clamav:stableports: ["3310:3310"]# .envCLAMAV_ADDR=tcp://clamav:3310 # or unix:///var/run/clamav/clamd.sockCLAMAV_FAIL_OPEN=false # no scanner, no upload
CLAMAV_FAIL_OPEN is the decision only you can make. When clamd is unreachable you either refuse every upload or allow every upload. The default is to refuse, which is safe and is also how a crashed sidecar stops uploads product-wide.
Checking it works
Upload the EICAR test file, which is the industry-standard string every scanner recognises. It is detected by name, so a refusal tells you the pipeline is live rather than that some other rule happened to catch it.
# The end-to-end tests ship in every generated project.cd apps/apigo test ./internal/scan/ ./internal/threats/ -vgo test ./internal/handlers/ -run Scan -v# A renamed executable, through the real handler:# scan: REFUSED holiday-photo.jpg [exe.pe] The file was uploaded as an# image but it is a Windows executable. sha256=5381... fingerprint=fe5e...
Every setting
CLAMAV_ADDR empty. tcp://host:3310 or unix:///path adds themalware-database layer.CLAMAV_FAIL_OPEN false. true allows uploads when clamd is down.UPLOAD_BLOCK_SUSPICIOUS false. true refuses the grey areas too.UPLOAD_QUARANTINE_PREFIX empty. A private prefix keeps the bytes of arefused upload for examination, named by contenthash. Empty records only the hash.
What it does not do
- It does not unpack archives. A zip is identified and, on an image field, refused, but the files inside are not extracted and scanned. Unpacking untrusted archives is its own attack surface.
- Without ClamAV it knows categories, not malware. A novel payload that is not a recognised executable format, on a field that accepts its type, passes.
- It is not a substitute for not executing uploads. A web shell is dangerous because something runs it. Uploads are served from the storage origin, never from a path the API executes, with
nosniffand a sandboxing Content-Security-Policy. This is the second line.
