A Quarantine-to-Clean S3 Upload Pipeline with Step Functions
A practical design for accepting untrusted uploads without exposing them to applications. It combines private S3 buckets, SQS buffering, Step Functions orchestration, malware scanning, and explicit failure handling.
The boundary: uploads are untrusted
A common upload flow gives an application a presigned S3 URL and then lets downstream code read the object as soon as it appears. That makes the upload path simple, but it also creates a dangerous assumption: that a successful PUT means the file is safe to process.
Use two private buckets instead: a quarantine bucket for incoming objects and a clean bucket for objects that passed inspection. Applications that serve or transform files receive read access only to the clean bucket.
Request and storage flow
- A client asks an authenticated API for an upload URL. The API generates a random object key, records an upload ID and expected owner in DynamoDB, and returns a short-lived presigned PUT URL.
- The URL is constrained to the server-generated key, expected content type, and a reasonable size range. The client cannot choose a bucket or write into the clean prefix.
- The quarantine bucket enforces Block Public Access, TLS, encryption with a customer-managed KMS key, and a lifecycle rule that expires abandoned uploads. Keep the original S3 object version ID; scanning a moving key without pinning its version can inspect different bytes from the ones later promoted.
- An S3 event is sent to SQS. The queue absorbs bursts and has a dead-letter queue with an alarm for messages that exceed the retry limit.
Scan and promote
A Step Functions workflow claims the upload ID with a conditional DynamoDB write keyed by object key and version ID. That makes retries idempotent. It then starts a scanner sized to the file: Lambda can be economical for small files, while an ECS Fargate task avoids Lambda duration and memory limits for larger files. The scanner reads only the exact quarantined version and returns a verdict plus a hash and scanner-definition version.
For a clean result, the workflow copies that exact version into the clean bucket, verifies the destination checksum, and changes the upload record to AVAILABLE. For a malicious or unsupported file, it records a reason and keeps the object isolated for a defined retention period; it never copies the object into the serving bucket. A failed scan remains PENDING_SCAN and is retried or surfaced for operator review rather than being treated as clean.
Permissions and operations
Give the URL-issuing role permission to write only to the quarantine bucket. Give the scanner read access to quarantine and write access to clean; give the serving application read access only to clean. Restrict KMS key use to those roles and the required S3 encryption context. Do not let a client-supplied tag or filename decide whether an object is trusted.
Track queue age, DLQ depth, scan duration, clean/rejected/error counts, and the age of uploads still pending. Alert on old pending uploads and on any transition to AVAILABLE without a matching scan record. Add a reconciliation job that compares upload records with object versions so a lost or delayed event can be repaired safely.
The tradeoff is extra storage, queueing, and scan cost in exchange for a clear trust boundary. Set file-size limits, expire quarantine objects quickly after a terminal result, and use a lifecycle policy on the clean bucket that matches the product's retention needs. Before launch, test duplicate S3 events, scanner timeouts, a DLQ replay, object replacement during a scan, and KMS denial. The acceptance check is simple: no file becomes readable by the application until the exact stored version has a recorded clean verdict.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article