APIacFilterThrottler

An APIac.Filter plug for API requests rate-limiting

This plug uses the Exhammer package as its backend. This library uses the token bucket algorithm, which means that this plug is mainly suitable for limiting abuses, not for accurate rate limiting. By default, a local ETS backend is launched on startup.

def deps do
  [
    {:apiac_filter_throttler, "~> 1.0"}
  ]
end

Plug options

Example

Allow 50 request / 10 seconds per subject and per client:

plug APIacFilterThrottler, key: &APIacFilterThrottler.Functions.throttle_by_subject_client/1,
  scale: 10_000,
  limit: 50

Allow 5000 requests / minute per client, only for machine-to-machine access:

plug APIacFilterThrottler, key: &APIacFilterThrottler.Functions.throttle_by_client/1,
  exec_cond: &APIac.machine_to_machine?/1,
  scale: 60_000,
  limit: 5000

Security considerations

Consider the risk of collisions when constructing the key> For instance, a key function concatenating the ip address and a subject (username) would return the same key ("72.23.241.121edwards") for:

The more control an attacker has on choosing the key parameters (e.g. the username), the easier to find a collision.

Finding a collision can result in a DOS for the legitimate requester.

Using a hash function such as :erlang.phash2/1, MD5, etc. cam help mitigate the risk, at the expense of performance. Also note that :erlang.phash2/1 is not a collision-resistant hash function (as results are not uniformly distributed).