Captcha Protect

Traefik middleware to challenge individual IPs in a subnet when traffic spikes are detected from that subnet, using a captcha of your choice for the challenge (turnstile, recaptcha, or hcaptcha).
You may have seen CAPTCHAs added to individual forms on the web to prevent bots from spamming submissions. This plugin extends that concept to your entire site (or specific routes on your site), effectively placing your entire site behind a CAPTCHA. However, the CAPTCHA is only triggered when a spike in traffic is detected from the same IP subnet. Once the CAPTCHA is successfully completed, that IP is no longer challenged, allowing uninterrupted browsing.
The basic logic looks like
flowchart TD
Client(Client accesses path on website) --> IP{Has client passed captcha challenge in the last 24h?}
IP -- Yes --> Continue(Go to original destination)
IP -- No --> IP_BYPASS{Is client IP excluded by captcha-protect config?}
IP_BYPASS -- Yes --> Continue(Go to original destination)
IP_BYPASS -- No --> GOOD_BOT{Is client IP hostname in allowed bot list?}
GOOD_BOT -- No --> PROTECTED_ROUTE{Is this route protected?}
GOOD_BOT -- Yes --> CANONICAL_URL_BOT{Are there URL parameters?}
CANONICAL_URL_BOT -- Yes --> PROTECTED_ROUTE{Is this route prefix in protectRoutes?}
CANONICAL_URL_BOT -- No --> Continue(Go to original destination)
PROTECTED_ROUTE -- Yes --> RATE_LIMIT{Is this IP in a range seeing increased traffic?}
PROTECTED_ROUTE -- No --> Continue(Go to original destination)
RATE_LIMIT -- Yes --> REDIRECT(Redirect to /challenge)
RATE_LIMIT -- No --> Continue(Go to original destination)
REDIRECT --> CHALLENGE{turnstile/recaptcha/hcaptcha challenge}
CHALLENGE -- Pass --> Continue(Go to original destination)
CHALLENGE -- Fail --> Stuck
Config
Example
Below is an example docker-compose.yml with traefik as the frontend, and nginx as the backend. nginx is using this middleware to protect routes on the site that start with / (protectRoutes: "/")
Since the config values aren't specified, captcha-protect would use the default rateLimit: 20 and window: 86400 so any IPv4 in X.Y.0.0/16 (or ipv6 in /64) could only access the site 20 times before individual IPs in that subnet are required to pass a captcha to continue browsing.
networks:
default:
services:
nginx:
image: nginx:${NGINX_TAG}
labels:
traefik.enable: true
traefik.http.routers.nginx.entrypoints: http
traefik.http.routers.nginx.service: nginx
traefik.http.routers.nginx.rule: Host(`${DOMAIN}`)
traefik.http.services.nginx.loadbalancer.server.port: 80
traefik.http.routers.nginx.middlewares: captcha-protect@docker
traefik.http.middlewares.captcha-protect.plugin.captcha-protect.protectRoutes: "/"
traefik.http.middlewares.captcha-protect.plugin.captcha-protect.captchaProvider: turnstile
traefik.http.middlewares.captcha-protect.plugin.captcha-protect.siteKey: ${TURNSTILE_SITE_KEY}
traefik.http.middlewares.captcha-protect.plugin.captcha-protect.secretKey: ${TURNSTILE_SECRET_KEY}
traefik.http.middlewares.captcha-protect.plugin.captcha-protect.goodBots: apple.com,archive.org,duckduckgo.com,facebook.com,google.com,googlebot.com,googleusercontent.com,instagram.com,kagibot.org,linkedin.com,msn.com,openalex.org,twitter.com,x.com
traefik.http.middlewares.captcha-protect.plugin.captcha-protect.persistentStateFile: /tmp/state.json
networks:
default:
aliases:
- nginx
traefik:
image: traefik:${TRAEFIK_TAG}
command: >-
--api.insecure=false
--api.dashboard=false
--api.debug=false
--ping=true
--entryPoints.http.address=:80
--providers.docker=true
--providers.docker.network=default
--experimental.plugins.captcha-protect.modulename=github.com/libops/captcha-protect
--experimental.plugins.captcha-protect.version=v1.2.0
volumes:
- /var/run/docker.sock:/var/run/docker.sock:z
- /CHANGEME/TO/A/HOST/PATH/FOR/STATE/FILE:/tmp/state.json:rw
ports:
- "80:80"
networks:
default:
aliases:
- traefik
healthcheck:
test: traefik healthcheck --ping
depends_on:
nginx:
condition: service_started
Config options
| Parameter |
Type (Required) |
Default |
Description |
protectRoutes |
[]string (required) |
"" |
Comma-separated list of route prefixes to protect. e.g., "/" protects the entire site (including file/js/css downloads, which you likely don't want). "/browse" protects its subtree. |
captchaProvider |
string (required) |
"" |
The captcha type to use. Supported values: turnstile, hcaptcha, and recaptcha. |
siteKey |
string (required) |
"" |
The captcha site key. |
secretKey |
string (required) |
"" |
The captcha secret key. |
rateLimit |
uint |
20 |
Maximum requests allowed from a subnet before a challenge is triggered. |
window |
int |
86400 |
Duration (in seconds) for monitoring requests per subnet. |
ipv4subnetMask |
int |
16 |
CIDR subnet mask to group IPv4 addresses for rate limiting. |
ipv6subnetMask |
int |
64 |
CIDR subnet mask to group IPv6 addresses for rate limiting. |
ipForwardedHeader |
string |
"" |
Header to check for the original client IP if Traefik is behind a load balancer. |
goodBots |
[]string (encouraged) |
see below |
List of second-level domains for bots that are never challenged or rate-limited. |
protectParameters |
string |
"false" |
Forces rate limiting even for good bots if URL parameters are present. Useful for protecting faceted search pages. |
protectFileExtensions |
[]string |
"" |
Comma-separated file extensions to protect. By default, your protected routes only protect html files. This is to prevent files like CSS/JS/img from tripping the rate limit. |
exemptIps |
[]string |
privateIPs |
CIDR-formatted IPs that should never be challenged. Private IP ranges are always exempt. |
challengeURL |
string |
"/challenge" |
URL where challenges are served. This will override existing routes if there is a conflict. |
challengeTmpl |
string |
"./challenge.tmpl.html" |
Path to the Go HTML template for the captcha challenge page. |
enableStatsPage |
string |
"false" |
Allows exemptIps to access /captcha-protect/stats to monitor the rate limiter. |
logLevel |
string |
"INFO" |
Log level for the middleware. Options: ERROR, WARNING, INFO, or DEBUG. |
persistentStateFile |
string |
"" |
File path to persist rate limiter state across Traefik restarts. In Docker, mount this file from the host. |
Good Bots
To avoid having this middleware impact your SEO score, it's recommended to provide a value for goodBots. By default, no bots will be allowed to crawl your protected routes beyond the rate limit unless their second level domain (e.g. google.com) is configured as a good bot.
A good default value for goodBots would be:
goodBots: apple.com,archive.org,duckduckgo.com,facebook.com,google.com,googlebot.com,googleusercontent.com,instagram.com,kagibot.org,linkedin.com,msn.com,openalex.org,twitter.com,x.com
However if you set the config parameter protectParameters="true", even good bots won't be allowed to crawl protected routes if a URL parameter is on the request (e.g. /foo?bar=baz). This protectParameters feature is meant to help protect faceted search pages.
Similar projects
- Traefik RateLimit middleware - the core traefik ratelimit middleware will start sending 429 responses based on individual IPs, which might not be good enough to protect against traffic coming from distributed networks.
- crowdsec-bouncer-traefik-plugin has a captcha option, but requires integrating with crowdsec to verify individual IPs. This plugin (captcha-protect) instead just checks the traffic actually visiting your site and verifies the traffic is from a person only when the traffic exceeds some rate limit you configure.
Attribution