Make WordPress Core

Ticket #38506: 38506.2.diff

File 38506.2.diff, 1.8 KB (added by rmccue, 9 years ago)

Check for email length in REST API validation

  • src/wp-includes/rest-api.php

     
    10361036                                break;
    10371037
    10381038                        case 'email' :
    1039                                 if ( ! is_email( $value ) ) {
     1039                                // is_email() checks for 3 characters (a@b), but
     1040                                // wp_handle_comment_submission() requires 6 characters (a@b.co)
     1041                                //
     1042                                // https://core.trac.wordpress.org/ticket/38506
     1043                                if ( ! is_email( $value ) || strlen( $value ) < 6 ) {
    10401044                                        return new WP_Error( 'rest_invalid_email', __( 'The email address you provided is invalid.' ) );
    10411045                                }
    10421046                                break;
  • tests/phpunit/tests/rest-api/rest-comments-controller.php

     
    897897                update_option( 'require_name_email', 0 );
    898898        }
    899899
     900        public function test_create_comment_author_email_too_short() {
     901                wp_set_current_user( 0 );
     902
     903                $params = array(
     904                        'post'         => self::$post_id,
     905                        'author_name'  => 'Homer J. Simpson',
     906                        'author_email' => 'a@b',
     907                        'content'      => 'in this house, we obey the laws of thermodynamics!',
     908                );
     909
     910                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     911                $request->add_header( 'content-type', 'application/json' );
     912                $request->set_body( wp_json_encode( $params ) );
     913                $response = $this->server->dispatch( $request );
     914
     915                $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
     916
     917                $data = $response->get_data();
     918                $this->assertArrayHasKey( 'author_email', $data['data']['params'] );
     919        }
     920
    900921        public function test_create_item_invalid_blank_content() {
    901922                wp_set_current_user( 0 );
    902923