Make WordPress Core

Ticket #37569: 37569.4.diff

File 37569.4.diff, 1.9 KB (added by adamsilverstein, 7 years ago)

prevent recursion

  • src/wp-includes/js/wp-api.js

    diff --git src/wp-includes/js/wp-api.js src/wp-includes/js/wp-api.js
    index 0f5135f3c9..a8903eba0b 100644
     
    816816                        // Initialize the model.
    817817                        initialize: function() {
    818818
     819                                // Limit recursion in sync.
     820                                this.maxRecursion = 1;
     821
    819822                                /**
    820823                                * Types that don't support trashing require passing ?force=true to delete.
    821824                                *
     
    864867                                        };
    865868
    866869                                        // Update the nonce when a new nonce is returned with the response.
    867                                         options.complete = function( xhr ) {
     870                                        options.complete = _.bind( function( xhr ) {
    868871                                                var returnedNonce = xhr.getResponseHeader( 'X-WP-Nonce' );
    869872
    870873                                                if ( returnedNonce && _.isFunction( model.nonce ) && model.nonce() !== returnedNonce ) {
    871874                                                        model.endpointModel.set( 'nonce', returnedNonce );
     875                                                        if ( 'rest_cookie_invalid_nonce' === xhr.responseJSON.code && this.maxRecursion-- > 0 ) {
     876                                                                this.sync(  method, model, options );
     877                                                        } else {
     878                                                                this.maxRecursion = 1;
     879                                                        }
    872880                                                }
    873                                         };
     881                                        }, this );
     882
     883
    874884                                }
    875885
    876886                                // Add '?force=true' to use delete method when required.
  • src/wp-includes/rest-api.php

    diff --git src/wp-includes/rest-api.php src/wp-includes/rest-api.php
    index 82d856a063..016ac07f1b 100644
    function rest_cookie_check_errors( $result ) { 
    780780        // Check the nonce.
    781781        $result = wp_verify_nonce( $nonce, 'wp_rest' );
    782782
     783        if ( is_user_logged_in() ) {
     784
     785                // Send a refreshed nonce in header.
     786                rest_get_server()->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) );
     787        }
     788
    783789        if ( ! $result ) {
    784790                return new WP_Error( 'rest_cookie_invalid_nonce', __( 'Cookie nonce is invalid' ), array( 'status' => 403 ) );
    785791        }
    786792
    787         // Send a refreshed nonce in header.
    788         rest_get_server()->send_header( 'X-WP-Nonce', wp_create_nonce( 'wp_rest' ) );
    789793
    790794        return true;
    791795}