Unvalidated upload filenames

Unvalidated upload filenames

Description

A submitted filename that contains ../ or ..\ can direct a write outside the upload directory if you use it in the storage path.

Potential impact

A file may be created wherever the process can write. An existing file can be damaged if the write API allows replacement. Code execution also requires a server configured to execute the file.

Remediation

  1. Generate a storage name on the server, for example with a UUID.
  2. Apply an allow-list to submitted names. A pattern such as ^[a-zA-Z0-9._-]+$ restricts characters. Reject empty names, . and .. as well.
  3. Check that the destination stays inside a server-controlled directory such as /var/app/uploads.

Examples

For these javax.servlet excerpts, create uploads outside executable web paths and restrict who can modify it. Configure multipart handling, authentication, authorization, CSRF protection, size and content checks, and error handling separately.

Before

java
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/upload")
public class FileUploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Part filePart = request.getPart("file");
String fileName = filePart.getSubmittedFileName(); // Use the submitted name directly.
Files.copy(filePart.getInputStream(), Paths.get("uploads", fileName));
}
}

After

java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/upload")
public class SecureFileUploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Part filePart = request.getPart("file");
String fileName = filePart.getSubmittedFileName();

        if (fileName == null || fileName.equals(".") || fileName.equals("..") || !isFileNameAllowed(fileName)) {
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid file name");
            return;
        }

        // Generate the stored name on the server.
        String storedFileName = UUID.randomUUID().toString() + ".txt";
        Path targetPath = Paths.get("uploads").resolve(storedFileName);
        Files.copy(filePart.getInputStream(), targetPath);
    }

    private boolean isFileNameAllowed(String fileName) {
        return fileName.matches("^[a-zA-Z0-9._-]+$"); // Check the permitted filename pattern.
    }
}

The first Files.copy() uses the submitted name and fails by default if the destination already exists. The revised handler accepts only permitted names and stores the file under a new UUID with a .txt suffix. Check the contents separately to confirm the file is text.

References