Skip to content

Commit

Permalink
Privacy: Use SHA-256 hashing algorithm for Gravatar.
Browse files Browse the repository at this point in the history
This aims to improve privacy by switching to a more secure algorithm, as an MD5 string can be reversed.

Follow-up to [6748], [31107].

Props henry.wright, jucaduca, haozi, desrosj, dd32, SergeyBiryukov.
See #60638.

git-svn-id: https://develop.svn.wordpress.org/trunk@59532 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Dec 17, 2024
1 parent c697356 commit 68c4efc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
18 changes: 11 additions & 7 deletions src/wp-includes/link-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -4289,7 +4289,7 @@ function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
*
* @since 4.2.0
*
* @param mixed $id_or_email The avatar to retrieve a URL for. Accepts a user ID, Gravatar MD5 hash,
* @param mixed $id_or_email The avatar to retrieve a URL for. Accepts a user ID, Gravatar SHA-256 or MD5 hash,
* user email, WP_User object, WP_Post object, or WP_Comment object.
* @param array $args {
* Optional. Arguments to use instead of the default arguments.
Expand Down Expand Up @@ -4353,8 +4353,9 @@ function is_avatar_comment_type( $comment_type ) {
*
* @since 4.2.0
* @since 6.7.0 Gravatar URLs always use HTTPS.
* @since 6.8.0 Gravatar URLs use the SHA-256 hashing algorithm.

Check failure on line 4356 in src/wp-includes/link-template.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Whitespace found at end of line
*
* @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
* @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar SHA-256 or MD5 hash,
* user email, WP_User object, WP_Post object, or WP_Comment object.
* @param array $args {
* Optional. Arguments to use instead of the default arguments.
Expand Down Expand Up @@ -4474,7 +4475,7 @@ function get_avatar_data( $id_or_email, $args = null ) {
* @since 4.2.0
*
* @param array $args Arguments passed to get_avatar_data(), after processing.
* @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
* @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar SHA-256 or MD5 hash,
* user email, WP_User object, WP_Post object, or WP_Comment object.
*/
$args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
Expand All @@ -4496,7 +4497,10 @@ function get_avatar_data( $id_or_email, $args = null ) {
if ( is_numeric( $id_or_email ) ) {
$user = get_user_by( 'id', absint( $id_or_email ) );
} elseif ( is_string( $id_or_email ) ) {
if ( str_contains( $id_or_email, '@md5.gravatar.com' ) ) {
if ( str_contains( $id_or_email, '@sha256.gravatar.com' ) ) {
// SHA-256 hash.
list( $email_hash ) = explode( '@', $id_or_email );
} else if ( str_contains( $id_or_email, '@md5.gravatar.com' ) ) {
// MD5 hash.
list( $email_hash ) = explode( '@', $id_or_email );
} else {
Expand Down Expand Up @@ -4530,7 +4534,7 @@ function get_avatar_data( $id_or_email, $args = null ) {
}

if ( $email ) {
$email_hash = md5( strtolower( trim( $email ) ) );
$email_hash = hash( 'sha256', strtolower( trim( $email ) ) );
}
}

Expand Down Expand Up @@ -4564,7 +4568,7 @@ function get_avatar_data( $id_or_email, $args = null ) {
* @since 4.2.0
*
* @param string $url The URL of the avatar.
* @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
* @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar SHA-256 or MD5 hash,
* user email, WP_User object, WP_Post object, or WP_Comment object.
* @param array $args Arguments passed to get_avatar_data(), after processing.
*/
Expand All @@ -4576,7 +4580,7 @@ function get_avatar_data( $id_or_email, $args = null ) {
* @since 4.2.0
*
* @param array $args Arguments passed to get_avatar_data(), after processing.
* @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
* @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar SHA-256 or MD5 hash,
* user email, WP_User object, WP_Post object, or WP_Comment object.
*/
return apply_filters( 'get_avatar_data', $args, $id_or_email );
Expand Down
9 changes: 6 additions & 3 deletions tests/phpunit/tests/avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Tests_Avatar extends WP_UnitTestCase {
*/
public function test_get_avatar_url_gravatar_url() {
$url = get_avatar_url( 1 );
$this->assertSame( preg_match( '|^https?://secure.gravatar.com/avatar/[0-9a-f]{32}\?|', $url ), 1 );
$this->assertSame( preg_match( '|^https?://secure.gravatar.com/avatar/[0-9a-f]{64}\?|', $url ), 1 );
}

/**
Expand Down Expand Up @@ -90,9 +90,12 @@ public function test_get_avatar_url_user() {
$url2 = get_avatar_url( WP_TESTS_EMAIL );
$this->assertSame( $url, $url2 );

$url2 = get_avatar_url( md5( WP_TESTS_EMAIL ) . '@md5.gravatar.com' );
$url2 = get_avatar_url( hash( 'sha256', WP_TESTS_EMAIL ) . '@sha256.gravatar.com' );
$this->assertSame( $url, $url2 );

$url2 = get_avatar_url( md5( WP_TESTS_EMAIL ) . '@md5.gravatar.com' );
$this->assertSame( preg_match( '|^https?://secure.gravatar.com/avatar/[0-9a-f]{32}\?|', $url2 ), 1 );

$user = get_user_by( 'id', 1 );
$url2 = get_avatar_url( $user );
$this->assertSame( $url, $url2 );
Expand Down Expand Up @@ -267,7 +270,7 @@ public function test_get_avatar_data_should_return_gravatar_url_when_input_avata
$actual_data = get_avatar_data( $comment );

$this->assertTrue( is_avatar_comment_type( $comment_type ) );
$this->assertMatchesRegularExpression( '|^https?://secure.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] );
$this->assertMatchesRegularExpression( '|^https?://secure.gravatar.com/avatar/[0-9a-f]{64}\?|', $actual_data['url'] );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/rest-api/rest-schema-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,9 @@ public function test_build_wp_api_client_fixtures() {
'TagModel.meta.test_multi' => array(),
'TagModel.meta.test_tag_meta' => '',
'UsersCollection.0.link' => 'http://example.org/?author=1',
'UsersCollection.0.avatar_urls.24' => 'https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=24&d=mm&r=g',
'UsersCollection.0.avatar_urls.48' => 'https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=48&d=mm&r=g',
'UsersCollection.0.avatar_urls.96' => 'https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=96&d=mm&r=g',
'UsersCollection.0.avatar_urls.24' => 'https://secure.gravatar.com/avatar/9387ed9432ec25ef93df84b8a0b9697ddef435a945e7f244670c4f79f88363e9?s=24&d=mm&r=g',
'UsersCollection.0.avatar_urls.48' => 'https://secure.gravatar.com/avatar/9387ed9432ec25ef93df84b8a0b9697ddef435a945e7f244670c4f79f88363e9?s=48&d=mm&r=g',
'UsersCollection.0.avatar_urls.96' => 'https://secure.gravatar.com/avatar/9387ed9432ec25ef93df84b8a0b9697ddef435a945e7f244670c4f79f88363e9?s=96&d=mm&r=g',
'UsersCollection.0._links.self.0.href' => 'http://example.org/index.php?rest_route=/wp/v2/users/1',
'UsersCollection.0._links.collection.0.href' => 'http://example.org/index.php?rest_route=/wp/v2/users',
'UsersCollection.1.id' => 2,
Expand Down
36 changes: 18 additions & 18 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -13877,9 +13877,9 @@ mockedApiResponse.UsersCollection = [
"link": "http://example.org/?author=1",
"slug": "admin",
"avatar_urls": {
"24": "https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=96&d=mm&r=g"
"24": "https://secure.gravatar.com/avatar/9387ed9432ec25ef93df84b8a0b9697ddef435a945e7f244670c4f79f88363e9?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/9387ed9432ec25ef93df84b8a0b9697ddef435a945e7f244670c4f79f88363e9?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/9387ed9432ec25ef93df84b8a0b9697ddef435a945e7f244670c4f79f88363e9?s=96&d=mm&r=g"
},
"meta": {
"meta_key": "meta_value"
Expand Down Expand Up @@ -13914,9 +13914,9 @@ mockedApiResponse.UsersCollection = [
"link": "http://example.org/?author=2",
"slug": "restapiclientfixtureuser",
"avatar_urls": {
"24": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
"24": "https://secure.gravatar.com/avatar/ea862d9636c72500beece7b1990870e2776f89c2096d0c064c14f2beb910077d?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/ea862d9636c72500beece7b1990870e2776f89c2096d0c064c14f2beb910077d?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/ea862d9636c72500beece7b1990870e2776f89c2096d0c064c14f2beb910077d?s=96&d=mm&r=g"
},
"meta": {
"meta_key": ""
Expand Down Expand Up @@ -13953,9 +13953,9 @@ mockedApiResponse.UserModel = {
"link": "http://example.org/?author=2",
"slug": "restapiclientfixtureuser",
"avatar_urls": {
"24": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
"24": "https://secure.gravatar.com/avatar/ea862d9636c72500beece7b1990870e2776f89c2096d0c064c14f2beb910077d?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/ea862d9636c72500beece7b1990870e2776f89c2096d0c064c14f2beb910077d?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/ea862d9636c72500beece7b1990870e2776f89c2096d0c064c14f2beb910077d?s=96&d=mm&r=g"
},
"meta": {
"meta_key": ""
Expand All @@ -13970,9 +13970,9 @@ mockedApiResponse.me = {
"link": "http://example.org/?author=2",
"slug": "restapiclientfixtureuser",
"avatar_urls": {
"24": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
"24": "https://secure.gravatar.com/avatar/ea862d9636c72500beece7b1990870e2776f89c2096d0c064c14f2beb910077d?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/ea862d9636c72500beece7b1990870e2776f89c2096d0c064c14f2beb910077d?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/ea862d9636c72500beece7b1990870e2776f89c2096d0c064c14f2beb910077d?s=96&d=mm&r=g"
},
"meta": {
"meta_key": ""
Expand All @@ -13996,9 +13996,9 @@ mockedApiResponse.CommentsCollection = [
"status": "approved",
"type": "comment",
"author_avatar_urls": {
"24": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g"
"24": "https://secure.gravatar.com/avatar/9ca51ced0b389ffbeba3d269c6d824be664c84fa1b35503282abdd302e1f417c?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/9ca51ced0b389ffbeba3d269c6d824be664c84fa1b35503282abdd302e1f417c?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/9ca51ced0b389ffbeba3d269c6d824be664c84fa1b35503282abdd302e1f417c?s=96&d=mm&r=g"
},
"meta": {
"meta_key": "meta_value"
Expand Down Expand Up @@ -14050,9 +14050,9 @@ mockedApiResponse.CommentModel = {
"status": "approved",
"type": "comment",
"author_avatar_urls": {
"24": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g"
"24": "https://secure.gravatar.com/avatar/9ca51ced0b389ffbeba3d269c6d824be664c84fa1b35503282abdd302e1f417c?s=24&d=mm&r=g",
"48": "https://secure.gravatar.com/avatar/9ca51ced0b389ffbeba3d269c6d824be664c84fa1b35503282abdd302e1f417c?s=48&d=mm&r=g",
"96": "https://secure.gravatar.com/avatar/9ca51ced0b389ffbeba3d269c6d824be664c84fa1b35503282abdd302e1f417c?s=96&d=mm&r=g"
},
"meta": {
"meta_key": "meta_value"
Expand Down

0 comments on commit 68c4efc

Please sign in to comment.