Sub topic 8
Scanning and quarantining untrusted files
Validation checks the size, the declared type, and the first bytes of a file. It cannot tell you whether the file is harmful. A valid PDF can carry a malicious script, and an Office document can hide a macro. This chapter assumes you know presigned uploads and basic validation from the earlier chapters. It covers how to keep unscanned files away from users until a scanner approves them.
Why scanning is a separate step
Content validation looks at structure. Malware scanning looks at meaning: known signatures, suspicious macros, embedded executables, and archive bombs. Scanning is slow and can take seconds to minutes for large files, so it should not run inside the upload request. Run it after the upload, asynchronously.
The quarantine pattern
Quarantine means the file exists in storage but nothing can serve it. The usual design uses two locations:
- An incoming bucket or prefix where clients upload. No CDN origin, public read, or signed delivery URL points at it.
- A clean bucket or prefix that the CDN and your application read from.
A file moves from incoming to clean only after the scanner passes it. Files that fail go to a rejected location or are deleted.
Track status in your database
Store a status for every upload, such as pending, available, or rejected. Your API returns a media URL only when the status is available. This keeps a user from sharing a link to a file that has not been scanned. The status check matters more than the bucket layout, because it is the gate your application enforces.
A scanner worker
The storage event triggers a worker. The worker downloads the file, scans it, and records the result. This sketch uses the ClamAV command line scanner, which exits with 0 for clean, 1 for infected, and 2 for an error.
import subprocess
import boto3
s3 = boto3.client("s3")
INCOMING = "example-uploads"
CLEAN = "example-clean"
def scan_and_promote(key: str) -> str:
local_path = "/tmp/scan-target"
s3.download_file(INCOMING, key, local_path)
result = subprocess.run(
["clamscan", "--no-summary", local_path],
capture_output=True,
text=True,
)
if result.returncode == 0:
s3.copy_object(
Bucket=CLEAN,
Key=key,
CopySource={"Bucket": INCOMING, "Key": key},
)
s3.delete_object(Bucket=INCOMING, Key=key)
return "available"
if result.returncode == 1:
s3.delete_object(Bucket=INCOMING, Key=key)
return "rejected"
raise RuntimeError(f"Scanner error: {result.stderr}")The worker raises an error when the scanner fails. The file then stays pending and the event is retried. It is never treated as clean.
Common errors
- Failing open. If the scanner is down or times out, the file must stay quarantined. Never promote a file because the scan did not finish.
- Serving from incoming. A CDN origin or public bucket policy that covers the incoming prefix defeats the whole design.
- Scanning only once. Signature databases update daily. Consider rescanning stored files periodically.
- Ignoring archives. Set limits on extraction depth, total unpacked size, and file count, or a small zip can exhaust the scanner.
- Trusting the scan completely. Scanners miss new threats. Combine them with safe serving headers and a separate domain for user content, covered in a later chapter.
Exercise
A scanner worker crashes halfway through scanning a file. What should happen to the file and its database status, and why?
Show answer
The file stays in the incoming location and its status stays pending, and the event is retried. Promoting it would fail open and let an unscanned file reach users.