Security: Add a referrer policy header to the admin and login screens.

This sets a referrer policy of `same-origin` which adds hardening by preventing a referrer being sent from the admin area or login screens to other origins. This helps prevent unwanted exposure of potentially sensitive information that may be contained within URLs.

This change introduces a new filter, `admin_referrer_policy`, for filtering the referrer policy header value. The header can be disabled if necessary by removing the `wp_admin_headers` action from the `admin_init` and `login_init` hooks.

Props joostdevalk
Fixes #42036


git-svn-id: https://develop.svn.wordpress.org/trunk@41741 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2017-10-04 18:24:17 +00:00
parent b1faca5ca8
commit bd6ee706d0
2 changed files with 23 additions and 0 deletions

View File

@ -38,6 +38,8 @@ add_filter( 'media_upload_library', 'media_upload_library' );
add_filter( 'media_upload_tabs', 'update_gallery_tab' );
// Misc hooks.
add_action( 'admin_init', 'wp_admin_headers' );
add_action( 'login_init', 'wp_admin_headers' );
add_action( 'admin_head', 'wp_admin_canonical_url' );
add_action( 'admin_head', 'wp_color_scheme_settings' );
add_action( 'admin_head', 'wp_site_icon' );

View File

@ -919,6 +919,27 @@ function wp_admin_canonical_url() {
<?php
}
/**
* Send a referrer policy header so referrers are not sent externally from administration screens.
*
* @since 4.9.0
*/
function wp_admin_headers() {
$policy = 'same-origin';
/**
* Filters the admin referrer policy header value. Default 'same-origin'.
*
* @since 4.9.0
* @link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
*
* @param string $policy The referrer policy header value.
*/
$policy = apply_filters( 'admin_referrer_policy', $policy );
header( sprintf( 'Referrer-Policy: %s', $policy ) );
}
/**
* Outputs JS that reloads the page if the user navigated to it with the Back or Forward button.
*