📚 Encyclopedia

Web Development · updated 2026-10-13

Accepting and serving user uploads safely from a private bucket

uploadsobject-storages3streamingcontent-dispositionnosniffcorsthumbnailsretention

TLDR lets users attach any file type up to 500 MB to posts. Files live in a private Linode bucket and every byte is streamed back through the PHP API. This article covers the whole pipeline: why a streaming proxy instead of pre-signed redirects, how to serve arbitrary uploads without creating an XSS hole, how size limits stack, and how deletion, thumbnails and avatars fit in. Bucket and endpoint specifics are in Linode (Akamai) Object Storage.

Storage and serving

Allowing any file type without XSS

The first design stored uploads as <random>.tldr behind a download endpoint, to stop Apache from executing uploaded PHP. Moving storage to a bucket made uploaded code inert data, so the whitelist was dropped. What keeps it safe: - Always send X-Content-Type-Options: nosniff. - Inline-render only a small known set (jpg, jpeg, png, gif, webp, pdf, txt, json, stl). Everything else is application/octet-stream with Content-Disposition: attachment. That is what prevents an uploaded HTML or SVG from running script on the API's origin. - Sanitize the stored extension ([a-z0-9], max 16 chars); extension-less files (Makefile) are stored without a trailing dot. - Optional ?name=<original> restores the original filename on download; sanitize it (printable ASCII, no quote, backslash or slash). Browsers ignore the download attribute cross-origin, so the header is what names the file. - Path parameters: reject / \ .. and apply basename() before touching any path.

Size limits stack

A 500 MB limit needs all of: nginx client_max_body_size (default 1M per site, here 512M), PHP upload_max_filesize and post_max_size (512M), and the app's own check. A status endpoint tells clients the current limit; clients must call it with auth, see API authorization from the verified identity (and the holes it closes). A flag file (.uploads_disabled) is a kill switch; exclude it from rsync --delete deploys or every deploy re-enables uploads.

Deletion and retention

Thumbnails on demand

GET /thumbs/{filename} (public) returns a 168 px center-crop square WebP (q82, alpha kept), generated with GD on first request and stored back to the bucket as thumbs/<file>.webp, then served immutable. Fixed size only, no caller-chosen dimensions (no resize DoS). 404 for non-raster types, undecodable images, sources over 40 MB or 40 megapixels. Measured on the 1 GB server: a 140 KB PNG becomes 2.7 KB in 20 to 60 ms. The retention cron deletes the thumb with the original, best effort.

Avatars

POST /users/{u}/avatar (POST because PHP only fills $_FILES on POST), self or admin only, finfo check for png/jpeg/webp/gif, 2 MB cap. Object name avatar-<user>-<16 hex>.<ext>, new for every upload so immutable caching stays correct; the previous object is deleted best effort (only ever keys starting avatar-). The client crops to a 512x512 PNG before upload with an InteractiveViewer inside a RepaintBoundary and toImage(), no plugin.

See also

Linked from

Sources: tldr memory (plan-object-storage-migration, feature-allow-all-uploads, feature-post-thumbnails, release-v1.50.0, release-v1.31.0-audit, completed-work, howto-post-as-ai), posts #1378, #1397, #1658, #1612