'nosniff', 'X-Frame-Options' => 'SAMEORIGIN', 'Referrer-Policy' => 'strict-origin-when-cross-origin', ]; /** * How long a browser remembers to use HTTPS only, in seconds (one year). */ private const HSTS_VALUE = 'max-age=31536000'; /** * The security settings. * * @var array */ private array $settings; /** * Registers the settings fields and, when the switch is on, the header hooks. */ public function __construct() { $this->settings = Plugin::get_settings( 'security' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( !empty( $this->settings['security_headers'] ) ) { add_filter( 'wp_headers', [ $this, 'add_front_end_headers' ], 10, 2 ); add_action( 'login_init', [ $this, 'send_login_headers' ] ); } } /** * Adds the security headers switch and its HTTPS-only child switch. * * @param array $fields The existing array of settings fields. * * @return array The settings fields including the security headers switch. */ public function adminease_settings_fields( array $fields ): array { $fields['security']['fields'][] = [ 'type' => 'switch', 'id' => 'security-headers', 'name' => 'adminease[security][security_headers]', 'value' => $this->settings['security_headers'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Send security headers', 'adminease' ), 'risk' => 'careful', 'breaks' => __( 'Other sites can no longer show your pages inside a frame.', 'adminease' ), 'description' => __( 'Adds the headers that hosting and security scanners look for to the front end and the login page: X-Content-Type-Options (no content type guessing), X-Frame-Options SAMEORIGIN (only your own site can frame your pages) and Referrer-Policy strict-origin-when-cross-origin.', 'adminease' ), 'child_fields' => [ [ 'type' => 'switch', 'id' => 'security-headers-hsts', 'name' => 'adminease[security][security_headers_hsts]', 'value' => $this->settings['security_headers_hsts'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'label' => __( 'Also send HSTS (HTTPS only)', 'adminease' ), 'risk' => 'careful', 'breaks' => __( 'Browsers refuse plain HTTP for your site for a year.', 'adminease' ), 'description' => __( 'Tells browsers to open your site over HTTPS only for the next year. Only sent when the page itself is served over HTTPS.', 'adminease' ), 'field_description' => __( 'Turn this on only when every page of your site works over HTTPS. A browser that has seen this header will refuse plain HTTP for your site until the year is over, even after you turn this off.', 'adminease' ), 'attributes' => [ 'data-parent' => 'security-headers', ], ], ], ]; return $fields; } /** * Lists the headers to send for this request. * * @return array Header name => value. */ private function get_headers(): array { $headers = self::HEADERS; if( !empty( $this->settings['security_headers_hsts'] ) && is_ssl() ) { $headers['Strict-Transport-Security'] = self::HSTS_VALUE; } return $headers; } /** * Adds the headers to the ones WordPress sends for a front-end request. * * @param array $headers The headers WordPress is about to send. * @param \WP|null $wp The WordPress environment, when passed. * * @return array The headers with the security headers added. */ public function add_front_end_headers( $headers, $wp = null ): array { $added = $this->get_headers(); if( $wp instanceof \WP && !empty( $wp->query_vars['embed'] ) ) { unset( $added['X-Frame-Options'] ); } return array_merge( (array) $headers, $added ); } /** * Sends the headers on the login page, which does not go through wp_headers. * * @return void */ public function send_login_headers(): void { if( headers_sent() ) { return; } foreach( $this->get_headers() as $name => $value ) { header( $name . ': ' . $value ); } } }