Secure file uploads in a WordPress plugin
An upload form is the shortest way from the internet to the server’s disk. If a plugin accepts a file because its name ends with .pdf or because the browser said Content-Type: application/pdf, an attacker uploads shell.php with a fake type and calls it in the browser. WordPress has the checks that prevent this; a plugin only has to use them instead of moving the file itself.
Problem: A plugin lets members upload a document (PDF or image) to their profile. The handler checks the extension and calls move_uploaded_file() into the uploads folder, where anyone who guesses the URL can open the documents of other members.
Solution: Check nonce and capability, limit the size, pass an allowlist of MIME types to wp_handle_upload() so that WordPress verifies the real file type with wp_check_filetype_and_ext(), store the files in a private subfolder with a deny-all rule, and deliver them through PHP after a permission check.
The upload handler
add_action( 'admin_post_ha_upload_document', 'ha_handle_document_upload' );
function ha_handle_document_upload(): void {
check_admin_referer( 'ha_upload_document' );
if ( ! is_user_logged_in() || ! current_user_can( 'read' ) ) {
wp_die( esc_html__( 'You are not allowed to upload documents.', 'textdomain' ), 403 );
}
$file = $_FILES['ha_document'] ?? null;
if ( ! $file || UPLOAD_ERR_OK !== $file['error'] ) {
wp_die( esc_html__( 'The upload failed.', 'textdomain' ), 400 );
}
if ( $file['size'] > 5 * MB_IN_BYTES ) {
wp_die( esc_html__( 'The file is larger than 5 MB.', 'textdomain' ), 413 );
}
require_once ABSPATH . 'wp-admin/includes/file.php';
add_filter( 'upload_dir', 'ha_private_upload_dir' );
$result = wp_handle_upload( $file, array(
'test_form' => false, // the form is ours, the nonce was checked above
'mimes' => array(
'pdf' => 'application/pdf',
'jpg|jpeg|jpe' => 'image/jpeg',
'png' => 'image/png',
),
) );
remove_filter( 'upload_dir', 'ha_private_upload_dir' );
if ( isset( $result['error'] ) ) {
wp_die( esc_html( $result['error'] ), 415 );
}
update_user_meta( get_current_user_id(), 'ha_document', array(
'file' => $result['file'], // absolute path, never shown to the user
'type' => $result['type'],
) );
wp_safe_redirect( add_query_arg( 'uploaded', '1', wp_get_referer() ?: home_url( '/' ) ) );
exit;
}
function ha_private_upload_dir( array $dirs ): array {
$dirs['subdir'] = '/ha-private';
$dirs['path'] = $dirs['basedir'] . '/ha-private';
$dirs['url'] = $dirs['baseurl'] . '/ha-private';
return $dirs;
}
With mimes in the overrides, wp_handle_upload() accepts only these types, and because test_type stays at its default true, it runs wp_check_filetype_and_ext(): the extension must match the allowlist, and the real type is read from the file content (with getimagesize() for images and the PHP fileinfo extension for other files). It also gives the file a unique, sanitized name, so an existing file is never overwritten. The form itself and the nonce: form submissions with admin_post.
Close the private folder
# wp-content/uploads/ha-private/.htaccess (Apache 2.4) Require all denied
# Nginx, inside the server block
location ^~ /wp-content/uploads/ha-private/ {
deny all;
}
The rule blocks every direct request to the folder, including any PHP file that might get there. For the whole uploads folder, PHP execution should be disabled anyway: disable PHP execution in uploads.
Deliver the file after a permission check
add_action( 'admin_post_ha_download_document', 'ha_download_document' );
function ha_download_document(): void {
$user_id = absint( $_GET['user'] ?? 0 );
check_admin_referer( 'ha_download_document_' . $user_id );
if ( get_current_user_id() !== $user_id && ! current_user_can( 'edit_users' ) ) {
wp_die( esc_html__( 'You are not allowed to open this document.', 'textdomain' ), 403 );
}
$doc = get_user_meta( $user_id, 'ha_document', true );
if ( empty( $doc['file'] ) || ! is_readable( $doc['file'] ) ) {
wp_die( esc_html__( 'Document not found.', 'textdomain' ), 404 );
}
nocache_headers();
header( 'Content-Type: ' . $doc['type'] );
header( 'Content-Disposition: attachment; filename="' . basename( $doc['file'] ) . '"' );
header( 'Content-Length: ' . filesize( $doc['file'] ) );
header( 'X-Content-Type-Options: nosniff' );
readfile( $doc['file'] );
exit;
}
The link for the owner is built with wp_nonce_url( admin_url( 'admin-post.php?action=ha_download_document&user=' . $user_id ), 'ha_download_document_' . $user_id ). The user ID in the URL alone would be an IDOR: changing the number would open the document of another member, the capability check prevents that.
NOTE: Content-Disposition: attachment and nosniff matter even for allowed types: a PDF or an image that is displayed inline on your domain can still carry scripts that some viewers execute. Do not allow SVG in such a form at all, it is XML that can contain JavaScript. Saving files that the code itself downloads or creates (not a user form): programmatic file uploads with wp_handle_upload.