Per-User Rate Limiting
Published: 2021-04-18 10:53:04

---
title: Per-User Rate Limiting
published: true
date: 2021-04-14 20:35:00 UTC
tags: 
canonical_url: https://kevincox.ca/2021/04/14/per-user-rate-limiting/
---

<main><h2 id="the-problem"><a href="https://kevincox.ca/2021/04/14/per-user-rate-limiting/#the-problem">The Problem</a></h2>

<p>You want to rate limit requests based on application data such as User ID or User Loyalty, however you want to apply rate-limiting before the application layer so you have no way to trust the client provided values.</p>

<p>This is a common problem, making the industry-standard approach rate limiting by IP (or CIDR block). In fact some <a href="https://docs.aws.amazon.com/waf/latest/developerguide/waf-rule-statement-type-rate-based.html">popular</a> <a href="https://support.cloudflare.com/hc/en-us/articles/115001635128-Configuring-Cloudflare-Rate-Limiting#5iIwkkHwcJbNRynWjrDIGb">CDNs</a> only support rate-limits keyed by IP. Unfortunately IP-based limiting usually insufficient as it either allows too much malicious traffic or blocks a significant number of legitimate users (imagine a lot of users behind a carrier-grade or office NAT).</p>

<h2 id="the-solution"><a href="https://kevincox.ca/2021/04/14/per-user-rate-limiting/#the-solution">The Solution</a></h2>

<ol>
  <li>Pass the application info in a proxy-visible part of the request such as a cookie.</li>
  <li>Configure the rate limiter to use the application info. (likely falling back to IP-based keys)</li>
  <li>The application must validate the info and provide a “useless” response if it isn’t accurate.</li>
</ol>

<p>Step 1 and 2 are quite obvious but step 3 is the secret ingredient to allow a “dumb” proxy to apply rate-limiting based on application values. Step 3 ensues that spoofed values don’t provide any value to the attacker, defeating the purpose of spoofing them in the first place.</p>

<p>Note that “value to the attacker” can be very broad, so make sure that it reflects your rate limiting goals.</p>

<ul>
  <li>If you are trying to prevent DoS attacks ensure that the validation is sufficiently cheap.</li>
  <li>If you are trying to prevent scraping ensure that you don’t return any valuable data.</li>
</ul>

<p>Common actions upon a failed authentication may be:</p>
<ul>
  <li>If the information is being passed in a cookie unset (or correct) the cookie and redirect the user to the same URL.</li>
  <li>Redirect to a login page which only rate-limits by IP.</li>
</ul>

<h2 id="nginx-example"><a href="https://kevincox.ca/2021/04/14/per-user-rate-limiting/#nginx-example">NGINX Example</a></h2>

<p>NGINX can support this quite easily. Here is an example of rate limiting by User ID which is passed in the <code>uid</code> cookie.</p>

<div><pre><code><span>map</span> <span>$cookie_uid</span> <span>$rate_limit_key</span> <span>{</span>
	<span>""</span> <span>$binary_remote_addr</span><span>;</span>
	<span>default</span> <span>$cookie_uid</span><span>;</span>
<span>}</span>
<span>limit_req_zone</span> <span>$rate_limit_key</span> <span>zone=auth:10m</span> <span>rate=10/s</span><span>;</span>
</code></pre></div>
<p>This snippet applies the same limit to users and IPs but two <code>limit_req_zone</code>s can be used to enforce different limits for each group.</p>

<h2 id="concerns"><a href="https://kevincox.ca/2021/04/14/per-user-rate-limiting/#concerns">Concerns</a></h2>

<h3 id="lock-out-on-mismatch"><a href="https://kevincox.ca/2021/04/14/per-user-rate-limiting/#lock-out-on-mismatch">Lock-out on Mismatch</a></h3>

<p>Be sure that you don’t lock out a user based on a mismatch. For example if you use an auth-token in the cookie to validate the user ID don’t return a 401 if the auth token is expired or revoked. This will result in the user having all request blocked with no obvious way to fix the problem. (Your support team will probably need to ask them to clear their cookies).</p>

<p>As mentioned above a better option would be to clear the untrusted data so that the user reverts to the regular IP-based filtering.</p>

<h3 id="cost-of-validation"><a href="https://kevincox.ca/2021/04/14/per-user-rate-limiting/#cost-of-validation">Cost of Validation</a></h3>

<p>The cost of validation is likely low. This is because you will generally want to validate the auth-token for authenticated requests anyways and validating the auth-token will often give you enough information to validate the rate-limiting settings anyways (for example the user ID). However for requests for public information this validation may be otherwise unnecessary and raise the overall cost of serving the requests.</p>

<p>Unfortunately I don’t know of any great solutions, this is a cost of rate limiting based on authentication, you actually need to perform the authentication! One workaround is excluding requests for public data from rate limiting, then you don’t need to validate the authentication information. However be sure that the requests that bypass the rate-limit in sync between the rate-limiter and the application as any endpoint that isn’t validated is effectively unlimited (as an attacker can send a random rate-limit key for each request). One easy way to keep this in-sync is to strip the rate-limit key from the request before passing it to the application, then the application doesn’t have anything to validate. This approach will reduce the cost but will leave the application exposed to scrapers, using a very high IP-based rate limit may be enough to prevent the worst attacks without affecting many legitimate requests.</p>

<h2 id="possible-tweaks"><a href="https://kevincox.ca/2021/04/14/per-user-rate-limiting/#possible-tweaks">Possible Tweaks</a></h2>

<h3 id="key-passing"><a href="https://kevincox.ca/2021/04/14/per-user-rate-limiting/#key-passing">Key Passing</a></h3>

<p>Passing the rate-limiting info via cookie is an easy option because it is automatically added by the browser to all subsequent requests, however basically any method will work, including URL parameters or custom headers. Just be careful about locking a user out. If you are using a custom method to pass the rate-limiting information you need to ensure that all of your clients will gracefully handle a request that has been rejected due to mismatched rate-limiting information.</p>

<h3 id="variable-limits"><a href="https://kevincox.ca/2021/04/14/per-user-rate-limiting/#variable-limits">Variable Limits</a></h3>

<p>If you have a flexible enough rate-limiting system you can even set the rate limit based on the application data. For example logged-in users may get 10r/s but logged in users who have spent at least $20 can make 20r/s. This can be a good way to prevent the effectiveness of account-spamming while serving your most loyal customers reliably.</p>