Brewless Brewless Docs

Running your application

API Guardian

bunny.net checks requests to the API endpoints you pick against your OpenAPI document, at the edge. Optional, on Shield Advanced.

On this page

API Guardian is part of bunny.net Shield. It checks requests to the endpoints of your API against your OpenAPI document, at the edge, before they reach your container: the path, query, header and cookie parameters, the JSON body, and the authentication the document asks for. A rate limit per endpoint can go with it.

Warning

Laravel validation, authentication and authorization remain your primary controls. API Guardian is an extra layer in front of them, never a replacement.

It is optional, and it requires Shield Advanced ($9.50/month at bunny.net, billed by bunny.net to your own account). bunny.net marks API Guardian as a preview. On Shield Basic the deploy says so and changes nothing: Brewless never changes the plan from a deploy. Change it on the Security card of the environment, see Changing the plan.

Turning it on

Pick the endpoints under the environment, in brewless.yml. Advanced takes 10, Business 50.

brewless.yml
environments:
  production:
    firewall:
      api_guardian:
        mode: block
        endpoints:
          - POST /api/orders
          - GET /api/orders/{order}
          - POST /api/login
        rate_limit:
          POST /api/login: { requests: 10, per: 1m, by: ip }

Write each endpoint as its method and its path as a visitor asks it, with the parameters as your document writes them. The console lists the endpoints of the live release's document on the API Guardian card: tick the ones that matter most, and it writes this fragment for you, with a count of how many your plan takes. The file stays the truth: the console changes nothing at bunny.net.

Setting What it does Default
spec auto, or a JSON or YAML file of the repository with the document auto
mode log only records; block refuses a request that breaks the document log
endpoints The endpoints that are checked none
unknown_paths ignore, log or block: a request to a path the endpoints do not name ignore
validate_responses Also check responses against the document, only while API Guardian records false
rate_limit Per endpoint: requests, per (1s, 10s, 1m, 5m, 15m, 1h), by (ip or global) none

Only unknown_paths: ignore leaves the rest of your site to the managed WAF as it was.

Where the document comes from

Brewless never sends a request to your application to fetch a document. The build finds it, in this order:

  1. The file spec names. JSON or YAML, OpenAPI 3.0 or 3.1.
  2. Scramble: the build runs php artisan scramble:export, which reads your routes and code, not your database. Scramble has to be installed without --dev, because the build installs without dev dependencies. Otherwise export the document yourself and name it with spec.
  3. Scribe, from the endpoints it keeps in .scribe/: the build runs scribe:generate --no-extraction, which needs no database and makes no response calls, in a copy of your project so nothing it writes ends up in the image.

The build keeps the document with the image and next to the built files in the environment's storage zone, per image. The edge never serves it.

bunny.net takes OpenAPI 3.0 only. A 3.1 document, which Scramble writes, is converted: type: [x, "null"] becomes nullable, const becomes a one-value enum, examples becomes example, and the like. What 3.0 cannot say, such as if/then/else or patternProperties, is left out, and the deploy says how many parts it left out.

Only the endpoints you picked go to bunny.net, with only the parts of the document they use. A path under a server with a path of its own (Scramble's https://example.com/api) is written in full: /api/orders. bunny.net limits a document to 2 MB and limits how deep and how large its schemas are; an endpoint that would take it past a limit is left out, and the deploy names it.

Log first, then block

API Guardian changes after the release went live, never before: a stricter document must not refuse the requests of the release before it while that one still answers.

  • A new or changed document always starts in log mode. Brewless compares a hash of what it would send with what it sent last, and only sends a changed one.
  • With mode: block, API Guardian blocks once the document recorded nothing for 24 hours, as bunny.net's own metrics count it. The next deploy looks, and so does a daily look. Nothing is measured by calling your application.
  • Response validation is only on while API Guardian records. In block mode a response that differs from the document would become an error for the visitor, so it is switched off and the deploy says so.

Recorded requests in the log period point at a document that does not match what your application accepts. Fix the document, or the endpoint, and deploy: the log period starts again.

Rollback

brewless rollback gives API Guardian the document of the release it returns to, in log mode again. Leaving api_guardian out of brewless.yml switches API Guardian off with the next deploy; bunny.net keeps the last document, unused.

What the deploy says

API Guardian never fails a deploy. When something is left out, the deploy says what and why: a plan below Advanced, no document found, an endpoint the document does not have, more endpoints than the plan takes, a document over one of bunny.net's limits, or bunny.net out of reach.

In the console

The API Guardian card on the Shield tab of the environment's overview shows whether it records or blocks, until when it records when the file asks block, and per endpoint what bunny.net counted over the last 28 days: requests checked, recorded and blocked, invalid requests, failed authorization and rate limited. The figures are read live from bunny.net and kept a minute.