Under the hood
Malware scanning for S3 buckets without bucket events
How Brewless scans every Scaleway bucket with ClamAV inside your own account, when Scaleway Object Storage sends no events: request logs, batches and tags.
In this post
Files that people upload are the classic way in. A Laravel application that accepts avatars, invoices or documents stores them in a bucket, and sooner or later someone uploads something that should not be there. On AWS, GuardDuty Malware Protection for S3 scans new objects as they arrive. On Scaleway there was no such thing, so we built Malware Protection into Brewless: ClamAV, scanning every bucket of a Scaleway account, inside that account.
This post is about the one thing that made it harder than it sounds: Scaleway Object Storage sends no events.
How do you notice a new file without bucket events?
You read what Object Storage does keep. Scaleway's S3 API lists PutBucketNotification as "in development", so there is nothing to subscribe to. But Object Storage writes a log line for every request it serves to Cockpit, Scaleway's observability service, at no charge:
{"s3_call":"PutObject","bucket":"shop-uploads","URI":"/avatars/1.png","http_status_code":200, ...}
The line has the operation, the bucket and the key, also for an upload through a presigned URL. So a small job, the watcher, asks Cockpit every few minutes for the PutObject, CopyObject, PostObject and CompleteMultipartUpload lines since its last look, and queues the files they name. It does not load ClamAV and takes seconds.
Scaleway does not say how quickly a request reaches Cockpit, or that every line arrives. So a second job, the reconciler, lists every protected bucket every few hours and compares it with what was scanned. Listing is free: Scaleway does not bill S3 requests. Whatever the logs missed, the reconciler finds.
The result does not depend on the application at all. A file uploaded by a second application, a script or the AWS CLI is found the same way as one from Laravel.
Why scan in batches?
Because ClamAV pays for its signatures before it scans anything. We measured ClamAV 1.5.5 with the current signatures, about 3.6 million of them:
| Time | Memory at its peak | |
|---|---|---|
| Loading the signatures | about 20 s | 974 MiB |
| 500 small files, after that | about 12 s | 974 MiB |
| One file of 1 GiB | depends on the file | about 1.2 GiB |
Loading takes the same twenty seconds whether one file waits or five hundred, and more CPUs do not make it faster. Starting ClamAV per file would mostly pay for loading. So the watcher starts the scan job only when something waits, and the job takes everything waiting in one run, at most one run per interval. A day without uploads costs no ClamAV run at all. A day with ten thousand uploads costs as many runs as a day with a thousand.
What does the result look like?
A tag on the file, for the exact version that was scanned:
| Tag | Value |
|---|---|
brewless-av |
clean, infected, unsupported or failed |
brewless-av-etag |
The ETag that was scanned |
brewless-av-at |
When |
The ETag is the point. Overwrite a clean file with something else and the new object has no tags, and even a tag copied along would name the old ETag. A reader that checks the ETag never takes an old clean for the new file. Before it writes the tag, the scanner checks that the file did not change while it was being scanned.
unsupported is the honest answer for what ClamAV cannot judge: files above its 2 GiB limit, encrypted archives, archives that unpack to more than the limits. Without AlertExceedsMax, ClamAV would call those clean.
Can you stop people downloading an infected file?
Not with a bucket policy on Scaleway. On AWS the usual answer is a policy that denies reads of objects tagged as infected. Scaleway's bucket policies support no conditions on object tags, and in the current policy version a Deny statement does nothing. A policy also replaces the one already on the bucket, so writing one onto a customer's bucket is not something Brewless will do.
What works is a staging folder. In protection mode, the application uploads to incoming/, and the scanner moves a file to its real place only when it is clean. An infected file goes to quarantine; a file that cannot be scanned in full stays where it is, never released. The application reads only outside the folder.
Where does it all run?
In the customer's own Scaleway account, like everything Brewless deploys. Brewless makes:
- an IAM application with a policy that reads buckets and objects and writes tags;
- a private bucket per region for the signatures, the queue and the quarantine;
- four Serverless Jobs: watch, scan, update and reconcile.
The files are read and scanned there. Brewless receives a signed report with what was found: the bucket, the file name, the size and the signature, never the file. The signatures come from ClamAV's own mirrors through freshclam, every four hours, into that private bucket, so a scan run copies them over the network of the same region instead of downloading them from the internet.
Scaleway bills the jobs per second. With a handful of uploads a day that is cents a month; with thousands a day, a few euros. The documentation has the details, the permissions and the limits.
Frequently asked questions
Does Scaleway Object Storage support bucket notifications?
Not yet. PutBucketNotification is listed by Scaleway as in development. Object Storage does write a log line for every request to Cockpit, with the operation, the bucket and the key, which Brewless reads to find new files.
How much memory does ClamAV need?
In our measurements, ClamAV 1.5 needed about 970 MiB to load its signatures, with a peak around 1.2 GiB while scanning a large file. Brewless runs the scan job with 4 GB by default, and offers 2 GB as a smaller size.
Can a bucket policy on Scaleway block files tagged as infected?
No. Scaleway's bucket policies do not support conditions on object tags, and Deny statements have no effect. Brewless keeps unscanned files out with a staging folder instead, from which only clean files are released.
Do my files leave my Scaleway account to be scanned?
No. The scanner runs as Serverless Jobs in your own account and reads the files there. Brewless only receives a signed report with the bucket, the file name, its size and what was found.