plugins

BareProxy Rate Limiting and WAF Plugin: Per-Address Limits and Bad-Request Rules at the Edge

Status: built, number 8 of 21 in BareProxy’s build order, and out in BareProxy 0.10.0 as limits.wasm on the releases page. Its manual is the README in the code repository.

Two of the oldest reasons to put a proxy in front of an app are to stop one client from taking all of it, and to turn away requests that are obviously up to no good before the app has to look at them. Most small apps handle neither. A login form that takes a thousand guesses a minute, or a search endpoint hammered by a script, can take a site down long before anyone notices an attack.

What It Does

Limits. A limit counts requests per client address or for the whole site, on chosen paths or all of them, over a sliding window, and answers 429 Too Many Requests past it, with a Retry-After header. A typical setup gives the whole site a generous limit and the login path a tight one: limit login 5 per 1m path /login.

The counting is BareProxy’s own. A plugin runs as several copies to share the load, and a limit counted in each copy separately would let a client through several times over. So 0.10.0 adds counters to BareProxy itself, kept for a plugin and shared by all of its copies, with old windows swept away so a counter per visitor never piles up. The window slides, so a client can’t send a full allowance just before a minute turns and another just after.

Basic WAF rules. Simple checks that catch most of the junk:

  • methods a site never uses
  • URLs and headers over a size
  • paths no real visitor asks for, such as /wp-login.php on a site that isn’t WordPress, or /.env
  • known scanners by user agent

One line, common-rules on, adds a short list of the usual ones: dotfiles such as /.env and /.git/, and scanners such as sqlmap, nikto and nuclei.

The rules follow BareProxy’s own design rule: exact values, prefixes and words, no regular expressions. That keeps every rule readable, and it keeps a rule from becoming the next security bug. A full WAF with thousands of signatures is a different product; this plugin covers the part a small site actually needs.

Here is a config:

plugin limits /etc/bareproxy/plugins/limits.wasm
  config /etc/bareproxy/plugins/limits.conf

site example.com
  use limits
  route /* -> app

And the plugin’s own:

# limits.conf
limit site 600 per 1m
limit login 5 per 1m path /login
methods GET HEAD POST
common-rules on
allow 203.0.113.0/24

Seeing What It Did

Turning traffic away is only safe when you can see why. Every request the plugin answers carries a note in its record: which limit or rule matched, the count, the window. why on a 429 says which limit the client hit and when the window resets: “limit login (line 2): 6 requests in the last 60s from 203.0.113.9, over 5; the window resets in 34s”. A rule that blocks a real visitor shows up in the records the same day, not after a week of support mail.

This plugin replaces the “Limit” module of BareProxy’s earlier plan, which would have been compiled into the binary. As a plugin, it is installed by one config line and removed by deleting it.

The whole program is on the plugins page.