Make WordPress Core


Ignore:
Timestamp:
04/12/2018 09:19:24 PM (8 years ago)
Author:
azaozz
Message:

Privacy: add helper function for anonymizing data in a standardized way.

Props jesperher, allendav, iandunn, birgire, azaozz.
Fixes #43545.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesCommunityEvents.php

    r42968 r42971  
    480480         * Test that get_unsafe_client_ip() properly anonymizes all possible address formats
    481481         *
    482          * @dataProvider data_get_unsafe_client_ip_anonymization
     482         * @dataProvider data_get_unsafe_client_ip
    483483         *
    484484         * @ticket 41083
    485485         */
    486         public function test_get_unsafe_client_ip_anonymization( $raw_ip, $expected_result ) {
    487                 $_SERVER['REMOTE_ADDR'] = $raw_ip;
    488                 $actual_result          = WP_Community_Events::get_unsafe_client_ip();
     486        public function test_get_unsafe_client_ip( $raw_ip, $expected_result ) {
     487                $_SERVER['REMOTE_ADDR']    = 'this should not be used';
     488                $_SERVER['HTTP_CLIENT_IP'] = $raw_ip;
     489                $actual_result             = WP_Community_Events::get_unsafe_client_ip();
    489490
    490491                $this->assertEquals( $expected_result, $actual_result );
    491492        }
    492493
    493         public function data_get_unsafe_client_ip_anonymization() {
    494                 return array(
    495                         // Invalid IP.
     494        /**
     495         * Provide test cases for `test_get_unsafe_client_ip()`.
     496         *
     497         * @return array
     498         */
     499        public function data_get_unsafe_client_ip() {
     500                return array(
     501                        // Handle '::' returned from `wp_privacy_anonymize_ip()`.
    496502                        array(
    497                                 '',    // Raw IP address
    498                                 false, // Expected result
    499                         ),
    500                         // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings.
     503                                'or=\"[1000:0000:0000:0000:0000:0000:0000:0001',
     504                                false,
     505                        ),
     506
     507                        // Handle '0.0.0.0' returned from `wp_privacy_anonymize_ip()`.
    501508                        array(
    502509                                'unknown',
    503510                                false,
    504511                        ),
    505                         // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings.
     512
     513                        // Valid IPv4.
    506514                        array(
    507                                 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001',
    508                                 false,
    509                         ),
    510                         // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings.
    511                         array(
    512                                 'or=\"1000:0000:0000:0000:0000:0000:0000:0001',
    513                                 false,
    514                         ),
    515                         // Invalid IP. Sometimes proxies add things like this, or other arbitrary strings.
    516                         array(
    517                                 '1000:0000:0000:0000:0000:0000:0000:0001or=\"',
    518                                 false,
    519                         ),
    520                         // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings.
    521                         array(
    522                                 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001]:400',
    523                                 '1000::',
    524                         ),
    525                         // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings.
    526                         array(
    527                                 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001]',
    528                                 '1000::',
    529                         ),
    530                         // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings.
    531                         array(
    532                                 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001]400',
    533                                 '1000::',
    534                         ),
    535                         // Malformed string with valid IP substring. Sometimes proxies add things like this, or other arbitrary strings.
    536                         array(
    537                                 '[1000:0000:0000:0000:0000:0000:0000:0001]:235\"or=',
    538                                 '1000::',
    539                         ),
    540                         // IPv4, no port
    541                         array(
    542                                 '10.20.30.45',
    543                                 '10.20.30.0',
    544                         ),
    545                         // IPv4, port
    546                         array(
    547                                 '10.20.30.45:20000',
    548                                 '10.20.30.0',
    549                         ),
    550                         // IPv6, no port
     515                                '198.143.164.252',
     516                                '198.143.164.0',
     517                        ),
     518
     519                        // Valid IPv6.
    551520                        array(
    552521                                '2a03:2880:2110:df07:face:b00c::1',
    553522                                '2a03:2880:2110:df07::',
    554523                        ),
    555                         // IPv6, port
    556                         array(
    557                                 '[2a03:2880:2110:df07:face:b00c::1]:20000',
    558                                 '2a03:2880:2110:df07::',
    559                         ),
    560                         // IPv6, no port, reducible representation
    561                         array(
    562                                 '0000:0000:0000:0000:0000:0000:0000:0001',
    563                                 '::',
    564                         ),
    565                         // IPv6, no port, partially reducible representation
    566                         array(
    567                                 '1000:0000:0000:0000:0000:0000:0000:0001',
    568                                 '1000::',
    569                         ),
    570                         // IPv6, port, reducible representation
    571                         array(
    572                                 '[0000:0000:0000:0000:0000:0000:0000:0001]:1234',
    573                                 '::',
    574                         ),
    575                         // IPv6, port, partially reducible representation
    576                         array(
    577                                 '[1000:0000:0000:0000:0000:0000:0000:0001]:5678',
    578                                 '1000::',
    579                         ),
    580                         // IPv6, no port, reduced representation
    581                         array(
    582                                 '::',
    583                                 '::',
    584                         ),
    585                         // IPv6, no port, reduced representation
    586                         array(
    587                                 '::1',
    588                                 '::',
    589                         ),
    590                         // IPv6, port, reduced representation
    591                         array(
    592                                 '[::]:20000',
    593                                 '::',
    594                         ),
    595                         // IPv6, address brackets without port delimiter and number, reduced representation
    596                         array(
    597                                 '[::1]',
    598                                 '::',
    599                         ),
    600                         // IPv6, no port, compatibility mode
    601                         array(
    602                                 '::ffff:10.15.20.25',
    603                                 '::ffff:10.15.20.0',
    604                         ),
    605                         // IPv6, port, compatibility mode
    606                         array(
    607                                 '[::FFFF:10.15.20.25]:30000',
    608                                 '::ffff:10.15.20.0',
    609                         ),
    610                         // IPv6, no port, compatibility mode shorthand
    611                         array(
    612                                 '::127.0.0.1',
    613                                 '::ffff:127.0.0.0',
    614                         ),
    615                         // IPv6, port, compatibility mode shorthand
    616                         array(
    617                                 '[::127.0.0.1]:30000',
    618                                 '::ffff:127.0.0.0',
    619                         ),
    620                         // IPv6 with reachability scope
    621                         array(
    622                                 'fe80::b059:65f4:e877:c40%16',
    623                                 'fe80::',
    624                         ),
    625                         // IPv6 with reachability scope
    626                         array(
    627                                 'FE80::B059:65F4:E877:C40%eth0',
    628                                 'fe80::',
    629                         ),
    630524                );
    631525        }
Note: See TracChangeset for help on using the changeset viewer.