Security

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.

It is already onThe signature layer runs on every upload in every generated project, with no configuration and no dependencies. There is nothing to switch on and no way to switch it off. What is configurable is the second layer, and what happens to the grey areas.

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/jpeg
a web shell a .php, .jsp or .asp payload, whatever it is called
a polyglot a real image with code welded on after the end marker
a macro document .docx carrying vbaProject.bin
an app package .apk, .jar, .war
an SVG a document that can carry script, not an image
a disk image or archive on a field that did not ask for one
a decompression bomb refused from the header, before any pixels are allocated
known 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 = true
image/png re-encoded = true
image/webp re-encoded = true
image/gif re-encoded = false <- and GIF is in the default allowlist
image/avif re-encoded = false
application/pdf re-encoded = false
video/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 macro
document, an app package, known malware.
suspicious recorded and allowed, by default.
An archive on a field that did not ask for one, a PDF that
runs script when opened, an image with unrecognised bytes
appended, an SVG.
Why suspicious is allowed by defaultSome camera and editing software appends bytes to a JPEG. Refusing those on day one means the scanner's first visible effect is rejecting a customer's photograph, and a security check with a false-positive rate gets switched off, permanently, by your own team. They are recorded instead. Set 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 under
eleven 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 as
one: addresses are shared. It is for grouping, and twenty
refusals under one fingerprint is visible here when it is
invisible 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.

The uploader is told less than you areThe response is "This file was rejected by a security scan", with no rule name. The detail is in the admin. Naming the rule lets somebody tune a payload against it one upload at a time.

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.yml
clamav:
image: clamav/clamav:stable
ports: ["3310:3310"]
# .env
CLAMAV_ADDR=tcp://clamav:3310 # or unix:///var/run/clamav/clamd.sock
CLAMAV_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/api
go test ./internal/scan/ ./internal/threats/ -v
go 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 the
malware-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 a
refused upload for examination, named by content
hash. 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 nosniff and a sandboxing Content-Security-Policy. This is the second line.