Skip to content

security: rate-limit every route (express-rate-limit) — fixes CodeQL js/missing-rate-limiting - #76

Merged
alloevil merged 1 commit into
masterfrom
security/rate-limit
Oct 18, 2026
Merged

alloevil merged 1 commit into
masterfrom
security/rate-limit

Conversation

@alloevil

Copy link
Copy Markdown
Owner

根因

CodeQL 的 js/missing-rate-limiting 在 master 上有 11 条开着的告警,#75 上还有 1 条(#78)。原因是每个路由处理函数都会读写文件系统,但没有任何限流:

告警 位置
#64 lib/routes/backup.js:20
#65 #66 #67 #68 #69 #70 lib/routes/library.js:35 / 56 / 90 / 114 / 176 / 293
#71 #72 lib/routes/sessions.js:180 / 227
#73 lib/routes/watch.js:196
#74 server.js:67(SPA 兜底)
#78(在 #75 上) server.js:69(同一个 SPA 兜底,express 5 写法)

修复

  • 新文件 lib/rate-limit.js:express-rate-limit 8.7.1(2026-10-06 发布,持续维护)。按客户端 IP 每 60 秒最多 600 个请求,返回 draft-8 的 RateLimit / RateLimit-Policy 头,超出返回 429 和 JSON 错误。
  • server.js:在静态文件、/api/version 和所有路由模块之前 app.use(limiter),API、静态资源和 SPA 兜底都覆盖到。
  • 限额故意放得很宽:这是默认只绑 127.0.0.1 的本地工具,界面首次加载、轮询和 SSE 重连都远到不了 600/分钟。测试里没有关掉限流:每个测试文件各起一个服务,没有哪个测试接近这个量。
  • 新增测试(test/api.test.js):检查 /api/version、/ 和 SPA 兜底都带 RateLimit-Policy,且 q=600、w=60。测试总数因此变成 349,claims.json 的 test-count 和 docs/ROADMAP.md 同步改成 349。只改数字,门槛不变。

本地验证(Node 22.23.3)

  • npm ci(完整构建 UI):✓ built in 2.76s;pure.js 和 diagnostics.cjs 没有漂移;biome check exit 0
  • npm test:# tests 349 · # pass 349 · # fail 0 · # cancelled 0
  • verify-claims:16 machine-checked · 16 ok · 0 failed · 0 error · 3 manual

验收标准

  • test:# tests 349 # pass 349 # fail 0
  • claims:16 ok · 0 failed · 0 error
  • CodeQL 检查通过;合并后 master 上开着的 js/missing-rate-limiting 告警数为 0

…issing-rate-limiting)

Fixes the 11 open js/missing-rate-limiting alerts on master (#64-#74: backup.js:20,
library.js:35/56/90/114/176/293, sessions.js:180/227, watch.js:196, server.js:67) and
#78 on #75. Every route handler touches the filesystem and none had a limiter.

lib/rate-limit.js: express-rate-limit 8.7.1, 600 requests / 60 s per client IP,
draft-8 RateLimit headers, JSON 429 body. Mounted with app.use() in server.js before
the static files, /api/version and every route module, so it covers API, static and
the SPA fallback. Not disabled in tests: each test file starts its own server and no
test comes near 600 requests a minute.

New test (test/api.test.js) asserts the RateLimit-Policy q=600 / w=60 on /api/version,
/ and the SPA fallback, so the test count is 349: claims.json test-count and
docs/ROADMAP.md updated to 349 (the number follows the code, the gate is unchanged).

Local (Node 22.23.3): npm ci + build OK, no pure.js/diagnostics.cjs drift, biome
exit 0, npm test 349/349, verify-claims 16 ok / 0 error / 3 manual.
@alloevil
alloevil merged commit 2eaed65 into master Oct 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant