-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from Sidsector9/feature/GH#88
GH#88 Added feature to validate MIME type against extensions
- Loading branch information
Showing
6 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
wp-cli.local.yml | ||
node_modules/ | ||
vendor/ | ||
composer.lock |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Feature: Detect files having extensions with incorrect MIME-types | ||
|
||
Scenario: Detect a .png file with PHP code | ||
Given a WP install | ||
And a wp-content/uploads/image.png file: | ||
""" | ||
<?php malicious_code(); | ||
""" | ||
|
||
When I run `wp doctor check validate-mime` | ||
Then STDOUT should be a table containing rows: | ||
| name | status | message | | ||
| validate-mime | warning | Files detected with different MIME type. | |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace runcommand\Doctor\Checks; | ||
|
||
use WP_CLI; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use BrightNucleus\MimeTypes\MimeTypes; | ||
|
||
/** | ||
* Warns when the extension of a file doesn't match the MIME type. | ||
*/ | ||
class Validate_Mime extends Check { | ||
|
||
/** | ||
* Array containing list of files found in the uploads folder | ||
* | ||
* @var array | ||
*/ | ||
protected $php_files_array = array(); | ||
|
||
|
||
public function run() { | ||
|
||
// Path to the uploads folder. | ||
$wp_content_dir = wp_upload_dir(); | ||
$directory = new RecursiveDirectoryIterator( $wp_content_dir['basedir'], RecursiveDirectoryIterator::SKIP_DOTS ); | ||
$iterator = new RecursiveIteratorIterator( $directory, RecursiveIteratorIterator::CHILD_FIRST ); | ||
|
||
foreach ( $iterator as $file ) { | ||
$file_path = $file->getPathname(); | ||
$file_extension = $file->getExtension(); | ||
$file_mime_type = mime_content_type( $file_path ); | ||
|
||
if ( 'directory' !== $file_mime_type ) { | ||
$mime_types = MimeTypes::getTypesForExtension( $file_extension ); | ||
|
||
if ( is_array( $mime_types ) && ! in_array( $file_mime_type, $mime_types ) ) { | ||
$this->php_files_array[] = $file; | ||
} | ||
} | ||
} | ||
|
||
if ( ! empty( $this->php_files_array ) ) { | ||
$this->set_status( 'warning' ); | ||
$this->set_message( 'Files detected with different MIME type.' ); | ||
return; | ||
} | ||
|
||
$this->set_status( 'success' ); | ||
$this->set_message( 'All files have valid MIMEs' ); | ||
} | ||
} |