Giter VIP home page Giter VIP logo

wprestcop's Introduction

WP REST Cop

Manage access to the WP REST API with rate limits and IP-based rules.

Contributors: Brady Vercher Requires: WP 4.4+, PHP 5.4+ Tested up to: 4.4 License: GPL-2.0+

Rate Limits

Rate limits allow for configuring the number of requests a client can make within a certain interval. The default in WP Rest Cop is 500 requests per hour.

The rate limit functionality requires a persistent object cache.

Headers

A few headers are sent with every request so clients can keep track of their current limit:

Header Description
X-RateLimit-Limit Requests allowed per interval.
X-RateLimit-Remaining Remaining requests allowed in the current interval.
X-RateLimit-Reset Seconds until the limit is reset.

If client has reached their limit, an additional header will be sent.

Header Description
Retry-After Seconds until the limit is reset

Clients may send a HEAD request to view their current limit without ticking the meter.

Configuring Settings

Configure the default limit and interval settings using the simple API from the main plugin instance:

<?php
/**
 * Set the rate limit to 10 requests every 5 minutes.
 */
add_action( 'wprestcop_plugin_loaded', function( $wprestcop ) {
	$wprestcop
		->set_limit( 10 )
		->set_interval( 5 * MINUTE_IN_SECONDS );
} );

Settings can also be configured with the built-in WP CLI commands.

Disable Rate Limiting

If you just want the IP rules functionality and want to disable the rate limits, set the interval to -1.

IP Rules

IP rules can be configured globally, or at the route level as a simple whitelist or blacklist.

Global Configuration

<?php
/**
 * Global IP rules configuration.
 */
add_action( 'wprestcop_plugin_loaded', function( $wprestcop ) {
	$wprestcop->get_ip_rules()
		->allow( '192.168.50.4' ); // Also accepts an array of IP addresses.

	// Or...

	$wprestcop->get_ip_rules()
		->deny( '66.249.66.1' ); // Also accepts an array of IP addresses.
} );

When allowing an IP address, the policy is to deny any requests from IPs not in the whitelist.

The opposite is true when denying IP addresses. All IPs not in the blacklist will have access.

Global IP rules can also be configured with the built-in WP CLI commands.

Route Configuration

Routes may also be configured with their own IP rules:

<?php
/**
 * Register routes.
 */
add_action( 'rest_api_init', function () {
    register_rest_route( 'myplugin/v1', '/internal/(?P<id>\d+)', [
        'methods'  => 'GET',
        'callback' => 'my_awesome_expensive_func',
        'ips'      => [
            'allow' => [ '192.168.50.4' ],
            'deny'  => [ '66.249.66.1' ],
        ]
    ] );
} );

WP CLI Commands

A few WP CLI commands are included to configure the plugin without requiring code.

Command Description
wp restcop allow <ip>... Whitelist one or more IPs.
wp restcop check <ip> Check whether an IP has access.
wp restcop deny <ip>... Blacklist one or more IPs.
wp restcop set <key> <value> Update a setting value.
wp restcop status View global IP rules.

Potential Roadmap

  • Support for logging various events.
  • Additional rate limit strategies.
  • More route-level capabilities.
  • Advanced access rules.
  • Administration UI.

wprestcop's People

Contributors

dennisnissle avatar bradyvercher avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.