| 1 |
<?php |
| 2 |
/** |
| 3 |
* Author: Hoang Ngo |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Defender\Component\Security_Headers; |
| 7 |
|
| 8 |
use WP_Defender\Component\Security_Header; |
| 9 |
|
| 10 |
class Sh_Content_Type_Options extends Security_Header { |
| 11 |
static $rule_slug = 'sh_content_type_options'; |
| 12 |
|
| 13 |
public function check() { |
| 14 |
$model = $this->get_model(); |
| 15 |
|
| 16 |
if ( ! $model->sh_content_type_options ) { |
| 17 |
return false; |
| 18 |
} |
| 19 |
if ( isset( $model->sh_content_type_options_mode ) && 'nosniff' === $model->sh_content_type_options_mode ) { |
| 20 |
return true; |
| 21 |
} |
| 22 |
|
| 23 |
$headers = $this->head_request( network_site_url(), self::$rule_slug ); |
| 24 |
if ( is_wp_error( $headers ) ) { |
| 25 |
$this->log( sprintf( 'Self ping error: %s', $headers->get_error_message() ) ); |
| 26 |
|
| 27 |
return false; |
| 28 |
} |
| 29 |
if ( isset( $headers['x-content-type-options'] ) && is_null( $model->sh_content_type_options_mode ) ) { |
| 30 |
$model->sh_content_type_options_mode = 'nosniff'; |
| 31 |
$model->save(); |
| 32 |
|
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @return array |
| 41 |
*/ |
| 42 |
public function get_misc_data() { |
| 43 |
$model = $this->get_model(); |
| 44 |
|
| 45 |
return array( |
| 46 |
'intro_text' => esc_html__( 'The X-Content-Type-Options header is used to protect against MIME sniffing attacks. The most common example of this is when a website allows users to upload content to a website, however the user disguises a particular file type as something else.', 'wpdef' ), |
| 47 |
'mode' => $model->sh_content_type_options_mode ?? 'nosniff', |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
public function add_hooks() { |
| 52 |
add_action( 'send_headers', array( $this, 'append_header' ) ); |
| 53 |
} |
| 54 |
|
| 55 |
public function append_header() { |
| 56 |
if ( headers_sent() ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
$model = $this->get_model(); |
| 60 |
if ( ! $this->maybe_submit_header( 'X-Content-Type-Options', false ) ) { |
| 61 |
//this mean Defender can't override the already output, marked to show notification |
| 62 |
|
| 63 |
return; |
| 64 |
} |
| 65 |
if ( true === $model->sh_content_type_options && 'nosniff' === $model->sh_content_type_options_mode ) { |
| 66 |
header( 'X-Content-Type-Options: nosniff' ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
public function get_title() { |
| 74 |
return __( 'X-Content-Type-Options', 'wpdef' ); |
| 75 |
} |
| 76 |
} |
| 77 |
|