Mitigate distributed Gitea filter-permutation crawl
Gitea is again CPU-saturated by crawler traffic. The dominant source is a distributed crawl of combinatorial issue/pull filter URLs, e.g. /kosmos/chef/issues?assignee=0&labels=...&milestone=0&poster=0&project=0. It spreads ~1 request across tens of thousands of client IPs with spoofed browser User-Agents and no referer, so the existing per-IP zones (bots, expensive) never fill; the last hour saw ~45k permutation requests from ~43k distinct IPs, and the resulting render/DB load shows up as 499s and slow page loads. - Detect the filter-permutation list pages ($filter_perm, anchored so API paths never match) and cap the anonymous ones with a single non-IP "filters" bucket (1r/s, burst 5). - Exempt authenticated sessions: rewrite_by_lua_block validates the i_like_gitea cookie once against a signed-in-only route (/_gitea_signed_in -> /user/settings; 200 signed in, 303 anonymous) and caches the result per session. Cookie presence alone is not enough, since Gitea issues it to anonymous visitors too. Probe failures fail open so a flaky Gitea cannot throttle a signed-in user. - Skip the probe for rate-limited bot UAs and give the internal probe its own empty-key limit_req zone, so the subrequest cannot set r->main->limit_req_set and disable the main request's limits. - Disallow the combinatorial tree-browse routes in robots.txt (/*/src/branch/, /*/commits/branch/, and the tag variants). Reviewed and signed off by the review agent. Bump cookbook to 0.3.11. The CogBot denial is configured in the encrypted credentials/gitea data bag (bot_filter), which is managed by Ops.
This commit is contained in:
5 files changed
+124
-1
No files matched your search
@@ -2,6 +2,28 @@
|
||||
|
||||
This file is used to list changes made in each version of the kosmos_gitea cookbook.
|
||||
|
||||
# 0.3.11
|
||||
|
||||
- Mitigate a distributed anonymous crawl of combinatorial issue/pull filter
|
||||
URLs (e.g. `/kosmos/chef/issues?assignee=..&labels=..&milestone=..`). The
|
||||
crawler spreads ~1 request across tens of thousands of client IPs with
|
||||
spoofed browser User-Agents and no referer, so the existing per-IP zones
|
||||
(`bots`, `expensive`) never engage.
|
||||
- Detect the filter-permutation list pages (`$filter_perm`) and cap the
|
||||
anonymous ones with a single non-IP `filters` bucket (1r/s, burst 5).
|
||||
- Exempt authenticated sessions: `rewrite_by_lua_block` validates the
|
||||
`i_like_gitea` cookie once against a signed-in-only route
|
||||
(`/_gitea_signed_in` -> `/user/settings`, 200 when signed in, 303 for
|
||||
anonymous) and caches the result per session. Cookie presence alone is not
|
||||
enough, because Gitea issues it to anonymous visitors too. Probe failures
|
||||
fail open so a flaky Gitea cannot throttle a signed-in user.
|
||||
- The internal probe runs its own empty-key `limit_req` zone so it cannot
|
||||
poison `r->main->limit_req_set` and disable the main request's limits.
|
||||
- Disallow the combinatorial tree-browse routes (`/*/src/branch/`,
|
||||
`/*/commits/branch/`, and the `tag` variants) in `robots.txt`; these were not
|
||||
covered before and are what the current AI crawlers walk.
|
||||
- Bump cookbook to 0.3.11.
|
||||
|
||||
# 0.3.10
|
||||
|
||||
- Upgrade Gitea from 28.3.0 to 28.4.0 (maintenance/security release, no `app.ini`
|
||||
|
||||
@@ -4,7 +4,7 @@ maintainer_email 'ops@kosmos.org'
|
||||
license 'MIT'
|
||||
description 'Installs/configures Gitea'
|
||||
long_description 'Installs/configures Gitea'
|
||||
version '0.3.10'
|
||||
version '0.3.11'
|
||||
chef_version '>= 14.0'
|
||||
|
||||
depends "firewall"
|
||||
|
||||
@@ -34,6 +34,76 @@ server {
|
||||
# Only non-empty keys are counted, so normal requests are unaffected.
|
||||
limit_req zone=expensive burst=5 nodelay;
|
||||
|
||||
# --- Anonymous filter-permutation flood ------------------------------------
|
||||
# Count only anonymous issue/pull *filter* requests against a single global
|
||||
# bucket. Authenticated sessions are validated once (cached) and always
|
||||
# exempt; everything else stays unlimited. $filter_perm and the zones are
|
||||
# defined in /etc/openresty/conf.d/rate_limits.conf.
|
||||
set $filter_key "";
|
||||
set $probe_noop "";
|
||||
|
||||
rewrite_by_lua_block {
|
||||
if ngx.var.filter_perm ~= "1" then
|
||||
return
|
||||
end
|
||||
|
||||
-- Rate-limited bot UAs are never probed (this keeps them in the per-IP
|
||||
-- bots zone and avoids limit_req_set poisoning of the main request);
|
||||
-- treat them as anonymous.
|
||||
if (ngx.var.bot_name or "") ~= "" then
|
||||
ngx.var.filter_key = "filters"
|
||||
return
|
||||
end
|
||||
|
||||
local cookie = ngx.var.http_cookie
|
||||
local session = cookie and cookie:match("i_like_gitea=([^;]+)")
|
||||
if not session then
|
||||
ngx.var.filter_key = "filters"
|
||||
return
|
||||
end
|
||||
|
||||
local cache = ngx.shared.gitea_auth
|
||||
local key = ngx.md5(session)
|
||||
local signed_in = cache:get(key)
|
||||
if signed_in == nil then
|
||||
local status = ngx.location.capture("/_gitea_signed_in").status
|
||||
if status == 200 then
|
||||
signed_in = true
|
||||
cache:set(key, signed_in, 300) -- real login, cache 5 min
|
||||
elseif status >= 300 and status < 400 then
|
||||
signed_in = false
|
||||
cache:set(key, signed_in, 30) -- anonymous, cache 30 s
|
||||
else
|
||||
signed_in = true -- probe failed: fail open for auth users
|
||||
cache:set(key, signed_in, 10)
|
||||
end
|
||||
end
|
||||
|
||||
if not signed_in then
|
||||
ngx.var.filter_key = "filters"
|
||||
end
|
||||
}
|
||||
|
||||
limit_req zone=filters burst=5 nodelay;
|
||||
|
||||
# Internal: real-login probe. Gitea answers 200 when the session is signed in
|
||||
# and 303 -> /user/login otherwise. The subrequest inherits the client's
|
||||
# Cookie header. Its own empty-key limit_req replaces the inherited zones so
|
||||
# it cannot set the main request's limit_req flag.
|
||||
location = /_gitea_signed_in {
|
||||
internal;
|
||||
limit_req zone=probe_noop burst=1 nodelay;
|
||||
proxy_pass http://_gitea_web/user/settings;
|
||||
proxy_pass_request_body off;
|
||||
proxy_set_header Content-Length "";
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_read_timeout 2s;
|
||||
}
|
||||
|
||||
location ~ ^/(avatars|repo-avatars)/.*$ {
|
||||
proxy_buffers 1024 8k;
|
||||
proxy_pass http://_gitea_web;
|
||||
|
||||
@@ -62,3 +62,30 @@ map $expensive_request $expensive_key {
|
||||
}
|
||||
|
||||
limit_req_zone $expensive_key zone=expensive:20m rate=10r/m; # ~1 per 6s per IP
|
||||
|
||||
# Anonymous combinatorial filter-permutation flood.
|
||||
#
|
||||
# A distributed crawler spreads ~1 request across tens of thousands of client
|
||||
# IPs, so the per-IP zones above never fill. These list pages are filter
|
||||
# permutations of issue/pull lists and are CPU/DB heavy to render.
|
||||
#
|
||||
# $filter_perm is 1 only for /<owner>/<repo>/(issues|pulls) with filter query
|
||||
# params. Anchored so API paths (/api/v1/...) and other pages never match.
|
||||
map $request_uri $filter_perm {
|
||||
default 0;
|
||||
~*^/[^/]+/[^/]+/(issues|pulls)\?.*(assignee|labels|milestone|poster|project)= 1;
|
||||
}
|
||||
|
||||
# Per-session cache of "is this i_like_gitea cookie a real login?" answers.
|
||||
# Filled by rewrite_by_lua_block in the Gitea vhost.
|
||||
lua_shared_dict gitea_auth 10m;
|
||||
|
||||
# Only anonymous filter permutations reach this key; authenticated sessions and
|
||||
# everything else leave it empty, and empty keys are not counted.
|
||||
limit_req_zone $filter_key zone=filters:5m rate=1r/s;
|
||||
|
||||
# Dummy zone with an always-empty key. Referenced only by the internal
|
||||
# /_gitea_signed_in probe location so that its location-level limit_req
|
||||
# replaces the inherited server-level zones. This prevents the subrequest from
|
||||
# setting r->main->limit_req_set and disabling the main request's limits.
|
||||
limit_req_zone $probe_noop zone=probe_noop:1m rate=1000r/s;
|
||||
@@ -15,6 +15,10 @@ Disallow: /*/raw/
|
||||
Disallow: /*/archive/
|
||||
Disallow: /*/src/commit/
|
||||
Disallow: /*/commits/commit/
|
||||
Disallow: /*/src/branch/
|
||||
Disallow: /*/commits/branch/
|
||||
Disallow: /*/src/tag/
|
||||
Disallow: /*/commits/tag/
|
||||
Disallow: /*.patch$
|
||||
Disallow: /*.diff$
|
||||
Allow: /
|
||||
Reference in new issue
Block a user