Safely disable WP REST API

后端 未结 5 1735
花落未央
花落未央 2021-01-30 13:37

I am considering to improve security of my Wordpress website, and in doing so have come across WP REST API being enabled by default (since WP 4.4 if I\'m not mistaken).

相关标签:
5条回答
  • 2021-01-30 14:05

    You can disable it for requests other than localhost:

    function restrict_rest_api_to_localhost() {
        $whitelist = [ '127.0.0.1', "::1" ];
    
        if( ! in_array($_SERVER['REMOTE_ADDR'], $whitelist ) ){
            die( 'REST API is disabled.' );
        }
    }
    add_action( 'rest_api_init', 'restrict_rest_api_to_localhost', 0 );
    
    0 讨论(0)
  • 2021-01-30 14:11

    The accepted answer disables all API calls from unauthenticated users, but nowadays lot of plugins are dependent on this API's functionality.

    Disabling all calls will lead to unexpected site behavior which happened in my case also when I used this code.

    For example, ContactForm7 makes use of this API for sending contact info to DB (I think) and for ReCaptcha validation.

    I think it would be better to disable some (default) endpoints for unauthenticated users like this:

    // Disable some endpoints for unauthenticated users
    add_filter( 'rest_endpoints', 'disable_default_endpoints' );
    function disable_default_endpoints( $endpoints ) {
        $endpoints_to_remove = array(
            '/oembed/1.0',
            '/wp/v2',
            '/wp/v2/media',
            '/wp/v2/types',
            '/wp/v2/statuses',
            '/wp/v2/taxonomies',
            '/wp/v2/tags',
            '/wp/v2/users',
            '/wp/v2/comments',
            '/wp/v2/settings',
            '/wp/v2/themes',
            '/wp/v2/blocks',
            '/wp/v2/oembed',
            '/wp/v2/posts',
            '/wp/v2/pages',
            '/wp/v2/block-renderer',
            '/wp/v2/search',
            '/wp/v2/categories'
        );
    
        if ( ! is_user_logged_in() ) {
            foreach ( $endpoints_to_remove as $rem_endpoint ) {
                // $base_endpoint = "/wp/v2/{$rem_endpoint}";
                foreach ( $endpoints as $maybe_endpoint => $object ) {
                    if ( stripos( $maybe_endpoint, $rem_endpoint ) !== false ) {
                        unset( $endpoints[ $maybe_endpoint ] );
                    }
                }
            }
        }
        return $endpoints;
    }
    

    With this, the only endpoints now open are the ones installed by the plugins.

    For complete list of endpoints active on your site, see https://YOURSITE.com/wp-json/

    Feel free to edit $endpoints_to_remove array as per your requirement.

    If you have custom post type, make sure to add those all to the list too.

    In my case, I also changed the default endpoint prefix from wp-json to mybrand-api. This should act a deterrent for bots that were making thousands of brute-force requests.

    Here is what I did:

    // Custom rest api prefix (Make sure to go to Dashboard > Settings > Permalinks and press Save button to flush/rewrite url cache )
    add_filter( 'rest_url_prefix', 'rest_api_url_prefix' );
    function rest_api_url_prefix() {
        return 'mybrand-api';
    }
    
    0 讨论(0)
  • 2021-01-30 14:21

    With the plugin "Disable REST API" you can select which APIs you want to enable, e.g. the contact form 7 API. See the plugin's settings (yoursite.com/wp-admin/options-general.php?page=disable_rest_api_settings)

    0 讨论(0)
  • 2021-01-30 14:23

    From the author original question I've chosen option 2 that came from wordpress official recommendations(https://developer.wordpress.org/rest-api/using-the-rest-api/frequently-asked-questions/#can-i-disable-the-rest-api). So just put in your functions.php to let only logged in users use the rest api:

    add_filter( 'rest_authentication_errors', function( $result ) {
        if ( ! empty( $result ) ) {
            return $result;
        }
        if ( ! is_user_logged_in() ) {
            return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) );
        }
        return $result;
    });
    
    0 讨论(0)
  • 2021-01-30 14:31

    Disabling REST API was not a bad idea, after all. It actually opened a huge hole in all websites!

    In wordpress 4.4 there was a way

    Here, I've found a possible solution with .htaccess but should be carefully tested in combination with whatever else is in your .htaccess file (e.g., pretty-url rules added by wordpress itself):

    # WP REST API BLOCK JSON REQUESTS 
    # Block/Forbid Requests to: /wp-json/wp/
    # WP REST API REQUEST METHODS: GET, POST, PUT, PATCH, DELETE
    RewriteCond %{REQUEST_METHOD} ^(GET|POST|PUT|PATCH|DELETE) [NC]
    RewriteCond %{REQUEST_URI} ^.*wp-json/wp/ [NC]
    RewriteRule ^(.*)$ - [F]
    

    A very drastic method, is also to have a 404.html webpage in your root and then add this line:

    # WP REST API BLOCK JSON REQUESTS 
    # Redirect to a 404.html (you may want to add a 404 header!) 
    RewriteRule ^wp-json.*$ 404.html
    

    Note that, unless you use a static page, i.e., not involved with wordpress functions, if you want to return a 404 error with an appropriate error page, this is a complete separate topic, with a lot of issues when Wordpress is involved

    0 讨论(0)
提交回复
热议问题