| 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_Referrer_Policy extends Security_Header { |
| 11 |
static $rule_slug = 'sh_referrer_policy'; |
| 12 |
|
| 13 |
public function check() { |
| 14 |
$model = $this->get_model(); |
| 15 |
|
| 16 |
if ( ! $model->sh_referrer_policy ) { |
| 17 |
return false; |
| 18 |
} |
| 19 |
if ( isset( $model->sh_referrer_policy_mode ) && ! empty( $model->sh_referrer_policy_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 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* @return array |
| 34 |
*/ |
| 35 |
public function get_misc_data() { |
| 36 |
$model = $this->get_model(); |
| 37 |
|
| 38 |
return array( |
| 39 |
'intro_text' => esc_html__( 'The Referrer-Policy HTTP header tells browsers how referrer information should be handled when a user clicks a link that leads to another page or link. A referrer header tells website owners where inbound visitors come from (similar to Google Analytics Acquisition reports), though in some cases you may want to control or restrict the referrer information present in the header.', 'wpdef' ), |
| 40 |
'mode' => $model->sh_referrer_policy_mode ?? 'origin-when-cross-origin', |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
public function add_hooks() { |
| 45 |
add_action( 'send_headers', array( $this, 'append_header' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
public function append_header() { |
| 49 |
if ( headers_sent() ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
$model = $this->get_model(); |
| 53 |
if ( ! $this->maybe_submit_header( 'Referrer-Policy', false ) ) { |
| 54 |
|
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
if ( true === $model->sh_referrer_policy |
| 59 |
&& isset( $model->sh_referrer_policy_mode ) |
| 60 |
&& in_array( |
| 61 |
$model->sh_referrer_policy_mode, |
| 62 |
array( |
| 63 |
'no-referrer', |
| 64 |
'no-referrer-when-downgrade', |
| 65 |
'origin', |
| 66 |
'origin-when-cross-origin', |
| 67 |
'same-origin', |
| 68 |
'strict-origin', |
| 69 |
'strict-origin-when-cross-origin', |
| 70 |
'unsafe-url', |
| 71 |
), |
| 72 |
true |
| 73 |
) |
| 74 |
) { |
| 75 |
$headers = 'Referrer-Policy: ' . $model->sh_referrer_policy_mode; |
| 76 |
header( $headers ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* @return string |
| 82 |
*/ |
| 83 |
public function get_title() { |
| 84 |
return __( 'Referrer Policy', 'wpdef' ); |
| 85 |
} |
| 86 |
} |
| 87 |
|