Scope the Gitea filter gate to location / to avoid a subrequest cycle

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.
This commit is contained in:
raucao committed 2026-12-04 16:44:41 +04:00
1 parent af40895951
commit 1f67eefbac
1 file changed
+49 -42
@@ -39,51 +39,13 @@ server {
# 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.
#
# The gate itself lives in "location /" below, NOT here: a server-level
# rewrite_by_lua would be inherited by the internal probe location, and its
# subrequest would then re-enter the gate and try to capture itself.
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
@@ -132,6 +94,51 @@ server {
}
location / {
# Anonymous filter-permutation gate: sets $filter_key so the server-level
# "limit_req zone=filters" counts only anonymous filter requests. Scoped to
# this location so the internal probe location below does not inherit it.
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
}
proxy_buffers 1024 8k;
proxy_pass http://_gitea_web;
proxy_http_version 1.1;