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
3 Commits
Author SHA1 Message Date
raucao ac639dcbe9 Scope the Gitea filter gate to location / to avoid a subrequest cycle
kosmos/actions: Required labels / Required labels (pull_request) Successful in 12s
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.
2026-12-04 17:00:14 +04:00
raucao 246c751f4c Hard-ban CogBot at the Gitea edge
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).
2026-12-04 17:00:14 +04:00
raucao 7d620d4d03 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.
2026-12-04 17:00:14 +04:00