Make WordPress Core

Changeset 58822


Ignore:
Timestamp:
07/29/2024 01:57:11 AM (2 months ago)
Author:
peterwilsoncc
Message:

Users: Always use HTTPS URLs for Gravatar links.

Modifies gravatar image URLs to always use the HTTPS version from secure.gravatar.com.

Gravatar now redirects HTTP image requests to their HTTPS equivalent, resulting in redirects for sites running over an HTTP connection (is_ssl() === false). Since the introduction of HTTP/2 the use of sub-domains for different hashes ([1-3].gravatar.com) now represents a performance hinderance rather than improvement.

The scheme passed to get_avatar_data() is now ignored for the generation of Gravatar URLs but the setting retained to avoid introducing bugs for sites using either local avatars or third party providers.

Props neoxx, SergeyBiryukov, sippis, peterwilsoncc, mukesh27, costdev, dd32.
Fixes #37454.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r58807 r58822  
    43294329 *
    43304330 * @since 4.2.0
     4331 * @since 6.7.0 Gravatar URLs always use HTTPS.
    43314332 *
    43324333 * @param mixed $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash,
     
    43594360 *                                  Default is the value of the 'avatar_rating' option.
    43604361 *     @type string $scheme         URL scheme to use. See set_url_scheme() for accepted values.
     4362 *                                  For Gravatars this setting is ignored and HTTPS is used to avoid
     4363 *                                  unnecessary redirects. The setting is retained for systems using
     4364 *                                  the {@see 'pre_get_avatar_data'} filter to customize avatars.
    43614365 *                                  Default null.
    43624366 *     @type array  $processed_args When the function returns, the value will be the processed/sanitized $args
     
    45094513    if ( $email_hash ) {
    45104514        $args['found_avatar'] = true;
    4511         $gravatar_server      = hexdec( $email_hash[0] ) % 3;
    4512     } else {
    4513         $gravatar_server = rand( 0, 2 );
    45144515    }
    45154516
     
    45214522    );
    45224523
    4523     if ( is_ssl() ) {
    4524         $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
    4525     } else {
    4526         $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
    4527     }
     4524    /*
     4525     * Gravatars are always served over HTTPS.
     4526     *
     4527     * The Gravatar website redirects HTTP requests to HTTPS URLs so always
     4528     * use the HTTPS scheme to avoid unnecessary redirects.
     4529     */
     4530    $url = 'https://secure.gravatar.com/avatar/' . $email_hash;
    45284531
    45294532    $url = add_query_arg(
    45304533        rawurlencode_deep( array_filter( $url_args ) ),
    4531         set_url_scheme( $url, $args['scheme'] )
     4534        $url
    45324535    );
    45334536
  • trunk/tests/phpunit/tests/avatar.php

    r56547 r58822  
    1212    public function test_get_avatar_url_gravatar_url() {
    1313        $url = get_avatar_url( 1 );
    14         $this->assertSame( preg_match( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $url ), 1 );
     14        $this->assertSame( preg_match( '|^https?://secure.gravatar.com/avatar/[0-9a-f]{32}\?|', $url ), 1 );
    1515    }
    1616
     
    5757
    5858    /**
    59      * @ticket 21195
     59     * Ensures the get_avatar_url always returns an HTTPS scheme for gravatars.
     60     *
     61     * @ticket 21195
     62     * @ticket 37454
     63     *
     64     * @covers ::get_avatar_url
    6065     */
    6166    public function test_get_avatar_url_scheme() {
    6267        $url = get_avatar_url( 1 );
    63         $this->assertSame( preg_match( '|^http://|', $url ), 1 );
     68        $this->assertSame( preg_match( '|^https://|', $url ), 1, 'Avatars should default to the HTTPS scheme' );
    6469
    6570        $args = array( 'scheme' => 'https' );
    6671        $url  = get_avatar_url( 1, $args );
    67         $this->assertSame( preg_match( '|^https://|', $url ), 1 );
     72        $this->assertSame( preg_match( '|^https://|', $url ), 1, 'Requesting the HTTPS scheme should be respected' );
     73
     74        $args = array( 'scheme' => 'http' );
     75        $url  = get_avatar_url( 1, $args );
     76        $this->assertSame( preg_match( '|^https://|', $url ), 1, 'Requesting the HTTP scheme should return an HTTPS URL to avoid redirects' );
    6877
    6978        $args = array( 'scheme' => 'lolcat' );
    7079        $url  = get_avatar_url( 1, $args );
    71         $this->assertSame( preg_match( '|^lolcat://|', $url ), 0 );
     80        $this->assertSame( preg_match( '|^lolcat://|', $url ), 0, 'Unrecognized schemes should be ignored' );
     81        $this->assertSame( preg_match( '|^https://|', $url ), 1, 'Unrecognized schemes should return an HTTPS URL' );
    7282    }
    7383
     
    258268
    259269        $this->assertTrue( is_avatar_comment_type( $comment_type ) );
    260         $this->assertMatchesRegularExpression( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] );
     270        $this->assertMatchesRegularExpression( '|^https?://secure.gravatar.com/avatar/[0-9a-f]{32}\?|', $actual_data['url'] );
    261271    }
    262272
  • trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php

    r58340 r58822  
    730730        'TagModel.meta.test_tag_meta'                      => '',
    731731        'UsersCollection.0.link'                           => 'http://example.org/?author=1',
    732         'UsersCollection.0.avatar_urls.24'                 => 'http://0.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=24&d=mm&r=g',
    733         'UsersCollection.0.avatar_urls.48'                 => 'http://0.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=48&d=mm&r=g',
    734         'UsersCollection.0.avatar_urls.96'                 => 'http://0.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=96&d=mm&r=g',
     732        'UsersCollection.0.avatar_urls.24'                 => 'https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=24&d=mm&r=g',
     733        'UsersCollection.0.avatar_urls.48'                 => 'https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=48&d=mm&r=g',
     734        'UsersCollection.0.avatar_urls.96'                 => 'https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=96&d=mm&r=g',
    735735        'UsersCollection.0._links.self.0.href'             => 'http://example.org/index.php?rest_route=/wp/v2/users/1',
    736736        'UsersCollection.0._links.collection.0.href'       => 'http://example.org/index.php?rest_route=/wp/v2/users',
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r58452 r58822  
    1374913749        "slug": "admin",
    1375013750        "avatar_urls": {
    13751             "24": "http://0.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=24&d=mm&r=g",
    13752             "48": "http://0.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=48&d=mm&r=g",
    13753             "96": "http://0.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=96&d=mm&r=g"
     13751            "24": "https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=24&d=mm&r=g",
     13752            "48": "https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=48&d=mm&r=g",
     13753            "96": "https://secure.gravatar.com/avatar/96614ec98aa0c0d2ee75796dced6df54?s=96&d=mm&r=g"
    1375413754        },
    1375513755        "meta": {
     
    1377713777        "slug": "restapiclientfixtureuser",
    1377813778        "avatar_urls": {
    13779             "24": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=24&d=mm&r=g",
    13780             "48": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
    13781             "96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
     13779            "24": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=24&d=mm&r=g",
     13780            "48": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
     13781            "96": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
    1378213782        },
    1378313783        "meta": {
     
    1380713807    "slug": "restapiclientfixtureuser",
    1380813808    "avatar_urls": {
    13809         "24": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=24&d=mm&r=g",
    13810         "48": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
    13811         "96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
     13809        "24": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=24&d=mm&r=g",
     13810        "48": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
     13811        "96": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
    1381213812    },
    1381313813    "meta": {
     
    1382413824    "slug": "restapiclientfixtureuser",
    1382513825    "avatar_urls": {
    13826         "24": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=24&d=mm&r=g",
    13827         "48": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
    13828         "96": "http://2.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
     13826        "24": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=24&d=mm&r=g",
     13827        "48": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=48&d=mm&r=g",
     13828        "96": "https://secure.gravatar.com/avatar/57cbd982c963c7eb2294e2eee1b4448e?s=96&d=mm&r=g"
    1382913829    },
    1383013830    "meta": {
     
    1385013850        "type": "comment",
    1385113851        "author_avatar_urls": {
    13852             "24": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=24&d=mm&r=g",
    13853             "48": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=48&d=mm&r=g",
    13854             "96": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g"
     13852            "24": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=24&d=mm&r=g",
     13853            "48": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=48&d=mm&r=g",
     13854            "96": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g"
    1385513855        },
    1385613856        "meta": {
     
    1389513895    "type": "comment",
    1389613896    "author_avatar_urls": {
    13897         "24": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=24&d=mm&r=g",
    13898         "48": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=48&d=mm&r=g",
    13899         "96": "http://2.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g"
     13897        "24": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=24&d=mm&r=g",
     13898        "48": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=48&d=mm&r=g",
     13899        "96": "https://secure.gravatar.com/avatar/bd7c2b505bcf39cc71cfee564c614956?s=96&d=mm&r=g"
    1390013900    },
    1390113901    "meta": {
Note: See TracChangeset for help on using the changeset viewer.