Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
36.54% |
19 / 52 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Passport | |
36.54% |
19 / 52 |
|
20.00% |
1 / 5 |
122.23 | |
0.00% |
0 / 1 |
| read | |
90.00% |
18 / 20 |
|
0.00% |
0 / 1 |
13.17 | |||
| issue | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| revoke | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| signature | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set_cookie | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The passport: a first-party cookie that lets a commenter back in without the popup. |
| 4 | * |
| 5 | * @package automattic/jetpack-comments |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Comments; |
| 9 | |
| 10 | /** |
| 11 | * Signed with the site's own salt, so it is this site's and nobody else's. |
| 12 | */ |
| 13 | class Passport { |
| 14 | |
| 15 | // Prefixed like core's commenter cookies, so page caches serve the commenter a fresh page. |
| 16 | const COOKIE = 'comment_author_jetpack_identity'; |
| 17 | const LEGACY_COOKIE = 'jetpack_comment_identity'; |
| 18 | const DISPLAY_COOKIE = 'jetpack_comment_identity_display'; |
| 19 | const FIELDS = array( 'site_commenter_id', 'name', 'email', 'avatar', 'wpcom_user_id' ); |
| 20 | const LIFETIME = 30 * DAY_IN_SECONDS; |
| 21 | |
| 22 | /** |
| 23 | * Read the passport the browser sent, if it is intact and unexpired. |
| 24 | * |
| 25 | * @return array|null Keyed by FIELDS. |
| 26 | */ |
| 27 | public static function read() { |
| 28 | $legacy = ! isset( $_COOKIE[ self::COOKIE ] ) && isset( $_COOKIE[ self::LEGACY_COOKIE ] ); |
| 29 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Verified against its signature below. |
| 30 | $cookie = wp_unslash( $_COOKIE[ $legacy ? self::LEGACY_COOKIE : self::COOKIE ] ?? '' ); |
| 31 | |
| 32 | if ( ! is_string( $cookie ) || '' === $cookie || substr_count( $cookie, '.' ) !== 1 ) { |
| 33 | return null; |
| 34 | } |
| 35 | |
| 36 | list( $encoded, $signature ) = explode( '.', $cookie, 2 ); |
| 37 | |
| 38 | if ( ! hash_equals( self::signature( $encoded ), $signature ) ) { |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | $payload = json_decode( (string) base64_decode( strtr( $encoded, '-_', '+/' ) ), true ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- base64url is the cookie format. |
| 43 | |
| 44 | if ( ! is_array( $payload ) || empty( $payload['site_commenter_id'] ) ) { |
| 45 | return null; |
| 46 | } |
| 47 | |
| 48 | if ( (int) ( $payload['expires_at'] ?? 0 ) <= time() ) { |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | if ( (int) ( $payload['blog_id'] ?? 0 ) !== Checkpoint::blog_id() ) { |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | // Copied to the new name, so the next page skips the cache. The old one stays for code that still reads it. |
| 57 | if ( $legacy ) { |
| 58 | self::set_cookie( self::COOKIE, $cookie, (int) $payload['expires_at'], true ); |
| 59 | } |
| 60 | |
| 61 | $identity = array(); |
| 62 | foreach ( self::FIELDS as $field ) { |
| 63 | $identity[ $field ] = (string) ( $payload[ $field ] ?? '' ); |
| 64 | } |
| 65 | |
| 66 | return $identity; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Hand the browser a passport. |
| 71 | * |
| 72 | * @param array $identity From Checkpoint::exchange(). |
| 73 | * @return void |
| 74 | */ |
| 75 | public static function issue( array $identity ) { |
| 76 | $expires = time() + self::LIFETIME; |
| 77 | $payload = array(); |
| 78 | |
| 79 | foreach ( self::FIELDS as $field ) { |
| 80 | $payload[ $field ] = (string) ( $identity[ $field ] ?? '' ); |
| 81 | } |
| 82 | |
| 83 | $payload['expires_at'] = $expires; |
| 84 | $payload['blog_id'] = Checkpoint::blog_id(); |
| 85 | |
| 86 | $encoded = rtrim( strtr( base64_encode( (string) wp_json_encode( $payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ), '+/', '-_' ), '=' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- base64url is the cookie format. |
| 87 | |
| 88 | self::set_cookie( self::COOKIE, $encoded . '.' . self::signature( $encoded ), $expires, true ); |
| 89 | |
| 90 | // The display cookie is URL-encoded JSON the page's script decodes. |
| 91 | $shown = array(); |
| 92 | |
| 93 | foreach ( array( 'name', 'avatar' ) as $field ) { |
| 94 | $shown[ $field ] = (string) ( $identity[ $field ] ?? '' ); |
| 95 | } |
| 96 | |
| 97 | $shown['blog_id'] = Checkpoint::blog_id(); |
| 98 | |
| 99 | self::set_cookie( self::DISPLAY_COOKIE, rawurlencode( (string) wp_json_encode( $shown, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ), $expires, false ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Take the passport back. |
| 104 | * |
| 105 | * @return void |
| 106 | */ |
| 107 | public static function revoke() { |
| 108 | unset( $_COOKIE[ self::COOKIE ], $_COOKIE[ self::LEGACY_COOKIE ], $_COOKIE[ self::DISPLAY_COOKIE ] ); |
| 109 | self::set_cookie( self::COOKIE, '', time() - YEAR_IN_SECONDS, true ); |
| 110 | self::set_cookie( self::LEGACY_COOKIE, '', time() - YEAR_IN_SECONDS, true ); |
| 111 | self::set_cookie( self::DISPLAY_COOKIE, '', time() - YEAR_IN_SECONDS, false ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * HMAC over the encoded payload, keyed with the site's auth salt. |
| 116 | * |
| 117 | * @param string $encoded The base64url payload. |
| 118 | * @return string |
| 119 | */ |
| 120 | private static function signature( $encoded ) { |
| 121 | return hash_hmac( 'sha256', 'jetpack-comment-passport|' . $encoded, wp_salt( 'auth' ) ); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Send a cookie. Raw, so the display value reaches the script exactly as encoded. |
| 126 | * |
| 127 | * @param string $name Cookie name. |
| 128 | * @param string $value Cookie value. |
| 129 | * @param int $expires Unix time. |
| 130 | * @param bool $httponly Whether to keep it from the page's script. |
| 131 | * @return void |
| 132 | */ |
| 133 | private static function set_cookie( $name, $value, $expires, $httponly ) { |
| 134 | if ( headers_sent() ) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | setrawcookie( |
| 139 | $name, |
| 140 | $value, |
| 141 | array( |
| 142 | 'expires' => $expires, |
| 143 | 'path' => COOKIEPATH, |
| 144 | 'domain' => COOKIE_DOMAIN, |
| 145 | 'secure' => is_ssl(), |
| 146 | 'httponly' => $httponly, |
| 147 | 'samesite' => 'Lax', |
| 148 | ) |
| 149 | ); |
| 150 | } |
| 151 | } |