Summary
Modern Image Gallery App v1.0 contains an unrestricted file upload flaw in gallery/upload.php that allows arbitrary PHP files to be uploaded by spoofing the MIME type as an image.
Because the application preserves attacker-controlled file extensions and fails to validate file content, an unauthenticated user may achieve remote code execution and full system compromise.
Affected context
This vulnerability occurs in deployments of Sourcecodester Modern Image Gallery App v1.0 where the gallery/upload.php endpoint is exposed for image uploads.
The issue arises when the server:
- Accepts files from unauthenticated users or broadly authenticated roles
- Relies primarily on client-supplied MIME types for validation
- Preserves the original filename and extension when saving to a web-accessible directory
In such environments, uploaded files may be directly executable by the PHP runtime if placed under the document root.
Exploit preconditions
- Attack surface: Publicly accessible
gallery/upload.phpimage upload endpoint. - Required access: No prior authentication; attacker only needs HTTP access to the upload interface.
- Trigger point: Submission of a crafted upload request where the file contains PHP code and the MIME type is spoofed as an image (e.g.,
image/jpeg). - Why exploitation is possible:
- The server does not enforce strong server-side validation of file type/content.
- The application preserves user-controlled file extensions when saving.
- Uploaded files are stored in a location executed by the PHP interpreter when requested, allowing the attacker to invoke the uploaded script via a direct URL.
Why this happens
The underlying bug is a classic unrestricted file upload problem (CWE-434).
The upload handler trusts user-controlled metadata (such as MIME type) and does not robustly inspect or constrain the actual content and type of the uploaded file.
Additionally, the system preserves attacker-supplied file extensions and stores uploads in a web-exposed directory where PHP files are executable.
This lack of server-side type enforcement and unsafe storage path turns a benign image upload feature into an arbitrary code deployment channel.
Potential impact
- Remote execution of arbitrary PHP code on the server.
- Full compromise of the web application and its data.
- Lateral movement to other systems reachable from the compromised host.
- Deployment of web shells for persistent access.
- Exfiltration or tampering of databases and configuration files.
- Use of the compromised server for further attacks, such as hosting malware or phishing pages.
General mitigation guidance
Effective mitigations focus on constraining what can be uploaded, where it can be stored, and how it can be accessed:
- Enforce strict server-side validation of file types, using mechanisms such as checking file signatures ("magic numbers") rather than trusting MIME types or extensions.
- Maintain a strict allowlist of permitted file extensions (e.g.,
.jpg,.png,.gif) and reject anything else. - Store uploaded files outside of web-executable directories, serving them via a controlled mechanism that does not execute them as code.
- Configure the web server to treat upload directories as non-executable, disabling PHP or other dynamic handlers there.
- Normalize and override user-supplied filenames and extensions instead of preserving them, removing any special characters or executable suffixes.
- Apply least-privilege principles to the web server user so that, if a flaw is exploited, the attack surface and accessible resources are minimized.
How teams detect issues like this
Detection generally involves both proactive code inspection and runtime monitoring:
- Static analysis & code review:
- Flag upload endpoints that write user files under the document root or executable directories.
- Look for direct use of user-supplied filenames and extensions in filesystem operations.
- Identify upload logic that trusts
$_FILES['type'], client headers, or missing checks on file content. - Security testing:
- Incorporate file upload abuse tests into automated DAST/penetration testing, including attempts to upload scripts with spoofed MIME types.
- Runtime and log monitoring:
- Monitor web server logs for suspicious requests to uploaded file paths, especially files with atypical extensions or recently created files.
- Detect anomalies such as unexpected outbound connections or command execution traces on the web host.
- Automated security scanning tools can help surface unsafe upload patterns, but manual validation of upload flow and server configuration remains important.

