Repository navigation
Fix #27: key the demo API rate-limit zone per account, not per gateway - #40
Open
xiaoyao-666-coder wants to merge 2 commits into
Open
xiaoyao-666-coder wants to merge 2 commits into
xiaoyao-666-coder wants to merge 2 commits into
Conversation
This was referenced Nov 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
InstaBox demo 里 nginx 限流键共享:UI nginx(
deploy/instabox/nginx.demo.conf,limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s)与主网关共用同一份限流配置,但 demo 拓扑里所有请求都经由 API 的同级容器转发,nginx 眼中的$binary_remote_addr是同一个地址,所有访客共享同一个桶。结果:访客一多,合法请求被互相挤占触发限流,且无法对单个恶意 IP 限流。核实后,issue 的核心结论属实,但细节有三处与 main 不符(以代码为准):
127.0.0.1:8080→ Docker DNAT → UI nginx 容器(compose.demo.yml/compose.local.yml均如此绑定,两个拓扑共用同一份nginx.demo.conf)。容器内$remote_addr是所有访客共用的同一个地址(issue 写“恒为 127.0.0.1”不够准确——从本机回环口进来的连接在容器内看到的是 Docker 网关地址,但结论不变:所有人共享一个桶)。medguide_api: 30r/m、medguide_auth: 6r/m、limit_req_status 429。其中 auth 区 6r/m 全站共享是最尖锐的一处——两个人同一分钟内登录就可能互相 429。frontend/nginx.conf已有set_real_ip_from ${MEDGUIDE_TRUSTED_PROXY_CIDR}+real_ip_header X-Forwarded-For+real_ip_recursive on,Caddy(运营方控制、在同一 compose 网络内)会追加真实客户端 IP,所以主部署的$binary_remote_addr本来就是每访客独立的——没动主部署。修复
没用 XFF,改用 session cookie 做限流键。任务建议优先用 XFF,但这与仓库里明文写死的两条安全边界冲突,我没照做:
nginx.demo.conf原有注释:“Do not enable real_ip_header here: arbitrary public XFF is not an identity.”deploy/instabox/README.md安全边界:“不根据用户提交的 XFF 或协议头建立身份。”InstaBox 平台代理不在运营方控制之下,无法验证它是否追加真实客户端 IP(还是原样透传客户端自带的 XFF);一旦它透传,信任 XFF 就等于让任意访客伪造限流键、逃限流或挤占他人。所以:
medguide_api区(业务 API,真正产生流量的地方)改用map $cookie_medguide_session $medguide_api_key:有 cookie → 以 cookie 值为键(API 登录时签发的secrets.token_urlsafe(32),HttpOnly/Secure/SameSite=Strict,一个 cookie 就是一个账号,拿不到别人 session 就冒充不了别人的键);无 cookie → 回落到$binary_remote_addr(匿名请求反正会被auth_request401 掉)。速率 30r/m、burst 10、429 均未动。medguide_auth区(login/register)保持$binary_remote_addr,并在注释里写明为什么:匿名请求在这一层没有任何可信的 per-client 信号(cookie 还不存在、XFF 不可信),防爆破靠后端 [中] 登录接口无任何限流/锁定,可无限暴力破解 #23 的按账号锁定;共享桶只限制匿名请求的 aggregate 速率。deploy块、没动 compose、Caddyfile、应用代码;配置里没有${...},envsubst 模板不受影响。验证
MEDGUIDE_MODE=test python -m pytest -q backend/tests522 passed(基线 521 + 1 新)。测试覆盖:api 区键为$medguide_api_key且不再用$binary_remote_addr、速率/burst/429 未变、demo 配置无real_ip_header/real_ip_recursive/set_real_ip_from(去注释后检查)、主部署frontend/nginx.conf未动、两个 demo compose 都挂载了这份配置且 cookie 名均为medguide_session。;/{/}结尾、map与limit_req_zone均在 http 层(depth 0)、zone 引用的变量都有定义、两个 zone 都有引用、无${...}(envsubst 安全)。已知遗留
nginx -t没跑过;无 Docker,demo 栈真实行为(多访客并发下各账号独立桶、登录区 429 表现)没跑过,需在有 Docker 的环境按 issue 复现步骤确认。map里硬编码了medguide_session,与两个 compose 文件里MEDGUIDE_AUTH_COOKIE_NAME: medguide_session对应(注释里已写明)。如果将来改 cookie 名,这里也要同步改,否则限流键会静默退化回共享桶(安全回落,不会出错,只是不再按账号分桶)。$remote_addr,即网关地址),但 [中] 登录接口无任何限流/锁定,可无限暴力破解 #23 的按账号维度仍有效,所以登录保护不受影响。Closes #27