class-wp-recovery-mode-cookie-service.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Error Protection API: WP_Recovery_Mode_Cookie_Service class
  4. *
  5. * @package WordPress
  6. * @since 5.2.0
  7. */
  8. /**
  9. * Core class used to set, validate, and clear cookies that identify a Recovery Mode session.
  10. *
  11. * @since 5.2.0
  12. */
  13. final class WP_Recovery_Mode_Cookie_Service {
  14. /**
  15. * Checks whether the recovery mode cookie is set.
  16. *
  17. * @since 5.2.0
  18. *
  19. * @return bool True if the cookie is set, false otherwise.
  20. */
  21. public function is_cookie_set() {
  22. return ! empty( $_COOKIE[ RECOVERY_MODE_COOKIE ] );
  23. }
  24. /**
  25. * Sets the recovery mode cookie.
  26. *
  27. * This must be immediately followed by exiting the request.
  28. *
  29. * @since 5.2.0
  30. */
  31. public function set_cookie() {
  32. $value = $this->generate_cookie();
  33. /**
  34. * Filter the length of time a Recovery Mode cookie is valid for.
  35. *
  36. * @since 5.2.0
  37. *
  38. * @param int $length Length in seconds.
  39. */
  40. $length = apply_filters( 'recovery_mode_cookie_length', WEEK_IN_SECONDS );
  41. $expire = time() + $length;
  42. setcookie( RECOVERY_MODE_COOKIE, $value, $expire, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
  43. if ( COOKIEPATH !== SITECOOKIEPATH ) {
  44. setcookie( RECOVERY_MODE_COOKIE, $value, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
  45. }
  46. }
  47. /**
  48. * Clears the recovery mode cookie.
  49. *
  50. * @since 5.2.0
  51. */
  52. public function clear_cookie() {
  53. setcookie( RECOVERY_MODE_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
  54. setcookie( RECOVERY_MODE_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
  55. }
  56. /**
  57. * Validates the recovery mode cookie.
  58. *
  59. * @since 5.2.0
  60. *
  61. * @param string $cookie Optionally specify the cookie string.
  62. * If omitted, it will be retrieved from the super global.
  63. * @return true|WP_Error True on success, error object on failure.
  64. */
  65. public function validate_cookie( $cookie = '' ) {
  66. if ( ! $cookie ) {
  67. if ( empty( $_COOKIE[ RECOVERY_MODE_COOKIE ] ) ) {
  68. return new WP_Error( 'no_cookie', __( 'No cookie present.' ) );
  69. }
  70. $cookie = $_COOKIE[ RECOVERY_MODE_COOKIE ];
  71. }
  72. $parts = $this->parse_cookie( $cookie );
  73. if ( is_wp_error( $parts ) ) {
  74. return $parts;
  75. }
  76. list( , $created_at, $random, $signature ) = $parts;
  77. if ( ! ctype_digit( $created_at ) ) {
  78. return new WP_Error( 'invalid_created_at', __( 'Invalid cookie format.' ) );
  79. }
  80. /** This filter is documented in wp-includes/class-wp-recovery-mode-cookie-service.php */
  81. $length = apply_filters( 'recovery_mode_cookie_length', WEEK_IN_SECONDS );
  82. if ( time() > $created_at + $length ) {
  83. return new WP_Error( 'expired', __( 'Cookie expired.' ) );
  84. }
  85. $to_sign = sprintf( 'recovery_mode|%s|%s', $created_at, $random );
  86. $hashed = $this->recovery_mode_hash( $to_sign );
  87. if ( ! hash_equals( $signature, $hashed ) ) {
  88. return new WP_Error( 'signature_mismatch', __( 'Invalid cookie.' ) );
  89. }
  90. return true;
  91. }
  92. /**
  93. * Gets the session identifier from the cookie.
  94. *
  95. * The cookie should be validated before calling this API.
  96. *
  97. * @since 5.2.0
  98. *
  99. * @param string $cookie Optionally specify the cookie string.
  100. * If omitted, it will be retrieved from the super global.
  101. * @return string|WP_Error Session ID on success, or error object on failure.
  102. */
  103. public function get_session_id_from_cookie( $cookie = '' ) {
  104. if ( ! $cookie ) {
  105. if ( empty( $_COOKIE[ RECOVERY_MODE_COOKIE ] ) ) {
  106. return new WP_Error( 'no_cookie', __( 'No cookie present.' ) );
  107. }
  108. $cookie = $_COOKIE[ RECOVERY_MODE_COOKIE ];
  109. }
  110. $parts = $this->parse_cookie( $cookie );
  111. if ( is_wp_error( $parts ) ) {
  112. return $parts;
  113. }
  114. list( , , $random ) = $parts;
  115. return sha1( $random );
  116. }
  117. /**
  118. * Parses the cookie into its four parts.
  119. *
  120. * @since 5.2.0
  121. *
  122. * @param string $cookie Cookie content.
  123. * @return array|WP_Error Cookie parts array, or error object on failure.
  124. */
  125. private function parse_cookie( $cookie ) {
  126. $cookie = base64_decode( $cookie );
  127. $parts = explode( '|', $cookie );
  128. if ( 4 !== count( $parts ) ) {
  129. return new WP_Error( 'invalid_format', __( 'Invalid cookie format.' ) );
  130. }
  131. return $parts;
  132. }
  133. /**
  134. * Generates the recovery mode cookie value.
  135. *
  136. * The cookie is a base64 encoded string with the following format:
  137. *
  138. * recovery_mode|iat|rand|signature
  139. *
  140. * Where "recovery_mode" is a constant string,
  141. * iat is the time the cookie was generated at,
  142. * rand is a randomly generated password that is also used as a session identifier
  143. * and signature is an hmac of the preceding 3 parts.
  144. *
  145. * @since 5.2.0
  146. *
  147. * @return string Generated cookie content.
  148. */
  149. private function generate_cookie() {
  150. $to_sign = sprintf( 'recovery_mode|%s|%s', time(), wp_generate_password( 20, false ) );
  151. $signed = $this->recovery_mode_hash( $to_sign );
  152. return base64_encode( sprintf( '%s|%s', $to_sign, $signed ) );
  153. }
  154. /**
  155. * Gets a form of `wp_hash()` specific to Recovery Mode.
  156. *
  157. * We cannot use `wp_hash()` because it is defined in `pluggable.php` which is not loaded until after plugins are loaded,
  158. * which is too late to verify the recovery mode cookie.
  159. *
  160. * This tries to use the `AUTH` salts first, but if they aren't valid specific salts will be generated and stored.
  161. *
  162. * @since 5.2.0
  163. *
  164. * @param string $data Data to hash.
  165. * @return string|false The hashed $data, or false on failure.
  166. */
  167. private function recovery_mode_hash( $data ) {
  168. if ( ! defined( 'AUTH_KEY' ) || AUTH_KEY === 'put your unique phrase here' ) {
  169. $auth_key = get_site_option( 'recovery_mode_auth_key' );
  170. if ( ! $auth_key ) {
  171. if ( ! function_exists( 'wp_generate_password' ) ) {
  172. require_once ABSPATH . WPINC . '/pluggable.php';
  173. }
  174. $auth_key = wp_generate_password( 64, true, true );
  175. update_site_option( 'recovery_mode_auth_key', $auth_key );
  176. }
  177. } else {
  178. $auth_key = AUTH_KEY;
  179. }
  180. if ( ! defined( 'AUTH_SALT' ) || AUTH_SALT === 'put your unique phrase here' || AUTH_SALT === $auth_key ) {
  181. $auth_salt = get_site_option( 'recovery_mode_auth_salt' );
  182. if ( ! $auth_salt ) {
  183. if ( ! function_exists( 'wp_generate_password' ) ) {
  184. require_once ABSPATH . WPINC . '/pluggable.php';
  185. }
  186. $auth_salt = wp_generate_password( 64, true, true );
  187. update_site_option( 'recovery_mode_auth_salt', $auth_salt );
  188. }
  189. } else {
  190. $auth_salt = AUTH_SALT;
  191. }
  192. $secret = $auth_key . $auth_salt;
  193. return hash_hmac( 'sha1', $data, $secret );
  194. }
  195. }