| 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_X_Frame extends Security_Header { |
| 11 |
static $rule_slug = 'sh_xframe'; |
| 12 |
|
| 13 |
public function check() { |
| 14 |
$model = $this->get_model(); |
| 15 |
|
| 16 |
if ( ! $model->sh_xframe ) { |
| 17 |
return false; |
| 18 |
} |
| 19 |
if ( isset( $model->sh_xframe_mode ) && ! empty( $model->sh_xframe_mode ) ) { |
| 20 |
return true; |
| 21 |
} |
| 22 |
$headers = $this->head_request( network_site_url(), self::$rule_slug ); |
| 23 |
if ( is_wp_error( $headers ) ) { |
| 24 |
$this->log( sprintf( 'Self ping error: %s', $headers->get_error_message() ) ); |
| 25 |
|
| 26 |
return false; |
| 27 |
} |
| 28 |
|
| 29 |
if ( isset( $headers['x-frame-options'] ) ) { |
| 30 |
$header_xframe = is_array( $headers['x-frame-options'] ) ? $headers['x-frame-options'][0] : $headers['x-frame-options']; |
| 31 |
|
| 32 |
$content = strtolower( trim( $header_xframe ) ); |
| 33 |
// If deprecated header directive is ALLOW-FROM then redirect to Sameorigin tab. |
| 34 |
if ( stristr( $content, 'allow-from' ) ) { |
| 35 |
$model->sh_xframe_mode = 'sameorigin'; |
| 36 |
} elseif ( in_array( strtolower( $content ), array( 'sameorigin', 'deny' ), true ) ) { |
| 37 |
$model->sh_xframe_mode = strtolower( $content ); |
| 38 |
} |
| 39 |
$model->save(); |
| 40 |
|
| 41 |
return true; |
| 42 |
} |
| 43 |
|
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
public function get_misc_data() { |
| 48 |
$model = $this->get_model(); |
| 49 |
|
| 50 |
return array( |
| 51 |
'intro_text' => __( "The X-Frame-Options HTTP response header controls whether or not a browser can render a webpage inside a <frame>, <iframe> or <object> tag. Websites can avoid clickjacking attacks by ensuring that their content isn't embedded into other websites.", 'wpdef' ), |
| 52 |
/** |
| 53 |
* Directive ALLOW-FROM is deprecated. |
| 54 |
* @since 2.5.0 |
| 55 |
*/ |
| 56 |
'mode' => ( isset( $model->sh_xframe_mode ) && 'allow-from' !== $model->sh_xframe_mode ) |
| 57 |
? $model->sh_xframe_mode |
| 58 |
: 'sameorigin', |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
public function add_hooks() { |
| 63 |
add_action( 'send_headers', array( $this, 'append_header' ) ); |
| 64 |
add_filter( 'defender_filtering_data_settings', array( $this, 'filtering_headers' ) ); |
| 65 |
} |
| 66 |
|
| 67 |
public function filtering_headers( $data ) { |
| 68 |
if ( ! isset( $data['sh_xframe'] ) ) { |
| 69 |
return $data; |
| 70 |
} |
| 71 |
// Directive ALLOW-FROM is deleted. |
| 72 |
if ( isset( $data['sh_xframe_mode'] ) && 'allow-from' === $data['sh_xframe_mode'] ) { |
| 73 |
$data['sh_xframe_mode'] = 'sameorigin'; |
| 74 |
return $data; |
| 75 |
} |
| 76 |
|
| 77 |
return $data; |
| 78 |
} |
| 79 |
|
| 80 |
public function append_header() { |
| 81 |
if ( headers_sent() ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! $this->maybe_submit_header( 'X-Frame-Options', false ) ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$model = $this->get_model(); |
| 90 |
$mode = $model->sh_xframe_mode; |
| 91 |
if ( true === $model->sh_xframe && in_array( $mode, array( 'sameorigin', 'allow-from', 'deny' ), true ) ) { |
| 92 |
$mode = 'allow-from' === $mode ? 'sameorigin' : $mode; |
| 93 |
header( trim( 'X-Frame-Options: ' . $mode ) ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
public function get_title() { |
| 101 |
return __( 'X-Frame-Options', 'wpdef' ); |
| 102 |
} |
| 103 |
} |
| 104 |
|