Download the PHP package leuchtdiode/mezzio-fence without Composer

On this page you can find all versions of the php package leuchtdiode/mezzio-fence. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package mezzio-fence

mezzio-fence

Per ip rate limiting for a mezzio application. Everything that is routed is counted; an action that wants its own numbers says so with an attribute.

Install

Add the config provider in config/config.php:

And pipe the middleware in config/pipeline.php, directly after MethodNotAllowedMiddleware:

That position matters in both directions. Above it, the routing middlewares have put the RouteResult on the request, which is the only way the action behind the route is known. Below it come the session and the parsed body, which a refused request then never pays for.

Log\Middleware\GlobalLogMiddleware has to be piped before it - as the outermost middleware, which is where it belongs anyway - because the fence logs.

Add the counter directory to .gitignore.

Config

directory is relative to the working directory, which for a mezzio application is the project root. It must be writable and must not be inside the document root.

trustedIps takes wildcards - 10.0.*.*, 2001:db8:*:*::* - and is where whatever polls your health route belongs. A trusted address is recognised before any file is touched.

The attribute

#[Fence] goes on the action class. Whatever is left null keeps the configured default, so

An action that carries the attribute is also counted in a bucket of its own. Everything else shares one bucket per address, so a visitor touching a dozen endpoints in a minute owns one counter rather than a dozen.

The attribute is looked for up the class hierarchy, so a base class of related actions is a perfectly good place to put one. What is counted stays the concrete action either way.

What a refusal looks like

429, a Retry-After header saying how many seconds are left of the window, and a body in the shape the rest of the api answers in:

An application that answers screens rather than json writes its own Refusal\Responder, puts it in its own namespace and names it in fence.responder.

fence:prune

Removes the counters of addresses that have not been back inside pruneAfterSeconds. Nothing depends on it: a counter that is hit again finds its window has turned over and starts from nought, so every address still visiting keeps exactly one file however long it keeps visiting. Schedule it if you like a tidy disk.

What it does not do

The count is per node. The counters are files, so two application servers keep two counts and a limit of 300 is effectively 600. That is a deliberate trade. A database counter would have to be written before the application has read anything, and on a primary/replica connection the first write pins the whole request to the primary - every read after it, on every request, would leave the replicas idle. Isolating that means a second connection, a second entity manager, a schema filter so the application's own migrations diff stops trying to drop the table, and a migration in every application that installs this. For a guard whose job is to stop one script hammering one host, per node counting is the better bargain - nginx's own limiter counts per worker for the same reason.

Locking wants a local filesystem. flock is reliable on one, and is not on NFS. Put the directory on local disk, the same as your php session files.

The address has to be trustworthy. Common\Util\IpUtil reads X-Real-IP before REMOTE_ADDR. A reverse proxy that sets that header and overwrites whatever the client sent - traefik and nginx both do - is what makes it worth anything, and the application must not be reachable around that proxy. Without one in front, remove X-Real-IP at the edge or the guard can be walked past with a header.

It counts requests, not people. A proxy farm defeats it, as it defeats any per address limit. This raises the floor from nothing to something; it is not a bot detector.

Privacy

Nothing written to disk is an address anybody can read back: the key is a sha256, and a v6 address is cut to its /64 first so that a single customer's allocation is one counter rather than 18 quintillion. The address itself appears in one place, the warning logged as a limit is crossed, where it rotates with the rest of the log.


All versions of mezzio-fence with dependencies

PHP Build Version
Package Version
Requires php Version ~8.4.0 || ~8.5.0
leuchtdiode/mezzio-common Version ^2.0.0
leuchtdiode/mezzio-console Version ^0.1.0
leuchtdiode/mezzio-log Version ^1.1.0
psr/http-server-middleware Version ^1.0.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package leuchtdiode/mezzio-fence contains the following files

Loading the files please wait ...