Summary
Perl CGI versions before 5.00 return an uploaded file's client-supplied filename, including any path components, unchanged. If an application uses that value directly as a save path, an attacker may be able to write the upload outside the intended directory.
Affected context
The issue occurs in file-upload handling by CGI for Perl versions before 5.00: the returned upload filename is taken from the part's Content-Disposition header without removing client-supplied path components. The risk depends on how the application uses that value; applications that save uploads under the returned name without safely constraining the destination may allow writes outside the intended directory.
Exploit preconditions
- Attack surface: An application accepts file uploads through CGI for Perl.
- Required access: The attacker must be able to submit an upload; authentication requirements depend on the application.
- Trigger point: The application saves the uploaded file using the filename returned by CGI.
- Why exploitation is possible: Client-supplied path components are returned unchanged, and the application may resolve them outside its intended upload directory.
Why this happens
The upload-handling code preserves path information from an untrusted filename rather than ensuring that the value is only a filename. When an application uses that value to construct a filesystem path, traversal components may escape the intended directory. This maps to CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal').
Potential impact
- An attacker may write uploaded content outside the designated upload directory.
- Files may be overwritten or created if the application process has sufficient filesystem permissions.
- The affected application or host could be disrupted if sensitive or operational files are modified.
- Written files could be used to affect other components, depending on their location and how they are handled.
General mitigation guidance
Defensive patterns focus on treating upload filenames as untrusted metadata and controlling the destination independently of the supplied name.
- Prefer server-generated storage names and keep the original filename only as safely encoded display metadata.
- If retaining a client filename is necessary, reduce it to a basename and verify that the resolved destination remains within the designated upload directory.
- Use APIs and storage designs that constrain writes to a dedicated directory.
- Limit filesystem permissions for the application process and avoid granting write access to sensitive locations.
- Validate upload types and content as separate controls; validation does not replace safe path handling.
How teams detect issues like this
- Use static analysis to find upload-handling code that combines client-controlled filenames with filesystem paths.
- Review code for direct use of returned upload names in file creation, rename, or copy operations.
- Add tests and security checks that verify upload destinations remain within an approved directory for varied path formats.
- Enforce code-review or CI policies requiring server-generated storage names or verified path containment.

