PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.4.0
Defender Security – Malware Scanner, Login Security & Firewall v5.4.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / security-headers / class-sh-content-type-options.php

class-sh-content-type-options.php in Defender Security – Malware Scanner, Login Security & Firewall 5.4.0, at src/component/security-headers/class-sh-content-type-options.php

106 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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