| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the security header for 'X-Content-Type-Options'. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component\Security_Headers |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component\Security_Headers; |
| 9 |
|
| 10 |
use WP_Defender\Component\Security_Header; |
| 11 |
|
| 12 |
/** |
| 13 |
* Manages the 'X-Content-Type-Options' security header. |
| 14 |
*/ |
| 15 |
class Sh_Content_Type_Options extends Security_Header { |
| 16 |
|
| 17 |
/** |
| 18 |
* Unique identifier for this security header rule. |
| 19 |
* |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
public static $rule_slug = 'sh_content_type_options'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Checks if the Policy should be applied based on the current settings and site configuration. |
| 26 |
* |
| 27 |
* @return bool True if the header should be applied, false otherwise. |
| 28 |
*/ |
| 29 |
public function check() { |
| 30 |
$model = $this->get_model(); |
| 31 |
|
| 32 |
if ( ! $model->sh_content_type_options ) { |
| 33 |
return false; |
| 34 |
} |
| 35 |
if ( isset( $model->sh_content_type_options_mode ) && 'nosniff' === $model->sh_content_type_options_mode ) { |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
$headers = $this->head_request( network_site_url(), self::$rule_slug ); |
| 40 |
if ( is_wp_error( $headers ) ) { |
| 41 |
$this->log( sprintf( 'Self ping error: %s', $headers->get_error_message() ), wd_internal_log() ); |
| 42 |
|
| 43 |
return false; |
| 44 |
} |
| 45 |
if ( isset( $headers['x-content-type-options'] ) && is_null( $model->sh_content_type_options_mode ) ) { |
| 46 |
$model->sh_content_type_options_mode = 'nosniff'; |
| 47 |
$model->save(); |
| 48 |
|
| 49 |
return true; |
| 50 |
} |
| 51 |
|
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Retrieves miscellaneous data related to the Policy. |
| 57 |
* |
| 58 |
* @return array Contains introductory text, mode, and values for the Policy. |
| 59 |
*/ |
| 60 |
public function get_misc_data() { |
| 61 |
$model = $this->get_model(); |
| 62 |
|
| 63 |
return array( |
| 64 |
'intro_text' => esc_html__( |
| 65 |
'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.', |
| 66 |
'defender-security' |
| 67 |
), |
| 68 |
'mode' => $model->sh_content_type_options_mode ?? 'nosniff', |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Registers hooks related to sending headers. |
| 74 |
*/ |
| 75 |
public function add_hooks() { |
| 76 |
add_action( 'send_headers', array( $this, 'append_header' ) ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Appends the header to the response. |
| 81 |
*/ |
| 82 |
public function append_header() { |
| 83 |
if ( headers_sent() ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
$model = $this->get_model(); |
| 87 |
if ( ! $this->maybe_submit_header( 'X-Content-Type-Options', false ) ) { |
| 88 |
// this mean Defender can't override the already output, marked to show notification. |
| 89 |
|
| 90 |
return; |
| 91 |
} |
| 92 |
if ( true === $model->sh_content_type_options && 'nosniff' === $model->sh_content_type_options_mode ) { |
| 93 |
header( 'X-Content-Type-Options: nosniff' ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Retrieves the title of the Policy. |
| 99 |
* |
| 100 |
* @return string The title of the Policy. |
| 101 |
*/ |
| 102 |
public function get_title() { |
| 103 |
return esc_html__( 'X-Content-Type-Options', 'defender-security' ); |
| 104 |
} |
| 105 |
} |
| 106 |
|