๐ Image Uploads: Validation & AVIF
Last updated๏ผ2026-11-25
๐ Admin Image Upload: Security Validation and Automatic AVIF Conversion
This chapter is for ops / deployment / security: what actually happens with the admin “Upload Image” feature. Key takeaway: uploaded images are automatically compressed and converted to AVIF (or WebP) in the customer’s browser before submission, and the server only performs “real type validation” as a fallback, with zero server-side image-processing dependencies. All processing happens in the browser; the server spends no CPU on transcoding.
๐ฏ Design Goals
| Goal | Implementation |
|---|---|
| Smallest size, fastest loading | Browser-side Canvas converts images to image/avif (preferred) / image/webp before upload |
| Anti-XSS / disguised files | The server validates the real format with image.DecodeConfig + file magic numbers; disguised files are rejected and deleted outright |
| Zero extra deployment dependencies | No server-side CGO (customer machines lack gcc); transcoding happens entirely in the browser, so Windows deployment is worry-free |
| Legacy browser protection | Browsers that don’t support AVIF encoding are blocked from /admin with a prompt to update |
| logo / WeChat QR code not compressed | These two inputs are marked data-nocompress and upload the original image directly to preserve clarity |
โ๏ธ Browser-side Conversion Rules
Every admin “Upload Image” “ input is intercepted by the front-end script, following this flow:
| Step | Behavior |
|---|---|
| 1. Read | Read in with FileReader and draw onto a canvas |
| 2. Resize | Proportionally scale to at most 1000ร900 (scaled down if larger, kept at original size if smaller) |
| 3. Encode | Prefer canvas.toBlob('image/avif'); fall back to image/webp when the browser doesn’t support AVIF, then to image/png |
| 4. Submit | Upload the resulting Blob as a file to /admin/upload/image |
Note: inputs with data-nocompress (site logo, WeCom QR code) skip the above resizing and re-encoding; the original image is uploaded directly, keeping icons crisp and undistorted.
Images inserted in the rich text editor (wangeditor) go through the same conversion: the customUpload hook first calls window.cmsCompressImage, then uploads via fetch, so body images are also converted to AVIF.
๐ Upload Whitelist
The admin “Upload Image” allows only the following extensions; everything else is rejected (error returned, nothing written to disk).
| Category | Extensions |
|---|---|
| Allowed for upload | .jpg .jpeg .png .gif .webp .avif |
| Removed | .svg (XSS injection risk), .bmp (large size, no compression) |
This is not the same thing as “static asset extensions referencable from the front end” โ although .svg cannot be referenced by front-end templates (see Template Packaging Specification), it can still be placed manually under /static/ as a static asset and referenced; it just cannot be uploaded via the admin.
๐ก๏ธ Server-side Real Type Validation (Fallback)
The front end can be bypassed (by crafting requests directly), so the server validates the real format of every uploaded image โ the last link in the security loop:
- JPG / PNG / GIF: actually decode one frame with image.DecodeConfig; if dimensions cannot be obtained, it’s judged forged and rejected
- WebP: validates the file header magic number RIFF....WEBP
- AVIF: validates the file header ftyp followed by avif / avis
- Disguised files failing validation: immediately deleted with os.Remove โ never placed in the upload directory, never stored in the database
So even if an attacker forges a .jpg extension carrying malicious content, the server blocks it and it never lands in the site directory.
๐ช AVIF Browser Gate (/admin Guard)
Before entering the admin, the page runs a real encoding capability probe (not just a feature check, but an actual canvas.toBlob('image/avif') to see whether a Blob with type==='image/avif' is truly produced), preventing browsers that silently fall back to PNG while claiming support.
| Probe Result | What the User Sees |
|---|---|
| AVIF encoding supported | Login box / admin displays normally; operations continue |
| Not supported | Full-screen red-text overlay “Please update your browser to the latest Chrome / Edge / Firefox”, hiding the login box; on pages already inside the admin, a red bar shows at the top and upload controls are disabled |
This step guarantees: any browser that can enter /admin is capable of converting images to AVIF, making the whole “browser-side compression to AVIF” pipeline work.
๐ง Tunable Parameters (Environment Variables)
Compression size and quality can be overridden via environment variables (defaults below apply when unset).
CMS_IMG_MAX_WIDTH=1000 # Max width (default 1000)
CMS_IMG_MAX_HEIGHT=900 # Max height (default 900)
CMS_IMG_MAX_QUALITY=82 # Lossy compression quality 0-100 (default 82)
The defaults of 1000ร900 and quality 82 are hard-coded as fallback in the source; they apply even if unset. After changing them, restart the service process for them to take effect (this is server-side configuration, not template hot-reload).
โ FAQ
| Question | Answer |
|---|---|
| Does the server need ImageMagick / libvips? | No. Transcoding happens entirely in the browser; the server only validates โ zero extra dependencies. |
| If the customer uploads a PNG, will it become AVIF on the front end? | Yes. The browser converts to AVIF (WebP if unsupported) before upload, so what lands on disk is AVIF. |
| The logo got blurred by compression? | The site logo and WeCom QR code inputs are marked data-nocompress; the original is uploaded directly without resizing. |
| Upload errors on old browsers? | That’s normal protection โ prompt to update the browser; this is by security design. |
| Can POSTing the upload API directly bypass the front end? | No. Server-side real type validation intercepts and deletes disguised files. |
[โ Template Packaging Specification](19-package.html) [Admin Extensions and Operations โ](21-backend.html)
CloudSite SaaS