Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate MIME type against extensions #120

Merged
merged 10 commits into from
Oct 2, 2017
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
wp-cli.local.yml
node_modules/
vendor/
composer.lock
5 changes: 5 additions & 0 deletions command.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
return;
}

$autoload = dirname( __FILE__ ) . '/vendor/autoload.php';
if ( file_exists( $autoload ) ) {
require_once $autoload;
}

spl_autoload_register( function( $class ) {
$class = ltrim( $class, '\\' );
if ( 0 !== stripos( $class, 'runcommand\\Doctor\\' ) ) {
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"files": [ "command.php" ]
},
"require": {
"wp-cli/wp-cli": "*"
"wp-cli/wp-cli": "*",
"brightnucleus/mime-types": "^0.2.0"
},
"require-dev": {
"behat/behat": "~2.5"
Expand Down
2 changes: 2 additions & 0 deletions doctor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@ theme-update:
check: Theme_Update
php-in-upload:
check: PHP_In_Upload
validate-mime:
check: Validate_Mime
language-update:
check: Language_Update
13 changes: 13 additions & 0 deletions features/check-validate-mime.feature
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. |
53 changes: 53 additions & 0 deletions inc/checks/class-validate-mime.php
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' );
}
}