Upload Handling and Archive Extraction
Follow a logo and a backup archive through content checks, storage, extraction, and the response a browser receives.
You’re reading a preview. Premium includes the full lesson and specialist practice. View the full curriculum.
A workspace accepts a logo and a backup ZIP. The upload code cleans the outer filename. Is that enough to make either upload safe?
For the logo, the important question is what the saved bytes can do when a browser opens them. For the ZIP, each name inside the archive can choose a write destination. These are two separate journeys:
Logo: supplied bytes → validate image → controlled storage → browser response
ZIP: outer file → inspect every entry → confined writes → publish permitted output
We'll inspect those journeys separately. A tidy outer filename does not establish an image's content type or contain the entries inside an archive.
The logo: inspect the bytes and the browser response
A filename helps select a storage location. MIME metadata describes a claimed type. Neither proves the bytes are a permitted image. Inspect extension checks, decoder behavior, storage permissions, random naming, response headers, serving origin, and access policy as distinct decisions.
# Unsafe original logo flow.
name = os.path.basename(upload.filename)
path = os.path.join(LOGO_ROOT, name)
write_bytes(path, upload.read())
logos.save(path=path, mime=upload.content_type)
# Later download: caller-supplied MIME becomes response type.
return send_file(logo.path, mimetype=logo.mime)
An HTML file served as text/html under the application origin can become an active page when navigated to. This differs from claiming every SVG loaded through an img element executes script. Embedding mode matters. If templates referencing the upload are not shown, do not invent an unsafe embedding path; the reachable navigation/serving route may be the concrete active-content channel.
Select the stages and distinguish naming from interpretation.
Accept. Filename and MIME are claims supplied by the sender.
filename: logo.html
content-type: text/html
bytes: attacker-controlled
Interpret. Image decoders and archive entry writers grant different capabilities.
image = bounded_decode(raw)
# or: each entry selects a filesystem operation
Publish. Only validated server-owned output becomes visible under the intended origin and tenant.
Content-Type: image/png
X-Content-Type-Options: nosniff
Review question: A safe outer filename cannot authorize active content or every inner archive path.