Created
November 21, 2024 22:56
-
-
Save kharissulistiyo/3c2edb46a5ef67eb917ec72cb81a812d to your computer and use it in GitHub Desktop.
Bad PHP code sample: Arbitrary File Upload vulnerability
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function upload_file() { | |
$uploadedfile = $_FILES['file']; | |
$image_url = $uploadedfile['tmp_name']; | |
$upload_dir = wp_upload_dir(); | |
$image_data = file_get_contents( $image_url ); | |
$filename = $uploadedfile['name']; | |
$file = $upload_dir['basedir'] . '/dlm_uploads/' . date( 'Y/m/' ) . $filename; | |
if ( ! file_put_contents( $file, $image_data ) ) { | |
wp_send_json_error( array( 'errorMessage' => esc_html__( 'Failed to write the file at: ', 'download-monitor' ) . $file ) ); | |
} | |
$wp_filetype = wp_check_filetype( $filename, null ); | |
$attachment = array( | |
'post_mime_type' => $wp_filetype['type'], | |
'post_title' => sanitize_file_name( $filename ), | |
'post_content' => '', | |
'post_status' => 'inherit', | |
); | |
$attach_id = wp_insert_attachment( $attachment, $file ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment