Mitigate distributed Gitea filter-permutation crawl #720

Merged
greg merged 3 commits from chore/dos_mitigation into master 2026-12-04 13:22:04 +00:00
Owner

Gitea was again CPU-saturated by crawler traffic. The dominant source was a distributed crawl of combinatorial issue/pull filter URLs (for example /kosmos/chef/issues?assignee=0&labels=253,259&milestone=0&poster=0&project=0&state=all&type=all): roughly one request per IP across tens of thousands of IPs, with spoofed browser User-Agents and no referer, so the existing per-IP bots and expensive zones never filled. In the hour before the change there were about 45k permutation requests from about 43k distinct IPs; the resulting render and DB load showed up as 499s and slow page loads.

This adds an aggregate, non-IP bucket for the filter-permutation list pages and exempts authenticated sessions:

  • rate_limits.conf.erb: a $filter_perm map anchored to /<owner>/<repo>/(issues|pulls)? with filter params (so API paths such as /api/v1/... never match), a lua_shared_dict for the session cache, and a global filters zone at 1r/s with burst 5.
  • nginx_conf_web.erb: a rewrite_by_lua_block inside location / that marks only anonymous filter requests with the filters key. Authenticated sessions are validated once against a signed-in-only route (/_gitea_signed_in proxying /user/settings, which answers 200 when signed in and 303 otherwise) and cached per session; the probe fails open so a flaky Gitea cannot throttle a signed-in user. The gate is scoped to location / so the internal probe location does not inherit it and re-enter the gate (a server-level handler caused a subrequest cycle).
  • robots.txt.erb: disallow the combinatorial tree-browse routes (/*/src/branch/, /*/commits/branch/, and the tag variants), which the current AI crawlers walk.
  • credentials/gitea data bag: add CogBot to deny_user_agents (re-encrypted with the shared secret).

Verified on draco and fornax: nginx -t ok and zero lua/subrequest errors after the fix, anonymous filter requests return 429 once the burst is exhausted, plain pages and assets return 200, and CogBot and PerplexityBot return 403. Anonymous filter traffic is being shed (thousands of 429s per minute) and the gitea-2 load average dropped from roughly 4.4 to 2.8.

Bumps the cookbook to 0.3.11.

Gitea was again CPU-saturated by crawler traffic. The dominant source was a distributed crawl of combinatorial issue/pull filter URLs (for example `/kosmos/chef/issues?assignee=0&labels=253,259&milestone=0&poster=0&project=0&state=all&type=all`): roughly one request per IP across tens of thousands of IPs, with spoofed browser User-Agents and no referer, so the existing per-IP `bots` and `expensive` zones never filled. In the hour before the change there were about 45k permutation requests from about 43k distinct IPs; the resulting render and DB load showed up as `499`s and slow page loads. This adds an aggregate, non-IP bucket for the filter-permutation list pages and exempts authenticated sessions: - `rate_limits.conf.erb`: a `$filter_perm` map anchored to `/<owner>/<repo>/(issues|pulls)?` with filter params (so API paths such as `/api/v1/...` never match), a `lua_shared_dict` for the session cache, and a global `filters` zone at 1r/s with burst 5. - `nginx_conf_web.erb`: a `rewrite_by_lua_block` inside `location /` that marks only anonymous filter requests with the `filters` key. Authenticated sessions are validated once against a signed-in-only route (`/_gitea_signed_in` proxying `/user/settings`, which answers 200 when signed in and 303 otherwise) and cached per session; the probe fails open so a flaky Gitea cannot throttle a signed-in user. The gate is scoped to `location /` so the `internal` probe location does not inherit it and re-enter the gate (a server-level handler caused a subrequest cycle). - `robots.txt.erb`: disallow the combinatorial tree-browse routes (`/*/src/branch/`, `/*/commits/branch/`, and the `tag` variants), which the current AI crawlers walk. - `credentials/gitea` data bag: add CogBot to `deny_user_agents` (re-encrypted with the shared secret). Verified on draco and fornax: `nginx -t` ok and zero lua/subrequest errors after the fix, anonymous filter requests return `429` once the burst is exhausted, plain pages and assets return `200`, and CogBot and PerplexityBot return `403`. Anonymous filter traffic is being shed (thousands of `429`s per minute) and the `gitea-2` load average dropped from roughly 4.4 to 2.8. Bumps the cookbook to 0.3.11.
raucao added the
service
gitea
opsenhancement
labels 2026-12-04 12:59:21 +00:00
raucao requested review from greg 2026-12-04 12:59:33 +00:00
raucao added 3 commits 2026-12-04 13:00:30 +00:00
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.
CogBot (cognition.ai) was the second-largest source of the current crawler
load, walking /src/branch/ and /commits/branch/ tree routes with a declared
User-Agent. Add it to the credentials/gitea bot_filter deny_user_agents list,
so the edge returns 403 and robots.txt disallows it.

Data bag re-encrypted with the configured secret; all attributes remain
encrypted (aes-256-gcm v3).
Scope the Gitea filter gate to location / to avoid a subrequest cycle
kosmos/actions: Required labels / Required labels (pull_request) Successful in 12s
ac639dcbe9
The gate was a server-level rewrite_by_lua_block. Server-level rewrite_by_lua
is inherited by every location, including the internal /_gitea_signed_in probe.
Because $request_uri (which drives $filter_perm) is the *original* request URI
and is inherited by subrequests, the probe subrequest re-entered the gate and
captured "/_gitea_signed_in" again -> "lua subrequests cycle" and
"failed to issue subrequest: -1", i.e. a 500 for cookie-bearing clients.

Move the block into "location /" so the probe location no longer inherits it.
$filter_key is still set before the server-level limit_req (rewrite phase runs
before preaccess), so the behaviour is unchanged for real requests.

Verified on draco/fornax: nginx -t ok, zero cycle errors, anonymous filter
requests 429, plain pages 200.
raucao force-pushed chore/dos_mitigation from 1f67eefbac to ac639dcbe9 2026-12-04 13:00:30 +00:00 Compare
raucao added the kredits-2 label 2026-12-04 13:05:37 +00:00
raucao removed the kredits-2 label 2026-12-04 13:08:17 +00:00
raucao added the kredits-2 label 2026-12-04 13:10:28 +00:00
greg approved these changes 2026-12-04 13:21:46 +00:00
greg left a comment
Owner

👍

👍
greg merged commit 0fd69cc5e5 into master 2026-12-04 13:22:04 +00:00
greg deleted branch chore/dos_mitigation 2026-12-04 13:22:04 +00:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: kosmos/chef#720