An upload has two identities: the name the application checks and the handler the server uses. Security fails when those two disagree.
The filter uses a short blocklist of extensions. The web server is configured to execute other extensions. The attacker uploads a file with a missed extension and opens it in the public upload directory.
We trust the extension because it is visible. The server trusts the handler, which we cannot see.
What is actually at stake?
Before touching a payload, map the assets, the actors and the boundary that is supposed to protect them. A finding only becomes a vulnerability when you can connect the failed boundary to a real asset.
Assets
- Web server code execution
- Other users' files and sessions
- Storage and bandwidth
- Trusted document pipelines
Actors
- Authenticated user with an avatar or document upload
- Attacker with a public upload form
- Malicious file sent to a back-office reviewer
Trust boundaries
- Filename to filesystem path
- Content type to parser
- Upload root to executable root
- Uploaded document to preview renderer
Abuse cases
- Upload shell.php and request it
- Use a double extension such as shell.jpg.php
- Embed PHP in an image and include it through LFI
- Upload an SVG with script for stored XSS
- Use a filename with traversal to overwrite a config
Follow the decision, not the payload
Every family has a moment where trusted data becomes an authorization, parsing or state decision. Learn to point at that moment in the request path.
- 01
Receive
The server accepts multipart data with a filename, declared content type and bytes.
- 02
Name
The handler uses the client filename or derives an extension from it.
- 03
Validate
Checks may trust the Content-Type header, a blocklist or an extension that is easy to spoof.
- 04
Store
The file is written to a directory that may be web-accessible or executable.
- 05
Serve or parse
The file is downloaded, rendered, previewed or executed, and the risk depends on that sink.
The avatar that became a web shell
Composite case study from public upload disclosuresA composite case based on a common pattern: an application validated the declared image content type but stored files under their original extension in a web-accessible directory.
A tester uploaded a file named avatar.php with Content-Type image/png. The server accepted the header, stored the file, and the web server executed it on request.
The attacker obtained command execution as the web user, then read configuration and moved laterally. Even a low-privilege upload can become full server compromise.
Validation trusted client metadata, the extension was preserved, and the upload directory was executable.
Generate a random server-side name, validate the decoded content and re-encode images, store outside the web root or in object storage, and serve with a fixed content type.
Read it as a chain: discovery → failed decision → impact → fix. Then ask which link would have broken earliest.
A repeatable testing sequence
- Inspect allowed types, size limits and where files are stored.
- Try alternative executable extensions, case variants and double extensions.
- Check whether content type, magic bytes or content are validated.
- Upload a marker file and open it from the public path.
Where smart people get fooled
- A blocklist is never complete.
- Renaming the file client-side does not matter.
- A content-type check can be spoofed.
What would the logs show?
A control is stronger when someone can see it fail. These signals are starting points for a detection rule, not a complete SIEM use case.
- Uploads whose declared type differs from the detected content
- Executable extensions or double extensions in filenames
- Files written into web-accessible or executable directories
- A request to a recently uploaded file that returns code-like content
- Large or unusual files from one account
index=web sourcetype=app_logs action=upload | eval risky=if(match(filename,"(?i)\.(php|phtml|phar|jsp|aspx|sh|exe)(\.|$)") OR mime_declared!=mime_detected,1,0) | where risky=1 | stats count values(filename) by user_id, clientip | where count > 0Fix the decision, then prove it stays fixed
- Allowlist extensions and verify magic bytes.
- Store uploads outside the web root with random names.
- Disable execution in the upload directory and scan files.
const name = file.name;
await fs.writeFile(`public/uploads/${name}`, await file.arrayBuffer());const detected = await detectMime(file);
if (!["image/png", "image/jpeg"].includes(detected)) throw new Error("unsupported");
const key = `${randomUUID()}.${detected === "image/png" ? "png" : "jpg"}`;
await storage.put(key, await reencodeImage(file, detected), { contentType: detected });
return { key, url: `/media/${key}` };Do not trust the filename or Content-Type. Detect the bytes, re-encode active formats, generate the name, store outside the executable root, and serve through a handler with a safe content type.
Regression tests a reviewer would ask for
Executable extension
GivenAn upload form accepts images
WhenThe file is shell.php or shell.jpg.php
ThenThe upload is rejected and no executable file is stored
Content mismatch
GivenA file claims image/png
WhenThe bytes are PHP or HTML
ThenContent detection rejects or re-encodes it
Filename traversal
GivenThe client supplies a filename
WhenThe name contains ../ or an absolute path
ThenThe server ignores the client name and generates its own
Stored XSS via SVG
GivenSVG uploads are allowed
WhenThe SVG contains a script element
ThenIt is sanitized, served as an attachment or rejected
What this finding is not
Strong testers kill their own findings before someone else does. These are the shortcuts that turn a real bug into a weak report — or a false alarm into a wasted escalation.
- The upload rejects .php: test .phtml, .phar, double extensions, case variants and trailing dots or spaces.
- The Content-Type must be image/png: the header is client-controlled; verify the actual bytes.
- The file is stored outside the web root: it may still be reachable through a download handler or an include primitive.
- The image renders correctly: a polyglot can be a valid image and contain executable content.
Terms you should be able to use precisely
- Polyglot
- A file valid in two formats, such as an image that also contains executable code.
- Magic bytes
- The leading bytes of a file that identify its actual format.
- Double extension
- A filename such as shell.jpg.php that may be handled differently by validation and the web server.
- MIME sniffing
- A browser or server inferring content type from bytes rather than the declared header.
- Re-encoding
- Decoding and rewriting an image to strip embedded active content.
- Executable root
- A directory where the web server will run certain file types instead of serving them as data.
Answer these without reciting a payload
Why is checking the Content-Type header insufficient?
The client supplies it. An attacker can label a PHP file as image/png. The server must inspect the bytes and ideally re-encode the content.
How do you make uploads safe even if validation fails?
Store outside the executable root, generate random names, serve with a fixed content type and Content-Disposition, and never execute uploaded files.
What is the risk of allowing SVG uploads?
SVG is XML and can contain scripts, external references and entities. It should be sanitized, served as an attachment or converted to a raster image.
Can you separate the mechanism from the payload?
Four questions, graded on the server. Answers and explanations are released only after you submit, and signed-in learners keep a best score.
رفع الملفات خطر لأن السؤال ليس هل الملف صورة، بل ماذا سيفعل به الخادم لاحقًا: يخزنه، يعاينه، يضمّنه، أم ينفذه؟
لا تثق بالاسم ولا بنوع المحتوى المعلن. افحص البايتات، أعد ترميز الصور، وولّد اسمًا عشوائيًا. خزّن الملفات خارج جذر التنفيذ، وقدمها بنوع ثابت وتنزيل آمن. وإذا سمحت بـSVG فتذكّر أنه XML قابل للبرمجة، وليس صورة بريئة.
Which component decides whether this file is data or code?